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

feature (lib/runtime/wasmer): implement ext_default_child_storage_storage_kill_version_3 #1878

Merged
merged 24 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
91a559c
implement ext_default_child_storage_storage_kill_version_3
edwardmack Oct 8, 2021
bde6f4d
go fmt
edwardmack Oct 8, 2021
72c3a4a
address PR comments
edwardmack Oct 11, 2021
f946c80
lint
edwardmack Oct 11, 2021
8155b82
remove unused receivers
edwardmack Oct 11, 2021
710b472
skip tests with wasm errors
edwardmack Oct 12, 2021
0c8181a
use updated test wasm
edwardmack Oct 12, 2021
0737ef9
change func to return i64
edwardmack Oct 13, 2021
0e2dacc
remove todos
edwardmack Oct 13, 2021
35a908d
Merge branch 'development' into ed/issue-1793
edwardmack Oct 13, 2021
0c887b3
lint
edwardmack Oct 13, 2021
2498855
make vdt's private
edwardmack Oct 14, 2021
9a9df0d
Merge branch 'development' into ed/issue-1793
edwardmack Oct 14, 2021
51df326
update host api url to specific commit
edwardmack Oct 18, 2021
9b81ebc
Merge branch 'development' into ed/issue-1793
edwardmack Oct 18, 2021
630dd4b
go fmt
edwardmack Oct 18, 2021
f308ee0
Merge branch 'ed/issue-1793' of https://github.com/ChainSafe/gossamer…
edwardmack Oct 18, 2021
207a32d
update host api test runtime url
edwardmack Oct 19, 2021
2799cc8
Merge branch 'development' into ed/issue-1793
edwardmack Oct 19, 2021
8c11d04
Merge branch 'development' into ed/issue-1793
edwardmack Oct 19, 2021
a74e67e
Merge branch 'development' into ed/issue-1793
edwardmack Oct 19, 2021
68e12c2
Merge branch 'development' into ed/issue-1793
edwardmack Oct 22, 2021
2b5d2ba
Merge branch 'development' into ed/issue-1793
noot Oct 25, 2021
4a36e2e
Merge branch 'development' into ed/issue-1793
noot Oct 25, 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
64 changes: 56 additions & 8 deletions lib/runtime/wasmer/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...")
noot marked this conversation as resolved.
Show resolved Hide resolved
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
Copy link
Member Author

Choose a reason for hiding this comment

The 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return C.int32_t(0) perhaps?

Copy link
Member Author

Choose a reason for hiding this comment

The 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

Copy link
Member Author

Choose a reason for hiding this comment

The 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
Expand Down
48 changes: 48 additions & 0 deletions lib/runtime/wasmer/imports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the third case, this line could panic (nil pointer) since limit: nil

Copy link
Member Author

Choose a reason for hiding this comment

The 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).

Copy link
Member

Choose a reason for hiding this comment

The 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)

Expand Down