Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 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"
Comment thread
kishansagathiya marked this conversation as resolved.
)

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

// Substrate Wasm compression
// https://github.com/paritytech/substrate/blob/master/primitives/maybe-compressed-blob/src/lib.rs
compressionFlag := []byte{82, 188, 83, 118, 70, 219, 142, 5}
Comment thread
danforbes marked this conversation as resolved.
Outdated
if bytes.HasPrefix(code, compressionFlag) {
logger.Debug("Decompressing Wasm runtime code")
Comment thread
kishansagathiya marked this conversation as resolved.
Outdated
var decoder, err = zstd.NewReader(nil)
Comment thread
danforbes marked this conversation as resolved.
Outdated
if err != nil {
return nil, errors.New("failed to create zstd decoder")
Comment thread
danforbes marked this conversation as resolved.
Outdated
}

code, err = decoder.DecodeAll(code[len(compressionFlag):], nil)
if err != nil {
return nil, errors.New("failed to decompress Wasm")
Comment thread
danforbes marked this conversation as resolved.
Outdated
}
}

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

imports, err := cfg.Imports()
Expand Down