Move to iavl.VersionedTree

This commit is contained in:
Alexis Sellier 2017-10-04 17:05:38 +02:00
parent 0fdccfc60d
commit 36f8c59fee
6 changed files with 25 additions and 17 deletions

View File

@ -46,8 +46,8 @@ func MockStore() *Store {
return res
}
// NewStore initializes an in-memory IAVLTree, or attempts to load a persistant
// tree from disk
// NewStore initializes an in-memory iavl.VersionedTree, or attempts to load a
// persistant tree from disk
func NewStore(dbName string, cacheSize int, logger log.Logger) (*Store, error) {
// start at 1 so the height returned by query is for the
// next block, ie. the one that includes the AppHash for our current state
@ -55,9 +55,9 @@ func NewStore(dbName string, cacheSize int, logger log.Logger) (*Store, error) {
// Non-persistent case
if dbName == "" {
tree := iavl.NewIAVLTree(
tree := iavl.NewVersionedTree(
0,
nil,
dbm.NewMemDB(),
)
store := &Store{
State: state.NewState(tree, false),
@ -85,24 +85,26 @@ func NewStore(dbName string, cacheSize int, logger log.Logger) (*Store, error) {
// Open database called "dir/name.db", if it doesn't exist it will be created
db := dbm.NewDB(name, dbm.LevelDBBackendStr, dir)
tree := iavl.NewIAVLTree(cacheSize, db)
tree := iavl.NewVersionedTree(cacheSize, db)
var chainState ChainState
if empty {
logger.Info("no existing db, creating new db")
chainState = ChainState{
Hash: tree.Save(),
Hash: nil,
Height: initialHeight,
}
db.Set(stateKey, wire.BinaryBytes(chainState))
} else {
logger.Info("loading existing db")
eyesStateBytes := db.Get(stateKey)
err = wire.ReadBinaryBytes(eyesStateBytes, &chainState)
if err != nil {
if err = wire.ReadBinaryBytes(eyesStateBytes, &chainState); err != nil {
return nil, errors.Wrap(err, "Reading MerkleEyesState")
}
tree.Load(chainState.Hash)
if err = tree.Load(); err != nil {
return nil, errors.Wrap(err, "Loading tree")
}
}
res := &Store{

View File

@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/tendermint/iavl"
db "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/log"
sdk "github.com/cosmos/cosmos-sdk"
@ -17,7 +18,7 @@ import (
func makeState() state.SimpleDB {
// return state.NewMemKVStore()
return state.NewBonsai(iavl.NewIAVLTree(0, nil))
return state.NewBonsai(iavl.NewVersionedTree(0, db.NewMemDB()))
// tree with persistence....
// tmpDir, err := ioutil.TempDir("", "state-tests")

View File

@ -12,7 +12,7 @@ type nonce int64
// Bonsai is a deformed tree forced to fit in a small pot
type Bonsai struct {
id nonce
Tree *iavl.IAVLTree
Tree *iavl.VersionedTree
}
func (b *Bonsai) String() string {
@ -22,7 +22,7 @@ func (b *Bonsai) String() string {
var _ SimpleDB = &Bonsai{}
// NewBonsai wraps a merkle tree and tags it to track children
func NewBonsai(tree *iavl.IAVLTree) *Bonsai {
func NewBonsai(tree *iavl.VersionedTree) *Bonsai {
return &Bonsai{
id: nonce(rand.Int63()),
Tree: tree,

View File

@ -11,7 +11,7 @@ type State struct {
persistent bool
}
func NewState(tree *iavl.IAVLTree, persistent bool) State {
func NewState(tree *iavl.VersionedTree, persistent bool) State {
base := NewBonsai(tree)
return State{
committed: base,
@ -70,7 +70,11 @@ func (s *State) Commit() ([]byte, error) {
var hash []byte
if s.persistent {
hash = s.committed.Tree.Save()
nextVersion := s.committed.Tree.LatestVersion() + 1
hash, err = s.committed.Tree.SaveVersion(nextVersion)
if err != nil {
return nil, err
}
} else {
hash = s.committed.Tree.Hash()
}

View File

@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/tendermint/iavl"
db "github.com/tendermint/tmlibs/db"
)
type keyVal struct {
@ -64,7 +65,7 @@ func TestStateCommitHash(t *testing.T) {
result := make([][]byte, len(tc.rounds))
// make the store...
tree := iavl.NewIAVLTree(0, nil)
tree := iavl.NewVersionedTree(0, db.NewMemDB())
store := NewState(tree, false)
for n, r := range tc.rounds {

View File

@ -17,11 +17,11 @@ func GetDBs() []SimpleDB {
panic(err)
}
db := dbm.NewDB("test-get-dbs", dbm.LevelDBBackendStr, tmpDir)
persist := iavl.NewIAVLTree(500, db)
persist := iavl.NewVersionedTree(500, db)
return []SimpleDB{
NewMemKVStore(),
NewBonsai(iavl.NewIAVLTree(0, nil)),
NewBonsai(iavl.NewVersionedTree(0, dbm.NewMemDB())),
NewBonsai(persist),
}
}