Browse Source

state: s -> state

pull/1628/head
Ethan Buchman 7 years ago
parent
commit
dd62f06994
4 changed files with 87 additions and 87 deletions
  1. +23
    -23
      state/execution.go
  2. +31
    -31
      state/state.go
  3. +7
    -7
      state/store.go
  4. +26
    -26
      state/validation.go

+ 23
- 23
state/execution.go View File

@ -59,8 +59,8 @@ func (blockExec *BlockExecutor) SetEventBus(eventBus types.BlockEventPublisher)
// If the block is invalid, it returns an error. // If the block is invalid, it returns an error.
// Validation does not mutate state, but does require historical information from the stateDB, // Validation does not mutate state, but does require historical information from the stateDB,
// ie. to verify evidence from a validator at an old height. // ie. to verify evidence from a validator at an old height.
func (blockExec *BlockExecutor) ValidateBlock(s State, block *types.Block) error {
return validateBlock(blockExec.db, s, block)
func (blockExec *BlockExecutor) ValidateBlock(state State, block *types.Block) error {
return validateBlock(blockExec.db, state, block)
} }
// ApplyBlock validates the block against the state, executes it against the app, // ApplyBlock validates the block against the state, executes it against the app,
@ -68,15 +68,15 @@ func (blockExec *BlockExecutor) ValidateBlock(s State, block *types.Block) error
// It's the only function that needs to be called // It's the only function that needs to be called
// from outside this package to process and commit an entire block. // from outside this package to process and commit an entire block.
// It takes a blockID to avoid recomputing the parts hash. // It takes a blockID to avoid recomputing the parts hash.
func (blockExec *BlockExecutor) ApplyBlock(s State, blockID types.BlockID, block *types.Block) (State, error) {
func (blockExec *BlockExecutor) ApplyBlock(state State, blockID types.BlockID, block *types.Block) (State, error) {
if err := blockExec.ValidateBlock(s, block); err != nil {
return s, ErrInvalidBlock(err)
if err := blockExec.ValidateBlock(state, block); err != nil {
return state, ErrInvalidBlock(err)
} }
abciResponses, err := execBlockOnProxyApp(blockExec.logger, blockExec.proxyApp, block) abciResponses, err := execBlockOnProxyApp(blockExec.logger, blockExec.proxyApp, block)
if err != nil { if err != nil {
return s, ErrProxyAppConn(err)
return state, ErrProxyAppConn(err)
} }
fail.Fail() // XXX fail.Fail() // XXX
@ -87,22 +87,22 @@ func (blockExec *BlockExecutor) ApplyBlock(s State, blockID types.BlockID, block
fail.Fail() // XXX fail.Fail() // XXX
// update the state with the block and responses // update the state with the block and responses
s, err = updateState(s, blockID, block.Header, abciResponses)
state, err = updateState(state, blockID, block.Header, abciResponses)
if err != nil { if err != nil {
return s, fmt.Errorf("Commit failed for application: %v", err)
return state, fmt.Errorf("Commit failed for application: %v", err)
} }
// lock mempool, commit state, update mempoool // lock mempool, commit state, update mempoool
appHash, err := blockExec.Commit(block) appHash, err := blockExec.Commit(block)
if err != nil { if err != nil {
return s, fmt.Errorf("Commit failed for application: %v", err)
return state, fmt.Errorf("Commit failed for application: %v", err)
} }
fail.Fail() // XXX fail.Fail() // XXX
// update the app hash and save the state // update the app hash and save the state
s.AppHash = appHash
SaveState(blockExec.db, s)
state.AppHash = appHash
SaveState(blockExec.db, state)
fail.Fail() // XXX fail.Fail() // XXX
@ -115,7 +115,7 @@ func (blockExec *BlockExecutor) ApplyBlock(s State, blockID types.BlockID, block
// NOTE: if we crash between Commit and Save, events wont be fired during replay // NOTE: if we crash between Commit and Save, events wont be fired during replay
fireEvents(blockExec.logger, blockExec.eventBus, block, abciResponses) fireEvents(blockExec.logger, blockExec.eventBus, block, abciResponses)
return s, nil
return state, nil
} }
// Commit locks the mempool, runs the ABCI Commit message, and updates the mempool. // Commit locks the mempool, runs the ABCI Commit message, and updates the mempool.
@ -283,20 +283,20 @@ func updateValidators(currentSet *types.ValidatorSet, updates []abci.Validator)
} }
// updateState returns a new State updated according to the header and responses. // updateState returns a new State updated according to the header and responses.
func updateState(s State, blockID types.BlockID, header *types.Header,
func updateState(state State, blockID types.BlockID, header *types.Header,
abciResponses *ABCIResponses) (State, error) { abciResponses *ABCIResponses) (State, error) {
// copy the valset so we can apply changes from EndBlock // copy the valset so we can apply changes from EndBlock
// and update s.LastValidators and s.Validators // and update s.LastValidators and s.Validators
prevValSet := s.Validators.Copy()
prevValSet := state.Validators.Copy()
nextValSet := prevValSet.Copy() nextValSet := prevValSet.Copy()
// update the validator set with the latest abciResponses // update the validator set with the latest abciResponses
lastHeightValsChanged := s.LastHeightValidatorsChanged
lastHeightValsChanged := state.LastHeightValidatorsChanged
if len(abciResponses.EndBlock.ValidatorUpdates) > 0 { if len(abciResponses.EndBlock.ValidatorUpdates) > 0 {
err := updateValidators(nextValSet, abciResponses.EndBlock.ValidatorUpdates) err := updateValidators(nextValSet, abciResponses.EndBlock.ValidatorUpdates)
if err != nil { if err != nil {
return s, fmt.Errorf("Error changing validator set: %v", err)
return state, fmt.Errorf("Error changing validator set: %v", err)
} }
// change results from this height but only applies to the next height // change results from this height but only applies to the next height
lastHeightValsChanged = header.Height + 1 lastHeightValsChanged = header.Height + 1
@ -306,14 +306,14 @@ func updateState(s State, blockID types.BlockID, header *types.Header,
nextValSet.IncrementAccum(1) nextValSet.IncrementAccum(1)
// update the params with the latest abciResponses // update the params with the latest abciResponses
nextParams := s.ConsensusParams
lastHeightParamsChanged := s.LastHeightConsensusParamsChanged
nextParams := state.ConsensusParams
lastHeightParamsChanged := state.LastHeightConsensusParamsChanged
if abciResponses.EndBlock.ConsensusParamUpdates != nil { if abciResponses.EndBlock.ConsensusParamUpdates != nil {
// NOTE: must not mutate s.ConsensusParams // NOTE: must not mutate s.ConsensusParams
nextParams = s.ConsensusParams.Update(abciResponses.EndBlock.ConsensusParamUpdates)
nextParams = state.ConsensusParams.Update(abciResponses.EndBlock.ConsensusParamUpdates)
err := nextParams.Validate() err := nextParams.Validate()
if err != nil { if err != nil {
return s, fmt.Errorf("Error updating consensus params: %v", err)
return state, fmt.Errorf("Error updating consensus params: %v", err)
} }
// change results from this height but only applies to the next height // change results from this height but only applies to the next height
lastHeightParamsChanged = header.Height + 1 lastHeightParamsChanged = header.Height + 1
@ -322,13 +322,13 @@ func updateState(s State, blockID types.BlockID, header *types.Header,
// NOTE: the AppHash has not been populated. // NOTE: the AppHash has not been populated.
// It will be filled on state.Save. // It will be filled on state.Save.
return State{ return State{
ChainID: s.ChainID,
ChainID: state.ChainID,
LastBlockHeight: header.Height, LastBlockHeight: header.Height,
LastBlockTotalTx: s.LastBlockTotalTx + header.NumTxs,
LastBlockTotalTx: state.LastBlockTotalTx + header.NumTxs,
LastBlockID: blockID, LastBlockID: blockID,
LastBlockTime: header.Time, LastBlockTime: header.Time,
Validators: nextValSet, Validators: nextValSet,
LastValidators: s.Validators.Copy(),
LastValidators: state.Validators.Copy(),
LastHeightValidatorsChanged: lastHeightValsChanged, LastHeightValidatorsChanged: lastHeightValsChanged,
ConsensusParams: nextParams, ConsensusParams: nextParams,
LastHeightConsensusParamsChanged: lastHeightParamsChanged, LastHeightConsensusParamsChanged: lastHeightParamsChanged,


+ 31
- 31
state/state.go View File

@ -55,67 +55,67 @@ type State struct {
} }
// Copy makes a copy of the State for mutating. // Copy makes a copy of the State for mutating.
func (s State) Copy() State {
func (state State) Copy() State {
return State{ return State{
ChainID: s.ChainID,
ChainID: state.ChainID,
LastBlockHeight: s.LastBlockHeight,
LastBlockTotalTx: s.LastBlockTotalTx,
LastBlockID: s.LastBlockID,
LastBlockTime: s.LastBlockTime,
LastBlockHeight: state.LastBlockHeight,
LastBlockTotalTx: state.LastBlockTotalTx,
LastBlockID: state.LastBlockID,
LastBlockTime: state.LastBlockTime,
Validators: s.Validators.Copy(),
LastValidators: s.LastValidators.Copy(),
LastHeightValidatorsChanged: s.LastHeightValidatorsChanged,
Validators: state.Validators.Copy(),
LastValidators: state.LastValidators.Copy(),
LastHeightValidatorsChanged: state.LastHeightValidatorsChanged,
ConsensusParams: s.ConsensusParams,
LastHeightConsensusParamsChanged: s.LastHeightConsensusParamsChanged,
ConsensusParams: state.ConsensusParams,
LastHeightConsensusParamsChanged: state.LastHeightConsensusParamsChanged,
AppHash: s.AppHash,
AppHash: state.AppHash,
LastResultsHash: s.LastResultsHash,
LastResultsHash: state.LastResultsHash,
} }
} }
// Equals returns true if the States are identical. // Equals returns true if the States are identical.
func (s State) Equals(s2 State) bool {
sbz, s2bz := s.Bytes(), s2.Bytes()
func (state State) Equals(state2 State) bool {
sbz, s2bz := state.Bytes(), state2.Bytes()
return bytes.Equal(sbz, s2bz) return bytes.Equal(sbz, s2bz)
} }
// Bytes serializes the State using go-amino. // Bytes serializes the State using go-amino.
func (s State) Bytes() []byte {
return cdc.MustMarshalBinaryBare(s)
func (state State) Bytes() []byte {
return cdc.MustMarshalBinaryBare(state)
} }
// IsEmpty returns true if the State is equal to the empty State. // IsEmpty returns true if the State is equal to the empty State.
func (s State) IsEmpty() bool {
return s.Validators == nil // XXX can't compare to Empty
func (state State) IsEmpty() bool {
return state.Validators == nil // XXX can't compare to Empty
} }
// GetValidators returns the last and current validator sets. // GetValidators returns the last and current validator sets.
func (s State) GetValidators() (last *types.ValidatorSet, current *types.ValidatorSet) {
return s.LastValidators, s.Validators
func (state State) GetValidators() (last *types.ValidatorSet, current *types.ValidatorSet) {
return state.LastValidators, state.Validators
} }
//------------------------------------------------------------------------ //------------------------------------------------------------------------
// Create a block from the latest state // Create a block from the latest state
// MakeBlock builds a block with the given txs and commit from the current state. // MakeBlock builds a block with the given txs and commit from the current state.
func (s State) MakeBlock(height int64, txs []types.Tx, commit *types.Commit) (*types.Block, *types.PartSet) {
func (state State) MakeBlock(height int64, txs []types.Tx, commit *types.Commit) (*types.Block, *types.PartSet) {
// build base block // build base block
block := types.MakeBlock(height, txs, commit) block := types.MakeBlock(height, txs, commit)
// fill header with state data // fill header with state data
block.ChainID = s.ChainID
block.TotalTxs = s.LastBlockTotalTx + block.NumTxs
block.LastBlockID = s.LastBlockID
block.ValidatorsHash = s.Validators.Hash()
block.AppHash = s.AppHash
block.ConsensusHash = s.ConsensusParams.Hash()
block.LastResultsHash = s.LastResultsHash
return block, block.MakePartSet(s.ConsensusParams.BlockGossip.BlockPartSizeBytes)
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)
} }
//------------------------------------------------------------------------ //------------------------------------------------------------------------


+ 7
- 7
state/store.go View File

@ -80,15 +80,15 @@ func loadState(db dbm.DB, key []byte) (state State) {
} }
// SaveState persists the State, the ValidatorsInfo, and the ConsensusParamsInfo to the database. // SaveState persists the State, the ValidatorsInfo, and the ConsensusParamsInfo to the database.
func SaveState(db dbm.DB, s State) {
saveState(db, s, stateKey)
func SaveState(db dbm.DB, state State) {
saveState(db, state, stateKey)
} }
func saveState(db dbm.DB, s State, key []byte) {
nextHeight := s.LastBlockHeight + 1
saveValidatorsInfo(db, nextHeight, s.LastHeightValidatorsChanged, s.Validators)
saveConsensusParamsInfo(db, nextHeight, s.LastHeightConsensusParamsChanged, s.ConsensusParams)
db.SetSync(stateKey, s.Bytes())
func saveState(db dbm.DB, state State, key []byte) {
nextHeight := state.LastBlockHeight + 1
saveValidatorsInfo(db, nextHeight, state.LastHeightValidatorsChanged, state.Validators)
saveConsensusParamsInfo(db, nextHeight, state.LastHeightConsensusParamsChanged, state.ConsensusParams)
db.SetSync(stateKey, state.Bytes())
} }
//------------------------------------------------------------------------ //------------------------------------------------------------------------


+ 26
- 26
state/validation.go View File

@ -12,18 +12,18 @@ import (
//----------------------------------------------------- //-----------------------------------------------------
// Validate block // Validate block
func validateBlock(stateDB dbm.DB, s State, b *types.Block) error {
func validateBlock(stateDB dbm.DB, state State, b *types.Block) error {
// validate internal consistency // validate internal consistency
if err := b.ValidateBasic(); err != nil { if err := b.ValidateBasic(); err != nil {
return err return err
} }
// validate basic info // validate basic info
if b.ChainID != s.ChainID {
return fmt.Errorf("Wrong Block.Header.ChainID. Expected %v, got %v", s.ChainID, b.ChainID)
if b.ChainID != state.ChainID {
return fmt.Errorf("Wrong Block.Header.ChainID. Expected %v, got %v", state.ChainID, b.ChainID)
} }
if b.Height != s.LastBlockHeight+1 {
return fmt.Errorf("Wrong Block.Header.Height. Expected %v, got %v", s.LastBlockHeight+1, b.Height)
if b.Height != state.LastBlockHeight+1 {
return fmt.Errorf("Wrong Block.Header.Height. Expected %v, got %v", state.LastBlockHeight+1, b.Height)
} }
/* TODO: Determine bounds for Time /* TODO: Determine bounds for Time
See blockchain/reactor "stopSyncingDurationMinutes" See blockchain/reactor "stopSyncingDurationMinutes"
@ -34,26 +34,26 @@ func validateBlock(stateDB dbm.DB, s State, b *types.Block) error {
*/ */
// validate prev block info // validate prev block info
if !b.LastBlockID.Equals(s.LastBlockID) {
return fmt.Errorf("Wrong Block.Header.LastBlockID. Expected %v, got %v", s.LastBlockID, b.LastBlockID)
if !b.LastBlockID.Equals(state.LastBlockID) {
return fmt.Errorf("Wrong Block.Header.LastBlockID. Expected %v, got %v", state.LastBlockID, b.LastBlockID)
} }
newTxs := int64(len(b.Data.Txs)) newTxs := int64(len(b.Data.Txs))
if b.TotalTxs != s.LastBlockTotalTx+newTxs {
return fmt.Errorf("Wrong Block.Header.TotalTxs. Expected %v, got %v", s.LastBlockTotalTx+newTxs, b.TotalTxs)
if b.TotalTxs != state.LastBlockTotalTx+newTxs {
return fmt.Errorf("Wrong Block.Header.TotalTxs. Expected %v, got %v", state.LastBlockTotalTx+newTxs, b.TotalTxs)
} }
// validate app info // validate app info
if !bytes.Equal(b.AppHash, s.AppHash) {
return fmt.Errorf("Wrong Block.Header.AppHash. Expected %X, got %v", s.AppHash, b.AppHash)
if !bytes.Equal(b.AppHash, state.AppHash) {
return fmt.Errorf("Wrong Block.Header.AppHash. Expected %X, got %v", state.AppHash, b.AppHash)
} }
if !bytes.Equal(b.ConsensusHash, s.ConsensusParams.Hash()) {
return fmt.Errorf("Wrong Block.Header.ConsensusHash. Expected %X, got %v", s.ConsensusParams.Hash(), b.ConsensusHash)
if !bytes.Equal(b.ConsensusHash, state.ConsensusParams.Hash()) {
return fmt.Errorf("Wrong Block.Header.ConsensusHash. Expected %X, got %v", state.ConsensusParams.Hash(), b.ConsensusHash)
} }
if !bytes.Equal(b.LastResultsHash, s.LastResultsHash) {
return fmt.Errorf("Wrong Block.Header.LastResultsHash. Expected %X, got %v", s.LastResultsHash, b.LastResultsHash)
if !bytes.Equal(b.LastResultsHash, state.LastResultsHash) {
return fmt.Errorf("Wrong Block.Header.LastResultsHash. Expected %X, got %v", state.LastResultsHash, b.LastResultsHash)
} }
if !bytes.Equal(b.ValidatorsHash, s.Validators.Hash()) {
return fmt.Errorf("Wrong Block.Header.ValidatorsHash. Expected %X, got %v", s.Validators.Hash(), b.ValidatorsHash)
if !bytes.Equal(b.ValidatorsHash, state.Validators.Hash()) {
return fmt.Errorf("Wrong Block.Header.ValidatorsHash. Expected %X, got %v", state.Validators.Hash(), b.ValidatorsHash)
} }
// Validate block LastCommit. // Validate block LastCommit.
@ -62,19 +62,19 @@ func validateBlock(stateDB dbm.DB, s State, b *types.Block) error {
return errors.New("Block at height 1 (first block) should have no LastCommit precommits") return errors.New("Block at height 1 (first block) should have no LastCommit precommits")
} }
} else { } else {
if len(b.LastCommit.Precommits) != s.LastValidators.Size() {
if len(b.LastCommit.Precommits) != state.LastValidators.Size() {
return fmt.Errorf("Invalid block commit size. Expected %v, got %v", return fmt.Errorf("Invalid block commit size. Expected %v, got %v",
s.LastValidators.Size(), len(b.LastCommit.Precommits))
state.LastValidators.Size(), len(b.LastCommit.Precommits))
} }
err := s.LastValidators.VerifyCommit(
s.ChainID, s.LastBlockID, b.Height-1, b.LastCommit)
err := state.LastValidators.VerifyCommit(
state.ChainID, state.LastBlockID, b.Height-1, b.LastCommit)
if err != nil { if err != nil {
return err return err
} }
} }
for _, ev := range b.Evidence.Evidence { for _, ev := range b.Evidence.Evidence {
if err := VerifyEvidence(stateDB, s, ev); err != nil {
if err := VerifyEvidence(stateDB, state, ev); err != nil {
return types.NewEvidenceInvalidErr(ev, err) return types.NewEvidenceInvalidErr(ev, err)
} }
} }
@ -87,17 +87,17 @@ func validateBlock(stateDB dbm.DB, s State, b *types.Block) error {
// VerifyEvidence verifies the evidence fully by checking it is internally // VerifyEvidence verifies the evidence fully by checking it is internally
// consistent and sufficiently recent. // consistent and sufficiently recent.
func VerifyEvidence(stateDB dbm.DB, s State, evidence types.Evidence) error {
height := s.LastBlockHeight
func VerifyEvidence(stateDB dbm.DB, state State, evidence types.Evidence) error {
height := state.LastBlockHeight
evidenceAge := height - evidence.Height() evidenceAge := height - evidence.Height()
maxAge := s.ConsensusParams.EvidenceParams.MaxAge
maxAge := state.ConsensusParams.EvidenceParams.MaxAge
if evidenceAge > maxAge { if evidenceAge > maxAge {
return fmt.Errorf("Evidence from height %d is too old. Min height is %d", return fmt.Errorf("Evidence from height %d is too old. Min height is %d",
evidence.Height(), height-maxAge) evidence.Height(), height-maxAge)
} }
if err := evidence.Verify(s.ChainID); err != nil {
if err := evidence.Verify(state.ChainID); err != nil {
return err return err
} }


Loading…
Cancel
Save