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
|
|
|
|
checkCache map[string]checkAccount
|
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-22 13:07:03 -07:00
|
|
|
chainID: "",
|
|
|
|
eyesCli: eyesCli,
|
|
|
|
checkCache: make(map[string]checkAccount),
|
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-22 13:07:03 -07:00
|
|
|
//----------------------------------------
|
|
|
|
// CheckTx state
|
|
|
|
|
|
|
|
type checkAccount struct {
|
|
|
|
sequence int
|
|
|
|
balance int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) GetCheckAccount(addr []byte, defaultSequence int, defaultBalance int64) (sequence int, balance int64) {
|
|
|
|
cAcc, ok := s.checkCache[string(addr)]
|
|
|
|
if !ok {
|
|
|
|
return defaultSequence, defaultBalance
|
|
|
|
}
|
|
|
|
return cAcc.sequence, cAcc.balance
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) SetCheckAccount(addr []byte, sequence int, balance int64) {
|
|
|
|
s.checkCache[string(addr)] = checkAccount{sequence, balance}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) ResetCacheState() {
|
|
|
|
s.checkCache = make(map[string]checkAccount)
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------
|
|
|
|
|
2016-03-20 03:00:43 -07:00
|
|
|
func (s *State) GetAccount(addr []byte) *types.Account {
|
2016-03-23 02:47:05 -07:00
|
|
|
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
|
|
|
}
|
2016-03-23 02:47:05 -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
|
2016-03-23 02:47:05 -07:00
|
|
|
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)
|
2016-03-23 02:47:05 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|