cosmos-sdk/state/state.go

77 lines
1.6 KiB
Go
Raw Normal View History

2016-03-20 03:00:43 -07:00
package state
import (
"github.com/tendermint/basecoin/types"
2016-03-22 13:07:03 -07:00
. "github.com/tendermint/go-common"
2016-03-20 03:00:43 -07:00
"github.com/tendermint/go-wire"
eyes "github.com/tendermint/merkleeyes/client"
)
type State struct {
2016-03-27 12:47:50 -07:00
chainID string
eyesCli *eyes.Client
2016-03-29 14:25:17 -07:00
checkCache *types.AccountCache
2016-03-20 03:00:43 -07:00
LastBlockHeight uint64
LastBlockHash []byte
GasLimit int64
}
2016-03-22 13:07:03 -07:00
func NewState(eyesCli *eyes.Client) *State {
2016-03-20 03:00:43 -07:00
s := &State{
2016-03-29 14:25:17 -07:00
chainID: "",
eyesCli: eyesCli,
2016-03-20 03:00:43 -07:00
}
2016-03-29 14:25:17 -07:00
s.checkCache = types.NewAccountCache(s)
2016-03-20 03:00:43 -07:00
return s
}
2016-03-22 13:07:03 -07:00
func (s *State) SetChainID(chainID string) {
s.chainID = chainID
}
func (s *State) GetChainID() string {
if s.chainID == "" {
PanicSanity("Expected to have set SetChainID")
}
2016-03-22 10:13:34 -07:00
return s.chainID
}
2016-03-20 03:00:43 -07:00
func (s *State) GetAccount(addr []byte) *types.Account {
2016-04-17 12:41:26 -07:00
res := s.eyesCli.GetSync(AccountKey(addr))
if res.IsErr() {
2016-03-27 22:41:57 -07:00
panic(Fmt("Error loading account addr %X error: %v", addr, res.Error()))
2016-03-20 03:00:43 -07:00
}
if len(res.Data) == 0 {
2016-03-20 03:00:43 -07:00
return nil
}
2016-03-27 22:41:57 -07:00
var acc *types.Account
err := wire.ReadBinaryBytes(res.Data, &acc)
2016-03-20 03:00:43 -07:00
if err != nil {
2016-03-27 22:41:57 -07:00
panic(Fmt("Error reading account %X error: %v", res.Data, err.Error()))
2016-03-20 03:00:43 -07:00
}
2016-03-27 22:41:57 -07:00
return acc
2016-03-20 03:00:43 -07:00
}
2016-04-17 12:41:26 -07:00
func (s *State) SetAccount(addr []byte, acc *types.Account) {
2016-03-20 03:00:43 -07:00
accBytes := wire.BinaryBytes(acc)
2016-04-17 12:41:26 -07:00
res := s.eyesCli.SetSync(AccountKey(addr), accBytes)
if res.IsErr() {
2016-04-17 12:41:26 -07:00
panic(Fmt("Error storing account addr %X error: %v", addr, res.Error()))
2016-03-20 03:00:43 -07:00
}
}
2016-03-29 14:25:17 -07:00
func (s *State) GetCheckCache() *types.AccountCache {
return s.checkCache
}
func (s *State) ResetCacheState() {
s.checkCache = types.NewAccountCache(s)
}
2016-04-17 12:41:26 -07:00
//----------------------------------------
func AccountKey(addr []byte) []byte {
return append([]byte("base/a/"), addr...)
}