Skip to content

Commit

Permalink
feat: reorganise extension node insertion to use separate pointers fo…
Browse files Browse the repository at this point in the history
…r the child node
  • Loading branch information
h5law committed Mar 20, 2024
1 parent 4831b52 commit 46dc5c5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
11 changes: 0 additions & 11 deletions proofs.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,17 +193,6 @@ func (proof *SparseMerkleClosestProof) Unmarshal(bz []byte) error {
return dec.Decode(proof)
}

// GetValueHash returns the value hash of the closest proof.
func (proof *SparseMerkleClosestProof) GetValueHash(spec *TrieSpec) []byte {
if proof.ClosestValueHash == nil {
return nil
}
if spec.sumTrie {
return proof.ClosestValueHash[:len(proof.ClosestValueHash)-sumSize]
}
return proof.ClosestValueHash
}

func (proof *SparseMerkleClosestProof) validateBasic(spec *TrieSpec) error {
// ensure the depth of the leaf node being proven is within the path size
if proof.Depth < 0 || proof.Depth > spec.ph.PathSize()*8 {
Expand Down
29 changes: 23 additions & 6 deletions smt.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,19 +178,36 @@ func (smt *SMT) update(
}
// We insert an "extension" representing multiple single-branch inner nodes
last := &node
var newInner *innerNode
if getPathBit(path, prefixlen) == left {
newInner = &innerNode{
leftChild: newLeaf,
rightChild: leaf,
}
} else {
newInner = &innerNode{
leftChild: leaf,
rightChild: newLeaf,
}
}
// Determine if we need to insert an extension or a branch
if depth < prefixlen {
// note: this keeps path slice alive - GC inefficiency?
if depth > 0xff {
panic("invalid depth")
}
ext := extensionNode{path: path, pathBounds: [2]byte{byte(depth), byte(prefixlen)}}
ext := extensionNode{
child: newInner,
path: path,
pathBounds: [2]byte{
byte(depth), byte(prefixlen),
},
}
// Dereference the last node to replace it with the extension node
*last = &ext
last = &ext.child
}
if getPathBit(path, prefixlen) == left {
*last = &innerNode{leftChild: newLeaf, rightChild: leaf}
} else {
*last = &innerNode{leftChild: leaf, rightChild: newLeaf}
// Dereference the last node to replace it with the new inner node
*last = newInner
}
return node, nil
}
Expand Down

0 comments on commit 46dc5c5

Please sign in to comment.