Skip to content

Commit

Permalink
test cleanup 1
Browse files Browse the repository at this point in the history
  • Loading branch information
rian committed Jun 3, 2024
1 parent c9aa4b1 commit 4612a4b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 71 deletions.
Binary file added core/trie/__debug_bin2978894929
Binary file not shown.
7 changes: 3 additions & 4 deletions core/trie/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import (

// A Node represents a node in the [Trie]
type Node struct {
Value *felt.Felt
Left *Key
Right *Key
IsProof bool
Value *felt.Felt
Left *Key
Right *Key
}

// Hash calculates the hash of a [Node]
Expand Down
82 changes: 26 additions & 56 deletions core/trie/proof_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package trie_test

import (
"fmt"
"testing"

"github.com/NethermindEth/juno/core/crypto"
Expand Down Expand Up @@ -179,9 +178,9 @@ func TestGetProofs(t *testing.T) {
require.NoError(t, err)

// Better inspection
for _, pNode := range proofNodes {
pNode.PrettyPrint()
}
// for _, pNode := range proofNodes {
// pNode.PrettyPrint()
// }
require.Equal(t, expectedProofNodes, proofNodes)
})

Expand Down Expand Up @@ -214,9 +213,9 @@ func TestGetProofs(t *testing.T) {
require.NoError(t, err)

// Better inspection
for _, pNode := range proofNodes {
pNode.PrettyPrint()
}
// for _, pNode := range proofNodes {
// pNode.PrettyPrint()
// }
require.Equal(t, expectedProofNodes, proofNodes)
})

Expand Down Expand Up @@ -252,9 +251,9 @@ func TestGetProofs(t *testing.T) {
require.NoError(t, err)

// Better inspection
for _, pNode := range proofNodes {
pNode.PrettyPrint()
}
// for _, pNode := range proofNodes {
// pNode.PrettyPrint()
// }
require.Equal(t, expectedProofNodes, proofNodes)
})

Expand Down Expand Up @@ -282,9 +281,9 @@ func TestGetProofs(t *testing.T) {
require.NoError(t, err)

// Better inspection
for _, pNode := range proofNodes {
pNode.PrettyPrint()
}
// for _, pNode := range proofNodes {
// pNode.PrettyPrint()
// }
require.Equal(t, expectedProofNodes, proofNodes)
})

Expand Down Expand Up @@ -325,10 +324,9 @@ func TestGetProofs(t *testing.T) {
require.NoError(t, err)

// Better inspection
for i, pNode := range proofNodes {
fmt.Println(i)
pNode.PrettyPrint()
}
// for _, pNode := range proofNodes {
// pNode.PrettyPrint()
// }
require.Equal(t, expectedProofNodes, proofNodes)
})
}
Expand Down Expand Up @@ -390,30 +388,6 @@ func TestVerifyProofs(t *testing.T) {
assert.True(t, trie.VerifyProof(root, new(felt.Felt).SetUint64(0), val1, expectedProofNodes, crypto.Pedersen))
})

Check failure on line 390 in core/trie/proof_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
t.Run("Simple binary trie - fail missing key", func(t *testing.T) {
tempTrie := buildSimpleTrie(t)
zero := trie.NewKey(250, []byte{0})
expectedProofNodes := []trie.ProofNode{
{
Edge: &trie.Edge{
Path: &zero,
Child: utils.HexToFelt(t, "0x05774FA77B3D843AE9167ABD61CF80365A9B2B02218FC2F628494B5BDC9B33B8"),
},
},
{
Binary: &trie.Binary{
LeftHash: utils.HexToFelt(t, "0x0000000000000000000000000000000000000000000000000000000000000002"),
RightHash: utils.HexToFelt(t, "0x0000000000000000000000000000000000000000000000000000000000000003"),
},
},
}

root, err := tempTrie.Root()
require.NoError(t, err)
key1 := utils.HexToFelt(t, "0x4") // Non-existent key
val1 := new(felt.Felt).SetUint64(2) // key=0 has val=2
assert.False(t, trie.VerifyProof(root, key1, val1, expectedProofNodes, crypto.Pedersen))
})
}

Check failure on line 391 in core/trie/proof_test.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary trailing newline (whitespace)

func TestProofToPath(t *testing.T) {
Expand Down Expand Up @@ -478,28 +452,25 @@ func TestProofToPath(t *testing.T) {

rootNodes, err := tempTrie.GetNodeFromKey(rootKey)
require.NoError(t, err)
fmt.Println(rootNodes.Value.String())
require.Equal(t, 2, len(sns))
require.Equal(t, rootKey.Len(), sns[0].Key().Len())
require.Equal(t, rootNodes.Left, sns[0].Node().Left)
require.NotEqual(t, rootNodes.Right, sns[0].Node().Right)
})

t.Run("boundary proofs wth three key trie", func(t *testing.T) {
// Todo: check leaf
tri := build3KeyTrie(t)
rootKey := tri.RootKey()
rootNode, err := tri.GetNodeFromKey(rootKey)
require.NoError(t, err)
fmt.Println(rootNode)

key1 := new(felt.Felt).SetUint64(0)
key3 := new(felt.Felt).SetUint64(2)
bProofs, err := trie.GetBoundaryProofs(key1, key3, tri)
require.NoError(t, err)

trie.PrettyPrintProofPath(bProofs[0], key1)
trie.PrettyPrintProofPath(bProofs[1], key3)
// trie.PrettyPrintProofPath(bProofs[0], key1)

Check failure on line 472 in core/trie/proof_test.go

View workflow job for this annotation

GitHub Actions / lint

commentedOutCode: may want to remove commented-out code (gocritic)
// trie.PrettyPrintProofPath(bProofs[1], key3)

leftProofPath, err := trie.ProofToPath(bProofs[0], key1, crypto.Pedersen)
require.Equal(t, 3, len(leftProofPath))
Expand All @@ -526,8 +497,8 @@ func TestProofToPath(t *testing.T) {
func TestBuildTrie(t *testing.T) {
t.Run("Simple binary trie proof to path", func(t *testing.T) {
compareLeftRight := func(t *testing.T, want, got *trie.Node) {
require.Equal(t, want.Left, got.Left)
require.Equal(t, want.Right, got.Right)
require.Equal(t, want.Left, got.Left, "left fail")
require.Equal(t, want.Right, got.Right, "right fail")
}

// Node (edge path 249)
Expand Down Expand Up @@ -556,11 +527,10 @@ func TestBuildTrie(t *testing.T) {
bProofs, err := trie.GetBoundaryProofs(key1, key3, tri)
require.NoError(t, err)

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

rightProof, err := trie.ProofToPath(bProofs[1], key3, crypto.Pedersen) // correct
rightProof, err := trie.ProofToPath(bProofs[1], key3, crypto.Pedersen)
require.NoError(t, err)

keys := []*felt.Felt{new(felt.Felt).SetUint64(1)}
Expand All @@ -569,15 +539,15 @@ func TestBuildTrie(t *testing.T) {
require.NoError(t, err)

builtRootKey := builtTrie.RootKey()
builtRootNode, err := builtTrie.GetNodeFromKey(builtRootKey) // looks correct
builtRootNode, err := builtTrie.GetNodeFromKey(builtRootKey)
require.NoError(t, err)
builtLeftNode, err := builtTrie.GetNodeFromKey(builtRootNode.Left) // looks correct
builtLeftNode, err := builtTrie.GetNodeFromKey(builtRootNode.Left)
require.NoError(t, err)
builtRightNode, err := builtTrie.GetNodeFromKey(builtRootNode.Right) // looks correct
builtRightNode, err := builtTrie.GetNodeFromKey(builtRootNode.Right)
require.NoError(t, err)
builtLeftLeftNode, err := builtTrie.GetNodeFromKey(builtLeftNode.Left) // looks correct
builtLeftLeftNode, err := builtTrie.GetNodeFromKey(builtLeftNode.Left)
require.NoError(t, err)
builtLeftRightNode, err := builtTrie.GetNodeFromKey(builtLeftNode.Right) // looks correct
builtLeftRightNode, err := builtTrie.GetNodeFromKey(builtLeftNode.Right)
require.NoError(t, err)

// Assert the structure is correct
Expand Down
18 changes: 7 additions & 11 deletions core/trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func (t *Trie) insertOrUpdateValue(nodeKey *Key, node *Node, nodes []StorageNode
return nil
}

func (t *Trie) insertOrUpdateValueProof(nodeKey *Key, node *Node, nodes []StorageNode, sibling StorageNode) error {
func (t *Trie) insertOrUpdateValueProof(nodeKey *Key, node *Node, nodes []StorageNode, sibling StorageNode, IsProof bool) error {

Check failure on line 298 in core/trie/trie.go

View workflow job for this annotation

GitHub Actions / lint

captLocal: `IsProof' should not be capitalized (gocritic)
commonKey, _ := findCommonKey(nodeKey, sibling.key)

newParent := &Node{}
Expand All @@ -318,15 +318,15 @@ func (t *Trie) insertOrUpdateValueProof(nodeKey *Key, node *Node, nodes []Storag
}

// Don't modify the structure outlined by the proof paths
if len(nodes) > 1 && !sibling.node.IsProof { // sibling has a parent
if len(nodes) > 1 && IsProof { // sibling has a parent
siblingParent := (nodes)[len(nodes)-2]

t.replaceLinkWithNewParent(sibling.key, commonKey, siblingParent) // error with overwritting right arises here
if err := t.storage.Put(siblingParent.key, siblingParent.node); err != nil {
return err
}
t.dirtyNodes = append(t.dirtyNodes, &commonKey)
} else if !sibling.node.IsProof {
} else if IsProof {
t.setRootKey(&commonKey)
} else {
t.dirtyNodes = append(t.dirtyNodes, &commonKey)
Expand Down Expand Up @@ -433,11 +433,11 @@ func (t *Trie) PutWithProof(key, value *felt.Felt, lProofPath, rProofPath []Stor
return nil, nil // no-op
}

found := false
IsProof, found := false, false
for i, proof := range lProofPath {
if proof.key.Equal(sibling.key) {
sibling = lProofPath[i+1]
sibling.node.IsProof = true
IsProof = true
found = true
break
}
Expand All @@ -446,13 +446,13 @@ func (t *Trie) PutWithProof(key, value *felt.Felt, lProofPath, rProofPath []Stor
for i, proof := range rProofPath {
if proof.key.Equal(sibling.key) {
sibling = rProofPath[i+1]
sibling.node.IsProof = true
IsProof = true
break
}
}
}

err := t.insertOrUpdateValueProof(&nodeKey, node, nodes, sibling)
err := t.insertOrUpdateValueProof(&nodeKey, node, nodes, sibling, IsProof)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -519,11 +519,7 @@ func (t *Trie) updateValueIfDirty(key *Key) (*Node, error) {
leftPath := path(node.Left, key)
rightPath := path(node.Right, key)

fmt.Println("--", node.Value.String())
fmt.Println("--", leftChild.Value.String())
fmt.Println("--", rightChild.Value.String())
node.Value = t.hash(leftChild.Hash(&leftPath, t.hash), rightChild.Hash(&rightPath, t.hash))
fmt.Println("--", node.Value)
if err = t.storage.Put(key, node); err != nil {
return nil, err
}
Expand Down

0 comments on commit 4612a4b

Please sign in to comment.