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

bugfix: orphans are not totally collected when Remove #177

Merged
merged 1 commit into from
Sep 28, 2019

Conversation

rickyyangz
Copy link
Contributor

Details:

Summary:

Remove operation will leave some orphans not collected. So these orphans would never be pruned from database. So many nodeDB operations would get the wrong result, like traverse, traversePrefix, traverseNodes, size(), etc.

Steps To Reproduce:

  1. Run the test code
func TestMutableTree_Remove(t *testing.T) {
	db := db.NewDB("test", db.MemDBBackend, "")
	tree := NewMutableTree(db, 0)
	tree.Set([]byte("k1"), []byte("v1"))
	tree.Set([]byte("k2"), []byte("v1"))
	tree.Set([]byte("k3"), []byte("v1"))
	tree.Set([]byte("k4"), []byte("v1"))
	tree.Set([]byte("k5"), []byte("v1"))
	tree.SaveVersion()
	tree.Remove([]byte("k5"))
	require.Equal(t, 4, len(tree.orphans))
	tree.SaveVersion()
}

The test will failed because the Remove only collect 3 orphans instead of the expected 4.
The tree looks like this

           3                                                3
         /   \                                            /   \    
        2      4                                         2     4
      /  \    /  \                =>                   /  \   /  \
     1    2  3    5                                  1   2  3    4
                 /  \
                4    5

The orphans should be [5, 5, 4, 3], but we got [5,4,3] instead. The inner node 5 is lost.

Copy link
Member

@AdityaSripal AdityaSripal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this fix!

@AdityaSripal AdityaSripal merged commit 3a4c322 into cosmos:master Sep 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants