tendermint/state/validation.go

203 lines
6.0 KiB
Go
Raw Normal View History

2017-12-27 20:03:48 -05:00
package state
import (
"bytes"
"errors"
"fmt"
"github.com/tendermint/tendermint/crypto"
2018-07-01 22:36:49 -04:00
dbm "github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/types"
2017-12-27 20:03:48 -05:00
)
//-----------------------------------------------------
// Validate block
func validateBlock(evidencePool EvidencePool, stateDB dbm.DB, state State, block *types.Block) error {
// Validate internal consistency.
2018-06-04 13:33:22 -07:00
if err := block.ValidateBasic(); err != nil {
2017-12-27 20:03:48 -05:00
return err
}
// Validate basic info.
if block.Version != state.Version.Consensus {
return fmt.Errorf("Wrong Block.Header.Version. Expected %v, got %v",
state.Version.Consensus,
block.Version,
)
}
2018-06-04 13:33:22 -07:00
if block.ChainID != state.ChainID {
return fmt.Errorf("Wrong Block.Header.ChainID. Expected %v, got %v",
state.ChainID,
block.ChainID,
)
2017-12-27 20:03:48 -05:00
}
2018-06-04 13:33:22 -07:00
if block.Height != state.LastBlockHeight+1 {
return fmt.Errorf("Wrong Block.Header.Height. Expected %v, got %v",
state.LastBlockHeight+1,
block.Height,
)
2017-12-27 20:03:48 -05:00
}
// Validate prev block info.
2018-06-04 13:33:22 -07:00
if !block.LastBlockID.Equals(state.LastBlockID) {
return fmt.Errorf("Wrong Block.Header.LastBlockID. Expected %v, got %v",
state.LastBlockID,
block.LastBlockID,
)
2017-12-27 20:03:48 -05:00
}
2018-06-04 13:33:22 -07:00
newTxs := int64(len(block.Data.Txs))
if block.TotalTxs != state.LastBlockTotalTx+newTxs {
return fmt.Errorf("Wrong Block.Header.TotalTxs. Expected %v, got %v",
state.LastBlockTotalTx+newTxs,
block.TotalTxs,
)
2017-12-27 20:03:48 -05:00
}
// Validate app info
2018-06-04 13:33:22 -07:00
if !bytes.Equal(block.AppHash, state.AppHash) {
return fmt.Errorf("Wrong Block.Header.AppHash. Expected %X, got %v",
state.AppHash,
block.AppHash,
)
2017-12-27 20:03:48 -05:00
}
2018-06-04 13:33:22 -07:00
if !bytes.Equal(block.ConsensusHash, state.ConsensusParams.Hash()) {
return fmt.Errorf("Wrong Block.Header.ConsensusHash. Expected %X, got %v",
state.ConsensusParams.Hash(),
block.ConsensusHash,
)
2017-12-27 20:03:48 -05:00
}
2018-06-04 13:33:22 -07:00
if !bytes.Equal(block.LastResultsHash, state.LastResultsHash) {
return fmt.Errorf("Wrong Block.Header.LastResultsHash. Expected %X, got %v",
state.LastResultsHash,
block.LastResultsHash,
)
2017-12-27 20:03:48 -05:00
}
2018-06-04 13:33:22 -07:00
if !bytes.Equal(block.ValidatorsHash, state.Validators.Hash()) {
return fmt.Errorf("Wrong Block.Header.ValidatorsHash. Expected %X, got %v",
state.Validators.Hash(),
block.ValidatorsHash,
)
2017-12-27 20:03:48 -05:00
}
if !bytes.Equal(block.NextValidatorsHash, state.NextValidators.Hash()) {
return fmt.Errorf("Wrong Block.Header.NextValidatorsHash. Expected %X, got %v",
state.NextValidators.Hash(),
block.NextValidatorsHash,
)
}
2017-12-27 20:03:48 -05:00
// Validate block LastCommit.
2018-06-04 13:33:22 -07:00
if block.Height == 1 {
if len(block.LastCommit.Precommits) != 0 {
return errors.New("Block at height 1 can't have LastCommit precommits")
2017-12-27 20:03:48 -05:00
}
} else {
2018-06-04 13:33:22 -07:00
if len(block.LastCommit.Precommits) != state.LastValidators.Size() {
state: add more tests for block validation (#3674) * Expose priv validators for use in testing * Generalize block header validation test past height 1 * Remove ineffectual assignment * Remove redundant SaveState call * Reorder comment for clarity * Use the block executor ApplyBlock function instead of implementing a stripped-down version of it * Remove commented-out code * Remove unnecessary test The required tests already appear to be implemented (implicitly) through the TestValidateBlockHeader test. * Allow for catching of specific error types during TestValidateBlockCommit * Make return error testable * Clean up and add TestValidateBlockCommit code * Fix formatting * Extract function to create a new mock test app * Update comment for clarity * Fix comment * Add skeleton code for evidence-related test * Allow for addressing priv val by address * Generalize test beyond a single validator * Generalize TestValidateBlockEvidence past first height * Reorder code to clearly separate tests and utility code * Use a common constant for stop height for testing in state/validation_test.go * Refactor errors to resemble existing conventions * Fix formatting * Extract common helper functions Having the tests littered with helper functions makes them less easily readable imho, so I've pulled them out into a separate file. This also makes it easier to see what helper functions are available during testing, so we minimize the chance of duplication when writing new tests. * Remove unused parameter * Remove unused parameters * Add field keys * Remove unused height constant * Fix typo * Fix incorrect return error * Add field keys * Use separate package for tests This refactors all of the state package's tests into a state_test package, so as to keep any usage of the state package's internal methods explicit. Any internal methods/constants used by tests are now explicitly exported in state/export_test.go * Refactor: extract helper function to make, validate, execute and commit a block * Rename state function to makeState * Remove redundant constant for number of validators * Refactor mock evidence registration into TestMain * Remove extraneous nVals variable * Replace function-level TODOs with file-level TODO and explanation * Remove extraneous comment * Fix linting issues brought up by GolangCI (pulled in from latest merge from develop)
2019-06-21 17:29:29 -04:00
return types.NewErrInvalidCommitPrecommits(state.LastValidators.Size(), len(block.LastCommit.Precommits))
2017-12-27 20:03:48 -05:00
}
2018-06-04 13:31:57 -07:00
err := state.LastValidators.VerifyCommit(
2018-06-04 13:33:22 -07:00
state.ChainID, state.LastBlockID, block.Height-1, block.LastCommit)
2017-12-27 20:03:48 -05:00
if err != nil {
return err
}
}
// Validate block Time
if block.Height > 1 {
if !block.Time.After(state.LastBlockTime) {
return fmt.Errorf("Block time %v not greater than last block time %v",
block.Time,
state.LastBlockTime,
)
}
medianTime := MedianTime(block.LastCommit, state.LastValidators)
if !block.Time.Equal(medianTime) {
return fmt.Errorf("Invalid block time. Expected %v, got %v",
medianTime,
block.Time,
)
}
} else if block.Height == 1 {
genesisTime := state.LastBlockTime
if !block.Time.Equal(genesisTime) {
return fmt.Errorf("Block time %v is not equal to genesis time %v",
block.Time,
genesisTime,
)
}
}
// Limit the amount of evidence
maxNumEvidence, _ := types.MaxEvidencePerBlock(state.ConsensusParams.Block.MaxBytes)
numEvidence := int64(len(block.Evidence.Evidence))
if numEvidence > maxNumEvidence {
return types.NewErrEvidenceOverflow(maxNumEvidence, numEvidence)
}
// Validate all evidence.
2018-06-04 13:33:22 -07:00
for _, ev := range block.Evidence.Evidence {
2018-06-04 13:31:57 -07:00
if err := VerifyEvidence(stateDB, state, ev); err != nil {
return types.NewErrEvidenceInvalid(ev, err)
2017-12-27 20:03:48 -05:00
}
if evidencePool != nil && evidencePool.IsCommitted(ev) {
return types.NewErrEvidenceInvalid(ev, errors.New("evidence was already committed"))
}
2017-12-27 20:03:48 -05:00
}
// NOTE: We can't actually verify it's the right proposer because we dont
// know what round the block was first proposed. So just check that it's
// a legit address and a known validator.
if len(block.ProposerAddress) != crypto.AddressSize ||
!state.Validators.HasAddress(block.ProposerAddress) {
return fmt.Errorf("Block.Header.ProposerAddress, %X, is not a validator",
block.ProposerAddress,
)
}
2017-12-27 20:03:48 -05:00
return nil
}
// VerifyEvidence verifies the evidence fully by checking:
// - it is sufficiently recent (MaxAge)
// - it is from a key who was a validator at the given height
// - it is internally consistent
// - it was properly signed by the alleged equivocator
2018-06-04 13:31:57 -07:00
func VerifyEvidence(stateDB dbm.DB, state State, evidence types.Evidence) error {
height := state.LastBlockHeight
2017-12-27 20:03:48 -05:00
evidenceAge := height - evidence.Height()
maxAge := state.ConsensusParams.Evidence.MaxAge
2017-12-27 20:03:48 -05:00
if evidenceAge > maxAge {
return fmt.Errorf("Evidence from height %d is too old. Min height is %d",
evidence.Height(), height-maxAge)
}
2017-12-28 21:08:39 -05:00
valset, err := LoadValidators(stateDB, evidence.Height())
if err != nil {
// TODO: if err is just that we cant find it cuz we pruned, ignore.
// TODO: if its actually bad evidence, punish peer
return err
}
// The address must have been an active validator at the height.
// NOTE: we will ignore evidence from H if the key was not a validator
// at H, even if it is a validator at some nearby H'
// XXX: this makes lite-client bisection as is unsafe
// See https://github.com/tendermint/tendermint/issues/3244
2017-12-27 20:03:48 -05:00
ev := evidence
height, addr := ev.Height(), ev.Address()
_, val := valset.GetByAddress(addr)
2017-12-27 20:03:48 -05:00
if val == nil {
2017-12-28 21:08:39 -05:00
return fmt.Errorf("Address %X was not a validator at height %d", addr, height)
}
2018-06-05 22:14:37 -07:00
if err := evidence.Verify(state.ChainID, val.PubKey); err != nil {
return err
2017-12-27 20:03:48 -05:00
}
2017-12-28 21:08:39 -05:00
return nil
2017-12-27 20:03:48 -05:00
}