package state
|
|
|
|
import (
|
|
"bytes"
|
|
"io/ioutil"
|
|
"sync"
|
|
"time"
|
|
|
|
. "github.com/tendermint/go-common"
|
|
dbm "github.com/tendermint/go-db"
|
|
"github.com/tendermint/go-wire"
|
|
"github.com/tendermint/tendermint/types"
|
|
)
|
|
|
|
var (
|
|
stateKey = []byte("stateKey")
|
|
)
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// NOTE: not goroutine-safe.
|
|
type State struct {
|
|
mtx sync.Mutex
|
|
db dbm.DB
|
|
GenesisDoc *types.GenesisDoc
|
|
ChainID string
|
|
LastBlockHeight int // Genesis state has this set to 0. So, Block(H=0) does not exist.
|
|
LastBlockID types.BlockID
|
|
LastBlockTime time.Time
|
|
Validators *types.ValidatorSet
|
|
LastValidators *types.ValidatorSet
|
|
AppHash []byte
|
|
}
|
|
|
|
func LoadState(db dbm.DB) *State {
|
|
s := &State{db: db}
|
|
buf := db.Get(stateKey)
|
|
if len(buf) == 0 {
|
|
return nil
|
|
} else {
|
|
r, n, err := bytes.NewReader(buf), new(int), new(error)
|
|
wire.ReadBinaryPtr(&s, r, 0, n, err)
|
|
if *err != nil {
|
|
// DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
|
|
Exit(Fmt("Data has been corrupted or its spec has changed: %v\n", *err))
|
|
}
|
|
// TODO: ensure that buf is completely read.
|
|
}
|
|
return s
|
|
}
|
|
|
|
func (s *State) Copy() *State {
|
|
return &State{
|
|
db: s.db,
|
|
GenesisDoc: s.GenesisDoc,
|
|
ChainID: s.ChainID,
|
|
LastBlockHeight: s.LastBlockHeight,
|
|
LastBlockID: s.LastBlockID,
|
|
LastBlockTime: s.LastBlockTime,
|
|
Validators: s.Validators.Copy(),
|
|
LastValidators: s.LastValidators.Copy(),
|
|
AppHash: s.AppHash,
|
|
}
|
|
}
|
|
|
|
func (s *State) Save() {
|
|
s.mtx.Lock()
|
|
defer s.mtx.Unlock()
|
|
s.db.Set(stateKey, s.Bytes())
|
|
}
|
|
|
|
func (s *State) Equals(s2 *State) bool {
|
|
return bytes.Equal(s.Bytes(), s2.Bytes())
|
|
}
|
|
|
|
func (s *State) Bytes() []byte {
|
|
buf, n, err := new(bytes.Buffer), new(int), new(error)
|
|
wire.WriteBinary(s, buf, n, err)
|
|
if *err != nil {
|
|
PanicCrisis(*err)
|
|
}
|
|
return buf.Bytes()
|
|
}
|
|
|
|
// Mutate state variables to match block and validators
|
|
func (s *State) SetBlockAndValidators(header *types.Header, blockPartsHeader types.PartSetHeader, prevValSet, nextValSet *types.ValidatorSet) {
|
|
s.LastBlockHeight = header.Height
|
|
s.LastBlockID = types.BlockID{block.Hash(), blockPartsHeader}
|
|
s.LastBlockTime = header.Time
|
|
s.Validators = nextValSet
|
|
s.LastValidators = prevValSet
|
|
}
|
|
|
|
func (s *State) GetValidators() (*types.ValidatorSet, *types.ValidatorSet) {
|
|
return s.LastValidators, s.Validators
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// Genesis
|
|
|
|
func MakeGenesisStateFromFile(db dbm.DB, genDocFile string) *State {
|
|
genDocJSON, err := ioutil.ReadFile(genDocFile)
|
|
if err != nil {
|
|
Exit(Fmt("Couldn't read GenesisDoc file: %v", err))
|
|
}
|
|
genDoc := types.GenesisDocFromJSON(genDocJSON)
|
|
return MakeGenesisState(db, genDoc)
|
|
}
|
|
|
|
func MakeGenesisState(db dbm.DB, genDoc *types.GenesisDoc) *State {
|
|
if len(genDoc.Validators) == 0 {
|
|
Exit(Fmt("The genesis file has no validators"))
|
|
}
|
|
|
|
if genDoc.GenesisTime.IsZero() {
|
|
genDoc.GenesisTime = time.Now()
|
|
}
|
|
|
|
// Make validators slice
|
|
validators := make([]*types.Validator, len(genDoc.Validators))
|
|
for i, val := range genDoc.Validators {
|
|
pubKey := val.PubKey
|
|
address := pubKey.Address()
|
|
|
|
// Make validator
|
|
validators[i] = &types.Validator{
|
|
Address: address,
|
|
PubKey: pubKey,
|
|
VotingPower: val.Amount,
|
|
}
|
|
}
|
|
|
|
return &State{
|
|
db: db,
|
|
GenesisDoc: genDoc,
|
|
ChainID: genDoc.ChainID,
|
|
LastBlockHeight: 0,
|
|
LastBlockID: types.BlockID{},
|
|
LastBlockTime: genDoc.GenesisTime,
|
|
Validators: types.NewValidatorSet(validators),
|
|
LastValidators: types.NewValidatorSet(nil),
|
|
AppHash: genDoc.AppHash,
|
|
}
|
|
}
|