Skip to content

Commit

Permalink
fix(sync/paseo): Do not use runtime mod instantiation (#4496)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimartiro authored Jan 24, 2025
1 parent b84a435 commit 10a9197
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 16 deletions.
13 changes: 9 additions & 4 deletions lib/runtime/wazero/imports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/ChainSafe/gossamer/pkg/trie/inmemory/proof"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tetratelabs/wazero"
)

var DefaultVersion = &runtime.Version{
Expand Down Expand Up @@ -875,29 +876,33 @@ func Test_ext_misc_runtime_version_version_1(t *testing.T) {
}
}

mod, err := inst.Runtime.InstantiateModule(context.Background(), inst.metadata.guestModule, wazero.NewModuleConfig())
require.NoError(t, err)

allocator := allocator.NewFreeingBumpHeapAllocator(0)
inst.Context.Allocator = allocator

data := bytes
dataLength := uint32(len(data))
inputPtr, err := inst.Context.Allocator.Allocate(inst.Module.Memory(), dataLength)
inputPtr, err := inst.Context.Allocator.Allocate(mod.Memory(), dataLength)
if err != nil {
t.Errorf("allocating input memory: %v", err)
}

// Store the data into memory
mem := inst.Module.Memory()

mem := mod.Memory()
ok := mem.Write(inputPtr, data)
if !ok {
panic("write overlow")
}

dataSpan := newPointerSize(inputPtr, dataLength)
ctx := context.WithValue(context.Background(), runtimeContextKey, inst.Context)
versionPtr := ext_misc_runtime_version_version_1(ctx, inst.Module, dataSpan)
versionPtr := ext_misc_runtime_version_version_1(ctx, mod, dataSpan)

var option *[]byte
versionData := read(inst.Module, versionPtr)
versionData := read(mod, versionPtr)
err = scale.Unmarshal(versionData, &option)
require.NoError(t, err)
require.NotNil(t, option)
Expand Down
19 changes: 7 additions & 12 deletions lib/runtime/wazero/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func NewInstanceFromTrie(t trie.Trie, cfg Config) (*Instance, error) {
func newRuntime(ctx context.Context,
code []byte,
config wazero.RuntimeConfig,
) (api.Module, wazero.Runtime, wazero.CompiledModule, error) {
) (wazero.Runtime, wazero.CompiledModule, error) {
rt := wazero.NewRuntimeWithConfig(ctx, config)

const i32, i64 = api.ValueTypeI32, api.ValueTypeI64
Expand Down Expand Up @@ -643,29 +643,25 @@ func newRuntime(ctx context.Context,
Compile(ctx)

if err != nil {
return nil, nil, nil, err
return nil, nil, err
}

_, err = rt.InstantiateModule(ctx, hostCompiledModule, wazero.NewModuleConfig())
if err != nil {
return nil, nil, nil, err
return nil, nil, err
}

code, err = decompressWasm(code)
if err != nil {
return nil, nil, nil, err
return nil, nil, err
}

guestCompiledModule, err := rt.CompileModule(ctx, code)
if err != nil {
return nil, nil, nil, err
}
mod, err := rt.Instantiate(ctx, code)
if err != nil {
return nil, nil, nil, err
return nil, nil, err
}

return mod, rt, guestCompiledModule, nil
return rt, guestCompiledModule, nil
}

// NewInstance instantiates a runtime from raw wasm bytecode
Expand All @@ -677,7 +673,7 @@ func NewInstance(code []byte, cfg Config) (instance *Instance, err error) {
ctx := context.Background()
cache := wazero.NewCompilationCache()
config := wazero.NewRuntimeConfig().WithCompilationCache(cache)
mod, rt, guestCompiledModule, err := newRuntime(ctx, code, config)
rt, guestCompiledModule, err := newRuntime(ctx, code, config)
if err != nil {
return nil, fmt.Errorf("creating runtime instance: %w", err)
}
Expand All @@ -694,7 +690,6 @@ func NewInstance(code []byte, cfg Config) (instance *Instance, err error) {
SigVerifier: crypto.NewSignatureVerifier(logger),
OffchainHTTPSet: offchain.NewHTTPSet(),
},
Module: mod,
codeHash: cfg.CodeHash,
metadata: wazeroMeta{
config: config,
Expand Down
45 changes: 45 additions & 0 deletions lib/runtime/wazero/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1362,6 +1362,51 @@ func newTrieFromKeyValueListV1(t *testing.T, filename string) trie.Trie {
return tr
}

/*
TODO: fix downloading the state and block in the CI
func TestInstance_ExecuteBlock_PaseoRuntime_PaseoBlock3796856(t *testing.T) {
paseoTrie := newTrieFromKeyValueListV1(t, "../test_data/paseo/block3796855.out")
expectedRoot := common.MustHexToHash("0x420b69ab8c34629fc347acfdd4ac27ed8c5f7830b18387e8db03585265d02063")
require.Equal(t, expectedRoot, trie.V1.MustHash(paseoTrie))
state := storage.NewTrieState(paseoTrie)
db, err := database.NewPebble("", true)
require.NoError(t, err)
cfg := Config{
Storage: state,
LogLvl: log.Info,
NodeStorage: runtime.NodeStorage{
LocalStorage: db,
PersistentStorage: db,
BaseDB: db,
},
}
instance, err := NewInstanceFromTrie(paseoTrie, cfg)
require.NoError(t, err)
blockResponse := &messages.BlockResponseMessage{}
data, err := os.ReadFile("../test_data/paseo/block3796856.out")
require.NoError(t, err)
err = blockResponse.Decode(common.MustHexToBytes(string(data))) //nolint:lll
require.NoError(t, err)
block := &types.Block{
Header: *blockResponse.BlockData[0].Header,
Body: *blockResponse.BlockData[0].Body,
}
_, err = instance.ExecuteBlock(block)
require.NoError(t, err)
expectedRootNew := common.MustHexToHash("0xc6834534ce95a984926143ce7b5fa10c9fcf00727a6378690f8b2796544f2c85")
require.Equal(t, expectedRootNew, state.Trie().MustHash())
}*/

func TestInstance_ExecuteBlock_PaseoRuntime_PaseoBlock1789153(t *testing.T) {
paseoTrie := newTrieFromKeyValueListV1(t, "../test_data/paseo/block1789152.out")
expectedRoot := common.MustHexToHash("0xf74a4a94758ac505a0bacdd52d7739d62b616eb03840d24d4a2df42df295c31d")
Expand Down

0 comments on commit 10a9197

Please sign in to comment.