Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updating verify proof range to handle empty proof keys #1901

Merged
merged 32 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
5cd826f
updating verify proof range to handle empty proof keys
Jun 7, 2024
3ecb395
test non set proof key - wip
Jun 7, 2024
d2fe491
wip - proof to Path doesn't work
Jun 7, 2024
2cd3157
store the hashes of children in ProofToPath
Jun 8, 2024
3603e28
ProoftoPath update to handle unset proof key
Jun 8, 2024
d8f5811
test - wip
Jun 8, 2024
40a3d04
test - wip
Jun 9, 2024
fb6c899
test passes! :D
Jun 10, 2024
07e99f5
lint
Jun 10, 2024
c0b933a
tidy
Jun 10, 2024
529aef2
fixing tests that broke wip
Jun 10, 2024
ca360e9
build4keyTrie passes again!
Jun 11, 2024
fdf6af2
fixed more tests
Jun 11, 2024
9f9f2fe
fix test wip
Jun 11, 2024
bad7267
fix more tests
Jun 11, 2024
92a98df
fix test -wip
Jun 11, 2024
727801b
fix tests wip
Jun 11, 2024
b4ae2b6
wip
Jun 11, 2024
a123730
all tests passgit add .
Jun 12, 2024
d249186
lint
Jun 12, 2024
21f9453
tidy
Jun 12, 2024
2d7c6e8
tidy logic
Jun 12, 2024
46c7b8a
tidy logic
Jun 12, 2024
0bb2edc
tidy logic
Jun 12, 2024
ce20e08
comment: update getLeftRightHash
Jun 12, 2024
ec2f917
update ProofToPath comment
Jun 12, 2024
dd21cc0
bubble up getLeftRightHash err
Jun 12, 2024
e4aafd1
Merge branch 'main' into rianhughes/proof-range-test
rianhughes Jun 25, 2024
1004a9e
Merge branch 'main' into rianhughes/proof-range-test
rianhughes Jun 25, 2024
dfbedca
Start using Artifactory for CI/CD in favour of Docker Registry
derrix060 Jun 25, 2024
83f5c08
Merge branch 'main' into rianhughes/proof-range-test
rianhughes Jun 28, 2024
f3b20cd
Merge branch 'main' into rianhughes/proof-range-test
rianhughes Jun 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
tidy
  • Loading branch information
rian committed Jun 12, 2024
commit 21f9453400a8412246af116b9b9e0f921ba46a1e
4 changes: 0 additions & 4 deletions core/trie/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions core/trie/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
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]
Expand Down Expand Up @@ -56,33 +56,33 @@
wrote, errInner := n.Left.WriteTo(buf)
totalBytes += wrote
if err != nil {
return totalBytes, errInner

Check warning on line 59 in core/trie/node.go

View check run for this annotation

Codecov / codecov/patch

core/trie/node.go#L59

Added line #L59 was not covered by tests
}
wrote, errInner = n.Right.WriteTo(buf) // n.Right is non-nil by design
totalBytes += wrote
if err != nil {
return totalBytes, errInner

Check warning on line 64 in core/trie/node.go

View check run for this annotation

Codecov / codecov/patch

core/trie/node.go#L64

Added line #L64 was not covered by tests
}
}

if n.LeftHash == nil && n.RightHash == nil {
return totalBytes, nil
} else if (n.LeftHash != nil && n.RightHash == nil) || (n.LeftHash == nil && n.RightHash != nil) {
return totalBytes, errors.New("cannot store only one lefthash or righthash")

Check warning on line 71 in core/trie/node.go

View check run for this annotation

Codecov / codecov/patch

core/trie/node.go#L71

Added line #L71 was not covered by tests
}

leftHashB := n.LeftHash.Bytes()
wrote, err = buf.Write(leftHashB[:])
rianhughes marked this conversation as resolved.
Show resolved Hide resolved
totalBytes += int64(wrote)
if err != nil {
return totalBytes, err

Check warning on line 78 in core/trie/node.go

View check run for this annotation

Codecov / codecov/patch

core/trie/node.go#L78

Added line #L78 was not covered by tests
}

rightHashB := n.RightHash.Bytes()
wrote, err = buf.Write(rightHashB[:])
totalBytes += int64(wrote)
if err != nil {
return totalBytes, err

Check warning on line 85 in core/trie/node.go

View check run for this annotation

Codecov / codecov/patch

core/trie/node.go#L85

Added line #L85 was not covered by tests
}

return totalBytes, nil
Expand Down Expand Up @@ -117,7 +117,7 @@
}
data = data[n.Left.EncodedLen():]
if err := n.Right.UnmarshalBinary(data); err != nil {
return err

Check warning on line 120 in core/trie/node.go

View check run for this annotation

Codecov / codecov/patch

core/trie/node.go#L120

Added line #L120 was not covered by tests
}
data = data[n.Right.EncodedLen():]

Expand All @@ -131,7 +131,7 @@
return nil
}
if len(data) != 2*felt.Bytes {
return errors.New("the node does not contain both left and right hash")

Check warning on line 134 in core/trie/node.go

View check run for this annotation

Codecov / codecov/patch

core/trie/node.go#L134

Added line #L134 was not covered by tests
}
n.LeftHash.SetBytes(data[:felt.Bytes])
data = data[felt.Bytes:]
Expand Down
17 changes: 8 additions & 9 deletions core/trie/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
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)
}
}

Expand All @@ -58,18 +57,17 @@
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) {
proofs := [2][]ProofNode{}
leftProof, err := GetProof(leftBoundary, tri)
if err != nil {
return proofs, err

Check warning on line 66 in core/trie/proof.go

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L66

Added line #L66 was not covered by tests
}
rightProof, err := GetProof(rightBoundary, tri)
if err != nil {
return proofs, err

Check warning on line 70 in core/trie/proof.go

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L70

Added line #L70 was not covered by tests
}
proofs[0] = leftProof
proofs[1] = rightProof
Expand Down Expand Up @@ -192,10 +190,11 @@
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) {
Expand All @@ -220,18 +219,18 @@
) (bool, error) {
// Step 0: checks
if len(keys) != len(values) {
return false, fmt.Errorf("inconsistent proof data, number of keys: %d, number of values: %d", len(keys), len(values))

Check warning on line 222 in core/trie/proof.go

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L222

Added line #L222 was not covered by tests
}

// Ensure all keys are monotonic increasing
if err := ensureMonotonicIncreasing(proofKeys, keys); err != nil {
return false, err

Check warning on line 227 in core/trie/proof.go

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L227

Added line #L227 was not covered by tests
}

// Ensure the inner values contain no deletions
for _, value := range values {
if value.Equal(&felt.Zero) {
return false, errors.New("range contains deletion")

Check warning on line 233 in core/trie/proof.go

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L233

Added line #L233 was not covered by tests
}
}

Expand All @@ -241,29 +240,29 @@
for i := 0; i < 2; i++ {
if proofs[i] != nil {
if !VerifyProof(root, proofKeys[i], proofValues[i], proofs[i], hash) {
return false, fmt.Errorf("invalid proof for key %x", proofKeys[i].String())

Check warning on line 243 in core/trie/proof.go

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L243

Added line #L243 was not covered by tests
}

proofPaths[i], err = ProofToPath(proofs[i], proofKeys[i], hash)
if err != nil {
return false, err

Check warning on line 248 in core/trie/proof.go

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L248

Added line #L248 was not covered by tests
}
}
}

// Step 2: Build trie from proofPaths and keys
tmpTrie, err := BuildTrie(proofPaths[0], proofPaths[1], keys, values) // Todo: left points to itself
tmpTrie, err := BuildTrie(proofPaths[0], proofPaths[1], keys, values)
if err != nil {
return false, err

Check warning on line 256 in core/trie/proof.go

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L256

Added line #L256 was not covered by tests
}

// Verify that the recomputed root hash matches the provided root hash
recomputedRoot, err := tmpTrie.Root()
if err != nil {
return false, err

Check warning on line 262 in core/trie/proof.go

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L262

Added line #L262 was not covered by tests
}
if !recomputedRoot.Equal(root) {
return false, errors.New("root hash mismatch")

Check warning on line 265 in core/trie/proof.go

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L265

Added line #L265 was not covered by tests
}

return true, nil
Expand All @@ -273,19 +272,19 @@
if proofKeys[0] != nil {
leftProofFelt := proofKeys[0].Felt()
if leftProofFelt.Cmp(keys[0]) >= 0 {
return errors.New("range is not monotonically increasing")

Check warning on line 275 in core/trie/proof.go

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L275

Added line #L275 was not covered by tests
}
}
if proofKeys[1] != nil {
rightProofFelt := proofKeys[1].Felt()
if keys[len(keys)-1].Cmp(&rightProofFelt) >= 0 {
return errors.New("range is not monotonically increasing")

Check warning on line 281 in core/trie/proof.go

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L281

Added line #L281 was not covered by tests
}
}
if len(keys) >= 2 {
for i := 0; i < len(keys)-1; i++ {
if keys[i].Cmp(keys[i+1]) >= 0 {
return errors.New("range is not monotonically increasing")

Check warning on line 287 in core/trie/proof.go

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L287

Added line #L287 was not covered by tests
}
}
}
Expand All @@ -296,13 +295,13 @@
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]
Expand All @@ -318,7 +317,7 @@
} else if parent.Binary.RightHash.Equal(childHash) {
return 1, child.Edge.Path.len, nil
} else {
return 0, 0, errors.New("can't determine the child hash from the parent and child")

Check warning on line 320 in core/trie/proof.go

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L320

Added line #L320 was not covered by tests
}
}

Expand All @@ -326,7 +325,7 @@
return 0, 1, nil
}

return 0, 1, nil

Check warning on line 328 in core/trie/proof.go

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L328

Added line #L328 was not covered by tests
}

func assignChild(crntNode *Node, nilKey, childKey *Key, isRight bool) {
Expand All @@ -346,7 +345,7 @@
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[:])

Expand Down Expand Up @@ -374,11 +373,11 @@
// Set the key of the current node
squishedParent, squishParentOffset, err := shouldSquish(i, proofNodes, hashF)
if err != nil {
return nil, err

Check warning on line 376 in core/trie/proof.go

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L376

Added line #L376 was not covered by tests
}
crntKey, err = getCrntKey(height, squishParentOffset, leafKey, pNode)
if err != nil {
return nil, err

Check warning on line 380 in core/trie/proof.go

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L380

Added line #L380 was not covered by tests
}

// Set the value of the current node
Expand All @@ -386,14 +385,14 @@

// End of the line, no children
if crntKey.len == 251 { //nolint:gomnd
break

Check warning on line 388 in core/trie/proof.go

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L388

Added line #L388 was not covered by tests
}

// Set the child key of the current node.
childInd := i + squishedParent + 1
childKey, err := getChildKey(childInd, crntKey, leafKey, &nilKey, proofNodes, hashF)
if err != nil {
return nil, err

Check warning on line 395 in core/trie/proof.go

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L395

Added line #L395 was not covered by tests
}
childIsRight := leafKey.Test(leafKey.len - crntKey.len - 1)
assignChild(&crntNode, &nilKey, childKey, childIsRight)
Expand Down Expand Up @@ -447,7 +446,7 @@
} else {
squishChild, squishChildOffset, err = shouldSquish(childIdx, proofNodes, hashF)
if err != nil {
return nil, err

Check warning on line 449 in core/trie/proof.go

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L449

Added line #L449 was not covered by tests
}
}
if crntKey.len+uint8(squishChild)+squishChildOffset == 251 { //nolint:gomnd
Expand All @@ -460,7 +459,7 @@
func getHeight(idx int, pathNodes []StorageNode, proofNodes []ProofNode) uint8 {
if len(pathNodes) > 0 {
if proofNodes[idx].Edge != nil {
return pathNodes[len(pathNodes)-1].key.len + proofNodes[idx].Edge.Path.len

Check warning on line 462 in core/trie/proof.go

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L462

Added line #L462 was not covered by tests
} else {
return pathNodes[len(pathNodes)-1].key.len + 1
}
Expand All @@ -473,7 +472,7 @@
func BuildTrie(leftProofPath, rightProofPath []StorageNode, keys, values []*felt.Felt) (*Trie, error) { //nolint:gocyclo
tempTrie, err := NewTriePedersen(newMemStorage(), 251) //nolint:gomnd
if err != nil {
return nil, err

Check warning on line 475 in core/trie/proof.go

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L475

Added line #L475 was not covered by tests
}

// merge proof paths
Expand All @@ -481,40 +480,40 @@
// 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

Check warning on line 483 in core/trie/proof.go

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L483

Added line #L483 was not covered by tests
}
if leftProofPath[i].key.Equal(rightProofPath[i].key) {
leftProofPath[i].node.Right = rightProofPath[i].node.Right
rightProofPath[i].node.Left = leftProofPath[i].node.Left
} else {
break

Check warning on line 489 in core/trie/proof.go

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L489

Added line #L489 was not covered by tests
}
}

for _, sNode := range leftProofPath {
if sNode.node.Left == nil || sNode.node.Right == nil {
break

Check warning on line 495 in core/trie/proof.go

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L495

Added line #L495 was not covered by tests
}
_, err := tempTrie.PutInner(sNode.key, sNode.node)
if err != nil {
return nil, err

Check warning on line 499 in core/trie/proof.go

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L499

Added line #L499 was not covered by tests
}
}

for _, sNode := range rightProofPath {
if sNode.node.Left == nil || sNode.node.Right == nil {
break

Check warning on line 505 in core/trie/proof.go

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L505

Added line #L505 was not covered by tests
}
_, err := tempTrie.PutInner(sNode.key, sNode.node)
if err != nil {
return nil, err

Check warning on line 509 in core/trie/proof.go

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L509

Added line #L509 was not covered by tests
}
}

for i := range len(keys) {
_, err := tempTrie.PutWithProof(keys[i], values[i], leftProofPath, rightProofPath)
if err != nil {
return nil, err

Check warning on line 516 in core/trie/proof.go

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L516

Added line #L516 was not covered by tests
}
}
return tempTrie, nil
Expand Down
1 change: 0 additions & 1 deletion core/trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@
return sn.node
}

func NewStorageNode(key *Key, node *Node) *StorageNode {
return &StorageNode{key: key, node: node}

Check warning on line 147 in core/trie/trie.go

View check run for this annotation

Codecov / codecov/patch

core/trie/trie.go#L146-L147

Added lines #L146 - L147 were not covered by tests
}

// nodesFromRoot enumerates the set of [Node] objects that need to be traversed from the root
Expand Down Expand Up @@ -266,7 +266,7 @@
if siblingIsParentProof {
newParent, err = t.GetNodeFromKey(&commonKey)
if err != nil {
return nil

Check warning on line 269 in core/trie/trie.go

View check run for this annotation

Codecov / codecov/patch

core/trie/trie.go#L269

Added line #L269 was not covered by tests
}
if nodeKey.Test(nodeKey.Len() - commonKey.Len() - 1) {
newParent.Right = nodeKey
Expand All @@ -276,7 +276,7 @@
newParent.LeftHash = node.Hash(nodeKey, t.hash)
}
if err := t.storage.Put(&commonKey, newParent); err != nil {
return err

Check warning on line 279 in core/trie/trie.go

View check run for this annotation

Codecov / codecov/patch

core/trie/trie.go#L279

Added line #L279 was not covered by tests
}
t.dirtyNodes = append(t.dirtyNodes, &commonKey)
} else {
Expand All @@ -293,7 +293,7 @@

newParent.Value = t.hash(leftChild.Hash(&leftPath, t.hash), rightChild.Hash(&rightPath, t.hash))
if err := t.storage.Put(&commonKey, newParent); err != nil {
return err

Check warning on line 296 in core/trie/trie.go

View check run for this annotation

Codecov / codecov/patch

core/trie/trie.go#L296

Added line #L296 was not covered by tests
}

if len(nodes) > 1 { // sibling has a parent
Expand All @@ -301,7 +301,7 @@

t.replaceLinkWithNewParent(sibling.key, commonKey, siblingParent)
if err := t.storage.Put(siblingParent.key, siblingParent.node); err != nil {
return err

Check warning on line 304 in core/trie/trie.go

View check run for this annotation

Codecov / codecov/patch

core/trie/trie.go#L304

Added line #L304 was not covered by tests
}
t.dirtyNodes = append(t.dirtyNodes, &commonKey)
} else {
Expand Down Expand Up @@ -360,7 +360,7 @@
}
err := t.insertOrUpdateValue(&nodeKey, node, nodes, sibling, false)
if err != nil {
return nil, err

Check warning on line 363 in core/trie/trie.go

View check run for this annotation

Codecov / codecov/patch

core/trie/trie.go#L363

Added line #L363 was not covered by tests
}
return &old, nil
}
Expand All @@ -369,7 +369,7 @@
// Put updates the corresponding `value` for a `key`
func (t *Trie) PutWithProof(key, value *felt.Felt, lProofPath, rProofPath []StorageNode) (*felt.Felt, error) {
if key.Cmp(t.maxKey) > 0 {
return nil, fmt.Errorf("key %s exceeds trie height %d", key, t.height)

Check warning on line 372 in core/trie/trie.go

View check run for this annotation

Codecov / codecov/patch

core/trie/trie.go#L372

Added line #L372 was not covered by tests
}

old := felt.Zero
Expand All @@ -381,12 +381,12 @@
oldValue, err := t.updateLeaf(nodeKey, node, value)
// xor operation, because we don't want to return result if the error is nil and the old value is nil
if (err != nil) != (oldValue != nil) {
return oldValue, err

Check warning on line 384 in core/trie/trie.go

View check run for this annotation

Codecov / codecov/patch

core/trie/trie.go#L384

Added line #L384 was not covered by tests
}

nodes, err := t.nodesFromRoot(&nodeKey) // correct for key,value
if err != nil {
return nil, err

Check warning on line 389 in core/trie/trie.go

View check run for this annotation

Codecov / codecov/patch

core/trie/trie.go#L389

Added line #L389 was not covered by tests
}
defer func() {
for _, n := range nodes {
Expand All @@ -404,10 +404,10 @@
oldValue, err = t.deleteExistingKey(sibling, nodeKey, nodes)
// xor operation, because we don't want to return if the error is nil and the old value is nil
if (err != nil) != (oldValue != nil) {
return oldValue, err

Check warning on line 407 in core/trie/trie.go

View check run for this annotation

Codecov / codecov/patch

core/trie/trie.go#L407

Added line #L407 was not covered by tests
} else if value.IsZero() {
// trying to insert 0 to a key that does not exist
return nil, nil // no-op

Check warning on line 410 in core/trie/trie.go

View check run for this annotation

Codecov / codecov/patch

core/trie/trie.go#L410

Added line #L410 was not covered by tests
}

// override the sibling to be the parent if it's a proof
Expand Down Expand Up @@ -441,7 +441,7 @@
// Put updates the corresponding `value` for a `key`
func (t *Trie) PutInner(key *Key, node *Node) (*felt.Felt, error) {
if err := t.storage.Put(key, node); err != nil {
return nil, err

Check warning on line 444 in core/trie/trie.go

View check run for this annotation

Codecov / codecov/patch

core/trie/trie.go#L444

Added line #L444 was not covered by tests
}
if t.rootKey == nil {
t.setRootKey(key)
Expand All @@ -454,7 +454,6 @@
t.rootKeyIsDirty = true
}

// Todo: update so that the proof nodes are always updated
func (t *Trie) updateValueIfDirty(key *Key) (*Node, error) { //nolint:gocyclo
zeroFeltBytes := new(felt.Felt).Bytes()
nilKey := NewKey(0, zeroFeltBytes[:])
Expand All @@ -481,7 +480,7 @@

// Update inner proof nodes
if node.Left.Equal(&nilKey) && node.Right.Equal(&nilKey) { // leaf
shouldUpdate = false

Check warning on line 483 in core/trie/trie.go

View check run for this annotation

Codecov / codecov/patch

core/trie/trie.go#L483

Added line #L483 was not covered by tests
} else if node.Left.Equal(&nilKey) || node.Right.Equal(&nilKey) { // inner
shouldUpdate = true
}
Expand Down Expand Up @@ -536,13 +535,13 @@
if !leftIsProof {
leftChild, err = t.updateValueIfDirty(root.Left)
if err != nil {
return nil, nil, err

Check warning on line 538 in core/trie/trie.go

View check run for this annotation

Codecov / codecov/patch

core/trie/trie.go#L538

Added line #L538 was not covered by tests
}
}
if !rightIsProof {
rightChild, err = t.updateValueIfDirty(root.Right)
if err != nil {
return nil, nil, err

Check warning on line 544 in core/trie/trie.go

View check run for this annotation

Codecov / codecov/patch

core/trie/trie.go#L544

Added line #L544 was not covered by tests
}
}
return leftChild, rightChild, nil
Expand Down
32 changes: 0 additions & 32 deletions core/trie/trie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
Expand Down
Loading