2014-09-03 20:41:57 -07:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-09-05 21:57:36 -04:00
|
|
|
"fmt"
|
2015-08-10 20:38:45 -07:00
|
|
|
"io/ioutil"
|
2014-09-03 20:41:57 -07:00
|
|
|
"time"
|
|
|
|
|
2015-11-01 11:34:08 -08:00
|
|
|
"github.com/tendermint/tendermint/types"
|
2014-09-03 20:41:57 -07:00
|
|
|
)
|
|
|
|
|
2017-12-20 23:53:15 -05:00
|
|
|
// database keys
|
2014-09-03 20:41:57 -07:00
|
|
|
var (
|
2017-12-25 13:47:16 -05:00
|
|
|
stateKey = []byte("stateKey")
|
2014-09-03 20:41:57 -07:00
|
|
|
)
|
|
|
|
|
2014-10-06 21:28:49 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2017-12-20 23:53:15 -05:00
|
|
|
// State is a short description of the latest committed block of the Tendermint consensus.
|
|
|
|
// It keeps all information necessary to validate new blocks,
|
|
|
|
// including the last validator set and the consensus params.
|
|
|
|
// All fields are exposed so the struct can be easily serialized,
|
2017-12-27 17:50:16 -05:00
|
|
|
// but none of them should be mutated directly.
|
2017-12-28 18:26:13 -05:00
|
|
|
// Instead, use state.Copy() or state.NextState(...).
|
2014-10-07 01:05:54 -07:00
|
|
|
// NOTE: not goroutine-safe.
|
2014-09-03 20:41:57 -07:00
|
|
|
type State struct {
|
2017-12-20 23:53:15 -05:00
|
|
|
// Immutable
|
2017-10-12 14:50:09 +04:00
|
|
|
ChainID string
|
2016-09-11 13:16:23 -04:00
|
|
|
|
2017-09-12 14:37:32 -04:00
|
|
|
// LastBlockHeight=0 at genesis (ie. block(H=0) does not exist)
|
2017-12-12 13:16:03 +01:00
|
|
|
LastBlockHeight int64
|
|
|
|
LastBlockTotalTx int64
|
|
|
|
LastBlockID types.BlockID
|
|
|
|
LastBlockTime time.Time
|
2017-12-20 23:53:15 -05:00
|
|
|
|
|
|
|
// LastValidators is used to validate block.LastCommit.
|
|
|
|
// Validators are persisted to the database separately every time they change,
|
|
|
|
// so we can query for historical validator sets.
|
|
|
|
// Note that if s.LastBlockHeight causes a valset change,
|
2017-09-05 21:57:36 -04:00
|
|
|
// we set s.LastHeightValidatorsChanged = s.LastBlockHeight + 1
|
2017-12-20 23:53:15 -05:00
|
|
|
Validators *types.ValidatorSet
|
|
|
|
LastValidators *types.ValidatorSet
|
2017-12-01 19:04:53 -06:00
|
|
|
LastHeightValidatorsChanged int64
|
2017-08-21 16:31:54 -04:00
|
|
|
|
2017-12-20 23:53:15 -05:00
|
|
|
// Consensus parameters used for validating blocks.
|
|
|
|
// Changes returned by EndBlock and updated after Commit.
|
|
|
|
ConsensusParams types.ConsensusParams
|
|
|
|
LastHeightConsensusParamsChanged int64
|
|
|
|
|
2017-12-25 13:47:16 -05:00
|
|
|
// Merkle root of the results from executing prev block
|
2017-12-26 19:53:26 -05:00
|
|
|
LastResultsHash []byte
|
2017-12-22 15:21:02 +01:00
|
|
|
|
2017-12-20 23:53:15 -05:00
|
|
|
// The latest AppHash we've received from calling abci.Commit()
|
2017-10-16 16:01:32 +02:00
|
|
|
AppHash []byte
|
2017-05-02 11:53:32 +04:00
|
|
|
}
|
|
|
|
|
2017-08-21 16:12:07 -04:00
|
|
|
// Copy makes a copy of the State for mutating.
|
2018-06-04 13:31:57 -07:00
|
|
|
func (state State) Copy() State {
|
2017-12-27 19:21:16 -05:00
|
|
|
return State{
|
2018-06-04 13:31:57 -07:00
|
|
|
ChainID: state.ChainID,
|
2017-12-20 23:53:15 -05:00
|
|
|
|
2018-06-04 13:31:57 -07:00
|
|
|
LastBlockHeight: state.LastBlockHeight,
|
|
|
|
LastBlockTotalTx: state.LastBlockTotalTx,
|
|
|
|
LastBlockID: state.LastBlockID,
|
|
|
|
LastBlockTime: state.LastBlockTime,
|
2017-12-20 23:53:15 -05:00
|
|
|
|
2018-06-04 13:31:57 -07:00
|
|
|
Validators: state.Validators.Copy(),
|
|
|
|
LastValidators: state.LastValidators.Copy(),
|
|
|
|
LastHeightValidatorsChanged: state.LastHeightValidatorsChanged,
|
2017-12-20 23:53:15 -05:00
|
|
|
|
2018-06-04 13:31:57 -07:00
|
|
|
ConsensusParams: state.ConsensusParams,
|
|
|
|
LastHeightConsensusParamsChanged: state.LastHeightConsensusParamsChanged,
|
2017-12-20 23:53:15 -05:00
|
|
|
|
2018-06-04 13:31:57 -07:00
|
|
|
AppHash: state.AppHash,
|
2017-12-20 23:53:15 -05:00
|
|
|
|
2018-06-04 13:31:57 -07:00
|
|
|
LastResultsHash: state.LastResultsHash,
|
2017-09-22 18:17:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-21 16:12:07 -04:00
|
|
|
// Equals returns true if the States are identical.
|
2018-06-04 13:31:57 -07:00
|
|
|
func (state State) Equals(state2 State) bool {
|
|
|
|
sbz, s2bz := state.Bytes(), state2.Bytes()
|
2018-04-03 06:50:53 -07:00
|
|
|
return bytes.Equal(sbz, s2bz)
|
2016-08-23 21:44:07 -04:00
|
|
|
}
|
2014-12-09 18:49:04 -08:00
|
|
|
|
2018-04-03 06:50:53 -07:00
|
|
|
// Bytes serializes the State using go-amino.
|
2018-06-04 13:31:57 -07:00
|
|
|
func (state State) Bytes() []byte {
|
|
|
|
return cdc.MustMarshalBinaryBare(state)
|
2016-08-23 21:44:07 -04:00
|
|
|
}
|
|
|
|
|
2017-12-27 20:03:48 -05:00
|
|
|
// IsEmpty returns true if the State is equal to the empty State.
|
2018-06-04 13:31:57 -07:00
|
|
|
func (state State) IsEmpty() bool {
|
|
|
|
return state.Validators == nil // XXX can't compare to Empty
|
2016-11-19 20:29:07 -05:00
|
|
|
}
|
|
|
|
|
2017-08-21 16:12:07 -04:00
|
|
|
// GetValidators returns the last and current validator sets.
|
2018-06-04 13:31:57 -07:00
|
|
|
func (state State) GetValidators() (last *types.ValidatorSet, current *types.ValidatorSet) {
|
|
|
|
return state.LastValidators, state.Validators
|
2015-06-19 11:36:20 -04:00
|
|
|
}
|
|
|
|
|
2017-12-27 20:03:48 -05:00
|
|
|
//------------------------------------------------------------------------
|
|
|
|
// Create a block from the latest state
|
|
|
|
|
|
|
|
// MakeBlock builds a block with the given txs and commit from the current state.
|
2018-06-04 13:31:57 -07:00
|
|
|
func (state State) MakeBlock(height int64, txs []types.Tx, commit *types.Commit) (*types.Block, *types.PartSet) {
|
2017-12-27 20:03:48 -05:00
|
|
|
// build base block
|
|
|
|
block := types.MakeBlock(height, txs, commit)
|
|
|
|
|
|
|
|
// fill header with state data
|
2018-06-04 13:31:57 -07:00
|
|
|
block.ChainID = state.ChainID
|
|
|
|
block.TotalTxs = state.LastBlockTotalTx + block.NumTxs
|
|
|
|
block.LastBlockID = state.LastBlockID
|
|
|
|
block.ValidatorsHash = state.Validators.Hash()
|
|
|
|
block.AppHash = state.AppHash
|
|
|
|
block.ConsensusHash = state.ConsensusParams.Hash()
|
|
|
|
block.LastResultsHash = state.LastResultsHash
|
|
|
|
|
|
|
|
return block, block.MakePartSet(state.ConsensusParams.BlockGossip.BlockPartSizeBytes)
|
2017-12-27 20:03:48 -05:00
|
|
|
}
|
|
|
|
|
2017-08-31 13:54:08 +02:00
|
|
|
//------------------------------------------------------------------------
|
2015-08-10 20:38:45 -07:00
|
|
|
// Genesis
|
2014-10-07 23:11:04 -07:00
|
|
|
|
2017-08-31 13:54:08 +02:00
|
|
|
// MakeGenesisStateFromFile reads and unmarshals state from the given
|
|
|
|
// file.
|
2017-01-19 13:33:58 +04:00
|
|
|
//
|
|
|
|
// Used during replay and in tests.
|
2017-12-27 20:03:48 -05:00
|
|
|
func MakeGenesisStateFromFile(genDocFile string) (State, error) {
|
2017-09-20 18:29:36 -04:00
|
|
|
genDoc, err := MakeGenesisDocFromFile(genDocFile)
|
|
|
|
if err != nil {
|
2017-12-27 20:03:48 -05:00
|
|
|
return State{}, err
|
2017-09-20 18:29:36 -04:00
|
|
|
}
|
2017-12-27 17:50:16 -05:00
|
|
|
return MakeGenesisState(genDoc)
|
2017-09-20 18:29:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// MakeGenesisDocFromFile reads and unmarshals genesis doc from the given file.
|
|
|
|
func MakeGenesisDocFromFile(genDocFile string) (*types.GenesisDoc, error) {
|
2015-12-01 20:12:01 -08:00
|
|
|
genDocJSON, err := ioutil.ReadFile(genDocFile)
|
2015-08-10 20:38:45 -07:00
|
|
|
if err != nil {
|
2017-09-20 18:29:36 -04:00
|
|
|
return nil, fmt.Errorf("Couldn't read GenesisDoc file: %v", err)
|
2015-08-10 20:38:45 -07:00
|
|
|
}
|
2017-01-29 13:50:04 -08:00
|
|
|
genDoc, err := types.GenesisDocFromJSON(genDocJSON)
|
|
|
|
if err != nil {
|
2017-09-20 18:29:36 -04:00
|
|
|
return nil, fmt.Errorf("Error reading GenesisDoc: %v", err)
|
2017-01-29 13:50:04 -08:00
|
|
|
}
|
2017-09-20 18:29:36 -04:00
|
|
|
return genDoc, nil
|
2014-09-03 20:41:57 -07:00
|
|
|
}
|
2014-10-13 20:07:26 -07:00
|
|
|
|
2017-01-19 13:33:58 +04:00
|
|
|
// MakeGenesisState creates state from types.GenesisDoc.
|
2017-12-27 20:03:48 -05:00
|
|
|
func MakeGenesisState(genDoc *types.GenesisDoc) (State, error) {
|
2017-09-11 16:48:41 -04:00
|
|
|
err := genDoc.ValidateAndComplete()
|
|
|
|
if err != nil {
|
2017-12-27 20:03:48 -05:00
|
|
|
return State{}, fmt.Errorf("Error in genesis file: %v", err)
|
2015-08-10 20:38:45 -07:00
|
|
|
}
|
|
|
|
|
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,
|
2017-09-21 14:37:34 -04:00
|
|
|
VotingPower: val.Power,
|
2015-08-10 20:38:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-27 20:03:48 -05:00
|
|
|
return State{
|
2017-09-22 18:17:21 -06:00
|
|
|
|
2017-10-12 14:50:09 +04:00
|
|
|
ChainID: genDoc.ChainID,
|
2017-10-05 16:50:05 +04:00
|
|
|
|
2017-12-20 23:53:15 -05:00
|
|
|
LastBlockHeight: 0,
|
|
|
|
LastBlockID: types.BlockID{},
|
|
|
|
LastBlockTime: genDoc.GenesisTime,
|
|
|
|
|
2017-08-21 16:31:54 -04:00
|
|
|
Validators: types.NewValidatorSet(validators),
|
|
|
|
LastValidators: types.NewValidatorSet(nil),
|
2017-09-04 18:27:04 -04:00
|
|
|
LastHeightValidatorsChanged: 1,
|
2017-12-20 23:53:15 -05:00
|
|
|
|
|
|
|
ConsensusParams: *genDoc.ConsensusParams,
|
|
|
|
LastHeightConsensusParamsChanged: 1,
|
|
|
|
|
|
|
|
AppHash: genDoc.AppHash,
|
2017-09-20 18:29:36 -04:00
|
|
|
}, nil
|
2014-10-13 20:07:26 -07:00
|
|
|
}
|