-
Notifications
You must be signed in to change notification settings - Fork 146
feature (lib/runtime/wasmer): implement ext_default_child_storage_storage_kill_version_3 #1878
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
Changes from all commits
91a559c
bde6f4d
72c3a4a
f946c80
8155b82
710b472
0c8181a
0737ef9
0e2dacc
35a908d
0c887b3
2498855
9a9df0d
51df326
9b81ebc
630dd4b
f308ee0
207a32d
2799cc8
8c11d04
a74e67e
68e12c2
2b5d2ba
4a36e2e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1142,19 +1142,67 @@ func ext_default_child_storage_storage_kill_version_2(context unsafe.Pointer, ch | |
| return 0 | ||
| } | ||
|
|
||
| //export ext_default_child_storage_storage_kill_version_3 | ||
| func ext_default_child_storage_storage_kill_version_3(context unsafe.Pointer, childStorageKeySpan, _ C.int64_t) C.int64_t { | ||
| logger.Debug("[ext_default_child_storage_storage_kill_version_3] executing...") | ||
| logger.Warn("[ext_default_child_storage_storage_kill_version_3] somewhat unimplemented") | ||
| // TODO: need to use `limit` parameter (#1793) | ||
| type noneRemain uint32 | ||
| type someRemain uint32 | ||
|
|
||
| func (noneRemain) Index() uint { | ||
| return 0 | ||
| } | ||
| func (someRemain) Index() uint { | ||
| return 1 | ||
| } | ||
|
|
||
| //export ext_default_child_storage_storage_kill_version_3 | ||
| func ext_default_child_storage_storage_kill_version_3(context unsafe.Pointer, childStorageKeySpan, lim C.int64_t) C.int64_t { | ||
| logger.Debug("executing...") | ||
| instanceContext := wasm.IntoInstanceContext(context) | ||
| ctx := instanceContext.Data().(*runtime.Context) | ||
| storage := ctx.Storage | ||
|
|
||
| childStorageKey := asMemorySlice(instanceContext, childStorageKeySpan) | ||
| storage.DeleteChild(childStorageKey) | ||
| return 0 | ||
|
|
||
| limitBytes := asMemorySlice(instanceContext, lim) | ||
| buf := &bytes.Buffer{} | ||
| buf.Write(limitBytes) | ||
|
|
||
| limit, err := optional.NewBytes(true, nil).Decode(buf) | ||
| if err != nil { | ||
| logger.Warn("cannot generate limit", "error", err) | ||
| } | ||
|
|
||
| deleted, all, err := storage.DeleteChildLimit(childStorageKey, limit) | ||
| if err != nil { | ||
| logger.Warn("cannot get child storage", "error", err) | ||
| return C.int64_t(0) | ||
| } | ||
|
|
||
| vdt, err := scale.NewVaryingDataType(noneRemain(0), someRemain(0)) | ||
| if err != nil { | ||
| logger.Warn("cannot create new varying data type", "error", err) | ||
| } | ||
|
Comment on lines
+1178
to
+1181
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CI isn't passing code coverage because I'm not testing for these types of error conditions, any suggestions how I could test for these? It seem these are edge cases that are testing the functions within the function I'm testing, not the function itself. Please advise. @timwu20 ? |
||
|
|
||
| if all { | ||
| err = vdt.Set(noneRemain(deleted)) | ||
| } else { | ||
| err = vdt.Set(someRemain(deleted)) | ||
| } | ||
| if err != nil { | ||
| logger.Warn("cannot set varying data type", "error", err) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated |
||
| return C.int64_t(0) | ||
| } | ||
|
|
||
| encoded, err := scale.Marshal(vdt) | ||
| if err != nil { | ||
| logger.Warn("problem marshaling varying data type", "error", err) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated. |
||
| return C.int64_t(0) | ||
| } | ||
|
|
||
| out, err := toWasmMemoryOptional(instanceContext, encoded) | ||
| if err != nil { | ||
| logger.Warn("failed to allocate", "error", err) | ||
| return 0 | ||
| } | ||
|
|
||
| return C.int64_t(out) | ||
| } | ||
|
|
||
| //export ext_allocator_free_version_1 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1226,6 +1226,54 @@ func Test_ext_default_child_storage_storage_kill_version_2_limit_none(t *testing | |
| require.Nil(t, child) | ||
| } | ||
|
|
||
| func Test_ext_default_child_storage_storage_kill_version_3(t *testing.T) { | ||
| inst := NewTestInstance(t, runtime.HOST_API_TEST_RUNTIME) | ||
|
|
||
| tr := trie.NewEmptyTrie() | ||
| tr.Put([]byte(`key2`), []byte(`value2`)) | ||
| tr.Put([]byte(`key1`), []byte(`value1`)) | ||
| tr.Put([]byte(`key3`), []byte(`value3`)) | ||
| err := inst.ctx.Storage.SetChild(testChildKey, tr) | ||
| require.NoError(t, err) | ||
|
|
||
| testLimitBytes := make([]byte, 4) | ||
| binary.LittleEndian.PutUint32(testLimitBytes, uint32(2)) | ||
| optLimit2 := optional.NewBytes(true, testLimitBytes) | ||
|
|
||
| testCases := []struct { | ||
| key []byte | ||
| limit *optional.Bytes | ||
| expected []byte | ||
| errMsg string | ||
| }{ | ||
| {key: []byte(`fakekey`), limit: optLimit2, expected: []byte{0, 0, 0, 0, 0}, errMsg: "Failed to call the `rtm_ext_default_child_storage_storage_kill_version_3` exported function."}, | ||
| {key: testChildKey, limit: optLimit2, expected: []byte{1, 2, 0, 0, 0}}, | ||
| {key: testChildKey, limit: nil, expected: []byte{0, 1, 0, 0, 0}}, | ||
| } | ||
|
|
||
| for _, test := range testCases { | ||
| encChildKey, err := scale.Marshal(test.key) | ||
| require.NoError(t, err) | ||
| encOptLimit, err := test.limit.Encode() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in the third case, this line could panic (nil pointer) since
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't panic since the .Encode receiver check if the pointer in nil, and handles that (returning byte[0] since it's considered an optional without a value).
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok! |
||
| require.NoError(t, err) | ||
| res, err := inst.Exec("rtm_ext_default_child_storage_storage_kill_version_3", append(encChildKey, encOptLimit...)) | ||
| if test.errMsg != "" { | ||
| require.Error(t, err) | ||
| require.EqualError(t, err, test.errMsg) | ||
| continue | ||
| } | ||
|
|
||
| require.NoError(t, err) | ||
|
|
||
| buf := &bytes.Buffer{} | ||
| buf.Write(res) | ||
|
|
||
| read, err := new(optional.Bytes).Decode(buf) | ||
| require.NoError(t, err) | ||
| require.Equal(t, test.expected, read.Value()) | ||
| } | ||
| } | ||
|
|
||
| func Test_ext_storage_append_version_1(t *testing.T) { | ||
| inst := NewTestInstance(t, runtime.HOST_API_TEST_RUNTIME) | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.