Skip to content

Commit

Permalink
Add test to check for consistent tracking of orphans
Browse files Browse the repository at this point in the history
  • Loading branch information
UnitylChaos committed Aug 3, 2018
1 parent f1657ab commit 8ad44f7
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package iavl
import (
"bytes"
"flag"
"fmt"
"os"
"runtime"
"testing"

"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/db"

mathrand "math/rand"

cmn "github.com/tendermint/tendermint/libs/common"
)

Expand Down Expand Up @@ -1022,6 +1025,38 @@ func TestVersionedTreeProofs(t *testing.T) {
require.Error(proof.Verify(root2))
}

func TestOrphans(t *testing.T) {
//If you create a sequence of saved versions
//Then randomly delete versions other than the first and last until only those two remain
//Any remaining orphan nodes should be constrained to just the first version
require := require.New(t)
tree := NewMutableTree(db.NewMemDB(), 100)

NUMVERSIONS := 100
NUMUPDATES := 100

for i := 0; i < NUMVERSIONS; i++ {
for j := 1; j < NUMUPDATES; j++ {
tree.Set(randBytes(2), randBytes(2))
}
_, _, err := tree.SaveVersion()
require.NoError(err, "SaveVersion should not error")
}

idx := mathrand.Perm(NUMVERSIONS - 2)
for i := range idx {
err := tree.DeleteVersion(int64(i + 2))
require.NoError(err, "DeleteVersion should not error")
}

tree.ndb.traverseOrphans(func(k, v []byte) {
var fromVersion, toVersion int64
fmt.Sscanf(string(k), orphanKeyFmt, &toVersion, &fromVersion)
require.Equal(fromVersion, int64(1), "fromVersion should be 1")
require.Equal(toVersion, int64(1), "toVersion should be 1")
})
}

func TestVersionedTreeHash(t *testing.T) {
require := require.New(t)
tree := NewMutableTree(db.NewMemDB(), 0)
Expand Down

0 comments on commit 8ad44f7

Please sign in to comment.