Skip to content

Commit

Permalink
tidy few things
Browse files Browse the repository at this point in the history
  • Loading branch information
rian committed May 9, 2024
1 parent a862473 commit 338df4c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 21 deletions.
27 changes: 8 additions & 19 deletions core/trie/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,11 @@ func isEdge(parentKey *Key, sNode storageNode) bool {
if parentKey == nil { // Root
return sNodeLen != 0
}
if sNodeLen-parentKey.len > 1 {
return true
}
return false
return sNodeLen-parentKey.len > 1
}

// Note: we need to account for the fact that Junos Trie has nodes that are Binary AND Edge,
// whereas the protocol requires nodes that are BINARY XOR Edge
// whereas the protocol requires nodes that are Binary XOR Edge
func transformNode(tri *Trie, parentKey *Key, sNode storageNode) (*Edge, *Binary, error) {
isEdgeBool := isEdge(parentKey, sNode)

Expand All @@ -77,11 +74,8 @@ func transformNode(tri *Trie, parentKey *Key, sNode storageNode) (*Edge, *Binary
Path: &edgePath,
Child: sNode.node.Value,
}
if sNode.key.len == tri.height {
return edge, nil, nil
}
}
if sNode.key.len == tri.height {
if sNode.key.len == tri.height { // Leaf
return edge, nil, nil
}
lNode, err := tri.GetNodeFromKey(sNode.node.Left)
Expand Down Expand Up @@ -131,27 +125,24 @@ func GetProof(leaf *felt.Felt, tri *Trie) ([]ProofNode, error) {
var parentKey *Key

for i := 0; i < len(nodesToLeaf); i++ {
sNode := nodesToLeaf[i]
isLeaf := sNode.key.len == tri.height
if i != 0 {
parentKey = nodesToLeaf[i-1].key
}
sNode := nodesToLeaf[i]
sNodeEdge, sNodeBinary, err := transformNode(tri, parentKey, sNode)
if err != nil {
return nil, err

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L135

Added line #L135 was not covered by tests
}
if sNodeEdge == nil && sNodeBinary == nil {
break
}

isLeaf := sNode.key.len == tri.height
if sNodeEdge != nil && !isLeaf { // Internal Edge
proofNodes = append(proofNodes, []ProofNode{{Edge: sNodeEdge}, {Binary: sNodeBinary}}...)
} else if sNodeEdge == nil && !isLeaf { // Internal Binary
proofNodes = append(proofNodes, []ProofNode{{Binary: sNodeBinary}}...)
} else if sNodeEdge != nil && isLeaf { // pre-leaf Edge
} else if sNodeEdge != nil && isLeaf { // Leaf Edge
proofNodes = append(proofNodes, []ProofNode{{Edge: sNodeEdge}}...)
} else if sNodeEdge == nil && isLeaf { // pre-leaf binary
proofNodes = append(proofNodes, []ProofNode{{Binary: sNodeBinary}}...)
} else if sNodeEdge == nil && sNodeBinary == nil { // sNode is a binary leaf
break
}
}
return proofNodes, nil
Expand Down Expand Up @@ -180,8 +171,6 @@ func VerifyProof(root *felt.Felt, key *Key, value *felt.Felt, proofs []ProofNode
}
remainingPath.RemoveLastBit()
case proofNode.Edge != nil:
// The next "proofNode.Edge.len" bits must match
// Todo: Isn't edge.path from root? and remaining from edge to leaf??
if !proofNode.Edge.Path.Equal(remainingPath.SubKey(proofNode.Edge.Path.Len())) {
return false

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

View check run for this annotation

Codecov / codecov/patch

core/trie/proof.go#L175

Added line #L175 was not covered by tests
}
Expand Down
4 changes: 2 additions & 2 deletions core/trie/proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func TestVerifyProofs(t *testing.T) {
expectedProofNodes := []trie.ProofNode{
{
Edge: &trie.Edge{
Path: &zero, // Todo: pathfinder returns 0? But shouldn't be zero?...
Path: &zero,
Child: utils.HexToFelt(t, "0x05774FA77B3D843AE9167ABD61CF80365A9B2B02218FC2F628494B5BDC9B33B8"),
},
},
Expand All @@ -314,7 +314,7 @@ func TestVerifyProofs(t *testing.T) {
expectedProofNodes := []trie.ProofNode{
{
Edge: &trie.Edge{
Path: &zero, // Todo: 0x7469c4000fe0 ???
Path: &zero,
Child: utils.HexToFelt(t, "0x055C81F6A791FD06FC2E2CCAD922397EC76C3E35F2E06C0C0D43D551005A8DEA"),
},
},
Expand Down

0 comments on commit 338df4c

Please sign in to comment.