tendermint/state/state.go

173 lines
4.3 KiB
Go
Raw Normal View History

package state
import (
"bytes"
"io/ioutil"
2015-12-01 20:12:01 -08:00
"sync"
"time"
. "github.com/tendermint/go-common"
2016-08-24 21:18:03 -07:00
cfg "github.com/tendermint/go-config"
dbm "github.com/tendermint/go-db"
"github.com/tendermint/go-wire"
2015-11-01 11:34:08 -08:00
"github.com/tendermint/tendermint/types"
)
var (
2015-11-01 11:34:08 -08:00
stateKey = []byte("stateKey")
)
2014-10-06 21:28:49 -07:00
//-----------------------------------------------------------------------------
2014-10-07 01:05:54 -07:00
// NOTE: not goroutine-safe.
type State struct {
// mtx for writing to db
mtx sync.Mutex
db dbm.DB
// should not change
GenesisDoc *types.GenesisDoc
ChainID string
// updated at end of ExecBlock
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
// AppHash is updated after Commit;
// it's stale after ExecBlock and before Commit
AppHashIsStale bool
AppHash []byte
}
func LoadState(db dbm.DB) *State {
2015-12-01 20:12:01 -08:00
s := &State{db: db}
buf := db.Get(stateKey)
if len(buf) == 0 {
2014-10-03 17:59:54 -07:00
return nil
} 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)
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-10-11 21:27:58 -07:00
// TODO: ensure that buf is completely read.
}
return s
}
func (s *State) Copy() *State {
if s.AppHashIsStale {
PanicSanity(Fmt("App hash is stale: %v", s))
}
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(),
AppHashIsStale: false,
AppHash: s.AppHash,
}
}
2015-12-01 20:12:01 -08:00
func (s *State) Save() {
s.mtx.Lock()
defer s.mtx.Unlock()
2016-08-23 18:44:07 -07:00
s.db.Set(stateKey, s.Bytes())
}
func (s *State) Equals(s2 *State) bool {
return bytes.Equal(s.Bytes(), s2.Bytes())
}
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
// 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
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
s.AppHashIsStale = true
}
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
}
//-----------------------------------------------------------------------------
// 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)
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)
}
2015-11-01 11:34:08 -08:00
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()
}
2015-11-01 11:34:08 -08:00
// 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{
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),
AppHash: genDoc.AppHash,
}
}