From 8ad44f7ec61881ac05d40ab28ddd2b6ea1c27adf Mon Sep 17 00:00:00 2001 From: Jeremiah Andrews Date: Thu, 2 Aug 2018 15:31:35 -0700 Subject: [PATCH] Add test to check for consistent tracking of orphans --- tree_test.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tree_test.go b/tree_test.go index 01f2d1e4e..13bde104d 100644 --- a/tree_test.go +++ b/tree_test.go @@ -3,6 +3,7 @@ package iavl import ( "bytes" "flag" + "fmt" "os" "runtime" "testing" @@ -10,6 +11,8 @@ import ( "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/libs/db" + mathrand "math/rand" + cmn "github.com/tendermint/tendermint/libs/common" ) @@ -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)