Skip to content

Commit

Permalink
feat(lib/runtime): support Substrate WASM compression (#2213)
Browse files Browse the repository at this point in the history
* Support Substrate Wasm compression

https://github.com/paritytech/substrate/blob/master/primitives/maybe-compressed-blob/src/lib.rs

* Apply suggestions from code review

Co-authored-by: noot <[email protected]>

* Review comments

- slices cannot be const
- create function & write tests
- `go mod tidy`

* Apply suggestions from code review

Co-authored-by: Quentin McGaw <[email protected]>

Co-authored-by: noot <[email protected]>
Co-authored-by: Quentin McGaw <[email protected]>
  • Loading branch information
3 people authored Jan 21, 2022
1 parent 17e557d commit fd60061
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ require (
github.com/ipfs/go-ds-badger2 v0.1.1
github.com/ipfs/go-ipns v0.1.2 //indirect
github.com/jpillora/ipfilter v1.2.3
github.com/klauspost/compress v1.12.3
github.com/libp2p/go-libp2p v0.15.1
github.com/libp2p/go-libp2p-core v0.9.0
github.com/libp2p/go-libp2p-discovery v0.5.1
Expand Down Expand Up @@ -84,7 +85,6 @@ require (
github.com/jbenet/goprocess v0.1.4 // indirect
github.com/jcelliott/lumber v0.0.0-20160324203708-dd349441af25 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/klauspost/compress v1.12.3 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/koron/go-ssdp v0.0.2 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
Expand Down
25 changes: 25 additions & 0 deletions lib/runtime/wasmer/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package wasmer

import (
"bytes"
"errors"
"fmt"
"sync"
Expand All @@ -18,6 +19,8 @@ import (
"github.com/ChainSafe/gossamer/lib/crypto"

wasm "github.com/wasmerio/go-ext-wasm/wasmer"

"github.com/klauspost/compress/zstd"
)

// Name represents the name of the interpreter
Expand Down Expand Up @@ -94,6 +97,12 @@ func NewInstance(code []byte, cfg *Config) (*Instance, error) {
return nil, errors.New("code is empty")
}

var err error
code, err = decompressWasm(code)
if err != nil {
return nil, fmt.Errorf("cannot decompress WASM code: %w", err)
}

logger.Patch(log.SetLevel(cfg.LogLvl), log.SetCallerFunc(true))

imports, err := cfg.Imports()
Expand Down Expand Up @@ -157,6 +166,22 @@ func NewInstance(code []byte, cfg *Config) (*Instance, error) {
return inst, nil
}

// decompressWasm decompresses a Wasm blob that may or may not be compressed with zstd
// ref: https://github.com/paritytech/substrate/blob/master/primitives/maybe-compressed-blob/src/lib.rs
func decompressWasm(code []byte) ([]byte, error) {
compressionFlag := []byte{82, 188, 83, 118, 70, 219, 142, 5}
if !bytes.HasPrefix(code, compressionFlag) {
return code, nil
}

decoder, err := zstd.NewReader(nil)
if err != nil {
return nil, fmt.Errorf("failed to create zstd decoder: %w", err)
}

return decoder.DecodeAll(code[len(compressionFlag):], nil)
}

// GetCodeHash returns the code of the instance
func (in *Instance) GetCodeHash() common.Hash {
return in.codeHash
Expand Down
40 changes: 40 additions & 0 deletions lib/runtime/wasmer/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/stretchr/testify/require"

"github.com/klauspost/compress/zstd"
)

// test used for ensuring runtime exec calls can me made concurrently
Expand Down Expand Up @@ -63,3 +65,41 @@ func TestInstance_CheckRuntimeVersion(t *testing.T) {
require.Equal(t, expected.ImplVersion(), version.ImplVersion())
require.Equal(t, expected.TransactionVersion(), version.TransactionVersion())
}

func TestDecompressWasm(t *testing.T) {
encoder, err := zstd.NewWriter(nil)
require.NoError(t, err)

cases := []struct {
in []byte
expected []byte
msg string
}{
{
[]byte{82, 188, 83, 118, 70, 219, 142},
[]byte{82, 188, 83, 118, 70, 219, 142},
"partial compression flag",
},
{
[]byte{82, 188, 83, 118, 70, 219, 142, 6},
[]byte{82, 188, 83, 118, 70, 219, 142, 6},
"wrong compression flag",
},
{
[]byte{82, 188, 83, 118, 70, 219, 142, 6, 221},
[]byte{82, 188, 83, 118, 70, 219, 142, 6, 221},
"wrong compression flag with data",
},
{
append([]byte{82, 188, 83, 118, 70, 219, 142, 5}, encoder.EncodeAll([]byte("compressed"), nil)...),
[]byte("compressed"),
"compressed data",
},
}

for _, test := range cases {
actual, err := decompressWasm(test.in)
require.NoError(t, err)
require.Equal(t, test.expected, actual)
}
}

0 comments on commit fd60061

Please sign in to comment.