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

chore(trie): proof code refactoring and fixes #2604

Merged
merged 24 commits into from
Jul 7, 2022
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
47e2d0b
Merge in `internal/trie/proof`
qdm12 May 25, 2022
50e221e
lib/trie: use Get `Database` interface
qdm12 May 26, 2022
f641cb4
lib/trie: rename `proofEncodedNodes` to `encodedProofNodes`
qdm12 May 26, 2022
f93b54c
lib/runtime/wasmer: rename `decProofs` to `encodedProofNodes`
qdm12 May 26, 2022
e5dc44e
Refactor proof functions
qdm12 May 26, 2022
90d2546
Move proof trie build functions to `internal/trie/proof`
qdm12 Jun 14, 2022
7285e16
Fix `buildTrie` to detect root node for roots with encoding smaller t…
qdm12 Jun 14, 2022
31ccd8e
Add check for root not found
qdm12 Jun 14, 2022
6882bb6
Fix `buildTrie` trigger sizes for root hash check
qdm12 Jun 15, 2022
ce04e19
Add tests with full coverage
qdm12 Jun 15, 2022
e8d88b4
Remove older tests
qdm12 Jun 15, 2022
b64e74b
`Generate` takes []byte instead of common.Hash to match `Verify` sign…
qdm12 Jun 15, 2022
b5b33c2
Move `internal/trie/proof` -> `lib/trie/proof`
qdm12 Jun 15, 2022
2c89af8
Add Verify export comment
qdm12 Jun 15, 2022
ea84414
Deduplicate encoded nodes (as before)
qdm12 Jun 15, 2022
ee988d5
Use `encodeNode` helper in `Test_walk`
qdm12 Jun 15, 2022
da0c760
Review some error wrappings
qdm12 Jun 15, 2022
84b5a57
Set all temporary proof trie nodes as dirty
qdm12 Jun 15, 2022
9a5bb95
Comments fixes
qdm12 Jun 15, 2022
9f70103
Add walk benchmark
qdm12 Jun 17, 2022
99541b9
Rename variables
qdm12 Jun 17, 2022
69cd49f
WIP
qdm12 Jun 30, 2022
53e4a64
`Generate` generates deduplicated encoded proof nodes for N full keys
qdm12 Jul 4, 2022
25b4e57
Do not add inlined nodes to proof nodes
qdm12 Jul 4, 2022
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
6 changes: 4 additions & 2 deletions dot/state/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/ChainSafe/gossamer/lib/common"
rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/ChainSafe/gossamer/lib/trie/proof"
)

// storagePrefix storage key prefix.
Expand Down Expand Up @@ -301,6 +302,7 @@ func (s *StorageState) LoadCodeHash(hash *common.Hash) (common.Hash, error) {
}

// GenerateTrieProof returns the proofs related to the keys on the state root trie
func (s *StorageState) GenerateTrieProof(stateRoot common.Hash, keys [][]byte) ([][]byte, error) {
return trie.GenerateProof(stateRoot[:], keys, s.db)
func (s *StorageState) GenerateTrieProof(stateRoot common.Hash, keys [][]byte) (
encodedProofNodes [][]byte, err error) {
return proof.Generate(stateRoot[:], keys, s.db)
}
10 changes: 10 additions & 0 deletions internal/trie/node/children.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,13 @@ func (n *Node) NumChildren() (count int) {
}
return count
}

// HasChild returns true if the node has at least one child.
func (n *Node) HasChild() (has bool) {
for _, child := range n.Children {
if child != nil {
return true
}
}
return false
}
39 changes: 39 additions & 0 deletions internal/trie/node/children_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,42 @@ func Test_Node_NumChildren(t *testing.T) {
})
}
}

func Test_Node_HasChild(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
node Node
has bool
}{
"no child": {},
"one child at index 0": {
node: Node{
Children: []*Node{
{},
},
},
has: true,
},
"one child at index 1": {
node: Node{
Children: []*Node{
nil,
{},
},
},
has: true,
},
}

for name, testCase := range testCases {
testCase := testCase
t.Run(name, func(t *testing.T) {
t.Parallel()

has := testCase.node.HasChild()

assert.Equal(t, testCase.has, has)
})
}
}
10 changes: 0 additions & 10 deletions internal/trie/record/node.go

This file was deleted.

27 changes: 0 additions & 27 deletions internal/trie/record/recorder.go

This file was deleted.

122 changes: 0 additions & 122 deletions internal/trie/record/recorder_test.go

This file was deleted.

14 changes: 5 additions & 9 deletions lib/runtime/wasmer/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ import (
rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage"
"github.com/ChainSafe/gossamer/lib/transaction"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/ChainSafe/gossamer/lib/trie/proof"
"github.com/ChainSafe/gossamer/pkg/scale"

wasm "github.com/wasmerio/go-ext-wasm/wasmer"
Expand Down Expand Up @@ -886,8 +887,8 @@ func ext_trie_blake2_256_verify_proof_version_1(context unsafe.Pointer, rootSpan
instanceContext := wasm.IntoInstanceContext(context)

toDecProofs := asMemorySlice(instanceContext, proofSpan)
var decProofs [][]byte
err := scale.Unmarshal(toDecProofs, &decProofs)
var encodedProofNodes [][]byte
err := scale.Unmarshal(toDecProofs, &encodedProofNodes)
if err != nil {
logger.Errorf("[ext_trie_blake2_256_verify_proof_version_1]: %s", err)
return C.int32_t(0)
Expand All @@ -899,18 +900,13 @@ func ext_trie_blake2_256_verify_proof_version_1(context unsafe.Pointer, rootSpan
mem := instanceContext.Memory().Data()
trieRoot := mem[rootSpan : rootSpan+32]

exists, err := trie.VerifyProof(decProofs, trieRoot, []trie.Pair{{Key: key, Value: value}})
err = proof.Verify(encodedProofNodes, trieRoot, key, value)
if err != nil {
logger.Errorf("[ext_trie_blake2_256_verify_proof_version_1]: %s", err)
return C.int32_t(0)
}

var result C.int32_t = 0
if exists {
result = 1
}

return result
return C.int32_t(1)
}

//export ext_misc_print_hex_version_1
Expand Down
15 changes: 8 additions & 7 deletions lib/runtime/wasmer/imports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/runtime/storage"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/ChainSafe/gossamer/lib/trie/proof"
"github.com/ChainSafe/gossamer/pkg/scale"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -1796,7 +1797,7 @@ func Test_ext_trie_blake2_256_verify_proof_version_1(t *testing.T) {
root := hash.ToBytes()
otherRoot := otherHash.ToBytes()

proof, err := trie.GenerateProof(root, keys, memdb)
allProofs, err := proof.Generate(root, keys, memdb)
require.NoError(t, err)

testcases := map[string]struct {
Expand All @@ -1805,17 +1806,17 @@ func Test_ext_trie_blake2_256_verify_proof_version_1(t *testing.T) {
expect bool
}{
"Proof should be true": {
root: root, key: []byte("do"), proof: proof, value: []byte("verb"), expect: true},
root: root, key: []byte("do"), proof: allProofs, value: []byte("verb"), expect: true},
"Root empty, proof should be false": {
root: []byte{}, key: []byte("do"), proof: proof, value: []byte("verb"), expect: false},
root: []byte{}, key: []byte("do"), proof: allProofs, value: []byte("verb"), expect: false},
"Other root, proof should be false": {
root: otherRoot, key: []byte("do"), proof: proof, value: []byte("verb"), expect: false},
root: otherRoot, key: []byte("do"), proof: allProofs, value: []byte("verb"), expect: false},
"Value empty, proof should be true": {
root: root, key: []byte("do"), proof: proof, value: nil, expect: true},
root: root, key: []byte("do"), proof: allProofs, value: nil, expect: true},
"Unknow key, proof should be false": {
root: root, key: []byte("unknow"), proof: proof, value: nil, expect: false},
root: root, key: []byte("unknow"), proof: allProofs, value: nil, expect: false},
"Key and value unknow, proof should be false": {
root: root, key: []byte("unknow"), proof: proof, value: []byte("unknow"), expect: false},
root: root, key: []byte("unknow"), proof: allProofs, value: []byte("unknow"), expect: false},
"Empty proof, should be false": {
root: root, key: []byte("do"), proof: [][]byte{}, value: nil, expect: false},
}
Expand Down
Loading