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(wazero): Add v2 runtime functions #3428

Merged
merged 48 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
76767e9
feat(trie): Add v2 runtime functions
dimartiro Aug 8, 2023
155cc4b
Merge branch 'development' into diego/trieV1/runtime-functions
dimartiro Aug 9, 2023
2703b93
chore(trie): version trie functions
dimartiro Aug 10, 2023
00d4231
Update tests
dimartiro Aug 10, 2023
447ccf5
Fix lint errors
dimartiro Aug 10, 2023
752ce6d
Fix deepsource checks
dimartiro Aug 10, 2023
0514501
Revisit TODOs
dimartiro Aug 15, 2023
a0b8dc1
Add more tests
dimartiro Aug 15, 2023
db45755
Fix linter
dimartiro Aug 15, 2023
5719b28
fix(dot/sync): rework on bootstrap/tip sync (#3227)
EclesioMeloJunior Aug 11, 2023
ae86ad0
chore(deps): bump github.com/multiformats/go-multiaddr from 0.10.1 to…
dependabot[bot] Aug 14, 2023
7046c94
chore(deps): bump github.com/go-playground/validator/v10 from 10.14.1…
dependabot[bot] Aug 15, 2023
9dc3af5
chore(deps): bump github.com/ethereum/go-ethereum from 1.12.0 to 1.12…
dependabot[bot] Aug 15, 2023
970e6aa
chore(deepsource): address Deepsource suggestions (#3436)
EclesioMeloJunior Aug 15, 2023
2ebf184
Fix linter
dimartiro Aug 15, 2023
1761922
Get hashed values
dimartiro Aug 16, 2023
7b4b6d1
Check memory db put value error
dimartiro Aug 16, 2023
5ec4460
Add memory db lock
dimartiro Aug 16, 2023
8d70d77
Fix linter
dimartiro Aug 16, 2023
461596e
Deepsource
dimartiro Aug 16, 2023
c9b42e9
Add default version
dimartiro Aug 16, 2023
503fc27
Fix doc
dimartiro Aug 16, 2023
3f6cdcc
Add test to cover case for #2329
dimartiro Aug 16, 2023
0c5f542
Fix linter
dimartiro Aug 16, 2023
3e3878a
Use state version param in runtime functions
dimartiro Aug 16, 2023
45394d7
Merge branch 'diego/trieV1/versioning' into diego/trieV1/runtime-func…
dimartiro Aug 16, 2023
c0bda83
Merge branch 'diego/trieV1/versioning' into diego/trieV1/runtime-func…
dimartiro Aug 16, 2023
265c747
Merge branch 'diego/trieV1/versioning' into diego/trieV1/runtime-func…
dimartiro Aug 23, 2023
bc167e2
Merge branch 'diego/trieV1/versioning' of https://github.com/ChainSaf…
dimartiro Aug 23, 2023
84541d8
Removes todos and skip validations for unused params
dimartiro Aug 24, 2023
b765f73
Merge branch 'diego/trieV1/versioning' into diego/trieV1/runtime-func…
dimartiro Aug 28, 2023
793d80c
Add new runtime functions tests
dimartiro Aug 28, 2023
b94c622
Fix get version in runtime functions
dimartiro Sep 5, 2023
c088287
Merge branch 'diego/trieV1/versioning' of https://github.com/ChainSaf…
dimartiro Sep 7, 2023
55c51b2
Merge branch 'development' of https://github.com/ChainSafe/gossamer i…
dimartiro Sep 7, 2023
48ccf91
Fix merge
dimartiro Sep 7, 2023
f695530
Improve version parsing and simplify checks
dimartiro Sep 8, 2023
31413a4
lint
dimartiro Sep 8, 2023
fd95c86
Merge branch 'diego/trieV1/versioning' into diego/trieV1/runtime-func…
dimartiro Sep 8, 2023
0043e31
Rollback runtime constants changes
dimartiro Sep 8, 2023
28ee3c9
Remove unused version parse switch case
dimartiro Sep 8, 2023
a8f1d23
Change panic to test fail
dimartiro Sep 8, 2023
ab2ee18
Merge branch 'diego/trieV1/versioning' of https://github.com/ChainSaf…
dimartiro Sep 13, 2023
233c2f2
Merge branch 'diego/trieV1/versioning' of https://github.com/ChainSaf…
dimartiro Sep 13, 2023
f32c939
Return empty array instead 0
dimartiro Sep 13, 2023
8e076bb
Fix return zero value
dimartiro Sep 13, 2023
ce04c1f
Merge branch 'diego/trieV1/versioning' of https://github.com/ChainSaf…
dimartiro Sep 13, 2023
97ce272
Add more test cases
dimartiro Sep 14, 2023
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
208 changes: 201 additions & 7 deletions lib/runtime/wazero/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,61 @@ func ext_trie_blake2_256_root_version_1(ctx context.Context, m api.Module, dataS
return ptr
}

func ext_trie_blake2_256_root_version_2(ctx context.Context, m api.Module, dataSpan uint64, version uint32) uint32 {
rtCtx := ctx.Value(runtimeContextKey).(*runtime.Context)
if rtCtx == nil {
panic("nil runtime context")
}

stateVersionBytes, _ := m.Memory().Read(version, 4)
stateVersion, err := trie.ParseVersion(binary.LittleEndian.Uint32(stateVersionBytes))
if err != nil {
logger.Errorf("failed parsing state version: %s", err)
return 0
}

data := read(m, dataSpan)

t := trie.NewEmptyTrie()

type kv struct {
Key, Value []byte
}

// this function is expecting an array of (key, value) tuples
var kvs []kv
if err := scale.Unmarshal(data, &kvs); err != nil {
logger.Errorf("failed scale decoding data: %s", err)
return 0
}

for _, kv := range kvs {
err := t.Put(kv.Key, kv.Value, stateVersion)
if err != nil {
logger.Errorf("failed putting key 0x%x and value 0x%x into trie: %s",
kv.Key, kv.Value, err)
return 0
}
}

// allocate memory for value and copy value to memory
ptr, err := rtCtx.Allocator.Allocate(32)
if err != nil {
logger.Errorf("failed allocating: %s", err)
return 0
}

hash, err := t.Hash()
if err != nil {
logger.Errorf("failed computing trie Merkle root hash: %s", err)
return 0
}

logger.Debugf("root hash is %s", hash)
m.Memory().Write(ptr, hash[:])
return ptr
}

func ext_trie_blake2_256_ordered_root_version_1(ctx context.Context, m api.Module, dataSpan uint64) uint32 {
rtCtx := ctx.Value(runtimeContextKey).(*runtime.Context)
if rtCtx == nil {
Expand Down Expand Up @@ -870,8 +925,62 @@ func ext_trie_blake2_256_ordered_root_version_1(ctx context.Context, m api.Modul

func ext_trie_blake2_256_ordered_root_version_2(
ctx context.Context, m api.Module, dataSpan uint64, version uint32) uint32 {
// TODO: update to use state trie version 1 (#2418)
return ext_trie_blake2_256_ordered_root_version_1(ctx, m, dataSpan)
rtCtx := ctx.Value(runtimeContextKey).(*runtime.Context)
if rtCtx == nil {
panic("nil runtime context")
}

data := read(m, dataSpan)

stateVersionBytes, _ := m.Memory().Read(version, 4)
stateVersion, err := trie.ParseVersion(binary.LittleEndian.Uint32(stateVersionBytes))
if err != nil {
logger.Errorf("failed parsing state version: %s", err)
return 0
}

t := trie.NewEmptyTrie()
var values [][]byte
err = scale.Unmarshal(data, &values)
if err != nil {
logger.Errorf("failed scale decoding data: %s", err)
return 0
}

for i, value := range values {
key, err := scale.Marshal(big.NewInt(int64(i)))
if err != nil {
logger.Errorf("failed scale encoding value index %d: %s", i, err)
return 0
}
logger.Tracef(
"put key=0x%x and value=0x%x",
key, value)

err = t.Put(key, value, stateVersion)
if err != nil {
logger.Errorf("failed putting key 0x%x and value 0x%x into trie: %s",
key, value, err)
return 0
}
}

// allocate memory for value and copy value to memory
ptr, err := rtCtx.Allocator.Allocate(32)
if err != nil {
logger.Errorf("failed allocating: %s", err)
return 0
}

hash, err := t.Hash()
if err != nil {
logger.Errorf("failed computing trie Merkle root hash: %s", err)
return 0
}

logger.Debugf("root hash is %s", hash)
m.Memory().Write(ptr, hash[:])
return ptr
}

func ext_trie_blake2_256_verify_proof_version_1(
Expand Down Expand Up @@ -906,6 +1015,45 @@ func ext_trie_blake2_256_verify_proof_version_1(
return 1
}

func ext_trie_blake2_256_verify_proof_version_2(
ctx context.Context, m api.Module, rootSpan uint32, proofSpan, keySpan, valueSpan uint64, version uint32) uint32 {
rtCtx := ctx.Value(runtimeContextKey).(*runtime.Context)
if rtCtx == nil {
panic("nil runtime context")
}

stateVersionBytes, _ := m.Memory().Read(version, 4)
_, err := trie.ParseVersion(binary.LittleEndian.Uint32(stateVersionBytes))
if err != nil {
logger.Errorf("failed parsing state version: %s", err)
return 0
}

toDecProofs := read(m, proofSpan)
var encodedProofNodes [][]byte
err = scale.Unmarshal(toDecProofs, &encodedProofNodes)
if err != nil {
logger.Errorf("failed scale decoding proof data: %s", err)
return uint32(0)
dimartiro marked this conversation as resolved.
Show resolved Hide resolved
}

key := read(m, keySpan)
value := read(m, valueSpan)

trieRoot, ok := m.Memory().Read(rootSpan, 32)
if !ok {
panic("read overflow")
}

err = proof.Verify(encodedProofNodes, trieRoot, key, value)
if err != nil {
logger.Errorf("failed proof verification: %s", err)
return 0
}

return 1
}

func ext_misc_print_hex_version_1(ctx context.Context, m api.Module, dataSpan uint64) {
data := read(m, dataSpan)
logger.Debugf("data: 0x%x", data)
Expand Down Expand Up @@ -1214,9 +1362,30 @@ func ext_default_child_storage_root_version_1(

//export ext_default_child_storage_root_version_2
func ext_default_child_storage_root_version_2(ctx context.Context, m api.Module, childStorageKey uint64,
stateVersion uint32) (ptrSize uint64) {
// TODO: Implement this after we have storage trie version 1 implemented #2418
return ext_default_child_storage_root_version_1(ctx, m, childStorageKey)
stateVersion uint32) (ptrSize uint64) { //skipcq: RVV-B0012
rtCtx := ctx.Value(runtimeContextKey).(*runtime.Context)
if rtCtx == nil {
panic("nil runtime context")
}
storage := rtCtx.Storage
child, err := storage.GetChild(read(m, childStorageKey))
if err != nil {
logger.Errorf("failed to retrieve child: %s", err)
return 0
dimartiro marked this conversation as resolved.
Show resolved Hide resolved
}

childRoot, err := child.Hash()
if err != nil {
logger.Errorf("failed to encode child root: %s", err)
return 0
}
childRootSlice := childRoot[:]

ret, err := write(m, rtCtx.Allocator, scale.MustMarshal(&childRootSlice))
if err != nil {
panic(err)
}
return ret
}

func ext_default_child_storage_storage_kill_version_1(ctx context.Context, m api.Module, childStorageKeySpan uint64) {
Expand Down Expand Up @@ -2188,8 +2357,33 @@ func ext_storage_root_version_1(ctx context.Context, m api.Module) uint64 {
}

func ext_storage_root_version_2(ctx context.Context, m api.Module, version uint32) uint64 { //skipcq: RVV-B0012
// TODO: update to use state trie version 1 (#2418)
return ext_storage_root_version_1(ctx, m)
rtCtx := ctx.Value(runtimeContextKey).(*runtime.Context)
if rtCtx == nil {
panic("nil runtime context")
}
storage := rtCtx.Storage

stateVersionBytes, _ := m.Memory().Read(version, 4)
_, err := trie.ParseVersion(binary.LittleEndian.Uint32(stateVersionBytes))
if err != nil {
logger.Errorf("failed parsing state version: %s", err)
return 0
dimartiro marked this conversation as resolved.
Show resolved Hide resolved
}

root, err := storage.Root()
if err != nil {
logger.Errorf("failed to get storage root: %s", err)
panic(err)
}

logger.Debugf("root hash is: %s", root)

rootSpan, err := write(m, rtCtx.Allocator, root[:])
if err != nil {
logger.Errorf("failed to allocate: %s", err)
panic(err)
}
return rootSpan
}

func ext_storage_set_version_1(ctx context.Context, m api.Module, keySpan, valueSpan uint64) {
Expand Down
Loading
Loading