From c6d38ac99aff3023e6179f59cc34eb34ed7702cf Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Thu, 19 Aug 2021 17:18:59 -0400 Subject: [PATCH 01/12] implement ext_offchain_index_set_version_1 --- lib/runtime/wasmer/imports.go | 14 +++++++++++++- lib/runtime/wasmer/imports_test.go | 20 ++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/runtime/wasmer/imports.go b/lib/runtime/wasmer/imports.go index 7b0573f8ec..7d84d3353d 100644 --- a/lib/runtime/wasmer/imports.go +++ b/lib/runtime/wasmer/imports.go @@ -1348,7 +1348,19 @@ func ext_hashing_twox_64_version_1(context unsafe.Pointer, dataSpan C.int64_t) C //export ext_offchain_index_set_version_1 func ext_offchain_index_set_version_1(context unsafe.Pointer, keySpan, valueSpan C.int64_t) { logger.Trace("[ext_offchain_index_set_version_1] executing...") - logger.Warn("[ext_offchain_index_set_version_1] unimplemented") + instanceContext := wasm.IntoInstanceContext(context) + runtimeCtx := instanceContext.Data().(*runtime.Context) + + storageKey := asMemorySlice(instanceContext, keySpan) + newValue := asMemorySlice(instanceContext, valueSpan) + cp := make([]byte, len(newValue)) + copy(cp, newValue) + + err := runtimeCtx.NodeStorage.PersistentStorage.Put(storageKey, cp) + + if err != nil { + logger.Error("[ext_offchain_index_set_version_1] failed to set value in storage", "error", err) + } } //export ext_offchain_local_storage_clear_version_1 diff --git a/lib/runtime/wasmer/imports_test.go b/lib/runtime/wasmer/imports_test.go index aba4174383..7091b6689e 100644 --- a/lib/runtime/wasmer/imports_test.go +++ b/lib/runtime/wasmer/imports_test.go @@ -409,6 +409,26 @@ func Test_ext_storage_set_version_1(t *testing.T) { require.Equal(t, testvalue, val) } +func Test_ext_offline_index_set_version_1(t *testing.T) { + // TODO this currently fails with error could nat find exported function, determine how else to test this + inst := NewTestInstance(t, runtime.HOST_API_TEST_RUNTIME) + + testkey := []byte("noot") + testvalue := []byte("washere") + + encKey, err := scale.Encode(testkey) + require.NoError(t, err) + encValue, err := scale.Encode(testvalue) + require.NoError(t, err) + + _, err = inst.Exec("rtm_ext_offline_index_set_version_1", append(encKey, encValue...)) + require.NoError(t, err) + + val, err := inst.ctx.NodeStorage.PersistentStorage.Get(testkey) + require.NoError(t, err) + require.Equal(t, testvalue, val) +} + func Test_ext_crypto_ed25519_generate_version_1(t *testing.T) { inst := NewTestInstance(t, runtime.HOST_API_TEST_RUNTIME) From 99fe2c4f83ccd720e362813e2c0fed1ce38fc210 Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Fri, 20 Aug 2021 13:49:27 -0400 Subject: [PATCH 02/12] add storage to local --- lib/runtime/wasmer/imports.go | 6 +++++- lib/runtime/wasmer/imports_test.go | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/runtime/wasmer/imports.go b/lib/runtime/wasmer/imports.go index 7d84d3353d..998787e4df 100644 --- a/lib/runtime/wasmer/imports.go +++ b/lib/runtime/wasmer/imports.go @@ -1357,9 +1357,13 @@ func ext_offchain_index_set_version_1(context unsafe.Pointer, keySpan, valueSpan copy(cp, newValue) err := runtimeCtx.NodeStorage.PersistentStorage.Put(storageKey, cp) + if err != nil { + logger.Error("[ext_offchain_index_set_version_1] failed to set value in persistent storage", "error", err) + } + err = runtimeCtx.NodeStorage.LocalStorage.Put(storageKey, cp) if err != nil { - logger.Error("[ext_offchain_index_set_version_1] failed to set value in storage", "error", err) + logger.Error("[ext_offchain_index_set_version_1] failed to set value in local storage", "error", err) } } diff --git a/lib/runtime/wasmer/imports_test.go b/lib/runtime/wasmer/imports_test.go index 7091b6689e..21b98214e8 100644 --- a/lib/runtime/wasmer/imports_test.go +++ b/lib/runtime/wasmer/imports_test.go @@ -411,6 +411,7 @@ func Test_ext_storage_set_version_1(t *testing.T) { func Test_ext_offline_index_set_version_1(t *testing.T) { // TODO this currently fails with error could nat find exported function, determine how else to test this + t.Skip() inst := NewTestInstance(t, runtime.HOST_API_TEST_RUNTIME) testkey := []byte("noot") From cbdb7f29741d30374c1dada0b2e4c893b8eccb38 Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Tue, 24 Aug 2021 15:36:31 -0400 Subject: [PATCH 03/12] add baseDB to runtime context --- dot/core/test_helpers.go | 13 +++++++++++-- dot/services.go | 6 ++++++ lib/runtime/types.go | 1 + lib/runtime/wasmer/imports.go | 9 ++------- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/dot/core/test_helpers.go b/dot/core/test_helpers.go index 94966f6d6b..c4971b494b 100644 --- a/dot/core/test_helpers.go +++ b/dot/core/test_helpers.go @@ -18,19 +18,21 @@ package core import ( "io/ioutil" + "path/filepath" "testing" + coremocks "github.com/ChainSafe/gossamer/dot/core/mocks" "github.com/ChainSafe/gossamer/dot/network" "github.com/ChainSafe/gossamer/dot/state" "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/crypto/sr25519" "github.com/ChainSafe/gossamer/lib/genesis" "github.com/ChainSafe/gossamer/lib/keystore" + "github.com/ChainSafe/gossamer/lib/runtime" rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage" "github.com/ChainSafe/gossamer/lib/runtime/wasmer" + "github.com/ChainSafe/gossamer/lib/utils" log "github.com/ChainSafe/log15" - - coremocks "github.com/ChainSafe/gossamer/dot/core/mocks" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" ) @@ -105,6 +107,13 @@ func NewTestService(t *testing.T, cfg *Config) *Service { rtCfg.CodeHash, err = cfg.StorageState.LoadCodeHash(nil) require.NoError(t, err) + nodeStorage := runtime.NodeStorage{} + + nodeStorage.BaseDB, err = utils.SetupDatabase(filepath.Join(testDatadirPath, "offline_storage"), false) + require.NoError(t, err) + + rtCfg.NodeStorage = nodeStorage + cfg.Runtime, err = wasmer.NewRuntimeFromGenesis(gen, rtCfg) require.NoError(t, err) } diff --git a/dot/services.go b/dot/services.go index 2df030659b..36805f08d8 100644 --- a/dot/services.go +++ b/dot/services.go @@ -120,9 +120,15 @@ func createRuntime(cfg *Config, st *state.Service, ks *keystore.GlobalKeystore, return nil, err } + baseDB, err := utils.SetupDatabase(filepath.Join(st.DB().Path(), "offline_storage"), false) + if err != nil { + return nil, err + } + ns := runtime.NodeStorage{ LocalStorage: localStorage, PersistentStorage: chaindb.NewTable(st.DB(), "offlinestorage"), + BaseDB: baseDB, } codeHash, err := st.Storage.LoadCodeHash(nil) diff --git a/lib/runtime/types.go b/lib/runtime/types.go index c45be933be..cfbbe77cda 100644 --- a/lib/runtime/types.go +++ b/lib/runtime/types.go @@ -35,6 +35,7 @@ const NodeStorageTypeLocal NodeStorageType = 2 type NodeStorage struct { LocalStorage BasicStorage PersistentStorage BasicStorage + BaseDB BasicStorage } // InstanceConfig represents a runtime instance configuration diff --git a/lib/runtime/wasmer/imports.go b/lib/runtime/wasmer/imports.go index 998787e4df..3da307e203 100644 --- a/lib/runtime/wasmer/imports.go +++ b/lib/runtime/wasmer/imports.go @@ -1356,14 +1356,9 @@ func ext_offchain_index_set_version_1(context unsafe.Pointer, keySpan, valueSpan cp := make([]byte, len(newValue)) copy(cp, newValue) - err := runtimeCtx.NodeStorage.PersistentStorage.Put(storageKey, cp) + err := runtimeCtx.NodeStorage.BaseDB.Put(storageKey, cp) if err != nil { - logger.Error("[ext_offchain_index_set_version_1] failed to set value in persistent storage", "error", err) - } - - err = runtimeCtx.NodeStorage.LocalStorage.Put(storageKey, cp) - if err != nil { - logger.Error("[ext_offchain_index_set_version_1] failed to set value in local storage", "error", err) + logger.Error("[ext_offchain_index_set_version_1] failed to set value in raw storage", "error", err) } } From 53d11f5ad75a0b3d7aefa37adfec278b4d70ee88 Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Tue, 24 Aug 2021 16:33:23 -0400 Subject: [PATCH 04/12] add init base db for tests --- lib/runtime/wasmer/exports_test.go | 3 +++ lib/runtime/wasmer/test_helpers.go | 1 + 2 files changed, 4 insertions(+) diff --git a/lib/runtime/wasmer/exports_test.go b/lib/runtime/wasmer/exports_test.go index 96e2198b83..91f81a2775 100644 --- a/lib/runtime/wasmer/exports_test.go +++ b/lib/runtime/wasmer/exports_test.go @@ -266,6 +266,9 @@ func TestNodeRuntime_ValidateTransaction(t *testing.T) { cfg := &Config{} cfg.Storage = genState cfg.LogLvl = 4 + nodeStorage := runtime.NodeStorage{} + nodeStorage.BaseDB = runtime.NewInMemoryDB(t) + cfg.NodeStorage = nodeStorage rt, err := NewRuntimeFromGenesis(gen, cfg) require.NoError(t, err) diff --git a/lib/runtime/wasmer/test_helpers.go b/lib/runtime/wasmer/test_helpers.go index f9e9d91690..556739bca1 100644 --- a/lib/runtime/wasmer/test_helpers.go +++ b/lib/runtime/wasmer/test_helpers.go @@ -71,6 +71,7 @@ func setupConfig(t *testing.T, targetRuntime string, tt *trie.Trie, lvl log.Lvl, ns := runtime.NodeStorage{ LocalStorage: runtime.NewInMemoryDB(t), PersistentStorage: runtime.NewInMemoryDB(t), // we're using a local storage here since this is a test runtime + BaseDB: runtime.NewInMemoryDB(t), // we're using a local storage here since this is a test runtime } cfg := &Config{ Imports: ImportsNodeRuntime, From caea8b0df7a524133e05c69bd9bca237d1c9d255 Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Tue, 24 Aug 2021 17:59:24 -0400 Subject: [PATCH 05/12] add base db to node storage in tests --- lib/babe/babe_test.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/babe/babe_test.go b/lib/babe/babe_test.go index bacab21429..22b54c5388 100644 --- a/lib/babe/babe_test.go +++ b/lib/babe/babe_test.go @@ -21,12 +21,14 @@ import ( "io/ioutil" "math/big" "os" + "path/filepath" "testing" "time" "github.com/ChainSafe/gossamer/dot/core" "github.com/ChainSafe/gossamer/dot/state" "github.com/ChainSafe/gossamer/dot/types" + "github.com/ChainSafe/gossamer/lib/babe/mocks" "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/crypto/sr25519" "github.com/ChainSafe/gossamer/lib/genesis" @@ -34,11 +36,10 @@ import ( rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage" "github.com/ChainSafe/gossamer/lib/runtime/wasmer" "github.com/ChainSafe/gossamer/lib/trie" + "github.com/ChainSafe/gossamer/lib/utils" log "github.com/ChainSafe/log15" - "github.com/stretchr/testify/require" - - "github.com/ChainSafe/gossamer/lib/babe/mocks" mock "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" ) var ( @@ -98,8 +99,9 @@ func createTestService(t *testing.T, cfg *ServiceConfig) *Service { cfg.TransactionState = state.NewTransactionState() } + testDatadirPath, err := ioutil.TempDir("/tmp", "test-datadir-*") //nolint + if cfg.BlockState == nil || cfg.StorageState == nil || cfg.EpochState == nil { - testDatadirPath, err := ioutil.TempDir("/tmp", "test-datadir-*") //nolint require.NoError(t, err) config := state.Config{ @@ -138,6 +140,11 @@ func createTestService(t *testing.T, cfg *ServiceConfig) *Service { rtCfg.CodeHash, err = storageState.LoadCodeHash(nil) require.NoError(t, err) + nodeStorage := runtime.NodeStorage{} + nodeStorage.BaseDB, err = utils.SetupDatabase(filepath.Join(testDatadirPath, "offline_storage"), false) + require.NoError(t, err) + rtCfg.NodeStorage = nodeStorage + cfg.Runtime, err = wasmer.NewRuntimeFromGenesis(gen, rtCfg) require.NoError(t, err) } From 9ddda6d0db943a0d48f232fcf7ddd60b0efb4909 Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Tue, 24 Aug 2021 18:37:39 -0400 Subject: [PATCH 06/12] add base db to node storage for sync/test_helpers --- dot/sync/test_helpers.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/dot/sync/test_helpers.go b/dot/sync/test_helpers.go index ae8eacf7fc..592b81debf 100644 --- a/dot/sync/test_helpers.go +++ b/dot/sync/test_helpers.go @@ -19,12 +19,12 @@ package sync import ( "io/ioutil" "math/big" + "path/filepath" "testing" "time" - "github.com/stretchr/testify/mock" - "github.com/ChainSafe/gossamer/dot/state" + syncmocks "github.com/ChainSafe/gossamer/dot/sync/mocks" "github.com/ChainSafe/gossamer/dot/types" "github.com/ChainSafe/gossamer/lib/babe" "github.com/ChainSafe/gossamer/lib/common" @@ -34,11 +34,11 @@ import ( "github.com/ChainSafe/gossamer/lib/runtime/wasmer" "github.com/ChainSafe/gossamer/lib/transaction" "github.com/ChainSafe/gossamer/lib/trie" + "github.com/ChainSafe/gossamer/lib/utils" "github.com/ChainSafe/gossamer/pkg/scale" log "github.com/ChainSafe/log15" + "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" - - syncmocks "github.com/ChainSafe/gossamer/dot/sync/mocks" ) // NewMockFinalityGadget create and return sync FinalityGadget interface mock @@ -100,6 +100,11 @@ func NewTestSyncer(t *testing.T, usePolkadotGenesis bool) *Service { rtCfg.CodeHash, err = cfg.StorageState.LoadCodeHash(nil) require.NoError(t, err) + nodeStorage := runtime.NodeStorage{} + nodeStorage.BaseDB, err = utils.SetupDatabase(filepath.Join(testDatadirPath, "offline_storage"), false) + require.NoError(t, err) + rtCfg.NodeStorage = nodeStorage + instance, err := wasmer.NewRuntimeFromGenesis(gen, rtCfg) //nolint require.NoError(t, err) cfg.Runtime = instance From 1cdf40943ceee3f1f3aa39be6b01fd06c40778ef Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Wed, 1 Sep 2021 15:27:48 -0400 Subject: [PATCH 07/12] use baseDB instead of creating new db --- dot/services.go | 7 +------ dot/state/base.go | 8 ++++++++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/dot/services.go b/dot/services.go index 36805f08d8..1ba40f5324 100644 --- a/dot/services.go +++ b/dot/services.go @@ -120,15 +120,10 @@ func createRuntime(cfg *Config, st *state.Service, ks *keystore.GlobalKeystore, return nil, err } - baseDB, err := utils.SetupDatabase(filepath.Join(st.DB().Path(), "offline_storage"), false) - if err != nil { - return nil, err - } - ns := runtime.NodeStorage{ LocalStorage: localStorage, PersistentStorage: chaindb.NewTable(st.DB(), "offlinestorage"), - BaseDB: baseDB, + BaseDB: st.Base, } codeHash, err := st.Storage.LoadCodeHash(nil) diff --git a/dot/state/base.go b/dot/state/base.go index 9e0e8884f3..8dd851ffa7 100644 --- a/dot/state/base.go +++ b/dot/state/base.go @@ -125,6 +125,14 @@ func (s *BaseState) LoadCodeSubstitutedBlockHash() common.Hash { return common.NewHash(hash) } +func (s *BaseState) Put(key []byte, value []byte) error { + return s.db.Put(key, value) +} + +func (s *BaseState) Get(key []byte) ([]byte, error) { + return s.db.Get(key) +} + func (s *BaseState) storeSkipToEpoch(epoch uint64) error { buf := make([]byte, 8) binary.LittleEndian.PutUint64(buf, epoch) From fcb12382356788784da090a16342ce6f951c7ef4 Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Wed, 1 Sep 2021 15:44:53 -0400 Subject: [PATCH 08/12] lint issues --- dot/state/base.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dot/state/base.go b/dot/state/base.go index 8dd851ffa7..b347d98d2a 100644 --- a/dot/state/base.go +++ b/dot/state/base.go @@ -125,10 +125,12 @@ func (s *BaseState) LoadCodeSubstitutedBlockHash() common.Hash { return common.NewHash(hash) } +// Put stores key/value pair in database func (s *BaseState) Put(key []byte, value []byte) error { return s.db.Put(key, value) } +// Get retrieves value by key from database func (s *BaseState) Get(key []byte) ([]byte, error) { return s.db.Get(key) } From f9c9cdf07010014a30cb511ed102e52bf2fc4419 Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Thu, 2 Sep 2021 13:27:15 -0400 Subject: [PATCH 09/12] address deep source issues --- dot/state/base.go | 2 +- lib/runtime/wasmer/imports.go | 52 +++++++++++++++--------------- lib/runtime/wasmer/test_helpers.go | 2 +- 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/dot/state/base.go b/dot/state/base.go index b347d98d2a..060247beb4 100644 --- a/dot/state/base.go +++ b/dot/state/base.go @@ -126,7 +126,7 @@ func (s *BaseState) LoadCodeSubstitutedBlockHash() common.Hash { } // Put stores key/value pair in database -func (s *BaseState) Put(key []byte, value []byte) error { +func (s *BaseState) Put(key, value []byte) error { return s.db.Put(key, value) } diff --git a/lib/runtime/wasmer/imports.go b/lib/runtime/wasmer/imports.go index 3da307e203..9d26768c19 100644 --- a/lib/runtime/wasmer/imports.go +++ b/lib/runtime/wasmer/imports.go @@ -133,7 +133,7 @@ import ( ) //export ext_logging_log_version_1 -func ext_logging_log_version_1(context unsafe.Pointer, level C.int32_t, targetData C.int64_t, msgData C.int64_t) { +func ext_logging_log_version_1(context unsafe.Pointer, level C.int32_t, targetData, msgData C.int64_t) { logger.Trace("[ext_logging_log_version_1] executing...") instanceContext := wasm.IntoInstanceContext(context) @@ -157,66 +157,66 @@ func ext_logging_log_version_1(context unsafe.Pointer, level C.int32_t, targetDa } //export ext_logging_max_level_version_1 -func ext_logging_max_level_version_1(context unsafe.Pointer) C.int32_t { +func ext_logging_max_level_version_1(_ unsafe.Pointer) C.int32_t { logger.Trace("[ext_logging_max_level_version_1] executing...") return 4 } //export ext_transaction_index_index_version_1 -func ext_transaction_index_index_version_1(context unsafe.Pointer, a, b, c C.int32_t) { +func ext_transaction_index_index_version_1(_ unsafe.Pointer, _, _, _ C.int32_t) { logger.Trace("[ext_transaction_index_index_version_1] executing...") logger.Warn("[ext_transaction_index_index_version_1] unimplemented") } //export ext_transaction_index_renew_version_1 -func ext_transaction_index_renew_version_1(context unsafe.Pointer, a, b C.int32_t) { +func ext_transaction_index_renew_version_1(_ unsafe.Pointer, _, _ C.int32_t) { logger.Trace("[ext_transaction_index_renew_version_1] executing...") logger.Warn("[ext_transaction_index_renew_version_1] unimplemented") } //export ext_sandbox_instance_teardown_version_1 -func ext_sandbox_instance_teardown_version_1(context unsafe.Pointer, a C.int32_t) { +func ext_sandbox_instance_teardown_version_1(_ unsafe.Pointer, _ C.int32_t) { logger.Trace("[ext_sandbox_instance_teardown_version_1] executing...") logger.Warn("[ext_sandbox_instance_teardown_version_1] unimplemented") } //export ext_sandbox_instantiate_version_1 -func ext_sandbox_instantiate_version_1(context unsafe.Pointer, a C.int32_t, x, y C.int64_t, z C.int32_t) C.int32_t { +func ext_sandbox_instantiate_version_1(_ unsafe.Pointer, _ C.int32_t, _, _ C.int64_t, _ C.int32_t) C.int32_t { logger.Trace("[ext_sandbox_instantiate_version_1] executing...") logger.Warn("[ext_sandbox_instantiate_version_1] unimplemented") return 0 } //export ext_sandbox_invoke_version_1 -func ext_sandbox_invoke_version_1(context unsafe.Pointer, a C.int32_t, x, y C.int64_t, z, d, e C.int32_t) C.int32_t { +func ext_sandbox_invoke_version_1(_ unsafe.Pointer, _ C.int32_t, _, _ C.int64_t, _, _, _ C.int32_t) C.int32_t { logger.Trace("[ext_sandbox_invoke_version_1] executing...") logger.Warn("[ext_sandbox_invoke_version_1] unimplemented") return 0 } //export ext_sandbox_memory_get_version_1 -func ext_sandbox_memory_get_version_1(context unsafe.Pointer, a, z, d, e C.int32_t) C.int32_t { +func ext_sandbox_memory_get_version_1(_ unsafe.Pointer, _, _, _, _ C.int32_t) C.int32_t { logger.Trace("[ext_sandbox_memory_get_version_1] executing...") logger.Warn("[ext_sandbox_memory_get_version_1] unimplemented") return 0 } //export ext_sandbox_memory_new_version_1 -func ext_sandbox_memory_new_version_1(context unsafe.Pointer, a, z C.int32_t) C.int32_t { +func ext_sandbox_memory_new_version_1(_ unsafe.Pointer, _, _ C.int32_t) C.int32_t { logger.Trace("[ext_sandbox_memory_new_version_1] executing...") logger.Warn("[ext_sandbox_memory_new_version_1] unimplemented") return 0 } //export ext_sandbox_memory_set_version_1 -func ext_sandbox_memory_set_version_1(context unsafe.Pointer, a, z, d, e C.int32_t) C.int32_t { +func ext_sandbox_memory_set_version_1(_ unsafe.Pointer, _, _, _, _ C.int32_t) C.int32_t { logger.Trace("[ext_sandbox_memory_set_version_1] executing...") logger.Warn("[ext_sandbox_memory_set_version_1] unimplemented") return 0 } //export ext_sandbox_memory_teardown_version_1 -func ext_sandbox_memory_teardown_version_1(context unsafe.Pointer, a C.int32_t) { +func ext_sandbox_memory_teardown_version_1(_ unsafe.Pointer, a C.int32_t) { logger.Trace("[ext_sandbox_memory_teardown_version_1] executing...") logger.Warn("[ext_sandbox_memory_teardown_version_1] unimplemented") } @@ -319,7 +319,7 @@ func ext_crypto_ed25519_public_keys_version_1(context unsafe.Pointer, keyTypeID } //export ext_crypto_ed25519_sign_version_1 -func ext_crypto_ed25519_sign_version_1(context unsafe.Pointer, keyTypeID C.int32_t, key C.int32_t, msg C.int64_t) C.int64_t { +func ext_crypto_ed25519_sign_version_1(context unsafe.Pointer, keyTypeID, key C.int32_t, msg C.int64_t) C.int64_t { logger.Debug("[ext_crypto_ed25519_sign_version_1] executing...") instanceContext := wasm.IntoInstanceContext(context) @@ -715,7 +715,7 @@ func ext_crypto_sr25519_verify_version_2(context unsafe.Pointer, sig C.int32_t, } //export ext_crypto_start_batch_verify_version_1 -func ext_crypto_start_batch_verify_version_1(context unsafe.Pointer) { +func ext_crypto_start_batch_verify_version_1(_ unsafe.Pointer) { logger.Debug("[ext_crypto_start_batch_verify_version_1] executing...") // TODO: fix and re-enable signature verification @@ -856,7 +856,7 @@ func ext_trie_blake2_256_ordered_root_version_1(context unsafe.Pointer, dataSpan } //export ext_trie_blake2_256_verify_proof_version_1 -func ext_trie_blake2_256_verify_proof_version_1(context unsafe.Pointer, a C.int32_t, b, c, d C.int64_t) C.int32_t { +func ext_trie_blake2_256_verify_proof_version_1(_ unsafe.Pointer, _ C.int32_t, _, _, _ C.int64_t) C.int32_t { logger.Debug("[ext_trie_blake2_256_verify_proof_version_1] executing...") logger.Warn("[ext_trie_blake2_256_verify_proof_version_1] unimplemented") return 0 @@ -872,7 +872,7 @@ func ext_misc_print_hex_version_1(context unsafe.Pointer, dataSpan C.int64_t) { } //export ext_misc_print_num_version_1 -func ext_misc_print_num_version_1(context unsafe.Pointer, data C.int64_t) { +func ext_misc_print_num_version_1(_ unsafe.Pointer, data C.int64_t) { logger.Trace("[ext_misc_print_num_version_1] executing...") logger.Debug("[ext_misc_print_num_version_1]", "num", fmt.Sprintf("%d", int64(data))) @@ -932,7 +932,7 @@ func ext_misc_runtime_version_version_1(context unsafe.Pointer, dataSpan C.int64 } //export ext_default_child_storage_read_version_1 -func ext_default_child_storage_read_version_1(context unsafe.Pointer, childStorageKey C.int64_t, key C.int64_t, valueOut C.int64_t, offset C.int32_t) C.int64_t { +func ext_default_child_storage_read_version_1(context unsafe.Pointer, childStorageKey, key, valueOut C.int64_t, offset C.int32_t) C.int64_t { logger.Debug("[ext_default_child_storage_read_version_1] executing...") instanceContext := wasm.IntoInstanceContext(context) @@ -979,7 +979,7 @@ func ext_default_child_storage_clear_version_1(context unsafe.Pointer, childStor } //export ext_default_child_storage_clear_prefix_version_1 -func ext_default_child_storage_clear_prefix_version_1(context unsafe.Pointer, childStorageKey C.int64_t, prefixSpan C.int64_t) { +func ext_default_child_storage_clear_prefix_version_1(context unsafe.Pointer, childStorageKey, prefixSpan C.int64_t) { logger.Debug("[ext_default_child_storage_clear_prefix_version_1] executing...") instanceContext := wasm.IntoInstanceContext(context) @@ -996,7 +996,7 @@ func ext_default_child_storage_clear_prefix_version_1(context unsafe.Pointer, ch } //export ext_default_child_storage_exists_version_1 -func ext_default_child_storage_exists_version_1(context unsafe.Pointer, childStorageKey C.int64_t, key C.int64_t) C.int32_t { +func ext_default_child_storage_exists_version_1(context unsafe.Pointer, childStorageKey, key C.int64_t) C.int32_t { logger.Debug("[ext_default_child_storage_exists_version_1] executing...") instanceContext := wasm.IntoInstanceContext(context) @@ -1036,7 +1036,7 @@ func ext_default_child_storage_get_version_1(context unsafe.Pointer, childStorag } //export ext_default_child_storage_next_key_version_1 -func ext_default_child_storage_next_key_version_1(context unsafe.Pointer, childStorageKey C.int64_t, key C.int64_t) C.int64_t { +func ext_default_child_storage_next_key_version_1(context unsafe.Pointer, childStorageKey, key C.int64_t) C.int64_t { logger.Debug("[ext_default_child_storage_next_key_version_1] executing...") instanceContext := wasm.IntoInstanceContext(context) @@ -1363,7 +1363,7 @@ func ext_offchain_index_set_version_1(context unsafe.Pointer, keySpan, valueSpan } //export ext_offchain_local_storage_clear_version_1 -func ext_offchain_local_storage_clear_version_1(context unsafe.Pointer, a C.int32_t, b C.int64_t) { +func ext_offchain_local_storage_clear_version_1(_ unsafe.Pointer, _ C.int32_t, _ C.int64_t) { logger.Trace("[ext_offchain_local_storage_clear_version_1] executing...") logger.Warn("[ext_offchain_local_storage_clear_version_1] unimplemented") } @@ -1550,7 +1550,7 @@ func ext_offchain_submit_transaction_version_1(context unsafe.Pointer, data C.in } //export ext_offchain_timestamp_version_1 -func ext_offchain_timestamp_version_1(context unsafe.Pointer) C.int64_t { +func ext_offchain_timestamp_version_1(_ unsafe.Pointer) C.int64_t { logger.Trace("[ext_offchain_timestamp_version_1] executing...") logger.Warn("[ext_offchain_timestamp_version_1] unimplemented") return 0 @@ -1591,9 +1591,9 @@ func storageAppend(storage runtime.Storage, key, valueToAppend []byte) error { } // append new length prefix to start of items array - finalVal := append(lengthEnc, valueRes...) - logger.Debug("[ext_storage_append_version_1]", "resulting value", fmt.Sprintf("0x%x", finalVal)) - storage.Set(key, finalVal) + lengthEnc = append(lengthEnc, valueRes...) + logger.Debug("[ext_storage_append_version_1]", "resulting value", fmt.Sprintf("0x%x", lengthEnc)) + storage.Set(key, lengthEnc) return nil } @@ -1618,7 +1618,7 @@ func ext_storage_append_version_1(context unsafe.Pointer, keySpan, valueSpan C.i } //export ext_storage_changes_root_version_1 -func ext_storage_changes_root_version_1(context unsafe.Pointer, parentHashSpan C.int64_t) C.int64_t { +func ext_storage_changes_root_version_1(context unsafe.Pointer, _ C.int64_t) C.int64_t { logger.Trace("[ext_storage_changes_root_version_1] executing...") logger.Debug("[ext_storage_changes_root_version_1] returning None") @@ -1805,7 +1805,7 @@ func ext_storage_root_version_1(context unsafe.Pointer) C.int64_t { } //export ext_storage_set_version_1 -func ext_storage_set_version_1(context unsafe.Pointer, keySpan C.int64_t, valueSpan C.int64_t) { +func ext_storage_set_version_1(context unsafe.Pointer, keySpan, valueSpan C.int64_t) { logger.Trace("[ext_storage_set_version_1] executing...") instanceContext := wasm.IntoInstanceContext(context) diff --git a/lib/runtime/wasmer/test_helpers.go b/lib/runtime/wasmer/test_helpers.go index 556739bca1..ebf4ea3b5d 100644 --- a/lib/runtime/wasmer/test_helpers.go +++ b/lib/runtime/wasmer/test_helpers.go @@ -39,7 +39,7 @@ func NewTestInstance(t *testing.T, targetRuntime string) *Instance { } // NewTestInstanceWithTrie will create a new runtime (polkadot/test) with the supplied trie as the storage -func NewTestInstanceWithTrie(t *testing.T, targetRuntime string, tt *trie.Trie, lvl log.Lvl) *Instance { +func NewTestInstanceWithTrie(t *testing.T, targetRuntime string, tt *trie.Trie, _ log.Lvl) *Instance { fp, cfg := setupConfig(t, targetRuntime, tt, DefaultTestLogLvl, 0) r, err := NewInstanceFromFile(fp, cfg) require.NoError(t, err, "Got error when trying to create new VM", "targetRuntime", targetRuntime) From b1864dcbe2a4ee8062b4c715f916242ca3d192eb Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Fri, 3 Sep 2021 11:25:24 -0400 Subject: [PATCH 10/12] fix lint issues --- lib/runtime/wasmer/imports.go | 30 +++++++++++++++--------------- lib/runtime/wasmer/test_helpers.go | 2 +- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/runtime/wasmer/imports.go b/lib/runtime/wasmer/imports.go index 9d26768c19..0eacd32ccd 100644 --- a/lib/runtime/wasmer/imports.go +++ b/lib/runtime/wasmer/imports.go @@ -157,66 +157,66 @@ func ext_logging_log_version_1(context unsafe.Pointer, level C.int32_t, targetDa } //export ext_logging_max_level_version_1 -func ext_logging_max_level_version_1(_ unsafe.Pointer) C.int32_t { +func ext_logging_max_level_version_1(context unsafe.Pointer) C.int32_t { logger.Trace("[ext_logging_max_level_version_1] executing...") return 4 } //export ext_transaction_index_index_version_1 -func ext_transaction_index_index_version_1(_ unsafe.Pointer, _, _, _ C.int32_t) { +func ext_transaction_index_index_version_1(context unsafe.Pointer, a, b, c C.int32_t) { logger.Trace("[ext_transaction_index_index_version_1] executing...") logger.Warn("[ext_transaction_index_index_version_1] unimplemented") } //export ext_transaction_index_renew_version_1 -func ext_transaction_index_renew_version_1(_ unsafe.Pointer, _, _ C.int32_t) { +func ext_transaction_index_renew_version_1(context unsafe.Pointer, a, b C.int32_t) { logger.Trace("[ext_transaction_index_renew_version_1] executing...") logger.Warn("[ext_transaction_index_renew_version_1] unimplemented") } //export ext_sandbox_instance_teardown_version_1 -func ext_sandbox_instance_teardown_version_1(_ unsafe.Pointer, _ C.int32_t) { +func ext_sandbox_instance_teardown_version_1(context unsafe.Pointer, a C.int32_t) { logger.Trace("[ext_sandbox_instance_teardown_version_1] executing...") logger.Warn("[ext_sandbox_instance_teardown_version_1] unimplemented") } //export ext_sandbox_instantiate_version_1 -func ext_sandbox_instantiate_version_1(_ unsafe.Pointer, _ C.int32_t, _, _ C.int64_t, _ C.int32_t) C.int32_t { +func ext_sandbox_instantiate_version_1(context unsafe.Pointer, a C.int32_t, x, y C.int64_t, z C.int32_t) C.int32_t { logger.Trace("[ext_sandbox_instantiate_version_1] executing...") logger.Warn("[ext_sandbox_instantiate_version_1] unimplemented") return 0 } //export ext_sandbox_invoke_version_1 -func ext_sandbox_invoke_version_1(_ unsafe.Pointer, _ C.int32_t, _, _ C.int64_t, _, _, _ C.int32_t) C.int32_t { +func ext_sandbox_invoke_version_1(context unsafe.Pointer, a C.int32_t, x, y C.int64_t, z, d, e C.int32_t) C.int32_t { logger.Trace("[ext_sandbox_invoke_version_1] executing...") logger.Warn("[ext_sandbox_invoke_version_1] unimplemented") return 0 } //export ext_sandbox_memory_get_version_1 -func ext_sandbox_memory_get_version_1(_ unsafe.Pointer, _, _, _, _ C.int32_t) C.int32_t { +func ext_sandbox_memory_get_version_1(context unsafe.Pointer, a, z, d, e C.int32_t) C.int32_t { logger.Trace("[ext_sandbox_memory_get_version_1] executing...") logger.Warn("[ext_sandbox_memory_get_version_1] unimplemented") return 0 } //export ext_sandbox_memory_new_version_1 -func ext_sandbox_memory_new_version_1(_ unsafe.Pointer, _, _ C.int32_t) C.int32_t { +func ext_sandbox_memory_new_version_1(context unsafe.Pointer, a, z C.int32_t) C.int32_t { logger.Trace("[ext_sandbox_memory_new_version_1] executing...") logger.Warn("[ext_sandbox_memory_new_version_1] unimplemented") return 0 } //export ext_sandbox_memory_set_version_1 -func ext_sandbox_memory_set_version_1(_ unsafe.Pointer, _, _, _, _ C.int32_t) C.int32_t { +func ext_sandbox_memory_set_version_1(context unsafe.Pointer, a, z, d, e C.int32_t) C.int32_t { logger.Trace("[ext_sandbox_memory_set_version_1] executing...") logger.Warn("[ext_sandbox_memory_set_version_1] unimplemented") return 0 } //export ext_sandbox_memory_teardown_version_1 -func ext_sandbox_memory_teardown_version_1(_ unsafe.Pointer, a C.int32_t) { +func ext_sandbox_memory_teardown_version_1(context unsafe.Pointer, a C.int32_t) { logger.Trace("[ext_sandbox_memory_teardown_version_1] executing...") logger.Warn("[ext_sandbox_memory_teardown_version_1] unimplemented") } @@ -715,7 +715,7 @@ func ext_crypto_sr25519_verify_version_2(context unsafe.Pointer, sig C.int32_t, } //export ext_crypto_start_batch_verify_version_1 -func ext_crypto_start_batch_verify_version_1(_ unsafe.Pointer) { +func ext_crypto_start_batch_verify_version_1(context unsafe.Pointer) { logger.Debug("[ext_crypto_start_batch_verify_version_1] executing...") // TODO: fix and re-enable signature verification @@ -856,7 +856,7 @@ func ext_trie_blake2_256_ordered_root_version_1(context unsafe.Pointer, dataSpan } //export ext_trie_blake2_256_verify_proof_version_1 -func ext_trie_blake2_256_verify_proof_version_1(_ unsafe.Pointer, _ C.int32_t, _, _, _ C.int64_t) C.int32_t { +func ext_trie_blake2_256_verify_proof_version_1(context unsafe.Pointer, a C.int32_t, b, c, d C.int64_t) C.int32_t { logger.Debug("[ext_trie_blake2_256_verify_proof_version_1] executing...") logger.Warn("[ext_trie_blake2_256_verify_proof_version_1] unimplemented") return 0 @@ -1363,7 +1363,7 @@ func ext_offchain_index_set_version_1(context unsafe.Pointer, keySpan, valueSpan } //export ext_offchain_local_storage_clear_version_1 -func ext_offchain_local_storage_clear_version_1(_ unsafe.Pointer, _ C.int32_t, _ C.int64_t) { +func ext_offchain_local_storage_clear_version_1(context unsafe.Pointer, a C.int32_t, b C.int64_t) { logger.Trace("[ext_offchain_local_storage_clear_version_1] executing...") logger.Warn("[ext_offchain_local_storage_clear_version_1] unimplemented") } @@ -1550,7 +1550,7 @@ func ext_offchain_submit_transaction_version_1(context unsafe.Pointer, data C.in } //export ext_offchain_timestamp_version_1 -func ext_offchain_timestamp_version_1(_ unsafe.Pointer) C.int64_t { +func ext_offchain_timestamp_version_1(context unsafe.Pointer) C.int64_t { logger.Trace("[ext_offchain_timestamp_version_1] executing...") logger.Warn("[ext_offchain_timestamp_version_1] unimplemented") return 0 @@ -1618,7 +1618,7 @@ func ext_storage_append_version_1(context unsafe.Pointer, keySpan, valueSpan C.i } //export ext_storage_changes_root_version_1 -func ext_storage_changes_root_version_1(context unsafe.Pointer, _ C.int64_t) C.int64_t { +func ext_storage_changes_root_version_1(context unsafe.Pointer, parentHashSpan C.int64_t) C.int64_t { logger.Trace("[ext_storage_changes_root_version_1] executing...") logger.Debug("[ext_storage_changes_root_version_1] returning None") diff --git a/lib/runtime/wasmer/test_helpers.go b/lib/runtime/wasmer/test_helpers.go index ebf4ea3b5d..556739bca1 100644 --- a/lib/runtime/wasmer/test_helpers.go +++ b/lib/runtime/wasmer/test_helpers.go @@ -39,7 +39,7 @@ func NewTestInstance(t *testing.T, targetRuntime string) *Instance { } // NewTestInstanceWithTrie will create a new runtime (polkadot/test) with the supplied trie as the storage -func NewTestInstanceWithTrie(t *testing.T, targetRuntime string, tt *trie.Trie, _ log.Lvl) *Instance { +func NewTestInstanceWithTrie(t *testing.T, targetRuntime string, tt *trie.Trie, lvl log.Lvl) *Instance { fp, cfg := setupConfig(t, targetRuntime, tt, DefaultTestLogLvl, 0) r, err := NewInstanceFromFile(fp, cfg) require.NoError(t, err, "Got error when trying to create new VM", "targetRuntime", targetRuntime) From defa63fb6bb38b292817479e9853113dd4974b9e Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Tue, 7 Sep 2021 17:18:26 -0400 Subject: [PATCH 11/12] add db check, rename var --- dot/core/test_helpers.go | 8 ++++++-- dot/sync/test_helpers.go | 9 +++++++-- lib/babe/babe_test.go | 12 +++++++++--- lib/runtime/wasmer/imports.go | 8 ++++---- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/dot/core/test_helpers.go b/dot/core/test_helpers.go index c4971b494b..9c4c1426fc 100644 --- a/dot/core/test_helpers.go +++ b/dot/core/test_helpers.go @@ -109,8 +109,12 @@ func NewTestService(t *testing.T, cfg *Config) *Service { nodeStorage := runtime.NodeStorage{} - nodeStorage.BaseDB, err = utils.SetupDatabase(filepath.Join(testDatadirPath, "offline_storage"), false) - require.NoError(t, err) + if stateSrvc != nil { + nodeStorage.BaseDB = stateSrvc.Base + } else { + nodeStorage.BaseDB, err = utils.SetupDatabase(filepath.Join(testDatadirPath, "offline_storage"), false) + require.NoError(t, err) + } rtCfg.NodeStorage = nodeStorage diff --git a/dot/sync/test_helpers.go b/dot/sync/test_helpers.go index 592b81debf..4a4df5fd31 100644 --- a/dot/sync/test_helpers.go +++ b/dot/sync/test_helpers.go @@ -101,8 +101,13 @@ func NewTestSyncer(t *testing.T, usePolkadotGenesis bool) *Service { require.NoError(t, err) nodeStorage := runtime.NodeStorage{} - nodeStorage.BaseDB, err = utils.SetupDatabase(filepath.Join(testDatadirPath, "offline_storage"), false) - require.NoError(t, err) + if stateSrvc != nil { + nodeStorage.BaseDB = stateSrvc.Base + } else { + nodeStorage.BaseDB, err = utils.SetupDatabase(filepath.Join(testDatadirPath, "offline_storage"), false) + require.NoError(t, err) + } + rtCfg.NodeStorage = nodeStorage instance, err := wasmer.NewRuntimeFromGenesis(gen, rtCfg) //nolint diff --git a/lib/babe/babe_test.go b/lib/babe/babe_test.go index 22b54c5388..5c9529099b 100644 --- a/lib/babe/babe_test.go +++ b/lib/babe/babe_test.go @@ -101,6 +101,7 @@ func createTestService(t *testing.T, cfg *ServiceConfig) *Service { testDatadirPath, err := ioutil.TempDir("/tmp", "test-datadir-*") //nolint + var dbSrv *state.Service if cfg.BlockState == nil || cfg.StorageState == nil || cfg.EpochState == nil { require.NoError(t, err) @@ -108,7 +109,7 @@ func createTestService(t *testing.T, cfg *ServiceConfig) *Service { Path: testDatadirPath, LogLevel: log.LvlInfo, } - dbSrv := state.NewService(config) + dbSrv = state.NewService(config) dbSrv.UseMemDB() if cfg.EpochLength > 0 { @@ -141,8 +142,13 @@ func createTestService(t *testing.T, cfg *ServiceConfig) *Service { require.NoError(t, err) nodeStorage := runtime.NodeStorage{} - nodeStorage.BaseDB, err = utils.SetupDatabase(filepath.Join(testDatadirPath, "offline_storage"), false) - require.NoError(t, err) + if dbSrv != nil { + nodeStorage.BaseDB = dbSrv.Base + } else { + nodeStorage.BaseDB, err = utils.SetupDatabase(filepath.Join(testDatadirPath, "offline_storage"), false) + require.NoError(t, err) + } + rtCfg.NodeStorage = nodeStorage cfg.Runtime, err = wasmer.NewRuntimeFromGenesis(gen, rtCfg) diff --git a/lib/runtime/wasmer/imports.go b/lib/runtime/wasmer/imports.go index 0eacd32ccd..34e3f000fa 100644 --- a/lib/runtime/wasmer/imports.go +++ b/lib/runtime/wasmer/imports.go @@ -1584,16 +1584,16 @@ func storageAppend(storage runtime.Storage, key, valueToAppend []byte) error { nextLength = big.NewInt(0).Add(currLength, big.NewInt(1)) } - lengthEnc, err := scale.Encode(nextLength) + finalVal, err := scale.Encode(nextLength) if err != nil { logger.Trace("[ext_storage_append_version_1] failed to encode new length", "error", err) return err } // append new length prefix to start of items array - lengthEnc = append(lengthEnc, valueRes...) - logger.Debug("[ext_storage_append_version_1]", "resulting value", fmt.Sprintf("0x%x", lengthEnc)) - storage.Set(key, lengthEnc) + finalVal = append(finalVal, valueRes...) + logger.Debug("[ext_storage_append_version_1]", "resulting value", fmt.Sprintf("0x%x", finalVal)) + storage.Set(key, finalVal) return nil } From b10fd88c15b82a820ceafd977f8bdc55416a9e6e Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Fri, 10 Sep 2021 16:43:11 -0400 Subject: [PATCH 12/12] add init BaseDB to createRuntimeStorage --- dot/services.go | 1 + 1 file changed, 1 insertion(+) diff --git a/dot/services.go b/dot/services.go index 8ce7bf365e..c6e8bf5830 100644 --- a/dot/services.go +++ b/dot/services.go @@ -99,6 +99,7 @@ func createRuntimeStorage(st *state.Service) (*runtime.NodeStorage, error) { return &runtime.NodeStorage{ LocalStorage: localStorage, PersistentStorage: chaindb.NewTable(st.DB(), "offlinestorage"), + BaseDB: st.Base, }, nil }