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

implement some heretofore unimplemented iterator methods #219

Merged
merged 1 commit into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 13 additions & 2 deletions trie/verkle_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,18 @@ func (it *verkleNodeIterator) Parent() common.Hash {
// Callers must not retain references to the return value after calling Next.
// For leaf nodes, the last element of the path is the 'terminator symbol' 0x10.
func (it *verkleNodeIterator) Path() []byte {
panic("not completely implemented")
if it.Leaf() {
return it.LeafKey()
}
var path []byte
for i, state := range it.stack {
// skip the last byte
if i <= len(it.stack)-1 {
break
}
path = append(path, byte(state.Index))
}
return path
}

func (it *verkleNodeIterator) NodeBlob() []byte {
Expand Down Expand Up @@ -204,5 +215,5 @@ func (it *verkleNodeIterator) LeafProof() [][]byte {
// making trie.Database an interface and wrapping at that level. It's a huge
// refactor, but it could be worth it if another occurrence arises.
func (it *verkleNodeIterator) AddResolver(ethdb.KeyValueReader) {
panic("not completely implemented")
// Not implemented, but should not panic
}
65 changes: 65 additions & 0 deletions trie/verkle_iterator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2023 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package trie

import (
"math/big"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/trie/utils"
"github.com/gballet/go-verkle"
)

func TestVerkleIterator(t *testing.T) {
trie := NewVerkleTrie(verkle.New(), NewDatabase(rawdb.NewMemoryDatabase()), utils.NewPointCache())
account0 := &types.StateAccount{
Nonce: 1,
Balance: big.NewInt(2),
Root: emptyRoot,
CodeHash: nil,
}
// NOTE: the code size isn't written to the trie via TryUpdateAccount
// so it will be missing from the test nodes.
trie.TryUpdateAccount(zero[:], account0)
account1 := &types.StateAccount{
Nonce: 1337,
Balance: big.NewInt(2000),
Root: emptyRoot,
CodeHash: nil,
}
// This address is meant to hash to a value that has the same first byte as 0xbf
var clash = common.Hex2Bytes("69fd8034cdb20934dedffa7dccb4fb3b8062a8be")
trie.TryUpdateAccount(clash, account1)

// Manually go over every node to check that we get all
// the correct nodes.
it := trie.NodeIterator(nil)
var leafcount int
for it.Next(true) {
t.Logf("Node: %x", it.Path())
if it.Leaf() {
leafcount++
t.Logf("\tLeaf: %x", it.LeafKey())
}
}
if leafcount != 6 {
t.Fatalf("invalid leaf count: %d != 6", leafcount)
}
}