cosmos-sdk/state/state.go

71 lines
1.5 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 {
res := s.eyesCli.GetSync(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-03-24 11:27:44 -07:00
func (s *State) SetAccount(address []byte, acc *types.Account) {
2016-03-20 03:00:43 -07:00
accBytes := wire.BinaryBytes(acc)
2016-03-24 11:27:44 -07:00
res := s.eyesCli.SetSync(address, accBytes)
if res.IsErr() {
2016-03-27 22:41:57 -07:00
panic(Fmt("Error storing account addr %X error: %v", address, 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)
}