Skip to content

Commit

Permalink
feat(trie): Add v2 runtime functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dimartiro committed Aug 9, 2023
1 parent 4f48364 commit 76767e9
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 6 deletions.
185 changes: 179 additions & 6 deletions lib/runtime/wazero/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,56 @@ 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")
}

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 {
//TODO: use version parameter here
err := t.Put(kv.Key, kv.Value)
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
}

//TODO: use version parameter here
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 @@ -913,8 +963,57 @@ 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)

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)

//TODO: use version parameter here
err = t.Put(key, value)
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
}

//TODO: use version parameter here
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 @@ -949,6 +1048,39 @@ 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")
}

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

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

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

//TODO: use trie version parameter here
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 @@ -1273,8 +1405,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)
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
}

//TODO: version this call
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 @@ -2267,8 +2421,27 @@ 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

//TODO: user version parameter
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
6 changes: 6 additions & 0 deletions lib/runtime/wazero/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ func NewInstance(code []byte, cfg Config) (instance *Instance, err error) {
WithFunc(ext_trie_blake2_256_root_version_1).
Export("ext_trie_blake2_256_root_version_1").
NewFunctionBuilder().
WithFunc(ext_trie_blake2_256_root_version_2).
Export("ext_trie_blake2_256_root_version_2").
NewFunctionBuilder().
WithFunc(ext_trie_blake2_256_ordered_root_version_1).
Export("ext_trie_blake2_256_ordered_root_version_1").
NewFunctionBuilder().
Expand All @@ -216,6 +219,9 @@ func NewInstance(code []byte, cfg Config) (instance *Instance, err error) {
WithFunc(ext_trie_blake2_256_verify_proof_version_1).
Export("ext_trie_blake2_256_verify_proof_version_1").
NewFunctionBuilder().
WithFunc(ext_trie_blake2_256_verify_proof_version_2).
Export("ext_trie_blake2_256_verify_proof_version_2").
NewFunctionBuilder().
WithFunc(ext_misc_print_hex_version_1).
Export("ext_misc_print_hex_version_1").
NewFunctionBuilder().
Expand Down

0 comments on commit 76767e9

Please sign in to comment.