Skip to content

Commit

Permalink
tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
rian committed Jun 12, 2024
1 parent d249186 commit 21f9453
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 48 deletions.
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 @@ 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]
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 @@ 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)
}
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -252,7 +251,7 @@ func VerifyRangeProof(root *felt.Felt, keys, values []*felt.Felt, proofKeys [2]*
}

// 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
}
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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[:])

Expand Down
1 change: 0 additions & 1 deletion core/trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,6 @@ func (t *Trie) setRootKey(newRootKey *Key) {
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 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

0 comments on commit 21f9453

Please sign in to comment.