2017-07-26 12:19:37 -07:00
|
|
|
package state
|
2017-07-25 19:19:31 -07:00
|
|
|
|
2017-08-04 06:46:00 -07:00
|
|
|
import "github.com/tendermint/merkleeyes/iavl"
|
2017-07-25 19:19:31 -07:00
|
|
|
|
|
|
|
// State represents the app states, separating the commited state (for queries)
|
|
|
|
// from the working state (for CheckTx and AppendTx)
|
|
|
|
type State struct {
|
2017-07-27 07:40:17 -07:00
|
|
|
committed *Bonsai
|
2017-07-27 12:20:43 -07:00
|
|
|
deliverTx SimpleDB
|
|
|
|
checkTx SimpleDB
|
2017-07-25 19:19:31 -07:00
|
|
|
persistent bool
|
|
|
|
}
|
|
|
|
|
2017-08-04 06:46:00 -07:00
|
|
|
func NewState(tree *iavl.IAVLTree, persistent bool) State {
|
2017-07-27 07:40:17 -07:00
|
|
|
base := NewBonsai(tree)
|
2017-07-25 19:19:31 -07:00
|
|
|
return State{
|
2017-07-27 07:40:17 -07:00
|
|
|
committed: base,
|
2017-07-27 12:20:43 -07:00
|
|
|
deliverTx: base.Checkpoint(),
|
|
|
|
checkTx: base.Checkpoint(),
|
2017-07-25 19:19:31 -07:00
|
|
|
persistent: persistent,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 07:58:37 -07:00
|
|
|
func (s State) Size() int {
|
|
|
|
return s.committed.Tree.Size()
|
|
|
|
}
|
|
|
|
|
2017-07-25 21:06:12 -07:00
|
|
|
func (s State) Committed() *Bonsai {
|
2017-07-27 07:40:17 -07:00
|
|
|
return s.committed
|
2017-07-25 19:19:31 -07:00
|
|
|
}
|
|
|
|
|
2017-07-27 12:20:43 -07:00
|
|
|
func (s State) Append() SimpleDB {
|
2017-07-27 07:40:17 -07:00
|
|
|
return s.deliverTx
|
2017-07-25 19:19:31 -07:00
|
|
|
}
|
|
|
|
|
2017-07-27 12:20:43 -07:00
|
|
|
func (s State) Check() SimpleDB {
|
2017-07-27 07:40:17 -07:00
|
|
|
return s.checkTx
|
2017-07-25 19:19:31 -07:00
|
|
|
}
|
|
|
|
|
2017-07-27 12:20:43 -07:00
|
|
|
// Hash applies deliverTx to committed and calculates hash
|
|
|
|
//
|
|
|
|
// Note the usage:
|
|
|
|
// Hash -> gets the calculated hash but doesn't save
|
|
|
|
// BatchSet -> adds some out-of-bounds data
|
|
|
|
// Commit -> Save everything to disk and the same hash
|
|
|
|
func (s *State) Hash() ([]byte, error) {
|
|
|
|
err := s.committed.Commit(s.deliverTx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.deliverTx = s.committed.Checkpoint()
|
|
|
|
return s.committed.Tree.Hash(), nil
|
2017-07-25 19:19:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// BatchSet is used for some weird magic in storing the new height
|
|
|
|
func (s *State) BatchSet(key, value []byte) {
|
|
|
|
if s.persistent {
|
|
|
|
// This is in the batch with the Save, but not in the tree
|
2017-08-04 06:46:00 -07:00
|
|
|
s.committed.Tree.BatchSet(key, value)
|
2017-07-25 19:19:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit save persistent nodes to the database and re-copies the trees
|
2017-07-27 08:17:22 -07:00
|
|
|
func (s *State) Commit() ([]byte, error) {
|
2017-07-27 12:20:43 -07:00
|
|
|
// commit (if we didn't do hash earlier)
|
2017-07-27 07:40:17 -07:00
|
|
|
err := s.committed.Commit(s.deliverTx)
|
|
|
|
if err != nil {
|
2017-07-27 08:17:22 -07:00
|
|
|
return nil, err
|
2017-07-27 07:40:17 -07:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:19:31 -07:00
|
|
|
var hash []byte
|
|
|
|
if s.persistent {
|
2017-07-27 07:40:17 -07:00
|
|
|
hash = s.committed.Tree.Save()
|
2017-07-25 19:19:31 -07:00
|
|
|
} else {
|
2017-07-27 07:40:17 -07:00
|
|
|
hash = s.committed.Tree.Hash()
|
2017-07-25 19:19:31 -07:00
|
|
|
}
|
|
|
|
|
2017-07-27 12:20:43 -07:00
|
|
|
s.deliverTx = s.committed.Checkpoint()
|
|
|
|
s.checkTx = s.committed.Checkpoint()
|
2017-07-27 08:17:22 -07:00
|
|
|
return hash, nil
|
2017-07-25 19:19:31 -07:00
|
|
|
}
|