From 4e4a11df5dc186f2fa29149ed292838e3e56a0a8 Mon Sep 17 00:00:00 2001 From: rian Date: Sat, 8 Jun 2024 13:31:14 +0300 Subject: [PATCH] test - wip --- core/trie/proof.go | 17 +++++++++++++++-- core/trie/proof_test.go | 4 +++- core/trie/trie.go | 2 -- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/core/trie/proof.go b/core/trie/proof.go index 44da599ccb..6f1b94e5cf 100644 --- a/core/trie/proof.go +++ b/core/trie/proof.go @@ -453,7 +453,12 @@ func BuildTrie(leftProofPath, rightProofPath []StorageNode, keys, values []*felt } // merge proof paths - for i := range min(len(leftProofPath), len(rightProofPath)) { // + for i := range min(len(leftProofPath), len(rightProofPath)) { + // Can't store nil keys so stop merging + if leftProofPath[i].node.Left == nil || leftProofPath[i].node.Right == nil || + rightProofPath[i].node.Left == nil || rightProofPath[i].node.Right == nil { + break + } if leftProofPath[i].key.Equal(rightProofPath[i].key) { leftProofPath[i].node.Right = rightProofPath[i].node.Right rightProofPath[i].node.Left = leftProofPath[i].node.Left @@ -463,6 +468,10 @@ func BuildTrie(leftProofPath, rightProofPath []StorageNode, keys, values []*felt } for _, sNode := range leftProofPath { + // Can't store nil keys (reached end of line for proof) + if sNode.node.Left == nil || sNode.node.Right == nil { + break + } _, err := tempTrie.PutInner(sNode.key, sNode.node) if err != nil { return nil, err @@ -470,7 +479,11 @@ func BuildTrie(leftProofPath, rightProofPath []StorageNode, keys, values []*felt } for _, sNode := range rightProofPath { - _, err := tempTrie.PutInner(sNode.key, sNode.node) // Todo: incorrectly sets the proof key + // Can't store nil keys (reached end of line for proof) + if sNode.node.Left == nil || sNode.node.Right == nil { + break + } + _, err := tempTrie.PutInner(sNode.key, sNode.node) if err != nil { return nil, err } diff --git a/core/trie/proof_test.go b/core/trie/proof_test.go index 1f662be37a..8abd284dbb 100644 --- a/core/trie/proof_test.go +++ b/core/trie/proof_test.go @@ -866,15 +866,17 @@ func TestVerifyRangeProof(t *testing.T) { rightProof, err := trie.GetProof(proofKeys[1], tri) // Looks correct in terms of binary and edges require.NoError(t, err) + // Todo: remove, just included for inspection/testing leftProofPath, err := trie.ProofToPath(leftProof, proofKeys[0], proofValues[0], crypto.Pedersen) // correct require.NoError(t, err) rightProofPath, err := trie.ProofToPath(rightProof, proofKeys[1], proofValues[1], crypto.Pedersen) // gives the correct structure require.NoError(t, err) fmt.Println(leftProofPath, rightProofPath) + proofs := [2][]trie.ProofNode{leftProof, rightProof} rootCommitment, err := tri.Root() require.NoError(t, err) - verif, err := trie.VerifyRangeProof(rootCommitment, keys, values, proofKeys, proofValues, proofs, crypto.Pedersen) + verif, err := trie.VerifyRangeProof(rootCommitment, keys, values, proofKeys, proofValues, proofs, crypto.Pedersen) // todo: panics require.NoError(t, err) require.True(t, verif) }) diff --git a/core/trie/trie.go b/core/trie/trie.go index 30e8be53ca..c86364a835 100644 --- a/core/trie/trie.go +++ b/core/trie/trie.go @@ -279,8 +279,6 @@ func (t *Trie) insertOrUpdateValue(nodeKey *Key, node *Node, nodes []StorageNode newParent.Value = t.hash(node.LeftHash, rightChild.Hash(&rightPath, t.hash)) } else if node.RightHash != nil { newParent.Value = t.hash(leftChild.Hash(&leftPath, t.hash), node.RightHash) - } else { - return errors.New("proof node has neither right/left child hash") } } else { newParent.Value = t.hash(leftChild.Hash(&leftPath, t.hash), rightChild.Hash(&rightPath, t.hash))