Skip to content

Commit

Permalink
feat(pkg/trie): Add merkle proof verification support for TrieDB (#4091)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimartiro authored Jul 19, 2024
1 parent f308289 commit 7cec9b8
Show file tree
Hide file tree
Showing 8 changed files with 804 additions and 197 deletions.
34 changes: 11 additions & 23 deletions pkg/trie/triedb/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ func NewEncodedValue(value nodeValue, partial []byte, childF onChildStoreFn) (co
// Check and get new new value hash
switch cr := childRef.(type) {
case HashChildReference:
if cr.hash == common.EmptyHash {
if common.Hash(cr) == common.EmptyHash {
panic("new external value are always added before encoding a node")
}

if v.hash != common.EmptyHash {
if v.hash != cr.hash {
if v.hash != common.Hash(cr) {
panic("hash mismatch")
}
} else {
v.hash = cr.hash
v.hash = common.Hash(cr)
}
default:
panic("value node can never be inlined")
Expand Down Expand Up @@ -241,9 +241,9 @@ func NewChildReferences(encodedChildren [codec.ChildrenCapacity]codec.MerkleValu

switch n := child.(type) {
case codec.InlineNode:
children[i] = NewInlineChildReference(n.Data)
children[i] = InlineChildReference(n.Data)
case codec.HashedNode:
children[i] = NewHashChildReference(common.Hash(n.Data))
children[i] = HashChildReference(common.Hash(n.Data))
}
}
return children
Expand All @@ -256,41 +256,29 @@ type ChildReference interface {

type (
// HashChildReference is a reference to a child node that is not inlined
HashChildReference struct {
hash common.Hash
}
HashChildReference common.Hash
// InlineChildReference is a reference to an inlined child node
InlineChildReference struct {
encodedNode []byte
}
InlineChildReference []byte
)

func (h HashChildReference) getNodeData() []byte {
return h.hash.ToBytes()
return h[:]
}
func (i InlineChildReference) getNodeData() []byte {
return i.encodedNode
}

func NewHashChildReference(hash common.Hash) HashChildReference {
return HashChildReference{hash: hash}
}

func NewInlineChildReference(encodedNode []byte) InlineChildReference {
return InlineChildReference{encodedNode: encodedNode}
return i
}

type onChildStoreFn = func(node nodeToEncode, partialKey []byte, childIndex *byte) (ChildReference, error)

const emptyTrieBytes = byte(0)
const EmptyTrieBytes = byte(0)

// NewEncodedNode creates a new encoded node from a node and a child store function and return its bytes
func NewEncodedNode(node Node, childF onChildStoreFn) (encodedNode []byte, err error) {
encodingBuffer := bytes.NewBuffer(nil)

switch n := node.(type) {
case Empty:
return []byte{emptyTrieBytes}, nil
return []byte{EmptyTrieBytes}, nil
case Leaf:
pr := n.partialKey
value, err := NewEncodedValue(n.value, pr, childF)
Expand Down
Loading

0 comments on commit 7cec9b8

Please sign in to comment.