2014-09-03 20:41:57 -07:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-08-10 20:38:45 -07:00
|
|
|
"io/ioutil"
|
2015-12-01 20:12:01 -08:00
|
|
|
"sync"
|
2014-09-03 20:41:57 -07:00
|
|
|
"time"
|
|
|
|
|
2015-10-22 17:39:06 -07:00
|
|
|
. "github.com/tendermint/go-common"
|
2016-08-24 21:18:03 -07:00
|
|
|
cfg "github.com/tendermint/go-config"
|
2015-10-22 17:39:06 -07:00
|
|
|
dbm "github.com/tendermint/go-db"
|
|
|
|
"github.com/tendermint/go-wire"
|
2015-11-01 11:34:08 -08:00
|
|
|
"github.com/tendermint/tendermint/types"
|
2014-09-03 20:41:57 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-11-01 11:34:08 -08:00
|
|
|
stateKey = []byte("stateKey")
|
2014-09-03 20:41:57 -07:00
|
|
|
)
|
|
|
|
|
2014-10-06 21:28:49 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-10-07 01:05:54 -07:00
|
|
|
// NOTE: not goroutine-safe.
|
2014-09-03 20:41:57 -07:00
|
|
|
type State struct {
|
2016-09-11 10:16:23 -07:00
|
|
|
// mtx for writing to db
|
|
|
|
mtx sync.Mutex
|
|
|
|
db dbm.DB
|
|
|
|
|
|
|
|
// should not change
|
|
|
|
GenesisDoc *types.GenesisDoc
|
|
|
|
ChainID string
|
|
|
|
|
|
|
|
// updated at end of ExecBlock
|
2016-01-06 17:14:20 -08:00
|
|
|
LastBlockHeight int // Genesis state has this set to 0. So, Block(H=0) does not exist.
|
2016-08-16 14:59:19 -07:00
|
|
|
LastBlockID types.BlockID
|
2015-11-01 11:34:08 -08:00
|
|
|
LastBlockTime time.Time
|
|
|
|
Validators *types.ValidatorSet
|
2016-11-19 16:32:35 -08:00
|
|
|
LastValidators *types.ValidatorSet // block.LastCommit validated against this
|
2016-09-11 10:16:23 -07:00
|
|
|
|
|
|
|
// AppHash is updated after Commit;
|
|
|
|
// it's stale after ExecBlock and before Commit
|
2016-12-01 21:12:06 -08:00
|
|
|
AppHashIsStale bool
|
|
|
|
AppHash []byte
|
2014-09-03 20:41:57 -07:00
|
|
|
}
|
|
|
|
|
2015-01-14 20:34:53 -08:00
|
|
|
func LoadState(db dbm.DB) *State {
|
2015-12-01 20:12:01 -08:00
|
|
|
s := &State{db: db}
|
2014-09-03 20:41:57 -07:00
|
|
|
buf := db.Get(stateKey)
|
|
|
|
if len(buf) == 0 {
|
2014-10-03 17:59:54 -07:00
|
|
|
return nil
|
2014-09-03 20:41:57 -07:00
|
|
|
} else {
|
2015-11-10 13:10:43 -08:00
|
|
|
r, n, err := bytes.NewReader(buf), new(int), new(error)
|
2015-12-01 20:12:01 -08:00
|
|
|
wire.ReadBinaryPtr(&s, r, 0, n, err)
|
2014-12-09 18:49:04 -08:00
|
|
|
if *err != nil {
|
2015-06-16 21:16:58 -07:00
|
|
|
// DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
|
|
|
|
Exit(Fmt("Data has been corrupted or its spec has changed: %v\n", *err))
|
2014-09-03 20:41:57 -07:00
|
|
|
}
|
2014-10-11 21:27:58 -07:00
|
|
|
// TODO: ensure that buf is completely read.
|
2014-09-03 20:41:57 -07:00
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) Copy() *State {
|
2016-12-01 21:12:06 -08:00
|
|
|
if s.AppHashIsStale {
|
|
|
|
PanicSanity(Fmt("App hash is stale: %v", s))
|
|
|
|
}
|
2014-09-03 20:41:57 -07:00
|
|
|
return &State{
|
2015-12-01 20:12:01 -08:00
|
|
|
db: s.db,
|
|
|
|
GenesisDoc: s.GenesisDoc,
|
2015-11-01 11:34:08 -08:00
|
|
|
ChainID: s.ChainID,
|
|
|
|
LastBlockHeight: s.LastBlockHeight,
|
2016-08-16 14:59:19 -07:00
|
|
|
LastBlockID: s.LastBlockID,
|
2015-11-01 11:34:08 -08:00
|
|
|
LastBlockTime: s.LastBlockTime,
|
2015-12-01 20:12:01 -08:00
|
|
|
Validators: s.Validators.Copy(),
|
|
|
|
LastValidators: s.LastValidators.Copy(),
|
2016-12-01 21:12:06 -08:00
|
|
|
AppHashIsStale: false,
|
2016-01-06 17:14:20 -08:00
|
|
|
AppHash: s.AppHash,
|
2014-09-03 20:41:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-01 20:12:01 -08:00
|
|
|
func (s *State) Save() {
|
|
|
|
s.mtx.Lock()
|
|
|
|
defer s.mtx.Unlock()
|
2016-12-06 02:52:07 -08:00
|
|
|
s.db.SetSync(stateKey, s.Bytes())
|
2016-08-23 18:44:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) Equals(s2 *State) bool {
|
|
|
|
return bytes.Equal(s.Bytes(), s2.Bytes())
|
|
|
|
}
|
2014-12-09 18:49:04 -08:00
|
|
|
|
2016-08-23 18:44:07 -07:00
|
|
|
func (s *State) Bytes() []byte {
|
2015-12-01 20:12:01 -08:00
|
|
|
buf, n, err := new(bytes.Buffer), new(int), new(error)
|
|
|
|
wire.WriteBinary(s, buf, n, err)
|
|
|
|
if *err != nil {
|
|
|
|
PanicCrisis(*err)
|
2015-03-18 01:27:16 -07:00
|
|
|
}
|
2016-08-23 18:44:07 -07:00
|
|
|
return buf.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mutate state variables to match block and validators
|
2016-12-01 21:12:06 -08:00
|
|
|
// Since we don't have the new AppHash yet, we set s.AppHashIsStale=true
|
2016-08-23 18:44:07 -07:00
|
|
|
func (s *State) SetBlockAndValidators(header *types.Header, blockPartsHeader types.PartSetHeader, prevValSet, nextValSet *types.ValidatorSet) {
|
|
|
|
s.LastBlockHeight = header.Height
|
2016-11-16 13:13:17 -08:00
|
|
|
s.LastBlockID = types.BlockID{header.Hash(), blockPartsHeader}
|
2016-08-23 18:44:07 -07:00
|
|
|
s.LastBlockTime = header.Time
|
|
|
|
s.Validators = nextValSet
|
|
|
|
s.LastValidators = prevValSet
|
2016-09-11 10:16:23 -07:00
|
|
|
|
2016-12-01 21:12:06 -08:00
|
|
|
s.AppHashIsStale = true
|
2016-11-19 17:29:07 -08:00
|
|
|
}
|
|
|
|
|
2016-08-23 18:44:07 -07:00
|
|
|
func (s *State) GetValidators() (*types.ValidatorSet, *types.ValidatorSet) {
|
|
|
|
return s.LastValidators, s.Validators
|
2015-06-19 08:36:20 -07:00
|
|
|
}
|
|
|
|
|
2016-08-24 21:18:03 -07:00
|
|
|
// Load the most recent state from "state" db,
|
|
|
|
// or create a new one (and save) from genesis.
|
|
|
|
func GetState(config cfg.Config, stateDB dbm.DB) *State {
|
|
|
|
state := LoadState(stateDB)
|
|
|
|
if state == nil {
|
|
|
|
state = MakeGenesisStateFromFile(stateDB, config.GetString("genesis_file"))
|
|
|
|
state.Save()
|
|
|
|
}
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
2015-03-28 23:44:07 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-08-10 20:38:45 -07:00
|
|
|
// Genesis
|
2014-10-07 23:11:04 -07:00
|
|
|
|
2015-12-01 20:12:01 -08:00
|
|
|
func MakeGenesisStateFromFile(db dbm.DB, genDocFile string) *State {
|
|
|
|
genDocJSON, err := ioutil.ReadFile(genDocFile)
|
2015-08-10 20:38:45 -07:00
|
|
|
if err != nil {
|
|
|
|
Exit(Fmt("Couldn't read GenesisDoc file: %v", err))
|
|
|
|
}
|
2015-12-01 20:12:01 -08:00
|
|
|
genDoc := types.GenesisDocFromJSON(genDocJSON)
|
|
|
|
return MakeGenesisState(db, genDoc)
|
2014-09-03 20:41:57 -07:00
|
|
|
}
|
2014-10-13 20:07:26 -07:00
|
|
|
|
2015-11-01 11:34:08 -08:00
|
|
|
func MakeGenesisState(db dbm.DB, genDoc *types.GenesisDoc) *State {
|
2015-08-10 20:38:45 -07:00
|
|
|
if len(genDoc.Validators) == 0 {
|
|
|
|
Exit(Fmt("The genesis file has no validators"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if genDoc.GenesisTime.IsZero() {
|
|
|
|
genDoc.GenesisTime = time.Now()
|
|
|
|
}
|
|
|
|
|
2015-11-01 11:34:08 -08:00
|
|
|
// Make validators slice
|
2015-08-10 20:38:45 -07:00
|
|
|
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{
|
2015-12-01 20:12:01 -08:00
|
|
|
db: db,
|
|
|
|
GenesisDoc: genDoc,
|
2015-11-01 11:34:08 -08:00
|
|
|
ChainID: genDoc.ChainID,
|
|
|
|
LastBlockHeight: 0,
|
2016-08-16 14:59:19 -07:00
|
|
|
LastBlockID: types.BlockID{},
|
2015-11-01 11:34:08 -08:00
|
|
|
LastBlockTime: genDoc.GenesisTime,
|
|
|
|
Validators: types.NewValidatorSet(validators),
|
|
|
|
LastValidators: types.NewValidatorSet(nil),
|
2016-01-06 17:14:20 -08:00
|
|
|
AppHash: genDoc.AppHash,
|
2015-08-10 20:38:45 -07:00
|
|
|
}
|
2014-10-13 20:07:26 -07:00
|
|
|
}
|