cosmos-sdk/state/merkle.go

82 lines
1.8 KiB
Go
Raw Normal View History

package state
2017-08-04 06:46:00 -07:00
import "github.com/tendermint/merkleeyes/iavl"
// 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
deliverTx SimpleDB
checkTx SimpleDB
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)
return State{
2017-07-27 07:40:17 -07:00
committed: base,
deliverTx: base.Checkpoint(),
checkTx: base.Checkpoint(),
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
}
func (s State) Append() SimpleDB {
2017-07-27 07:40:17 -07:00
return s.deliverTx
}
func (s State) Check() SimpleDB {
2017-07-27 07:40:17 -07:00
return s.checkTx
}
// 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
}
// 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)
}
}
// 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) {
// 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
}
var hash []byte
if s.persistent {
2017-07-27 07:40:17 -07:00
hash = s.committed.Tree.Save()
} else {
2017-07-27 07:40:17 -07:00
hash = s.committed.Tree.Hash()
}
s.deliverTx = s.committed.Checkpoint()
s.checkTx = s.committed.Checkpoint()
2017-07-27 08:17:22 -07:00
return hash, nil
}