Skip to content

Commit

Permalink
leaf values are correct
Browse files Browse the repository at this point in the history
  • Loading branch information
rian committed Jun 2, 2024
1 parent 8d9d12e commit 4e5c3a5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
12 changes: 9 additions & 3 deletions core/trie/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,7 @@ func ProofToPath(proofNodes []ProofNode, leaf *felt.Felt, hashF hashFunc) ([]Sto

i := 0
offset := 0
// totalOffset := 0
for i <= len(proofNodes)-1 {

if len(pathNodes) > 0 {
if proofNodes[i].Edge != nil {
height = pathNodes[len(pathNodes)-1].key.len + proofNodes[i].Edge.Path.len
Expand Down Expand Up @@ -387,9 +385,16 @@ func ProofToPath(proofNodes []ProofNode, leaf *felt.Felt, hashF hashFunc) ([]Sto
}
// Add leaf
lastNode := pathNodes[len(pathNodes)-1].node
lastProof := proofNodes[len(proofNodes)-1]
if lastNode.Left.Equal(&leafKey) || lastNode.Right.Equal(&leafKey) {
leafNode := Node{}
leafNode.Value = proofNodes[len(proofNodes)-1].Hash(hashF)
if lastProof.Edge != nil {
leafNode.Value = lastProof.Edge.Child
} else if lastNode.Left.Equal(&leafKey) {
leafNode.Value = lastProof.Binary.LeftHash
} else {
leafNode.Value = lastProof.Binary.RightHash
}
pathNodes = append(pathNodes, StorageNode{key: &leafKey, node: &leafNode})
}
return pathNodes, nil
Expand All @@ -413,6 +418,7 @@ func BuildTrie(leftProof, rightProof []StorageNode, keys []*felt.Felt, values []

for _, sNode := range leftProof {
_, err := tempTrie.PutInner(sNode.key, sNode.node)
fmt.Println(sNode.node.Value.String()) // Lead value is incorrect
if err != nil {
return nil, err
}
Expand Down
25 changes: 14 additions & 11 deletions core/trie/proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ func TestBuildTrie(t *testing.T) {
require.NoError(t, err)

leftProof, err := trie.ProofToPath(bProofs[0], key1, crypto.Pedersen) // correct
fmt.Println(leftProof[len(leftProof)-1].Node().Value.String()) // correct
require.NoError(t, err)

rightProof, err := trie.ProofToPath(bProofs[1], key3, crypto.Pedersen) // correct
Expand All @@ -584,18 +585,20 @@ func TestBuildTrie(t *testing.T) {
builtLeftRightNode, err := builtTrie.GetNodeFromKey(builtLeftNode.Right) // looks correct
require.NoError(t, err)

require.Equal(t, rootKey, builtRootKey)
compareLeftRight(t, rootNode, builtRootNode)
compareLeftRight(t, leftNode, builtLeftNode)
compareLeftRight(t, rightNode, builtRightNode)
compareLeftRight(t, leftleftNode, builtLeftLeftNode)
compareLeftRight(t, leftrightNode, builtLeftRightNode)
require.Equal(t, leftleftNode.Value.String(), builtLeftLeftNode.Value.String(), "should be 0x4") // incorrect got..
require.Equal(t, leftrightNode.Value.String(), builtLeftRightNode.Value.String(), "should be 0x5")
require.Equal(t, rightNode.Value.String(), builtRightNode.Value.String(), "should be 0x6") // incorrect got..
// Assert the structure is correct
require.Equal(t, rootKey, builtRootKey) // correct
compareLeftRight(t, rootNode, builtRootNode) // correct
compareLeftRight(t, leftNode, builtLeftNode) // correct
compareLeftRight(t, rightNode, builtRightNode) // correct
compareLeftRight(t, leftleftNode, builtLeftLeftNode) // correct
compareLeftRight(t, leftrightNode, builtLeftRightNode) // correct

// If the trie has the correct structure, and each nodes left/right is correct, and the leaf nodes have the correct
// Values, then we should be able to reconstruct the correct root commitment
// Assert the leaf nodes have the correct values
require.Equal(t, leftleftNode.Value.String(), builtLeftLeftNode.Value.String(), "should be 0x4") // correct
require.Equal(t, leftrightNode.Value.String(), builtLeftRightNode.Value.String(), "should be 0x5") // correct
require.Equal(t, rightNode.Value.String(), builtRightNode.Value.String(), "should be 0x6") // correct

// Given the above two asserts pass, we should be able to reconstruct the correct commitment
reconstructedRootCommitment, err := builtTrie.Root() // Todo: need to force rehashing all nodes (since all are modified)??
require.NoError(t, err)
fmt.Println(reconstructedRootCommitment.String())
Expand Down
1 change: 0 additions & 1 deletion core/trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,6 @@ func (t *Trie) PutInner(key *Key, node *Node) (*felt.Felt, error) {
if t.rootKey == nil {
t.setRootKey(key)
}
// t.dirtyNodes = append(t.dirtyNodes, key) // todo: update everything?
return &felt.Zero, nil
}

Expand Down

0 comments on commit 4e5c3a5

Please sign in to comment.