Clean up store versions after a limit

This commit is contained in:
Ethan Frey 2017-10-18 15:21:25 +02:00
parent 422e67b382
commit 3b7020520c
3 changed files with 23 additions and 12 deletions

View File

@ -17,6 +17,9 @@ import (
sm "github.com/cosmos/cosmos-sdk/state" sm "github.com/cosmos/cosmos-sdk/state"
) )
// DefaultHistorySize is how many blocks of history to store for ABCI queries
const DefaultHistorySize = 10
// StoreApp contains a data store and all info needed // StoreApp contains a data store and all info needed
// to perform queries and handshakes. // to perform queries and handshakes.
// //
@ -41,7 +44,7 @@ type StoreApp struct {
// NewStoreApp creates a data store to handle queries // NewStoreApp creates a data store to handle queries
func NewStoreApp(appName, dbName string, cacheSize int, logger log.Logger) (*StoreApp, error) { func NewStoreApp(appName, dbName string, cacheSize int, logger log.Logger) (*StoreApp, error) {
state, err := loadState(dbName, cacheSize) state, err := loadState(dbName, cacheSize, DefaultHistorySize)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -234,11 +237,11 @@ func pubKeyIndex(val *abci.Validator, list []*abci.Validator) int {
return -1 return -1
} }
func loadState(dbName string, cacheSize int) (*sm.State, error) { func loadState(dbName string, cacheSize int, historySize uint64) (*sm.State, error) {
// memory backed case, just for testing // memory backed case, just for testing
if dbName == "" { if dbName == "" {
tree := iavl.NewVersionedTree(0, dbm.NewMemDB()) tree := iavl.NewVersionedTree(0, dbm.NewMemDB())
return sm.NewState(tree), nil return sm.NewState(tree, historySize), nil
} }
// Expand the path fully // Expand the path fully
@ -267,5 +270,5 @@ func loadState(dbName string, cacheSize int) (*sm.State, error) {
} }
} }
return sm.NewState(tree), nil return sm.NewState(tree, historySize), nil
} }

View File

@ -8,14 +8,16 @@ type State struct {
committed *Bonsai committed *Bonsai
deliverTx SimpleDB deliverTx SimpleDB
checkTx SimpleDB checkTx SimpleDB
historySize uint64
} }
func NewState(tree *iavl.VersionedTree) *State { func NewState(tree *iavl.VersionedTree, historySize uint64) *State {
base := NewBonsai(tree) base := NewBonsai(tree)
return &State{ return &State{
committed: base, committed: base,
deliverTx: base.Checkpoint(), deliverTx: base.Checkpoint(),
checkTx: base.Checkpoint(), checkTx: base.Checkpoint(),
historySize: historySize,
} }
} }
@ -51,6 +53,7 @@ func (s *State) Commit(version uint64) ([]byte, error) {
return nil, err return nil, err
} }
// store a new version
var hash []byte var hash []byte
if s.committed.Tree.Size() > 0 || s.committed.Tree.LatestVersion() > 0 { if s.committed.Tree.Size() > 0 || s.committed.Tree.LatestVersion() > 0 {
hash, err = s.committed.Tree.SaveVersion(version) hash, err = s.committed.Tree.SaveVersion(version)
@ -59,6 +62,11 @@ func (s *State) Commit(version uint64) ([]byte, error) {
} }
} }
// release an old version
if version > s.historySize {
s.committed.Tree.DeleteVersion(version - s.historySize)
}
s.deliverTx = s.committed.Checkpoint() s.deliverTx = s.committed.Checkpoint()
s.checkTx = s.committed.Checkpoint() s.checkTx = s.committed.Checkpoint()
return hash, nil return hash, nil

View File

@ -66,7 +66,7 @@ func TestStateCommitHash(t *testing.T) {
// make the store... // make the store...
tree := iavl.NewVersionedTree(0, db.NewMemDB()) tree := iavl.NewVersionedTree(0, db.NewMemDB())
store := NewState(tree) store := NewState(tree, 2)
for n, r := range tc.rounds { for n, r := range tc.rounds {
// start the cache // start the cache