Update iavl tree and address pr cleanup comments

This commit is contained in:
Ethan Frey 2017-10-20 19:41:01 +02:00
parent ba27d23ad1
commit 630c5ca9ff
4 changed files with 29 additions and 17 deletions

View File

@ -218,13 +218,13 @@ func TestInitState(t *testing.T) {
assert.Equal(unsortCoins, coins) assert.Equal(unsortCoins, coins)
err = app.InitState("base", "dslfkgjdas", "") err = app.InitState("base", "dslfkgjdas", "")
require.NotNil(err) require.Error(err)
err = app.InitState("", "dslfkgjdas", "") err = app.InitState("", "dslfkgjdas", "")
require.NotNil(err) require.Error(err)
err = app.InitState("dslfkgjdas", "szfdjzs", "") err = app.InitState("dslfkgjdas", "szfdjzs", "")
require.NotNil(err) require.Error(err)
} }
// Test CheckTx and DeliverTx with insufficient and sufficient balance // Test CheckTx and DeliverTx with insufficient and sufficient balance

View File

@ -258,18 +258,12 @@ func loadState(dbName string, cacheSize int, historySize uint64) (*sm.State, err
dir := path.Dir(dbPath) dir := path.Dir(dbPath)
name := path.Base(dbPath) name := path.Base(dbPath)
// Make sure the path exists
empty, _ := cmn.IsDirEmpty(dbPath + ".db")
// Open database called "dir/name.db", if it doesn't exist it will be created // Open database called "dir/name.db", if it doesn't exist it will be created
db := dbm.NewDB(name, dbm.LevelDBBackendStr, dir) db := dbm.NewDB(name, dbm.LevelDBBackendStr, dir)
tree := iavl.NewVersionedTree(cacheSize, db) tree := iavl.NewVersionedTree(cacheSize, db)
if !empty {
if err = tree.Load(); err != nil { if err = tree.Load(); err != nil {
return nil, errors.ErrInternal("Loading tree: " + err.Error()) return nil, errors.ErrInternal("Loading tree: " + err.Error())
} }
}
return sm.NewState(tree, historySize), nil return sm.NewState(tree, historySize), nil
} }

8
glide.lock generated
View File

@ -1,5 +1,5 @@
hash: fa04e2d0e8764d44c0f8e3400d7d12118b0f05aac6c12e5ff0d3dd1940c03100 hash: fa04e2d0e8764d44c0f8e3400d7d12118b0f05aac6c12e5ff0d3dd1940c03100
updated: 2017-10-19T16:01:35.775869665+02:00 updated: 2017-10-20T19:33:25.496934736+02:00
imports: imports:
- name: github.com/bgentry/speakeasy - name: github.com/bgentry/speakeasy
version: 4aabc24848ce5fd31929f7d1e4ea74d3709c14cd version: 4aabc24848ce5fd31929f7d1e4ea74d3709c14cd
@ -133,9 +133,9 @@ imports:
- data - data
- data/base58 - data/base58
- name: github.com/tendermint/iavl - name: github.com/tendermint/iavl
version: 4f9a4a2433e3b3022ae2ceec50a1f57689d54145 version: 721710e7aa59f61dbfbf558943a207ba3fe6b926
- name: github.com/tendermint/light-client - name: github.com/tendermint/light-client
version: 79125bb4dfe173fe8a7208327ac4ec4c73d1a8a7 version: f817693e93241ee10d8ad49935f7c05b4776d829
subpackages: subpackages:
- certifiers - certifiers
- certifiers/client - certifiers/client
@ -237,7 +237,7 @@ imports:
- tap - tap
- transport - transport
- name: gopkg.in/go-playground/validator.v9 - name: gopkg.in/go-playground/validator.v9
version: d529ee1b0f30352444f507cc6cdac96bfd12decc version: 6d8c18553ea1ac493d049edd6f102f52e618f085
- name: gopkg.in/yaml.v2 - name: gopkg.in/yaml.v2
version: cd8b52f8269e0feb286dfeef29f8fe4d5b397e0b version: cd8b52f8269e0feb286dfeef29f8fe4d5b397e0b
testImports: testImports:

View File

@ -11,6 +11,8 @@ type State struct {
historySize uint64 historySize uint64
} }
// NewState wraps a versioned tree and maintains all needed
// states for the abci app
func NewState(tree *iavl.VersionedTree, historySize uint64) *State { func NewState(tree *iavl.VersionedTree, historySize uint64) *State {
base := NewBonsai(tree) base := NewBonsai(tree)
return &State{ return &State{
@ -21,31 +23,47 @@ func NewState(tree *iavl.VersionedTree, historySize uint64) *State {
} }
} }
// Size is the number of nodes in the last commit
func (s State) Size() int { func (s State) Size() int {
return s.committed.Tree.Size() return s.committed.Tree.Size()
} }
// IsEmpty is true is no data was ever in the tree
// (and signals it is unsafe to save)
func (s State) IsEmpty() bool {
return s.committed.Tree.IsEmpty()
}
// Committed gives us read-only access to the committed
// state(s), including historical queries
func (s State) Committed() *Bonsai { func (s State) Committed() *Bonsai {
return s.committed return s.committed
} }
// Append gives us read-write access to the current working
// state (to be committed at EndBlock)
func (s State) Append() SimpleDB { func (s State) Append() SimpleDB {
return s.deliverTx return s.deliverTx
} }
// Append gives us read-write access to the current scratch
// state (to be reset at EndBlock)
func (s State) Check() SimpleDB { func (s State) Check() SimpleDB {
return s.checkTx return s.checkTx
} }
// LatestHeight is the last block height we have committed
func (s State) LatestHeight() uint64 { func (s State) LatestHeight() uint64 {
return s.committed.Tree.LatestVersion() return s.committed.Tree.LatestVersion()
} }
// LatestHash is the root hash of the last state we have
// committed
func (s State) LatestHash() []byte { func (s State) LatestHash() []byte {
return s.committed.Tree.Hash() return s.committed.Tree.Hash()
} }
// Commit save persistent nodes to the database and re-copies the trees // Commit saves persistent nodes to the database and re-copies the trees
func (s *State) Commit(version uint64) ([]byte, error) { func (s *State) Commit(version uint64) ([]byte, error) {
// commit (if we didn't do hash earlier) // commit (if we didn't do hash earlier)
err := s.committed.Commit(s.deliverTx) err := s.committed.Commit(s.deliverTx)
@ -55,7 +73,7 @@ func (s *State) Commit(version uint64) ([]byte, error) {
// store a new version // store a new version
var hash []byte var hash []byte
if s.committed.Tree.Size() > 0 || s.committed.Tree.LatestVersion() > 0 { if !s.IsEmpty() {
hash, err = s.committed.Tree.SaveVersion(version) hash, err = s.committed.Tree.SaveVersion(version)
if err != nil { if err != nil {
return nil, err return nil, err