Skip to content

Commit

Permalink
feat(lib/runtime/wasmer): implement ext_offchain_local_storage_versio…
Browse files Browse the repository at this point in the history
…n_1 (#1821)

* stub Test_ext_offchain_local_storage_clear_version_1

* add to tests

* implement ext_offchain_local_storage_clear_version_1

* lint

* update HOST_API_TEST_RUNTIME_URL to use master branch

* handle merge conflicts

* update wasm url

* update host runtime url

Co-authored-by: noot <36753753+noot@users.noreply.github.com>
  • Loading branch information
edwardmack and noot committed Oct 19, 2021
1 parent 88c59ea commit 0f63b17
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 4 deletions.
5 changes: 5 additions & 0 deletions dot/state/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/runtime/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/ChainSafe/polkadot-spec/blob/e8770476a4b8445cddd2bd1e8f0060a83eaccb38/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"
Expand Down
1 change: 1 addition & 0 deletions lib/runtime/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,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
Expand Down
35 changes: 32 additions & 3 deletions lib/runtime/wasmer/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -1366,9 +1367,28 @@ 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
Expand Down Expand Up @@ -1557,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
Expand Down Expand Up @@ -2172,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
Expand Down
44 changes: 44 additions & 0 deletions lib/runtime/wasmer/imports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,50 @@ func Test_ext_storage_clear_version_1(t *testing.T) {
require.Nil(t, val)
}

func Test_ext_offchain_local_storage_clear_version_1_Persistent(t *testing.T) {
inst := NewTestInstance(t, runtime.HOST_API_TEST_RUNTIME)

testkey := []byte("key1")
err := inst.NodeStorage().PersistentStorage.Put(testkey, []byte{1})
require.NoError(t, err)

kind := int32(1)
encKind, err := scale.Marshal(kind)
require.NoError(t, err)

encKey, err := scale.Marshal(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().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.Marshal(kind)
require.NoError(t, err)

encKey, err := scale.Marshal(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)
}

func Test_ext_storage_clear_prefix_version_1_hostAPI(t *testing.T) {
inst := NewTestInstance(t, runtime.HOST_API_TEST_RUNTIME)

Expand Down

0 comments on commit 0f63b17

Please sign in to comment.