From 0f67834602165742c5a0c01756a020a98c4504a4 Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Fri, 24 Sep 2021 11:45:42 -0400 Subject: [PATCH 1/8] stub Test_ext_offchain_local_storage_clear_version_1 --- lib/runtime/wasmer/imports_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/runtime/wasmer/imports_test.go b/lib/runtime/wasmer/imports_test.go index aba4174383..a302154170 100644 --- a/lib/runtime/wasmer/imports_test.go +++ b/lib/runtime/wasmer/imports_test.go @@ -179,6 +179,22 @@ func Test_ext_storage_clear_version_1(t *testing.T) { require.Nil(t, val) } +func Test_ext_offchain_local_storage_clear_version_1(t *testing.T) { + inst := NewTestInstance(t, runtime.HOST_API_TEST_RUNTIME) + + testkey := []byte("noot") + inst.ctx.Storage.Set(testkey, []byte{1}) + + enc, err := scale.Encode(testkey) + require.NoError(t, err) + + _, err = inst.Exec("rtm_ext_offchain_local_storage_clear_version_1", enc) + require.NoError(t, err) + + val := inst.ctx.Storage.Get(testkey) + require.Nil(t, val) +} + func Test_ext_storage_clear_prefix_version_1_hostAPI(t *testing.T) { inst := NewTestInstance(t, runtime.HOST_API_TEST_RUNTIME) From 6c45076ad02ecac0c8a1a7f295ec7aff1f3055f7 Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Fri, 24 Sep 2021 17:03:31 -0400 Subject: [PATCH 2/8] add to tests --- lib/runtime/wasmer/imports_test.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/runtime/wasmer/imports_test.go b/lib/runtime/wasmer/imports_test.go index a302154170..4ff3ab6930 100644 --- a/lib/runtime/wasmer/imports_test.go +++ b/lib/runtime/wasmer/imports_test.go @@ -185,10 +185,14 @@ func Test_ext_offchain_local_storage_clear_version_1(t *testing.T) { testkey := []byte("noot") inst.ctx.Storage.Set(testkey, []byte{1}) - enc, err := scale.Encode(testkey) + kind := int32(1) + encKind, err := scale.Encode(kind) + require.NoError(t, err) + + encKey, err := scale.Encode(testkey) require.NoError(t, err) - _, err = inst.Exec("rtm_ext_offchain_local_storage_clear_version_1", enc) + _, err = inst.Exec("rtm_ext_offchain_local_storage_clear_version_1", append(encKind, encKey...)) require.NoError(t, err) val := inst.ctx.Storage.Get(testkey) From a03fb5df03c357bb99c51f7ea7afc9e577db24a4 Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Thu, 30 Sep 2021 19:19:18 -0400 Subject: [PATCH 3/8] implement ext_offchain_local_storage_clear_version_1 --- dot/state/base.go | 5 +++++ lib/runtime/constants.go | 4 +++- lib/runtime/interface.go | 1 + lib/runtime/wasmer/imports.go | 24 ++++++++++++++++++++-- lib/runtime/wasmer/imports_test.go | 32 ++++++++++++++++++++++++++---- 5 files changed, 59 insertions(+), 7 deletions(-) diff --git a/dot/state/base.go b/dot/state/base.go index 060247beb4..9a2d64dfb2 100644 --- a/dot/state/base.go +++ b/dot/state/base.go @@ -135,6 +135,11 @@ func (s *BaseState) Get(key []byte) ([]byte, error) { return s.db.Get(key) } +// Del deletes key from database +func (s *BaseState) Del(key []byte) error { + return s.db.Del(key) +} + func (s *BaseState) storeSkipToEpoch(epoch uint64) error { buf := make([]byte, 8) binary.LittleEndian.PutUint64(buf, epoch) diff --git a/lib/runtime/constants.go b/lib/runtime/constants.go index a691a7fc67..16440cd9f7 100644 --- a/lib/runtime/constants.go +++ b/lib/runtime/constants.go @@ -42,7 +42,9 @@ const ( // v0.8 test API wasm HOST_API_TEST_RUNTIME = "hostapi_runtime" HOST_API_TEST_RUNTIME_FP = "hostapi_runtime.compact.wasm" - HOST_API_TEST_RUNTIME_URL = "https://github.com/noot/polkadot-spec/blob/master/test/hostapi_runtime.compact.wasm?raw=true" + //HOST_API_TEST_RUNTIME_URL = "https://github.com/noot/polkadot-spec/blob/master/test/hostapi_runtime.compact.wasm?raw=true" + // todo (ed) use above URL once ed/add_rtm_ext_default_child_storage_storage_kill_version_2 has been merged to master + HOST_API_TEST_RUNTIME_URL = "https://github.com/noot/polkadot-spec/blob/ed/impl_rtm_ext_offchain_local_storage_clear_version_1/test/hostapi_runtime.compact.wasm?raw=true" // v0.8 substrate runtime with modified name and babe C=(1, 1) DEV_RUNTIME = "dev_runtime" diff --git a/lib/runtime/interface.go b/lib/runtime/interface.go index 1448505524..dcc7cb8e16 100644 --- a/lib/runtime/interface.go +++ b/lib/runtime/interface.go @@ -86,6 +86,7 @@ type BasicNetwork interface { type BasicStorage interface { Put(key []byte, value []byte) error Get(key []byte) ([]byte, error) + Del(key []byte) error } // TransactionState interface for adding transactions to pool diff --git a/lib/runtime/wasmer/imports.go b/lib/runtime/wasmer/imports.go index 34e3f000fa..2acd19c9d7 100644 --- a/lib/runtime/wasmer/imports.go +++ b/lib/runtime/wasmer/imports.go @@ -1363,9 +1363,29 @@ 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(context unsafe.Pointer, kind C.int32_t, key C.int64_t) { logger.Trace("[ext_offchain_local_storage_clear_version_1] executing...") - logger.Warn("[ext_offchain_local_storage_clear_version_1] unimplemented") + instanceContext := wasm.IntoInstanceContext(context) + runtimeCtx := instanceContext.Data().(*runtime.Context) + + storageKey := asMemorySlice(instanceContext, key) + + memory := instanceContext.Memory().Data() + + kindInt := binary.LittleEndian.Uint32(memory[kind:kind+4]) + + var err error + + switch runtime.NodeStorageType(kindInt) { + case runtime.NodeStorageTypePersistent: + err = runtimeCtx.NodeStorage.PersistentStorage.Del(storageKey) + case runtime.NodeStorageTypeLocal: + err = runtimeCtx.NodeStorage.LocalStorage.Del(storageKey) + } + + if err != nil { + logger.Error("[ext_offchain_local_storage_clear_version_1] failed to clear value from storage", "error", err) + } } //export ext_offchain_is_validator_version_1 diff --git a/lib/runtime/wasmer/imports_test.go b/lib/runtime/wasmer/imports_test.go index ec07781749..6d4bae08c6 100644 --- a/lib/runtime/wasmer/imports_test.go +++ b/lib/runtime/wasmer/imports_test.go @@ -179,11 +179,12 @@ func Test_ext_storage_clear_version_1(t *testing.T) { require.Nil(t, val) } -func Test_ext_offchain_local_storage_clear_version_1(t *testing.T) { +func Test_ext_offchain_local_storage_clear_version_1_Persistent(t *testing.T) { inst := NewTestInstance(t, runtime.HOST_API_TEST_RUNTIME) - testkey := []byte("noot") - inst.ctx.Storage.Set(testkey, []byte{1}) + testkey := []byte("key1") + err := inst.NodeStorage().PersistentStorage.Put(testkey, []byte{1}) + require.NoError(t, err) kind := int32(1) encKind, err := scale.Encode(kind) @@ -195,7 +196,30 @@ func Test_ext_offchain_local_storage_clear_version_1(t *testing.T) { _, err = inst.Exec("rtm_ext_offchain_local_storage_clear_version_1", append(encKind, encKey...)) require.NoError(t, err) - val := inst.ctx.Storage.Get(testkey) + val, err := inst.NodeStorage().PersistentStorage.Get(testkey) + require.EqualError(t, err, "Key not found") + require.Nil(t, val) +} + +func Test_ext_offchain_local_storage_clear_version_1_Local(t *testing.T) { + inst := NewTestInstance(t, runtime.HOST_API_TEST_RUNTIME) + + testkey := []byte("key1") + err := inst.NodeStorage().LocalStorage.Put(testkey, []byte{1}) + require.NoError(t, err) + + kind := int32(2) + encKind, err := scale.Encode(kind) + require.NoError(t, err) + + encKey, err := scale.Encode(testkey) + require.NoError(t, err) + + _, err = inst.Exec("rtm_ext_offchain_local_storage_clear_version_1", append(encKind, encKey...)) + require.NoError(t, err) + + val, err := inst.NodeStorage().LocalStorage.Get(testkey) + require.EqualError(t, err, "Key not found") require.Nil(t, val) } From f1f84405bcbb324a88a59a6420d6ed162c23a778 Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Fri, 1 Oct 2021 10:43:53 -0400 Subject: [PATCH 4/8] lint --- lib/runtime/constants.go | 4 ++-- lib/runtime/wasmer/imports.go | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/runtime/constants.go b/lib/runtime/constants.go index 16440cd9f7..d36c0e1561 100644 --- a/lib/runtime/constants.go +++ b/lib/runtime/constants.go @@ -40,8 +40,8 @@ const ( POLKADOT_RUNTIME_URL = "https://github.com/noot/polkadot/blob/noot/v0.8.25/polkadot_runtime.wasm?raw=true" // v0.8 test API wasm - HOST_API_TEST_RUNTIME = "hostapi_runtime" - HOST_API_TEST_RUNTIME_FP = "hostapi_runtime.compact.wasm" + HOST_API_TEST_RUNTIME = "hostapi_runtime" + HOST_API_TEST_RUNTIME_FP = "hostapi_runtime.compact.wasm" //HOST_API_TEST_RUNTIME_URL = "https://github.com/noot/polkadot-spec/blob/master/test/hostapi_runtime.compact.wasm?raw=true" // todo (ed) use above URL once ed/add_rtm_ext_default_child_storage_storage_kill_version_2 has been merged to master HOST_API_TEST_RUNTIME_URL = "https://github.com/noot/polkadot-spec/blob/ed/impl_rtm_ext_offchain_local_storage_clear_version_1/test/hostapi_runtime.compact.wasm?raw=true" diff --git a/lib/runtime/wasmer/imports.go b/lib/runtime/wasmer/imports.go index 2acd19c9d7..4c65c4debb 100644 --- a/lib/runtime/wasmer/imports.go +++ b/lib/runtime/wasmer/imports.go @@ -1371,8 +1371,7 @@ func ext_offchain_local_storage_clear_version_1(context unsafe.Pointer, kind C.i storageKey := asMemorySlice(instanceContext, key) memory := instanceContext.Memory().Data() - - kindInt := binary.LittleEndian.Uint32(memory[kind:kind+4]) + kindInt := binary.LittleEndian.Uint32(memory[kind : kind+4]) var err error From 6bc338e513943e3b34b4a74f618bef20a8290459 Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Fri, 1 Oct 2021 15:16:09 -0400 Subject: [PATCH 5/8] update HOST_API_TEST_RUNTIME_URL to use master branch --- lib/runtime/constants.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/runtime/constants.go b/lib/runtime/constants.go index d36c0e1561..a691a7fc67 100644 --- a/lib/runtime/constants.go +++ b/lib/runtime/constants.go @@ -40,11 +40,9 @@ const ( POLKADOT_RUNTIME_URL = "https://github.com/noot/polkadot/blob/noot/v0.8.25/polkadot_runtime.wasm?raw=true" // v0.8 test API wasm - HOST_API_TEST_RUNTIME = "hostapi_runtime" - HOST_API_TEST_RUNTIME_FP = "hostapi_runtime.compact.wasm" - //HOST_API_TEST_RUNTIME_URL = "https://github.com/noot/polkadot-spec/blob/master/test/hostapi_runtime.compact.wasm?raw=true" - // todo (ed) use above URL once ed/add_rtm_ext_default_child_storage_storage_kill_version_2 has been merged to master - HOST_API_TEST_RUNTIME_URL = "https://github.com/noot/polkadot-spec/blob/ed/impl_rtm_ext_offchain_local_storage_clear_version_1/test/hostapi_runtime.compact.wasm?raw=true" + HOST_API_TEST_RUNTIME = "hostapi_runtime" + HOST_API_TEST_RUNTIME_FP = "hostapi_runtime.compact.wasm" + HOST_API_TEST_RUNTIME_URL = "https://github.com/noot/polkadot-spec/blob/master/test/hostapi_runtime.compact.wasm?raw=true" // v0.8 substrate runtime with modified name and babe C=(1, 1) DEV_RUNTIME = "dev_runtime" From 24302c72ba577548e40bac8547ac6e724f5843e0 Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Mon, 11 Oct 2021 12:51:47 -0400 Subject: [PATCH 6/8] handle merge conflicts --- lib/runtime/wasmer/imports_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/runtime/wasmer/imports_test.go b/lib/runtime/wasmer/imports_test.go index f16fa84193..6ca0feed2a 100644 --- a/lib/runtime/wasmer/imports_test.go +++ b/lib/runtime/wasmer/imports_test.go @@ -194,10 +194,10 @@ func Test_ext_offchain_local_storage_clear_version_1_Persistent(t *testing.T) { require.NoError(t, err) kind := int32(1) - encKind, err := scale.Encode(kind) + encKind, err := scale.Marshal(kind) require.NoError(t, err) - encKey, err := scale.Encode(testkey) + encKey, err := scale.Marshal(testkey) require.NoError(t, err) _, err = inst.Exec("rtm_ext_offchain_local_storage_clear_version_1", append(encKind, encKey...)) @@ -216,10 +216,10 @@ func Test_ext_offchain_local_storage_clear_version_1_Local(t *testing.T) { require.NoError(t, err) kind := int32(2) - encKind, err := scale.Encode(kind) + encKind, err := scale.Marshal(kind) require.NoError(t, err) - encKey, err := scale.Encode(testkey) + encKey, err := scale.Marshal(testkey) require.NoError(t, err) _, err = inst.Exec("rtm_ext_offchain_local_storage_clear_version_1", append(encKind, encKey...)) From cd212038f414438b1852d39e54e9234d5413deaa Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Wed, 13 Oct 2021 17:57:10 -0400 Subject: [PATCH 7/8] update wasm url --- lib/runtime/constants.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/runtime/constants.go b/lib/runtime/constants.go index 42408f6042..643ffb4c28 100644 --- a/lib/runtime/constants.go +++ b/lib/runtime/constants.go @@ -47,7 +47,7 @@ const ( // v0.8 test API wasm HOST_API_TEST_RUNTIME = "hostapi_runtime" HOST_API_TEST_RUNTIME_FP = "hostapi_runtime.compact.wasm" - HOST_API_TEST_RUNTIME_URL = "https://github.com/noot/polkadot-spec/blob/e8770476a4b8445cddd2bd1e8f0060a83eaccb38/test/hostapi_runtime.compact.wasm?raw=true" + HOST_API_TEST_RUNTIME_URL = "https://github.com/noot/polkadot-spec/blob/master/test/hostapi_runtime.compact.wasm?raw=true" // v0.8 substrate runtime with modified name and babe C=(1, 1) DEV_RUNTIME = "dev_runtime" From f0655007e0443691c53ce922b988483a8d096819 Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Tue, 19 Oct 2021 13:49:04 -0400 Subject: [PATCH 8/8] update host runtime url --- lib/runtime/constants.go | 2 +- lib/runtime/wasmer/imports.go | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/runtime/constants.go b/lib/runtime/constants.go index 2ed865d527..fb63ab3917 100644 --- a/lib/runtime/constants.go +++ b/lib/runtime/constants.go @@ -47,7 +47,7 @@ const ( // v0.8 test API wasm HOST_API_TEST_RUNTIME = "hostapi_runtime" HOST_API_TEST_RUNTIME_FP = "hostapi_runtime.compact.wasm" - HOST_API_TEST_RUNTIME_URL = "https://github.com/noot/polkadot-spec/blob/master/test/hostapi_runtime.compact.wasm?raw=true" + HOST_API_TEST_RUNTIME_URL = "https://github.com/ChainSafe/polkadot-spec/blob/f9f8c94397d155c4f2edc9c59828dc4ef2c62dd3/test/hostapi_runtime.compact.wasm?raw=true" // v0.8 substrate runtime with modified name and babe C=(1, 1) DEV_RUNTIME = "dev_runtime" diff --git a/lib/runtime/wasmer/imports.go b/lib/runtime/wasmer/imports.go index 8a349d85eb..4be947a08d 100644 --- a/lib/runtime/wasmer/imports.go +++ b/lib/runtime/wasmer/imports.go @@ -87,6 +87,7 @@ package wasmer // extern int32_t ext_offchain_random_seed_version_1(void *context); // extern int64_t ext_offchain_submit_transaction_version_1(void *context, int64_t a); // extern int64_t ext_offchain_timestamp_version_1(void *context); +// extern void ext_offchain_sleep_until_version_1(void *context, int64_t a); // // extern void ext_storage_append_version_1(void *context, int64_t a, int64_t b); // extern int64_t ext_storage_changes_root_version_1(void *context, int64_t a); @@ -1576,6 +1577,12 @@ func ext_offchain_timestamp_version_1(context unsafe.Pointer) C.int64_t { return 0 } +//export ext_offchain_sleep_until_version_1 +func ext_offchain_sleep_until_version_1(_ unsafe.Pointer, deadline C.int64_t) { + logger.Trace("executing...") + logger.Warn("unimplemented") +} + func storageAppend(storage runtime.Storage, key, valueToAppend []byte) error { nextLength := big.NewInt(1) var valueRes []byte @@ -2191,7 +2198,10 @@ func ImportsNodeRuntime() (*wasm.Imports, error) { //nolint if err != nil { return nil, err } - + _, err = imports.Append("ext_offchain_sleep_until_version_1", ext_offchain_sleep_until_version_1, C.ext_offchain_sleep_until_version_1) + if err != nil { + return nil, err + } _, err = imports.Append("ext_sandbox_instance_teardown_version_1", ext_sandbox_instance_teardown_version_1, C.ext_sandbox_instance_teardown_version_1) if err != nil { return nil, err