Skip to content

Commit 73151df

Browse files
noottimwu20
authored andcommitted
chore(lib/runtime): stub v0.9.8 runtime funcs, partially implement storage ext_ funcs (ChainSafe#1710)
1 parent b082f50 commit 73151df

File tree

5 files changed

+167
-34
lines changed

5 files changed

+167
-34
lines changed

lib/runtime/constants.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,16 @@ var runtimes = []string{HOST_API_TEST_RUNTIME, POLKADOT_RUNTIME, NODE_RUNTIME, D
2424

2525
//nolint
2626
const (
27-
// v0.8 substrate runtime
27+
// v0.9 substrate runtime
2828
NODE_RUNTIME = "node_runtime"
2929
NODE_RUNTIME_FP = "node_runtime.compact.wasm"
3030
NODE_RUNTIME_URL = "https://github.com/noot/substrate/blob/noot/v0.9/target/debug/wbuild/node-runtime/node_runtime.compact.wasm?raw=true"
3131

32+
// v0.9.8 substrate runtime
33+
NODE_RUNTIME_v098 = "node_runtime-v0.9.8"
34+
NODE_RUNTIME_FP_v098 = "node_runtime-v0.9.8.compact.wasm"
35+
NODE_RUNTIME_URL_v098 = "https://github.com/noot/substrate/blob/noot/v0.9.8/target/debug/wbuild/node-runtime/node_runtime.compact.wasm?raw=true"
36+
3237
// v0.8 polkadot runtime
3338
POLKADOT_RUNTIME = "polkadot_runtime"
3439
POLKADOT_RUNTIME_FP = "polkadot_runtime.compact.wasm"

lib/runtime/test_helpers.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,20 @@ func NewInMemoryDB(t *testing.T) chaindb.Database {
5252

5353
// GetRuntimeVars returns the testRuntimeFilePath and testRuntimeURL
5454
func GetRuntimeVars(targetRuntime string) (string, string) {
55-
var testRuntimeFilePath string
56-
var testRuntimeURL string
57-
5855
switch targetRuntime {
5956
case NODE_RUNTIME:
60-
testRuntimeFilePath, testRuntimeURL = GetAbsolutePath(NODE_RUNTIME_FP), NODE_RUNTIME_URL
57+
return GetAbsolutePath(NODE_RUNTIME_FP), NODE_RUNTIME_URL
58+
case NODE_RUNTIME_v098:
59+
return GetAbsolutePath(NODE_RUNTIME_FP_v098), NODE_RUNTIME_URL_v098
6160
case POLKADOT_RUNTIME:
62-
testRuntimeFilePath, testRuntimeURL = GetAbsolutePath(POLKADOT_RUNTIME_FP), POLKADOT_RUNTIME_URL
61+
return GetAbsolutePath(POLKADOT_RUNTIME_FP), POLKADOT_RUNTIME_URL
6362
case HOST_API_TEST_RUNTIME:
64-
testRuntimeFilePath, testRuntimeURL = GetAbsolutePath(HOST_API_TEST_RUNTIME_FP), HOST_API_TEST_RUNTIME_URL
63+
return GetAbsolutePath(HOST_API_TEST_RUNTIME_FP), HOST_API_TEST_RUNTIME_URL
6564
case DEV_RUNTIME:
66-
testRuntimeFilePath, testRuntimeURL = GetAbsolutePath(DEV_RUNTIME_FP), DEV_RUNTIME_URL
65+
return GetAbsolutePath(DEV_RUNTIME_FP), DEV_RUNTIME_URL
66+
default:
67+
return "", ""
6768
}
68-
69-
return testRuntimeFilePath, testRuntimeURL
7069
}
7170

7271
// GetAbsolutePath returns the completePath for a given targetDir

lib/runtime/wasmer/exports_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,38 @@ func createTestExtrinsic(t *testing.T, rt runtime.Instance, genHash common.Hash,
6363
return types.Extrinsic(common.MustHexToBytes(extEnc))
6464
}
6565

66+
func TestInstance_Version_NodeRuntime_v098(t *testing.T) {
67+
expected := runtime.NewVersionData(
68+
[]byte("node"),
69+
[]byte("substrate-node"),
70+
10,
71+
267,
72+
0,
73+
nil,
74+
2,
75+
)
76+
77+
instance := NewTestInstance(t, runtime.NODE_RUNTIME_v098)
78+
79+
version, err := instance.Version()
80+
require.Nil(t, err)
81+
82+
t.Logf("SpecName: %s\n", version.SpecName())
83+
t.Logf("ImplName: %s\n", version.ImplName())
84+
t.Logf("AuthoringVersion: %d\n", version.AuthoringVersion())
85+
t.Logf("SpecVersion: %d\n", version.SpecVersion())
86+
t.Logf("ImplVersion: %d\n", version.ImplVersion())
87+
t.Logf("TransactionVersion: %d\n", version.TransactionVersion())
88+
89+
require.Equal(t, 13, len(version.APIItems()))
90+
require.Equal(t, expected.SpecName(), version.SpecName())
91+
require.Equal(t, expected.ImplName(), version.ImplName())
92+
require.Equal(t, expected.AuthoringVersion(), version.AuthoringVersion())
93+
require.Equal(t, expected.SpecVersion(), version.SpecVersion())
94+
require.Equal(t, expected.ImplVersion(), version.ImplVersion())
95+
require.Equal(t, expected.TransactionVersion(), version.TransactionVersion())
96+
}
97+
6698
func TestInstance_Version_PolkadotRuntime(t *testing.T) {
6799
expected := runtime.NewVersionData(
68100
[]byte("polkadot"),

lib/runtime/wasmer/imports.go

Lines changed: 120 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,12 @@ package wasmer
4545
//
4646
// extern int32_t ext_trie_blake2_256_root_version_1(void *context, int64_t a);
4747
// extern int32_t ext_trie_blake2_256_ordered_root_version_1(void *context, int64_t a);
48+
// 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);
4849
//
50+
// extern int64_t ext_misc_runtime_version_version_1(void *context, int64_t a);
4951
// extern void ext_misc_print_hex_version_1(void *context, int64_t a);
5052
// extern void ext_misc_print_num_version_1(void *context, int64_t a);
5153
// extern void ext_misc_print_utf8_version_1(void *context, int64_t a);
52-
// extern int64_t ext_misc_runtime_version_version_1(void *context, int64_t a);
5354
//
5455
// extern void ext_default_child_storage_clear_version_1(void *context, int64_t a, int64_t b);
5556
// extern int64_t ext_default_child_storage_get_version_1(void *context, int64_t a, int64_t b);
@@ -59,6 +60,7 @@ package wasmer
5960
// extern void ext_default_child_storage_set_version_1(void *context, int64_t a, int64_t b, int64_t c);
6061
// extern void ext_default_child_storage_storage_kill_version_1(void *context, int64_t a);
6162
// extern int32_t ext_default_child_storage_storage_kill_version_2(void *context, int64_t a, int64_t b);
63+
// extern int64_t ext_default_child_storage_storage_kill_version_3(void *context, int64_t a, int64_t b);
6264
// extern void ext_default_child_storage_clear_prefix_version_1(void *context, int64_t a, int64_t b);
6365
// extern int32_t ext_default_child_storage_exists_version_1(void *context, int64_t a, int64_t b);
6466
//
@@ -75,17 +77,20 @@ package wasmer
7577
//
7678
// extern void ext_offchain_index_set_version_1(void *context, int64_t a, int64_t b);
7779
// extern int32_t ext_offchain_is_validator_version_1(void *context);
80+
// extern void ext_offchain_local_storage_clear_version_1(void *context, int32_t a, int64_t b);
7881
// extern int32_t ext_offchain_local_storage_compare_and_set_version_1(void *context, int32_t a, int64_t b, int64_t c, int64_t d);
7982
// extern int64_t ext_offchain_local_storage_get_version_1(void *context, int32_t a, int64_t b);
8083
// extern void ext_offchain_local_storage_set_version_1(void *context, int32_t a, int64_t b, int64_t c);
8184
// extern int64_t ext_offchain_network_state_version_1(void *context);
8285
// extern int32_t ext_offchain_random_seed_version_1(void *context);
8386
// extern int64_t ext_offchain_submit_transaction_version_1(void *context, int64_t a);
87+
// extern int64_t ext_offchain_timestamp_version_1(void *context);
8488
//
8589
// extern void ext_storage_append_version_1(void *context, int64_t a, int64_t b);
8690
// extern int64_t ext_storage_changes_root_version_1(void *context, int64_t a);
8791
// extern void ext_storage_clear_version_1(void *context, int64_t a);
8892
// extern void ext_storage_clear_prefix_version_1(void *context, int64_t a);
93+
// extern int64_t ext_storage_clear_prefix_version_2(void *context, int64_t a, int64_t b);
8994
// extern void ext_storage_commit_transaction_version_1(void *context);
9095
// extern int32_t ext_storage_exists_version_1(void *context, int64_t a);
9196
// extern int64_t ext_storage_get_version_1(void *context, int64_t a);
@@ -95,6 +100,9 @@ package wasmer
95100
// extern int64_t ext_storage_root_version_1(void *context);
96101
// extern void ext_storage_set_version_1(void *context, int64_t a, int64_t b);
97102
// extern void ext_storage_start_transaction_version_1(void *context);
103+
//
104+
// extern void ext_transaction_index_index_version_1(void *context, int32_t a, int32_t b, int32_t c);
105+
// extern void ext_transaction_index_renew_version_1(void *context, int32_t a, int32_t b);
98106
import "C"
99107

100108
import (
@@ -154,6 +162,18 @@ func ext_logging_max_level_version_1(context unsafe.Pointer) C.int32_t {
154162
return 4
155163
}
156164

165+
//export ext_transaction_index_index_version_1
166+
func ext_transaction_index_index_version_1(context unsafe.Pointer, a, b, c C.int32_t) {
167+
logger.Trace("[ext_transaction_index_index_version_1] executing...")
168+
logger.Warn("[ext_transaction_index_index_version_1] unimplemented")
169+
}
170+
171+
//export ext_transaction_index_renew_version_1
172+
func ext_transaction_index_renew_version_1(context unsafe.Pointer, a, b C.int32_t) {
173+
logger.Trace("[ext_transaction_index_renew_version_1] executing...")
174+
logger.Warn("[ext_transaction_index_renew_version_1] unimplemented")
175+
}
176+
157177
//export ext_sandbox_instance_teardown_version_1
158178
func ext_sandbox_instance_teardown_version_1(context unsafe.Pointer, a C.int32_t) {
159179
logger.Trace("[ext_sandbox_instance_teardown_version_1] executing...")
@@ -835,6 +855,13 @@ func ext_trie_blake2_256_ordered_root_version_1(context unsafe.Pointer, dataSpan
835855
return C.int32_t(ptr)
836856
}
837857

858+
//export ext_trie_blake2_256_verify_proof_version_1
859+
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 {
860+
logger.Debug("[ext_trie_blake2_256_verify_proof_version_1] executing...")
861+
logger.Warn("[ext_trie_blake2_256_verify_proof_version_1] unimplemented")
862+
return 0
863+
}
864+
838865
//export ext_misc_print_hex_version_1
839866
func ext_misc_print_hex_version_1(context unsafe.Pointer, dataSpan C.int64_t) {
840867
logger.Trace("[ext_misc_print_hex_version_1] executing...")
@@ -1093,9 +1120,36 @@ func ext_default_child_storage_storage_kill_version_1(context unsafe.Pointer, ch
10931120
}
10941121

10951122
//export ext_default_child_storage_storage_kill_version_2
1096-
func ext_default_child_storage_storage_kill_version_2(context unsafe.Pointer, a, b C.int64_t) C.int32_t {
1123+
func ext_default_child_storage_storage_kill_version_2(context unsafe.Pointer, childStorageKeySpan, _ C.int64_t) C.int32_t {
10971124
logger.Debug("[ext_default_child_storage_storage_kill_version_2] executing...")
1098-
logger.Warn("[ext_default_child_storage_storage_kill_version_2] unimplemented")
1125+
logger.Warn("[ext_default_child_storage_storage_kill_version_2] somewhat unimplemented")
1126+
// TODO: need to use `limit` parameter
1127+
1128+
instanceContext := wasm.IntoInstanceContext(context)
1129+
ctx := instanceContext.Data().(*runtime.Context)
1130+
storage := ctx.Storage
1131+
1132+
childStorageKey := asMemorySlice(instanceContext, childStorageKeySpan)
1133+
storage.DeleteChild(childStorageKey)
1134+
1135+
// note: this function always returns `KillStorageResult::AllRemoved`, which is 0
1136+
return 0
1137+
}
1138+
1139+
//export ext_default_child_storage_storage_kill_version_3
1140+
func ext_default_child_storage_storage_kill_version_3(context unsafe.Pointer, childStorageKeySpan, _ C.int64_t) C.int64_t {
1141+
logger.Debug("[ext_default_child_storage_storage_kill_version_3] executing...")
1142+
logger.Warn("[ext_default_child_storage_storage_kill_version_3] somewhat unimplemented")
1143+
// TODO: need to use `limit` parameter
1144+
1145+
instanceContext := wasm.IntoInstanceContext(context)
1146+
ctx := instanceContext.Data().(*runtime.Context)
1147+
storage := ctx.Storage
1148+
1149+
childStorageKey := asMemorySlice(instanceContext, childStorageKeySpan)
1150+
storage.DeleteChild(childStorageKey)
1151+
1152+
// TODO: this function returns a `KillStorageResult` which may be `AllRemoved` (0) or `SomeRemaining` (1)
10991153
return 0
11001154
}
11011155

@@ -1297,6 +1351,12 @@ func ext_offchain_index_set_version_1(context unsafe.Pointer, keySpan, valueSpan
12971351
logger.Warn("[ext_offchain_index_set_version_1] unimplemented")
12981352
}
12991353

1354+
//export ext_offchain_local_storage_clear_version_1
1355+
func ext_offchain_local_storage_clear_version_1(context unsafe.Pointer, a C.int32_t, b C.int64_t) {
1356+
logger.Trace("[ext_offchain_local_storage_clear_version_1] executing...")
1357+
logger.Warn("[ext_offchain_local_storage_clear_version_1] unimplemented")
1358+
}
1359+
13001360
//export ext_offchain_is_validator_version_1
13011361
func ext_offchain_is_validator_version_1(context unsafe.Pointer) C.int32_t {
13021362
logger.Debug("[ext_offchain_is_validator_version_1] executing...")
@@ -1478,6 +1538,13 @@ func ext_offchain_submit_transaction_version_1(context unsafe.Pointer, data C.in
14781538
return C.int64_t(ptr)
14791539
}
14801540

1541+
//export ext_offchain_timestamp_version_1
1542+
func ext_offchain_timestamp_version_1(context unsafe.Pointer) C.int64_t {
1543+
logger.Trace("[ext_offchain_timestamp_version_1] executing...")
1544+
logger.Warn("[ext_offchain_timestamp_version_1] unimplemented")
1545+
return 0
1546+
}
1547+
14811548
func storageAppend(storage runtime.Storage, key, valueToAppend []byte) error {
14821549
nextLength := big.NewInt(1)
14831550
var valueRes []byte
@@ -1584,6 +1651,27 @@ func ext_storage_clear_prefix_version_1(context unsafe.Pointer, prefixSpan C.int
15841651
}
15851652
}
15861653

1654+
//export ext_storage_clear_prefix_version_2
1655+
func ext_storage_clear_prefix_version_2(context unsafe.Pointer, prefixSpan, _ C.int64_t) C.int64_t {
1656+
logger.Trace("[ext_storage_clear_prefix_version_2] executing...")
1657+
logger.Warn("[ext_storage_clear_prefix_version_2] somewhat unimplemented")
1658+
// TODO: need to use unused `limit` parameter
1659+
1660+
instanceContext := wasm.IntoInstanceContext(context)
1661+
ctx := instanceContext.Data().(*runtime.Context)
1662+
storage := ctx.Storage
1663+
1664+
prefix := asMemorySlice(instanceContext, prefixSpan)
1665+
logger.Debug("[ext_storage_clear_prefix_version_1]", "prefix", fmt.Sprintf("0x%x", prefix))
1666+
1667+
err := storage.ClearPrefix(prefix)
1668+
if err != nil {
1669+
logger.Error("[ext_storage_clear_prefix_version_1]", "error", err)
1670+
}
1671+
1672+
return 1
1673+
}
1674+
15871675
//export ext_storage_exists_version_1
15881676
func ext_storage_exists_version_1(context unsafe.Pointer, keySpan C.int64_t) C.int32_t {
15891677
logger.Trace("[ext_storage_exists_version_1] executing...")
@@ -1961,6 +2049,10 @@ func ImportsNodeRuntime() (*wasm.Imports, error) { //nolint
19612049
if err != nil {
19622050
return nil, err
19632051
}
2052+
_, err = imports.Append("ext_default_child_storage_storage_kill_version_3", ext_default_child_storage_storage_kill_version_3, C.ext_default_child_storage_storage_kill_version_3)
2053+
if err != nil {
2054+
return nil, err
2055+
}
19642056

19652057
_, err = imports.Append("ext_hashing_blake2_128_version_1", ext_hashing_blake2_128_version_1, C.ext_hashing_blake2_128_version_1)
19662058
if err != nil {
@@ -2025,6 +2117,10 @@ func ImportsNodeRuntime() (*wasm.Imports, error) { //nolint
20252117
if err != nil {
20262118
return nil, err
20272119
}
2120+
_, err = imports.Append("ext_offchain_local_storage_clear_version_1", ext_offchain_local_storage_clear_version_1, C.ext_offchain_local_storage_clear_version_1)
2121+
if err != nil {
2122+
return nil, err
2123+
}
20282124
_, err = imports.Append("ext_offchain_local_storage_compare_and_set_version_1", ext_offchain_local_storage_compare_and_set_version_1, C.ext_offchain_local_storage_compare_and_set_version_1)
20292125
if err != nil {
20302126
return nil, err
@@ -2049,6 +2145,10 @@ func ImportsNodeRuntime() (*wasm.Imports, error) { //nolint
20492145
if err != nil {
20502146
return nil, err
20512147
}
2148+
_, err = imports.Append("ext_offchain_timestamp_version_1", ext_offchain_timestamp_version_1, C.ext_offchain_timestamp_version_1)
2149+
if err != nil {
2150+
return nil, err
2151+
}
20522152

20532153
_, err = imports.Append("ext_sandbox_instance_teardown_version_1", ext_sandbox_instance_teardown_version_1, C.ext_sandbox_instance_teardown_version_1)
20542154
if err != nil {
@@ -2095,6 +2195,10 @@ func ImportsNodeRuntime() (*wasm.Imports, error) { //nolint
20952195
if err != nil {
20962196
return nil, err
20972197
}
2198+
_, err = imports.Append("ext_storage_clear_prefix_version_2", ext_storage_clear_prefix_version_2, C.ext_storage_clear_prefix_version_2)
2199+
if err != nil {
2200+
return nil, err
2201+
}
20982202
_, err = imports.Append("ext_storage_commit_transaction_version_1", ext_storage_commit_transaction_version_1, C.ext_storage_commit_transaction_version_1)
20992203
if err != nil {
21002204
return nil, err
@@ -2140,6 +2244,19 @@ func ImportsNodeRuntime() (*wasm.Imports, error) { //nolint
21402244
if err != nil {
21412245
return nil, err
21422246
}
2247+
_, err = imports.Append("ext_trie_blake2_256_verify_proof_version_1", ext_trie_blake2_256_verify_proof_version_1, C.ext_trie_blake2_256_verify_proof_version_1)
2248+
if err != nil {
2249+
return nil, err
2250+
}
2251+
2252+
_, err = imports.Append("ext_transaction_index_index_version_1", ext_transaction_index_index_version_1, C.ext_transaction_index_index_version_1)
2253+
if err != nil {
2254+
return nil, err
2255+
}
2256+
_, err = imports.Append("ext_transaction_index_renew_version_1", ext_transaction_index_renew_version_1, C.ext_transaction_index_renew_version_1)
2257+
if err != nil {
2258+
return nil, err
2259+
}
21432260

21442261
return imports, nil
21452262
}

lib/runtime/wasmer/test_helpers.go

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
log "github.com/ChainSafe/log15"
2929
"github.com/stretchr/testify/mock"
3030
"github.com/stretchr/testify/require"
31-
wasm "github.com/wasmerio/go-ext-wasm/wasmer"
3231
)
3332

3433
// DefaultTestLogLvl is the log level used for test runtime instances
@@ -57,27 +56,8 @@ func NewTestInstanceWithRole(t *testing.T, targetRuntime string, role byte) *Ins
5756
return r
5857
}
5958

60-
// GetRuntimeImports ...
61-
func GetRuntimeImports(targetRuntime string) func() (*wasm.Imports, error) {
62-
var registerImports func() (*wasm.Imports, error)
63-
64-
switch targetRuntime {
65-
case runtime.NODE_RUNTIME:
66-
registerImports = ImportsNodeRuntime
67-
case runtime.POLKADOT_RUNTIME:
68-
registerImports = ImportsNodeRuntime
69-
case runtime.HOST_API_TEST_RUNTIME:
70-
registerImports = ImportsNodeRuntime
71-
default:
72-
registerImports = ImportsNodeRuntime
73-
}
74-
75-
return registerImports
76-
}
77-
7859
func setupConfig(t *testing.T, targetRuntime string, tt *trie.Trie, lvl log.Lvl, role byte) (string, *Config) {
7960
testRuntimeFilePath, testRuntimeURL := runtime.GetRuntimeVars(targetRuntime)
80-
importsFunc := GetRuntimeImports(targetRuntime)
8161

8262
_, err := runtime.GetRuntimeBlob(testRuntimeFilePath, testRuntimeURL)
8363
require.Nil(t, err, "Fail: could not get runtime", "targetRuntime", targetRuntime)
@@ -93,7 +73,7 @@ func setupConfig(t *testing.T, targetRuntime string, tt *trie.Trie, lvl log.Lvl,
9373
PersistentStorage: runtime.NewInMemoryDB(t), // we're using a local storage here since this is a test runtime
9474
}
9575
cfg := &Config{
96-
Imports: importsFunc,
76+
Imports: ImportsNodeRuntime,
9777
}
9878
cfg.Storage = s
9979
cfg.Keystore = keystore.NewGlobalKeystore()

0 commit comments

Comments
 (0)