From 249952c0b64352aa9098bedeee5fe0999b9c7d97 Mon Sep 17 00:00:00 2001 From: noot Date: Fri, 18 Mar 2022 15:42:57 -0400 Subject: [PATCH 1/5] stub ext_storage_root_version_2 and ext_trie_blake2_256_ordered_root_version_2 --- lib/runtime/constants.go | 7 ++- lib/runtime/test_helpers.go | 2 + lib/runtime/wasmer/exports_test.go | 35 +++++++++++++ lib/runtime/wasmer/imports.go | 82 ++++++++++++++++++++++++++++++ 4 files changed, 125 insertions(+), 1 deletion(-) diff --git a/lib/runtime/constants.go b/lib/runtime/constants.go index 458f6d3af2..06554cf206 100644 --- a/lib/runtime/constants.go +++ b/lib/runtime/constants.go @@ -7,7 +7,7 @@ import ( "github.com/ChainSafe/gossamer/lib/common" ) -var runtimes = []string{HOST_API_TEST_RUNTIME, POLKADOT_RUNTIME, NODE_RUNTIME, DEV_RUNTIME} +var runtimes = []string{HOST_API_TEST_RUNTIME, POLKADOT_RUNTIME, POLKADOT_RUNTIME_v0917, NODE_RUNTIME, DEV_RUNTIME} //nolint:revive const ( @@ -26,6 +26,11 @@ const ( POLKADOT_RUNTIME_FP_v0910 = "polkadot_runtime-v9100.compact.wasm" POLKADOT_RUNTIME_URL_v0910 = "https://github.com/paritytech/polkadot/releases/download/v0.9.10/polkadot_runtime-v9100.compact.wasm?raw=true" //nolint:lll + // v0.9.17 polkadot runtime + POLKADOT_RUNTIME_v0917 = "polkadot_runtime-v917" + POLKADOT_RUNTIME_FP_v0917 = "polkadot_runtime-v917.compact.wasm" + POLKADOT_RUNTIME_URL_v0917 = "https://github.com/paritytech/polkadot/releases/download/v0.9.17/polkadot_runtime-v9170.compact.wasm?raw=true" //nolint:lll + // v0.8 polkadot runtime POLKADOT_RUNTIME = "polkadot_runtime" POLKADOT_RUNTIME_FP = "polkadot_runtime.compact.wasm" diff --git a/lib/runtime/test_helpers.go b/lib/runtime/test_helpers.go index c8b6e399fe..8d437fa791 100644 --- a/lib/runtime/test_helpers.go +++ b/lib/runtime/test_helpers.go @@ -49,6 +49,8 @@ func GetRuntimeVars(targetRuntime string) (string, string) { return GetAbsolutePath(NODE_RUNTIME_FP), NODE_RUNTIME_URL case NODE_RUNTIME_v098: return GetAbsolutePath(NODE_RUNTIME_FP_v098), NODE_RUNTIME_URL_v098 + case POLKADOT_RUNTIME_v0917: + return GetAbsolutePath(POLKADOT_RUNTIME_FP_v0917), POLKADOT_RUNTIME_URL_v0917 case POLKADOT_RUNTIME_v0910: return GetAbsolutePath(POLKADOT_RUNTIME_FP_v0910), POLKADOT_RUNTIME_URL_v0910 case POLKADOT_RUNTIME: diff --git a/lib/runtime/wasmer/exports_test.go b/lib/runtime/wasmer/exports_test.go index ccb83fa45e..6ac88bd723 100644 --- a/lib/runtime/wasmer/exports_test.go +++ b/lib/runtime/wasmer/exports_test.go @@ -88,6 +88,41 @@ func TestInstance_Version_PolkadotRuntime_v0910(t *testing.T) { require.Equal(t, expected.TransactionVersion(), version.TransactionVersion()) } +func TestInstance_Version_PolkadotRuntime_v0917(t *testing.T) { + expected := runtime.NewVersionData( + []byte("polkadot"), + []byte("parity-polkadot"), + 0, + 9170, + 0, + nil, + 11, + ) + + _, cfg := setupConfig(t, runtime.POLKADOT_RUNTIME_v0917, nil, DefaultTestLogLvl, 0) + instance, err := NewInstanceFromFile("/home/e/polkadot/target/release/wbuild/polkadot-runtime/polkadot_runtime.wasm", cfg) + require.NoError(t, err) + + //instance := NewTestInstance(t, runtime.POLKADOT_RUNTIME_v0917) + version, err := instance.Version() + require.NoError(t, err) + + t.Logf("SpecName: %s\n", version.SpecName()) + t.Logf("ImplName: %s\n", version.ImplName()) + t.Logf("AuthoringVersion: %d\n", version.AuthoringVersion()) + t.Logf("SpecVersion: %d\n", version.SpecVersion()) + t.Logf("ImplVersion: %d\n", version.ImplVersion()) + t.Logf("TransactionVersion: %d\n", version.TransactionVersion()) + + require.Equal(t, 14, len(version.APIItems())) + require.Equal(t, expected.SpecName(), version.SpecName()) + require.Equal(t, expected.ImplName(), version.ImplName()) + require.Equal(t, expected.AuthoringVersion(), version.AuthoringVersion()) + require.Equal(t, expected.SpecVersion(), version.SpecVersion()) + require.Equal(t, expected.ImplVersion(), version.ImplVersion()) + require.Equal(t, expected.TransactionVersion(), version.TransactionVersion()) +} + func TestInstance_Version_PolkadotRuntime(t *testing.T) { expected := runtime.NewVersionData( []byte("polkadot"), diff --git a/lib/runtime/wasmer/imports.go b/lib/runtime/wasmer/imports.go index 529f5ddb9c..7d34a14528 100644 --- a/lib/runtime/wasmer/imports.go +++ b/lib/runtime/wasmer/imports.go @@ -35,6 +35,7 @@ package wasmer // // extern int32_t ext_trie_blake2_256_root_version_1(void *context, int64_t a); // extern int32_t ext_trie_blake2_256_ordered_root_version_1(void *context, int64_t a); +// extern int32_t ext_trie_blake2_256_ordered_root_version_2(void *context, int64_t a, int32_t b); // extern int32_t ext_trie_blake2_256_verify_proof_version_1(void *context, int32_t a, int64_t b, int64_t c, int64_t d); // // extern int64_t ext_misc_runtime_version_version_1(void *context, int64_t a); @@ -91,6 +92,7 @@ package wasmer // extern int64_t ext_storage_read_version_1(void *context, int64_t a, int64_t b, int32_t c); // extern void ext_storage_rollback_transaction_version_1(void *context); // extern int64_t ext_storage_root_version_1(void *context); +// extern int64_t ext_storage_root_version_2(void *context, int32_t a); // extern void ext_storage_set_version_1(void *context, int64_t a, int64_t b); // extern void ext_storage_start_transaction_version_1(void *context); // @@ -871,6 +873,54 @@ func ext_trie_blake2_256_ordered_root_version_1(context unsafe.Pointer, dataSpan return C.int32_t(ptr) } +//export ext_trie_blake2_256_ordered_root_version_2 +func ext_trie_blake2_256_ordered_root_version_2(context unsafe.Pointer, dataSpan C.int64_t, version C.int32_t) C.int32_t { + logger.Debug("executing...") + + instanceContext := wasm.IntoInstanceContext(context) + memory := instanceContext.Memory().Data() + runtimeCtx := instanceContext.Data().(*runtime.Context) + data := asMemorySlice(instanceContext, dataSpan) + + t := trie.NewEmptyTrie() + var values [][]byte + err := scale.Unmarshal(data, &values) + if err != nil { + logger.Errorf("[ext_trie_blake2_256_ordered_root_version_2]: %s", err) + return 0 + } + + for i, val := range values { + key, err := scale.Marshal(big.NewInt(int64(i))) + if err != nil { + logger.Errorf("[ext_trie_blake2_256_ordered_root_version_2]: %s", err) + return 0 + } + logger.Tracef( + "put key=0x%x and value=0x%x", + key, val) + + t.Put(key, val) + } + + // allocate memory for value and copy value to memory + ptr, err := runtimeCtx.Allocator.Allocate(32) + if err != nil { + logger.Errorf("[ext_trie_blake2_256_ordered_root_version_2]: %s", err) + return 0 + } + + hash, err := t.Hash() + if err != nil { + logger.Errorf("[ext_trie_blake2_256_ordered_root_version_2]: %s", err) + return 0 + } + + logger.Debugf("[ext_trie_blake2_256_ordered_root_version_2]: root hash is %s", hash) + copy(memory[ptr:ptr+32], hash[:]) + return C.int32_t(ptr) +} + //export ext_trie_blake2_256_verify_proof_version_1 func ext_trie_blake2_256_verify_proof_version_1(context unsafe.Pointer, rootSpan C.int32_t, proofSpan, keySpan, valueSpan C.int64_t) C.int32_t { logger.Debug("executing...") @@ -2055,6 +2105,30 @@ func ext_storage_root_version_1(context unsafe.Pointer) C.int64_t { return C.int64_t(rootSpan) } +//export ext_storage_root_version_2 +func ext_storage_root_version_2(context unsafe.Pointer, version C.int32_t) C.int64_t { + logger.Trace("executing...") + + instanceContext := wasm.IntoInstanceContext(context) + storage := instanceContext.Data().(*runtime.Context).Storage + + root, err := storage.Root() + if err != nil { + logger.Errorf("failed to get storage root: %s", err) + return 0 + } + + logger.Debugf("root hash is: %s", root) + + rootSpan, err := toWasmMemory(instanceContext, root[:]) + if err != nil { + logger.Errorf("failed to allocate: %s", err) + return 0 + } + + return C.int64_t(rootSpan) +} + //export ext_storage_set_version_1 func ext_storage_set_version_1(context unsafe.Pointer, keySpan, valueSpan C.int64_t) { logger.Trace("executing...") @@ -2530,6 +2604,10 @@ func ImportsNodeRuntime() (*wasm.Imports, error) { //nolint:gocyclo if err != nil { return nil, err } + _, err = imports.Append("ext_storage_root_version_2", ext_storage_root_version_2, C.ext_storage_root_version_2) + if err != nil { + return nil, err + } _, err = imports.Append("ext_storage_set_version_1", ext_storage_set_version_1, C.ext_storage_set_version_1) if err != nil { return nil, err @@ -2543,6 +2621,10 @@ func ImportsNodeRuntime() (*wasm.Imports, error) { //nolint:gocyclo if err != nil { return nil, err } + _, err = imports.Append("ext_trie_blake2_256_ordered_root_version_2", ext_trie_blake2_256_ordered_root_version_2, C.ext_trie_blake2_256_ordered_root_version_2) + if err != nil { + return nil, err + } _, err = imports.Append("ext_trie_blake2_256_root_version_1", ext_trie_blake2_256_root_version_1, C.ext_trie_blake2_256_root_version_1) if err != nil { return nil, err From 2d9ca5a4f56f7b279629eeea46248b5f39861603 Mon Sep 17 00:00:00 2001 From: noot Date: Mon, 21 Mar 2022 13:29:16 -0400 Subject: [PATCH 2/5] update v0.9.17 runtime url --- lib/runtime/constants.go | 2 +- lib/runtime/wasmer/exports_test.go | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/runtime/constants.go b/lib/runtime/constants.go index 06554cf206..378aacb556 100644 --- a/lib/runtime/constants.go +++ b/lib/runtime/constants.go @@ -29,7 +29,7 @@ const ( // v0.9.17 polkadot runtime POLKADOT_RUNTIME_v0917 = "polkadot_runtime-v917" POLKADOT_RUNTIME_FP_v0917 = "polkadot_runtime-v917.compact.wasm" - POLKADOT_RUNTIME_URL_v0917 = "https://github.com/paritytech/polkadot/releases/download/v0.9.17/polkadot_runtime-v9170.compact.wasm?raw=true" //nolint:lll + POLKADOT_RUNTIME_URL_v0917 = "https://github.com/paritytech/polkadot/blob/c583355a599200d64e9202d05f47860d824b4dcf/target/release/wbuild/polkadot-runtime/polkadot_runtime.compact.wasm?raw=true" //nolint:lll // v0.8 polkadot runtime POLKADOT_RUNTIME = "polkadot_runtime" diff --git a/lib/runtime/wasmer/exports_test.go b/lib/runtime/wasmer/exports_test.go index 6ac88bd723..4b428bd4e4 100644 --- a/lib/runtime/wasmer/exports_test.go +++ b/lib/runtime/wasmer/exports_test.go @@ -99,9 +99,10 @@ func TestInstance_Version_PolkadotRuntime_v0917(t *testing.T) { 11, ) - _, cfg := setupConfig(t, runtime.POLKADOT_RUNTIME_v0917, nil, DefaultTestLogLvl, 0) - instance, err := NewInstanceFromFile("/home/e/polkadot/target/release/wbuild/polkadot-runtime/polkadot_runtime.wasm", cfg) - require.NoError(t, err) + // _, cfg := setupConfig(t, runtime.POLKADOT_RUNTIME_v0917, nil, DefaultTestLogLvl, 0) + // instance, err := NewInstanceFromFile("/home/e/polkadot/target/release/wbuild/polkadot-runtime/polkadot_runtime.wasm", cfg) + // require.NoError(t, err) + instance := NewTestInstance(t, runtime.POLKADOT_RUNTIME_v0917) //instance := NewTestInstance(t, runtime.POLKADOT_RUNTIME_v0917) version, err := instance.Version() From 616822264e9bc2b910d1877b3c26280b454548e8 Mon Sep 17 00:00:00 2001 From: noot Date: Mon, 21 Mar 2022 15:42:05 -0400 Subject: [PATCH 3/5] stub --- lib/runtime/wasmer/imports.go | 68 +++-------------------------------- 1 file changed, 4 insertions(+), 64 deletions(-) diff --git a/lib/runtime/wasmer/imports.go b/lib/runtime/wasmer/imports.go index 7d34a14528..d69e3a16b7 100644 --- a/lib/runtime/wasmer/imports.go +++ b/lib/runtime/wasmer/imports.go @@ -875,50 +875,8 @@ func ext_trie_blake2_256_ordered_root_version_1(context unsafe.Pointer, dataSpan //export ext_trie_blake2_256_ordered_root_version_2 func ext_trie_blake2_256_ordered_root_version_2(context unsafe.Pointer, dataSpan C.int64_t, version C.int32_t) C.int32_t { - logger.Debug("executing...") - - instanceContext := wasm.IntoInstanceContext(context) - memory := instanceContext.Memory().Data() - runtimeCtx := instanceContext.Data().(*runtime.Context) - data := asMemorySlice(instanceContext, dataSpan) - - t := trie.NewEmptyTrie() - var values [][]byte - err := scale.Unmarshal(data, &values) - if err != nil { - logger.Errorf("[ext_trie_blake2_256_ordered_root_version_2]: %s", err) - return 0 - } - - for i, val := range values { - key, err := scale.Marshal(big.NewInt(int64(i))) - if err != nil { - logger.Errorf("[ext_trie_blake2_256_ordered_root_version_2]: %s", err) - return 0 - } - logger.Tracef( - "put key=0x%x and value=0x%x", - key, val) - - t.Put(key, val) - } - - // allocate memory for value and copy value to memory - ptr, err := runtimeCtx.Allocator.Allocate(32) - if err != nil { - logger.Errorf("[ext_trie_blake2_256_ordered_root_version_2]: %s", err) - return 0 - } - - hash, err := t.Hash() - if err != nil { - logger.Errorf("[ext_trie_blake2_256_ordered_root_version_2]: %s", err) - return 0 - } - - logger.Debugf("[ext_trie_blake2_256_ordered_root_version_2]: root hash is %s", hash) - copy(memory[ptr:ptr+32], hash[:]) - return C.int32_t(ptr) + // TODO: update to use state trie version 1 (#2418) + return ext_trie_blake2_256_ordered_root_version_1(context, dataSpan) } //export ext_trie_blake2_256_verify_proof_version_1 @@ -2107,26 +2065,8 @@ func ext_storage_root_version_1(context unsafe.Pointer) C.int64_t { //export ext_storage_root_version_2 func ext_storage_root_version_2(context unsafe.Pointer, version C.int32_t) C.int64_t { - logger.Trace("executing...") - - instanceContext := wasm.IntoInstanceContext(context) - storage := instanceContext.Data().(*runtime.Context).Storage - - root, err := storage.Root() - if err != nil { - logger.Errorf("failed to get storage root: %s", err) - return 0 - } - - logger.Debugf("root hash is: %s", root) - - rootSpan, err := toWasmMemory(instanceContext, root[:]) - if err != nil { - logger.Errorf("failed to allocate: %s", err) - return 0 - } - - return C.int64_t(rootSpan) + // TODO: update to use state trie version 1 (#2418) + return ext_storage_root_version_1(context) } //export ext_storage_set_version_1 From dcd9c38a5dfbb80ad310e177304a1e578ee1f518 Mon Sep 17 00:00:00 2001 From: noot Date: Mon, 21 Mar 2022 15:44:44 -0400 Subject: [PATCH 4/5] cleanup --- lib/runtime/wasmer/exports_test.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lib/runtime/wasmer/exports_test.go b/lib/runtime/wasmer/exports_test.go index 4b428bd4e4..159afa9af8 100644 --- a/lib/runtime/wasmer/exports_test.go +++ b/lib/runtime/wasmer/exports_test.go @@ -99,12 +99,7 @@ func TestInstance_Version_PolkadotRuntime_v0917(t *testing.T) { 11, ) - // _, cfg := setupConfig(t, runtime.POLKADOT_RUNTIME_v0917, nil, DefaultTestLogLvl, 0) - // instance, err := NewInstanceFromFile("/home/e/polkadot/target/release/wbuild/polkadot-runtime/polkadot_runtime.wasm", cfg) - // require.NoError(t, err) instance := NewTestInstance(t, runtime.POLKADOT_RUNTIME_v0917) - - //instance := NewTestInstance(t, runtime.POLKADOT_RUNTIME_v0917) version, err := instance.Version() require.NoError(t, err) From 614356dd33750c72a2b0c08f9e3c9dded71e4b5f Mon Sep 17 00:00:00 2001 From: noot Date: Tue, 22 Mar 2022 14:16:30 -0400 Subject: [PATCH 5/5] update test --- lib/runtime/wasmer/exports_test.go | 55 +++++++++++++++++------------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/lib/runtime/wasmer/exports_test.go b/lib/runtime/wasmer/exports_test.go index 159afa9af8..8407151cac 100644 --- a/lib/runtime/wasmer/exports_test.go +++ b/lib/runtime/wasmer/exports_test.go @@ -22,6 +22,7 @@ import ( "github.com/ChainSafe/gossamer/lib/utils" "github.com/ChainSafe/gossamer/pkg/scale" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -89,34 +90,42 @@ func TestInstance_Version_PolkadotRuntime_v0910(t *testing.T) { } func TestInstance_Version_PolkadotRuntime_v0917(t *testing.T) { - expected := runtime.NewVersionData( - []byte("polkadot"), - []byte("parity-polkadot"), - 0, - 9170, - 0, - nil, - 11, - ) - instance := NewTestInstance(t, runtime.POLKADOT_RUNTIME_v0917) version, err := instance.Version() require.NoError(t, err) - t.Logf("SpecName: %s\n", version.SpecName()) - t.Logf("ImplName: %s\n", version.ImplName()) - t.Logf("AuthoringVersion: %d\n", version.AuthoringVersion()) - t.Logf("SpecVersion: %d\n", version.SpecVersion()) - t.Logf("ImplVersion: %d\n", version.ImplVersion()) - t.Logf("TransactionVersion: %d\n", version.TransactionVersion()) + expectedSpecName := []byte("polkadot") + expectedImplName := []byte("parity-polkadot") + const ( + expectedAuthoringVersion uint32 = 0 + expectedSpecVersion uint32 = 9170 + expectedImplVersion uint32 = 0 + expectedTransactionVersion uint32 = 11 + ) + expectedAPIItems := []runtime.APIItem{ + {Name: [8]uint8{0xdf, 0x6a, 0xcb, 0x68, 0x99, 0x7, 0x60, 0x9b}, Ver: 0x4}, + {Name: [8]uint8{0x37, 0xe3, 0x97, 0xfc, 0x7c, 0x91, 0xf5, 0xe4}, Ver: 0x1}, + {Name: [8]uint8{0x40, 0xfe, 0x3a, 0xd4, 0x1, 0xf8, 0x95, 0x9a}, Ver: 0x5}, + {Name: [8]uint8{0xd2, 0xbc, 0x98, 0x97, 0xee, 0xd0, 0x8f, 0x15}, Ver: 0x3}, + {Name: [8]uint8{0xf7, 0x8b, 0x27, 0x8b, 0xe5, 0x3f, 0x45, 0x4c}, Ver: 0x2}, + {Name: [8]uint8{0xaf, 0x2c, 0x2, 0x97, 0xa2, 0x3e, 0x6d, 0x3d}, Ver: 0x2}, + {Name: [8]uint8{0x49, 0xea, 0xaf, 0x1b, 0x54, 0x8a, 0xc, 0xb0}, Ver: 0x1}, + {Name: [8]uint8{0x91, 0xd5, 0xdf, 0x18, 0xb0, 0xd2, 0xcf, 0x58}, Ver: 0x1}, + {Name: [8]uint8{0xed, 0x99, 0xc5, 0xac, 0xb2, 0x5e, 0xed, 0xf5}, Ver: 0x3}, + {Name: [8]uint8{0xcb, 0xca, 0x25, 0xe3, 0x9f, 0x14, 0x23, 0x87}, Ver: 0x2}, + {Name: [8]uint8{0x68, 0x7a, 0xd4, 0x4a, 0xd3, 0x7f, 0x3, 0xc2}, Ver: 0x1}, + {Name: [8]uint8{0xab, 0x3c, 0x5, 0x72, 0x29, 0x1f, 0xeb, 0x8b}, Ver: 0x1}, + {Name: [8]uint8{0xbc, 0x9d, 0x89, 0x90, 0x4f, 0x5b, 0x92, 0x3f}, Ver: 0x1}, + {Name: [8]uint8{0x37, 0xc8, 0xbb, 0x13, 0x50, 0xa9, 0xa2, 0xa8}, Ver: 0x1}, + } - require.Equal(t, 14, len(version.APIItems())) - require.Equal(t, expected.SpecName(), version.SpecName()) - require.Equal(t, expected.ImplName(), version.ImplName()) - require.Equal(t, expected.AuthoringVersion(), version.AuthoringVersion()) - require.Equal(t, expected.SpecVersion(), version.SpecVersion()) - require.Equal(t, expected.ImplVersion(), version.ImplVersion()) - require.Equal(t, expected.TransactionVersion(), version.TransactionVersion()) + assert.Equal(t, expectedAPIItems, version.APIItems()) + assert.Equal(t, expectedSpecName, version.SpecName()) + assert.Equal(t, expectedImplName, version.ImplName()) + assert.Equal(t, expectedAuthoringVersion, version.AuthoringVersion()) + assert.Equal(t, expectedSpecVersion, version.SpecVersion()) + assert.Equal(t, expectedImplVersion, version.ImplVersion()) + assert.Equal(t, expectedTransactionVersion, version.TransactionVersion()) } func TestInstance_Version_PolkadotRuntime(t *testing.T) {