Skip to content

Commit

Permalink
Merge pull request #120 from tendermint/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ebuchman authored Nov 29, 2018
2 parents 3acc91f + 69d0867 commit de07409
Show file tree
Hide file tree
Showing 14 changed files with 382 additions and 69 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Changelog

## 0.12.0 (November 26, 2018)

BREAKING CHANGES

- Uses new Tendermint ReverseIterator API. See https://github.com/tendermint/tendermint/pull/2913

## 0.11.1 (October 29, 2018)

IMPROVEMENTS

- Uses GoAmino v0.14

## 0.11.0 (September 7, 2018)

BREAKING CHANGES
Expand Down Expand Up @@ -35,6 +47,12 @@ IMPROVEMENTS

- Change tendermint dep to ^v0.22.0 (#91)

## 0.10.0 (July 11, 2018)

BREAKING CHANGES

- getRangeProof and Get\[Versioned\]\[Range\]WithProof return nil proof/error if tree is empty.

## 0.9.2 (July 3, 2018)

IMPROVEMENTS
Expand Down
8 changes: 8 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Contributing

Thank you for considering making contributions to IAVL+!
This repository follows the [contribution guidelines] of tendermint and the corresponding [coding repo].
Please take a look if you are not already familiar with those.

[contribution guidelines]: https://github.com/tendermint/tendermint/blob/master/CONTRIBUTING.md
[coding repo]: https://github.com/tendermint/coding
46 changes: 24 additions & 22 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
version = "1.1.4"

[[constraint]]
version = "=0.10.1"
name = "github.com/tendermint/go-amino"
version = "v0.14.0"

[[constraint]]
version = "^0.22.0"
name = "github.com/tendermint/tendermint"
version = "v0.27.0-dev1"

[prune]
go-tests = true
Expand Down
16 changes: 13 additions & 3 deletions basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,14 @@ func TestProof(t *testing.T) {
func TestTreeProof(t *testing.T) {
db := db.NewMemDB()
tree := NewMutableTree(db, 100)
assert.Equal(t, tree.Hash(), []byte(nil))

// should get false for proof with nil root
_, _, err := tree.GetWithProof([]byte("foo"))
assert.Error(t, err)
value, proof, err := tree.GetWithProof([]byte("foo"))
assert.Nil(t, value)
assert.Nil(t, proof)
assert.Error(t, proof.Verify([]byte(nil)))
assert.NoError(t, err)

// insert lots of info and store the bytes
keys := make([][]byte, 200)
Expand All @@ -436,9 +440,15 @@ func TestTreeProof(t *testing.T) {
keys[i] = []byte(key)
}

tree.SaveVersion()

// query random key fails
_, _, err = tree.GetWithProof([]byte("foo"))
value, proof, err = tree.GetWithProof([]byte("foo"))
assert.Nil(t, value)
assert.NotNil(t, proof)
assert.NoError(t, err)
assert.NoError(t, proof.Verify(tree.Hash()))
assert.NoError(t, proof.VerifyAbsence([]byte("foo")))

// valid proof for real keys
root := tree.WorkingHash()
Expand Down
36 changes: 35 additions & 1 deletion mutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,17 @@ func (tree *MutableTree) LoadVersion(targetVersion int64) (int64, error) {
return latestVersion, nil
}

// LoadVersionOverwrite returns the version number of targetVersion.
// Higher versions' data will be deleted.
func (tree *MutableTree) LoadVersionForOverwriting(targetVersion int64) (int64, error) {
latestVersion, err := tree.LoadVersion(targetVersion)
if err != nil {
return latestVersion, err
}
tree.deleteVersionsFrom(targetVersion+1)
return targetVersion, nil
}

// GetImmutable loads an ImmutableTree at a given version for querying
func (tree *MutableTree) GetImmutable(version int64) (*ImmutableTree, error) {
rootHash := tree.ndb.getRoot(version)
Expand Down Expand Up @@ -365,14 +376,37 @@ func (tree *MutableTree) DeleteVersion(version int64) error {
return cmn.ErrorWrap(ErrVersionDoesNotExist, "")
}

tree.ndb.DeleteVersion(version)
tree.ndb.DeleteVersion(version, true)
tree.ndb.Commit()

delete(tree.versions, version)

return nil
}

// deleteVersionsFrom deletes tree version from disk specified version to latest version. The version can then no
// longer be accessed.
func (tree *MutableTree) deleteVersionsFrom(version int64) error {
if version <= 0 {
return cmn.NewError("version must be greater than 0")
}
newLatestVersion := version - 1
lastestVersion := tree.ndb.getLatestVersion()
for ; version <= lastestVersion; version++ {
if version == tree.version {
return cmn.NewError("cannot delete latest saved version (%d)", version)
}
if _, ok := tree.versions[version]; !ok {
return cmn.ErrorWrap(ErrVersionDoesNotExist, "")
}
tree.ndb.DeleteVersion(version, false)
delete(tree.versions, version)
}
tree.ndb.Commit()
tree.ndb.resetLatestVersion(newLatestVersion)
return nil
}

// Rotate right and return the new node and orphan.
func (tree *MutableTree) rotateRight(node *Node) (*Node, *Node) {
version := tree.version + 1
Expand Down
16 changes: 10 additions & 6 deletions nodedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,12 @@ func (ndb *nodeDB) SaveBranch(node *Node) []byte {
}

// DeleteVersion deletes a tree version from disk.
func (ndb *nodeDB) DeleteVersion(version int64) {
func (ndb *nodeDB) DeleteVersion(version int64, checkLatestVersion bool) {
ndb.mtx.Lock()
defer ndb.mtx.Unlock()

ndb.deleteOrphans(version)
ndb.deleteRoot(version)
ndb.deleteRoot(version, checkLatestVersion)
}

// Saves orphaned nodes to disk under a special prefix.
Expand Down Expand Up @@ -244,10 +244,14 @@ func (ndb *nodeDB) updateLatestVersion(version int64) {
}
}

func (ndb *nodeDB) resetLatestVersion(version int64) {
ndb.latestVersion = version
}

func (ndb *nodeDB) getPreviousVersion(version int64) int64 {
itr := ndb.db.ReverseIterator(
rootKeyFormat.Key(version-1),
rootKeyFormat.Key(0),
rootKeyFormat.Key(1),
rootKeyFormat.Key(version),
)
defer itr.Close()

Expand All @@ -262,8 +266,8 @@ func (ndb *nodeDB) getPreviousVersion(version int64) int64 {
}

// deleteRoot deletes the root entry from disk, but not the node it points to.
func (ndb *nodeDB) deleteRoot(version int64) {
if version == ndb.getLatestVersion() {
func (ndb *nodeDB) deleteRoot(version int64, checkLatestVersion bool) {
if checkLatestVersion && version == ndb.getLatestVersion() {
panic("Tried to delete latest version")
}

Expand Down
3 changes: 0 additions & 3 deletions proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ var (

// ErrInvalidRoot is returned when the root passed in does not match the proof's.
ErrInvalidRoot = fmt.Errorf("invalid root")

// ErrNilRoot is returned when the root of the tree is nil.
ErrNilRoot = fmt.Errorf("tree root is nil")
)

//----------------------------------------
Expand Down
Loading

0 comments on commit de07409

Please sign in to comment.