diff --git a/core/trie/key.go b/core/trie/key.go index 698e3bf01a..db60754fb7 100644 --- a/core/trie/key.go +++ b/core/trie/key.go @@ -28,10 +28,6 @@ func (k *Key) SubKey(n uint8) (*Key, error) { if n > k.len { return nil, errors.New(fmt.Sprint("cannot subtract key of length %i from key of length %i", n, k.len)) } - if n == k.len { - newkey := NewKey(k.len, k.bitset[:]) - return &newkey, nil - } newKey := &Key{len: n} copy(newKey.bitset[:], k.bitset[len(k.bitset)-int((k.len+7)/8):]) //nolint:gomnd diff --git a/core/trie/node.go b/core/trie/node.go index eb6d50fd60..f2a5d92488 100644 --- a/core/trie/node.go +++ b/core/trie/node.go @@ -12,8 +12,8 @@ type Node struct { Value *felt.Felt Left *Key Right *Key - LeftHash *felt.Felt // To verify proofs, we need to store the hash of children - RightHash *felt.Felt // even when we can't derive their key + LeftHash *felt.Felt + RightHash *felt.Felt } // Hash calculates the hash of a [Node] diff --git a/core/trie/proof.go b/core/trie/proof.go index 03af44c03b..2c81c2852c 100644 --- a/core/trie/proof.go +++ b/core/trie/proof.go @@ -46,7 +46,6 @@ func (pn *ProofNode) PrettyPrint() { fmt.Printf(" Edge:\n") fmt.Printf(" Child: %v\n", pn.Edge.Child) fmt.Printf(" Path: %v\n", pn.Edge.Path) - fmt.Printf(" Value: %v\n", pn.Edge.Value) } } @@ -58,7 +57,6 @@ type Binary struct { type Edge struct { Child *felt.Felt // child hash Path *Key // path from parent to child - Value *felt.Felt // this nodes hash } func GetBoundaryProofs(leftBoundary, rightBoundary *Key, tri *Trie) ([2][]ProofNode, error) { @@ -192,10 +190,11 @@ func VerifyProof(root *felt.Felt, key *Key, value *felt.Felt, proofs []ProofNode return false } + // Todo: // If we are verifying the key doesn't exist, then we should // update subKey to point in the other direction if value == nil && i == len(proofs)-1 { - return true // todo: hack for non-set proof nodes + return true } if !proofNode.Edge.Path.Equal(subKey) { @@ -296,13 +295,13 @@ func ensureMonotonicIncreasing(proofKeys [2]*Key, keys []*felt.Felt) error { func shouldSquish(idx int, proofNodes []ProofNode, hashF hashFunc) (int, uint8, error) { parent := &proofNodes[idx] if idx == len(proofNodes)-1 { // The node may have children, but we can only derive their hashes here - var hack int + var isEdge int if parent.Edge != nil { - hack = 1 + isEdge = 1 } else if parent.Binary != nil { - hack = 0 + isEdge = 0 } - return hack, parent.Len(), nil + return isEdge, parent.Len(), nil } child := &proofNodes[idx+1] @@ -346,7 +345,7 @@ func assignChild(crntNode *Node, nilKey, childKey *Key, isRight bool) { func ProofToPath(proofNodes []ProofNode, leafKey *Key, hashF hashFunc) ([]StorageNode, error) { pathNodes := []StorageNode{} - // Hack: this allows us to store a right without an existing left node. + // Child keys that can't be derived are set to nilKey, so that we can store the node zeroFeltBytes := new(felt.Felt).Bytes() nilKey := NewKey(0, zeroFeltBytes[:]) diff --git a/core/trie/trie_test.go b/core/trie/trie_test.go index c51ba74d60..1fc03fbd09 100644 --- a/core/trie/trie_test.go +++ b/core/trie/trie_test.go @@ -7,7 +7,6 @@ import ( "github.com/NethermindEth/juno/core/felt" "github.com/NethermindEth/juno/core/trie" "github.com/NethermindEth/juno/db" - "github.com/NethermindEth/juno/db/pebble" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -83,37 +82,6 @@ func TestTriePut(t *testing.T) { }) } -func TestTriePutInner(t *testing.T) { - t.Run("put node to empty trie", func(t *testing.T) { - memdb := pebble.NewMemTest(t) - txn, err := memdb.NewTransaction(true) - require.NoError(t, err) - - tempTrie, err := trie.NewTriePedersen(trie.NewStorage(txn, []byte{0}), 251) - require.NoError(t, err) - - keyFeltBytes := new(felt.Felt).SetUint64(123).Bytes() - key := trie.NewKey(10, keyFeltBytes[:]) - - leftKeyFeltBytes := new(felt.Felt).SetUint64(789).Bytes() - leftKey := trie.NewKey(11, leftKeyFeltBytes[:]) - rightKeyFeltBytes := new(felt.Felt).SetUint64(135).Bytes() - rightKey := trie.NewKey(11, rightKeyFeltBytes[:]) - node := trie.Node{ - Value: new(felt.Felt).SetUint64(456), - Left: &leftKey, - Right: &rightKey, - } - - _, err = tempTrie.PutInner(&key, &node) - require.NoError(t, err) - - gotNode, err := tempTrie.GetNodeFromKey(&key) - require.NoError(t, err) - require.Equal(t, node, *gotNode) - }) -} - func TestTrieDeleteBasic(t *testing.T) { // left branch leftKeyNum, err := strconv.ParseUint("100", 2, 64)