Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lib/runtime/wasmer): implement ext_offchain_local_storage_version_1 #1821

Merged
merged 23 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
0f67834
stub Test_ext_offchain_local_storage_clear_version_1
edwardmack Sep 24, 2021
6c45076
add to tests
edwardmack Sep 24, 2021
40f034e
Merge branch 'development' into ed/issue-1794
edwardmack Sep 30, 2021
a03fb5d
implement ext_offchain_local_storage_clear_version_1
edwardmack Sep 30, 2021
f1f8440
lint
edwardmack Oct 1, 2021
6bc338e
update HOST_API_TEST_RUNTIME_URL to use master branch
edwardmack Oct 1, 2021
6e03813
Merge branch 'development' into ed/issue-1794
noot Oct 5, 2021
6358adf
Merge branch 'development' into ed/issue-1794
noot Oct 5, 2021
dbbb87c
Merge branch 'development' into ed/issue-1794
edwardmack Oct 5, 2021
c60cd59
Merge branch 'development' into ed/issue-1794
noot Oct 6, 2021
1dc234d
Merge branch 'development' into ed/issue-1794
noot Oct 7, 2021
4a5221a
Merge branch 'development' into ed/issue-1794
noot Oct 7, 2021
3c7d3f1
Merge branch 'development' into ed/issue-1794
noot Oct 7, 2021
7d2b39f
Merge branch 'development' into ed/issue-1794
noot Oct 8, 2021
3afc258
Merge branch 'development' into ed/issue-1794
noot Oct 8, 2021
8085aef
Merge branch 'development' into ed/issue-1794
edwardmack Oct 11, 2021
24302c7
handle merge conflicts
edwardmack Oct 11, 2021
52f27a3
Merge branch 'development' into ed/issue-1794
noot Oct 13, 2021
5f1554f
Merge branch 'development' into ed/issue-1794
edwardmack Oct 13, 2021
cd21203
update wasm url
edwardmack Oct 13, 2021
5ee00b8
Merge branch 'development' into ed/issue-1794
edwardmack Oct 18, 2021
f065500
update host runtime url
edwardmack Oct 19, 2021
9ba77ed
Merge branch 'development' into ed/issue-1794
edwardmack Oct 19, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 5 additions & 3 deletions lib/runtime/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ 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"
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"
noot marked this conversation as resolved.
Show resolved Hide resolved
// 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"
Expand Down
1 change: 1 addition & 0 deletions lib/runtime/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 21 additions & 2 deletions lib/runtime/wasmer/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -1363,9 +1363,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
44 changes: 44 additions & 0 deletions lib/runtime/wasmer/imports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,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.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().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)
}

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

Expand Down