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

fix: traverseNodes unexpected behaviour #718

Merged
merged 12 commits into from
Apr 14, 2023
47 changes: 26 additions & 21 deletions nodedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,33 +760,38 @@ func (ndb *nodeDB) size() int {
return size
}

func (ndb *nodeDB) traverseNodes(fn func(node *Node) error) error {
ndb.resetLatestVersion(0)
latest, err := ndb.getLatestVersion()
if err != nil {
return err
func isReferenceToRoot(bz []byte) bool {
if bz[0] == nodeKeyFormat.Prefix()[0] {
if len(bz) == 13 {
return true
}
}
return false
}

func (ndb *nodeDB) traverseNodes(fn func(node *Node) error) error {
nodes := []*Node{}
for version := int64(1); version <= latest; version++ {
if err := ndb.traverseRange(nodeKeyFormat.Key(version), nodeKeyFormat.Key(version+1), func(key, value []byte) error {
var (
version int64
nonce int32
)
nodeKeyFormat.Scan(key, &version, &nonce)
node, err := MakeNode(&NodeKey{
version: version,
nonce: nonce,
}, value)
if err != nil {
return err
}
nodes = append(nodes, node)

if err := ndb.traversePrefix(nodeKeyFormat.Key(), func(key, value []byte) error {
if isReferenceToRoot(value) {
return nil
}); err != nil {
}
var (
version int64
nonce int32
)
nodeKeyFormat.Scan(key, &version, &nonce)
node, err := MakeNode(&NodeKey{
version: version,
nonce: nonce,
}, value)
if err != nil {
return err
}
nodes = append(nodes, node)
return nil
}); err != nil {
return err
}

sort.Slice(nodes, func(i, j int) bool {
Expand Down
37 changes: 37 additions & 0 deletions nodedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package iavl

import (
"errors"
"fmt"
"strconv"
"testing"

Expand Down Expand Up @@ -253,6 +254,42 @@ func TestIsFastStorageEnabled_False(t *testing.T) {
require.NoError(t, err)
}

func TestTraverseNodes(t *testing.T) {
tree, _ := getTestTree(0)
// version 1
for i := 0; i < 20; i++ {
_, err := tree.Set([]byte{byte(i)}, []byte{byte(i)})
require.NoError(t, err)
}
_, _, err := tree.SaveVersion()
require.NoError(t, err)
// version 2, no commit
_, _, err = tree.SaveVersion()
require.NoError(t, err)
// version 3
for i := 20; i < 30; i++ {
_, err := tree.Set([]byte{byte(i)}, []byte{byte(i)})
require.NoError(t, err)
}
_, _, err = tree.SaveVersion()
require.NoError(t, err)

count := 0
err = tree.ndb.traverseNodes(func(node *Node) error {
actualNode, err := tree.ndb.GetNode(node.nodeKey)
if err != nil {
return err
}
if actualNode.String() != node.String() {
return fmt.Errorf("found unexpected node")
}
count++
return nil
})
require.NoError(t, err)
require.Equal(t, 64, count)
}

func assertOrphansAndBranches(t *testing.T, ndb *nodeDB, version int64, branches int, orphanKeys [][]byte) {
var branchCount, orphanIndex int
err := ndb.traverseOrphans(version, func(node *Node) error {
Expand Down