mirror of
https://github.com/tendermint/tendermint.git
synced 2026-01-03 11:45:18 +00:00
* [cherry-picked] abci++: Propagate vote extensions (RFC 017) (#8433) * Add protos for ExtendedCommit Cherry-pick from e73f0178b72a16ee81f8e856aadf651f2c62ec6e just the changes to the .proto files, since we have deleted the .intermediate files. Signed-off-by: Thane Thomson <connect@thanethomson.com> * make proto-gen Signed-off-by: Thane Thomson <connect@thanethomson.com> * BlockStore holds extended commit Cherry-pick 8d504d4b50ec6afbdffe2df7ababbef30e15053d and fix conflicts. Signed-off-by: Thane Thomson <connect@thanethomson.com> * Reshuffle ExtendedCommit and ExtendedCommitSig Separate the data structures and functions from their Commit-oriented counterparts to adhere to the current coding style. Signed-off-by: Thane Thomson <connect@thanethomson.com> * Fix exit condition in blocksync * Add note to remove TxResult proto As Sergio pointed out in 3e31aa6f583cdc71e208ed03a82f1d804ec0de49, this proto message can probably be removed. We should do this in a separate PR. Signed-off-by: Thane Thomson <connect@thanethomson.com> * Lift termination condition into for loop Signed-off-by: Thane Thomson <connect@thanethomson.com> * Enforce vote extension signature requirement Signed-off-by: Thane Thomson <connect@thanethomson.com> * Expand on comment for PeekTwoBlocks for posterity Signed-off-by: Thane Thomson <connect@thanethomson.com> * Isolate TODO more clearly Signed-off-by: Thane Thomson <connect@thanethomson.com> * make mockery Signed-off-by: Thane Thomson <connect@thanethomson.com> * Fix comment Signed-off-by: Thane Thomson <connect@thanethomson.com> * Make panic output from BlockStore.SaveBlock more readable Signed-off-by: Thane Thomson <connect@thanethomson.com> * Add helper methods to ExtendedCommitSig and ExtendedCommit Signed-off-by: Thane Thomson <connect@thanethomson.com> * Fix most tests except TestHandshake* Signed-off-by: Thane Thomson <connect@thanethomson.com> * Fix store prefix collision Signed-off-by: Thane Thomson <connect@thanethomson.com> * Fix TestBlockFetchAtHeight Signed-off-by: Thane Thomson <connect@thanethomson.com> * Remove global state from store tests Signed-off-by: Thane Thomson <connect@thanethomson.com> * Apply suggestions from code review Co-authored-by: M. J. Fromberger <fromberger@interchain.io> Co-authored-by: Sergio Mena <sergio@informal.systems> * blocksync: Just return error Signed-off-by: Thane Thomson <connect@thanethomson.com> * make format Signed-off-by: Thane Thomson <connect@thanethomson.com> * types: Remove unused/commented-out code Signed-off-by: Thane Thomson <connect@thanethomson.com> * blocksync: Change pool AddBlock function signature to return errors Signed-off-by: Thane Thomson <connect@thanethomson.com> * types: Improve legibility of switch statements Signed-off-by: Thane Thomson <connect@thanethomson.com> * blocksync: Expand on extended commit requirement in AddBlock description Signed-off-by: Thane Thomson <connect@thanethomson.com> * blocksync: Return error without also logging it Signed-off-by: Thane Thomson <connect@thanethomson.com> * consensus: Rename short-lived local variable Signed-off-by: Thane Thomson <connect@thanethomson.com> * consensus: Allocate TODO to Sergio Signed-off-by: Thane Thomson <connect@thanethomson.com> * evidence/pool_test: Inline slice construction Signed-off-by: Thane Thomson <connect@thanethomson.com> * state: Rename LoadBlockExtCommit to LoadBlockExtendedCommit Signed-off-by: Thane Thomson <connect@thanethomson.com> * proto: Remove TODO on TxResult Signed-off-by: Thane Thomson <connect@thanethomson.com> * types: Minor format Signed-off-by: Thane Thomson <connect@thanethomson.com> * types: Reformat ExtendedCommitSig.BlockID Signed-off-by: Thane Thomson <connect@thanethomson.com> * types: Remove NewExtendedCommit constructor Signed-off-by: Thane Thomson <connect@thanethomson.com> * types: Remove NewCommit constructor Signed-off-by: Thane Thomson <connect@thanethomson.com> * types: Shorten receiver names for ExtendedCommit Signed-off-by: Thane Thomson <connect@thanethomson.com> * types: Convert ExtendedCommit.Copy to a deep clone Signed-off-by: Thane Thomson <connect@thanethomson.com> * types: Assign TODO to Sergio Signed-off-by: Thane Thomson <connect@thanethomson.com> * types: Fix legibility nits Signed-off-by: Thane Thomson <connect@thanethomson.com> * types: Improve legibility Signed-off-by: Thane Thomson <connect@thanethomson.com> * store/state: Add TODO to move prefixes to common package Signed-off-by: Thane Thomson <connect@thanethomson.com> * Propagate validator info to PrepareProposal In order to propagate validator voting power through to PrepareProposal, we need to load the validator set info from the height corresponding to the extended commit that we're passing through to PrepareProposal as the "LocalLastCommit". Signed-off-by: Thane Thomson <connect@thanethomson.com> * Rename local var for clarity Signed-off-by: Thane Thomson <connect@thanethomson.com> * Fix TestMaxProposalBlockSize Signed-off-by: Thane Thomson <connect@thanethomson.com> * Rename local var for clarity Signed-off-by: Thane Thomson <connect@thanethomson.com> * Remove debug log Signed-off-by: Thane Thomson <connect@thanethomson.com> * Remove CommigSig.ForBlock helper Signed-off-by: Thane Thomson <connect@thanethomson.com> * Remove CommigSig.Absent helper Signed-off-by: Thane Thomson <connect@thanethomson.com> * Remove ExtendedCommitSig.ForBlock helper Signed-off-by: Thane Thomson <connect@thanethomson.com> * Remove ExtendedCommitSig.Absent helper Signed-off-by: Thane Thomson <connect@thanethomson.com> * There are no extended commits below the initial height Signed-off-by: Thane Thomson <connect@thanethomson.com> * Fix comment grammar Signed-off-by: Thane Thomson <connect@thanethomson.com> * Remove JSON encoding from ExtendedCommit Signed-off-by: Thane Thomson <connect@thanethomson.com> * Embed CommitSig into ExtendedCommitSig instead of duplicating fields Signed-off-by: Thane Thomson <connect@thanethomson.com> * Rename ExtendedCommit vote_extension field to extension for consistency with domain types Signed-off-by: Thane Thomson <connect@thanethomson.com> * blocksync: Panic if we peek a block without an extended commit Signed-off-by: Thane Thomson <connect@thanethomson.com> * Apply suggestions from code review Co-authored-by: M. J. Fromberger <fromberger@interchain.io> * Remove Sergio from TODO Signed-off-by: Thane Thomson <connect@thanethomson.com> * Increase hard-coded vote extension max size to 1MB Signed-off-by: Thane Thomson <connect@thanethomson.com> * state: Remove unnecessary comment Signed-off-by: Thane Thomson <connect@thanethomson.com> * state: Ensure no of commit sigs equals validator set length Signed-off-by: Thane Thomson <connect@thanethomson.com> * make format Signed-off-by: Thane Thomson <connect@thanethomson.com> * types: Minor legibility improvements Signed-off-by: Thane Thomson <connect@thanethomson.com> * Improve legibility Signed-off-by: Thane Thomson <connect@thanethomson.com> * types: Remove unused GetVotes function on VoteSet Signed-off-by: Thane Thomson <connect@thanethomson.com> * Refactor TestMaxProposalBlockSize to construct more realistic extended commit Signed-off-by: Thane Thomson <connect@thanethomson.com> * Refactor buildExtendedCommitInfo to resemble buildLastCommitInfo Signed-off-by: Thane Thomson <connect@thanethomson.com> * Apply suggestions from code review Co-authored-by: M. J. Fromberger <fromberger@interchain.io> * abci++: Disable VerifyVoteExtension call on nil precommits (#8491) Signed-off-by: Thane Thomson <connect@thanethomson.com> * types: Require vote extensions on non-nil precommits and not otherwise Signed-off-by: Thane Thomson <connect@thanethomson.com> * Disable lint Signed-off-by: Thane Thomson <connect@thanethomson.com> * Increase timeout for TestReactorVotingPowerChange to counter flakiness Signed-off-by: Thane Thomson <connect@thanethomson.com> * Only sign and verify vote extensions in non-nil precommits Signed-off-by: Thane Thomson <connect@thanethomson.com> * Revert "Disable lint" This reverts commit6fffbf9402. Signed-off-by: Thane Thomson <connect@thanethomson.com> * Add missing non-nil check uncovered non-deterministically in TestHandshakeReplayAll Signed-off-by: Thane Thomson <connect@thanethomson.com> * Expand error message for accuracy Signed-off-by: Thane Thomson <connect@thanethomson.com> * Only call ExtendVote when we make non-nil precommits Signed-off-by: Thane Thomson <connect@thanethomson.com> * Revert "Increase timeout for TestReactorVotingPowerChange to counter flakiness" This reverts commitaf514939db. Signed-off-by: Thane Thomson <connect@thanethomson.com> * Refactor ValidateBasic for ExtendedCommitSig for legibility Signed-off-by: Thane Thomson <connect@thanethomson.com> Co-authored-by: Sergio Mena <sergio@informal.systems> Co-authored-by: M. J. Fromberger <fromberger@interchain.io> * make proto-gen * cp-fix * monkey-see-monkey-do-fixes * Fix tests (build) * Fix forgotten tests * fix_ut * Fix units tests * Fixed TestReactorInvalidPrecommit * Fix TestFinalizeBlockCalled * Fix all UTs * Add missing comment Co-authored-by: Thane Thomson <connect@thanethomson.com> Co-authored-by: M. J. Fromberger <fromberger@interchain.io>
356 lines
11 KiB
Go
356 lines
11 KiB
Go
package state
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/cosmos/gogoproto/proto"
|
|
|
|
tmstate "github.com/tendermint/tendermint/proto/tendermint/state"
|
|
tmversion "github.com/tendermint/tendermint/proto/tendermint/version"
|
|
"github.com/tendermint/tendermint/types"
|
|
tmtime "github.com/tendermint/tendermint/types/time"
|
|
"github.com/tendermint/tendermint/version"
|
|
)
|
|
|
|
// database keys
|
|
var (
|
|
stateKey = []byte("stateKey")
|
|
)
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// InitStateVersion sets the Consensus.Block and Software versions,
|
|
// but leaves the Consensus.App version blank.
|
|
// The Consensus.App version will be set during the Handshake, once
|
|
// we hear from the app what protocol version it is running.
|
|
var InitStateVersion = tmstate.Version{
|
|
Consensus: tmversion.Consensus{
|
|
Block: version.BlockProtocol,
|
|
App: 0,
|
|
},
|
|
Software: version.TMCoreSemVer,
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// 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,
|
|
// but none of them should be mutated directly.
|
|
// Instead, use state.Copy() or state.NextState(...).
|
|
// NOTE: not goroutine-safe.
|
|
type State struct {
|
|
Version tmstate.Version
|
|
|
|
// immutable
|
|
ChainID string
|
|
InitialHeight int64 // should be 1, not 0, when starting from height 1
|
|
|
|
// LastBlockHeight=0 at genesis (ie. block(H=0) does not exist)
|
|
LastBlockHeight int64
|
|
LastBlockID types.BlockID
|
|
LastBlockTime time.Time
|
|
|
|
// 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,
|
|
// we set s.LastHeightValidatorsChanged = s.LastBlockHeight + 1 + 1
|
|
// Extra +1 due to nextValSet delay.
|
|
NextValidators *types.ValidatorSet
|
|
Validators *types.ValidatorSet
|
|
LastValidators *types.ValidatorSet
|
|
LastHeightValidatorsChanged int64
|
|
|
|
// Consensus parameters used for validating blocks.
|
|
// Changes returned by FinalizeBlock and updated after Commit.
|
|
ConsensusParams types.ConsensusParams
|
|
LastHeightConsensusParamsChanged int64
|
|
|
|
// Merkle root of the results from executing prev block
|
|
LastResultsHash []byte
|
|
|
|
// the latest AppHash we've received from calling abci.Commit()
|
|
AppHash []byte
|
|
}
|
|
|
|
// Copy makes a copy of the State for mutating.
|
|
func (state State) Copy() State {
|
|
|
|
return State{
|
|
Version: state.Version,
|
|
ChainID: state.ChainID,
|
|
InitialHeight: state.InitialHeight,
|
|
|
|
LastBlockHeight: state.LastBlockHeight,
|
|
LastBlockID: state.LastBlockID,
|
|
LastBlockTime: state.LastBlockTime,
|
|
|
|
NextValidators: state.NextValidators.Copy(),
|
|
Validators: state.Validators.Copy(),
|
|
LastValidators: state.LastValidators.Copy(),
|
|
LastHeightValidatorsChanged: state.LastHeightValidatorsChanged,
|
|
|
|
ConsensusParams: state.ConsensusParams,
|
|
LastHeightConsensusParamsChanged: state.LastHeightConsensusParamsChanged,
|
|
|
|
AppHash: state.AppHash,
|
|
|
|
LastResultsHash: state.LastResultsHash,
|
|
}
|
|
}
|
|
|
|
// Equals returns true if the States are identical.
|
|
func (state State) Equals(state2 State) bool {
|
|
sbz, s2bz := state.Bytes(), state2.Bytes()
|
|
return bytes.Equal(sbz, s2bz)
|
|
}
|
|
|
|
// Bytes serializes the State using protobuf.
|
|
// It panics if either casting to protobuf or serialization fails.
|
|
func (state State) Bytes() []byte {
|
|
sm, err := state.ToProto()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
bz, err := proto.Marshal(sm)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return bz
|
|
}
|
|
|
|
// IsEmpty returns true if the State is equal to the empty State.
|
|
func (state State) IsEmpty() bool {
|
|
return state.Validators == nil // XXX can't compare to Empty
|
|
}
|
|
|
|
// ToProto takes the local state type and returns the equivalent proto type
|
|
func (state *State) ToProto() (*tmstate.State, error) {
|
|
if state == nil {
|
|
return nil, errors.New("state is nil")
|
|
}
|
|
|
|
sm := new(tmstate.State)
|
|
|
|
sm.Version = state.Version
|
|
sm.ChainID = state.ChainID
|
|
sm.InitialHeight = state.InitialHeight
|
|
sm.LastBlockHeight = state.LastBlockHeight
|
|
|
|
sm.LastBlockID = state.LastBlockID.ToProto()
|
|
sm.LastBlockTime = state.LastBlockTime
|
|
vals, err := state.Validators.ToProto()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
sm.Validators = vals
|
|
|
|
nVals, err := state.NextValidators.ToProto()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
sm.NextValidators = nVals
|
|
|
|
if state.LastBlockHeight >= 1 { // At Block 1 LastValidators is nil
|
|
lVals, err := state.LastValidators.ToProto()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
sm.LastValidators = lVals
|
|
}
|
|
|
|
sm.LastHeightValidatorsChanged = state.LastHeightValidatorsChanged
|
|
sm.ConsensusParams = state.ConsensusParams.ToProto()
|
|
sm.LastHeightConsensusParamsChanged = state.LastHeightConsensusParamsChanged
|
|
sm.LastResultsHash = state.LastResultsHash
|
|
sm.AppHash = state.AppHash
|
|
|
|
return sm, nil
|
|
}
|
|
|
|
// FromProto takes a state proto message & returns the local state type
|
|
func FromProto(pb *tmstate.State) (*State, error) { //nolint:golint
|
|
if pb == nil {
|
|
return nil, errors.New("nil State")
|
|
}
|
|
|
|
state := new(State)
|
|
|
|
state.Version = pb.Version
|
|
state.ChainID = pb.ChainID
|
|
state.InitialHeight = pb.InitialHeight
|
|
|
|
bi, err := types.BlockIDFromProto(&pb.LastBlockID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
state.LastBlockID = *bi
|
|
state.LastBlockHeight = pb.LastBlockHeight
|
|
state.LastBlockTime = pb.LastBlockTime
|
|
|
|
vals, err := types.ValidatorSetFromProto(pb.Validators)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
state.Validators = vals
|
|
|
|
nVals, err := types.ValidatorSetFromProto(pb.NextValidators)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
state.NextValidators = nVals
|
|
|
|
if state.LastBlockHeight >= 1 { // At Block 1 LastValidators is nil
|
|
lVals, err := types.ValidatorSetFromProto(pb.LastValidators)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
state.LastValidators = lVals
|
|
} else {
|
|
state.LastValidators = types.NewValidatorSet(nil)
|
|
}
|
|
|
|
state.LastHeightValidatorsChanged = pb.LastHeightValidatorsChanged
|
|
state.ConsensusParams = types.ConsensusParamsFromProto(pb.ConsensusParams)
|
|
state.LastHeightConsensusParamsChanged = pb.LastHeightConsensusParamsChanged
|
|
state.LastResultsHash = pb.LastResultsHash
|
|
state.AppHash = pb.AppHash
|
|
|
|
return state, nil
|
|
}
|
|
|
|
//------------------------------------------------------------------------
|
|
// Create a block from the latest state
|
|
|
|
// MakeBlock builds a block from the current state with the given txs, commit,
|
|
// and evidence. Note it also takes a proposerAddress because the state does not
|
|
// track rounds, and hence does not know the correct proposer. TODO: fix this!
|
|
func (state State) MakeBlock(
|
|
height int64,
|
|
txs []types.Tx,
|
|
lastCommit *types.Commit,
|
|
evidence []types.Evidence,
|
|
proposerAddress []byte,
|
|
) *types.Block {
|
|
|
|
// Build base block with block data.
|
|
block := types.MakeBlock(height, txs, lastCommit, evidence)
|
|
|
|
// Set time.
|
|
var timestamp time.Time
|
|
if height == state.InitialHeight {
|
|
timestamp = state.LastBlockTime // genesis time
|
|
} else {
|
|
timestamp = MedianTime(lastCommit, state.LastValidators)
|
|
}
|
|
|
|
// Fill rest of header with state data.
|
|
block.Header.Populate(
|
|
state.Version.Consensus, state.ChainID,
|
|
timestamp, state.LastBlockID,
|
|
state.Validators.Hash(), state.NextValidators.Hash(),
|
|
state.ConsensusParams.Hash(), state.AppHash, state.LastResultsHash,
|
|
proposerAddress,
|
|
)
|
|
|
|
return block
|
|
}
|
|
|
|
// MedianTime computes a median time for a given Commit (based on Timestamp field of votes messages) and the
|
|
// corresponding validator set. The computed time is always between timestamps of
|
|
// the votes sent by honest processes, i.e., a faulty processes can not arbitrarily increase or decrease the
|
|
// computed value.
|
|
func MedianTime(commit *types.Commit, validators *types.ValidatorSet) time.Time {
|
|
weightedTimes := make([]*tmtime.WeightedTime, len(commit.Signatures))
|
|
totalVotingPower := int64(0)
|
|
|
|
for i, commitSig := range commit.Signatures {
|
|
if commitSig.BlockIDFlag == types.BlockIDFlagAbsent {
|
|
continue
|
|
}
|
|
_, validator := validators.GetByAddress(commitSig.ValidatorAddress)
|
|
// If there's no condition, TestValidateBlockCommit panics; not needed normally.
|
|
if validator != nil {
|
|
totalVotingPower += validator.VotingPower
|
|
weightedTimes[i] = tmtime.NewWeightedTime(commitSig.Timestamp, validator.VotingPower)
|
|
}
|
|
}
|
|
|
|
return tmtime.WeightedMedian(weightedTimes, totalVotingPower)
|
|
}
|
|
|
|
//------------------------------------------------------------------------
|
|
// Genesis
|
|
|
|
// MakeGenesisStateFromFile reads and unmarshals state from the given
|
|
// file.
|
|
//
|
|
// Used during replay and in tests.
|
|
func MakeGenesisStateFromFile(genDocFile string) (State, error) {
|
|
genDoc, err := MakeGenesisDocFromFile(genDocFile)
|
|
if err != nil {
|
|
return State{}, err
|
|
}
|
|
return MakeGenesisState(genDoc)
|
|
}
|
|
|
|
// MakeGenesisDocFromFile reads and unmarshals genesis doc from the given file.
|
|
func MakeGenesisDocFromFile(genDocFile string) (*types.GenesisDoc, error) {
|
|
genDocJSON, err := os.ReadFile(genDocFile)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("couldn't read GenesisDoc file: %v", err)
|
|
}
|
|
genDoc, err := types.GenesisDocFromJSON(genDocJSON)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error reading GenesisDoc: %v", err)
|
|
}
|
|
return genDoc, nil
|
|
}
|
|
|
|
// MakeGenesisState creates state from types.GenesisDoc.
|
|
func MakeGenesisState(genDoc *types.GenesisDoc) (State, error) {
|
|
err := genDoc.ValidateAndComplete()
|
|
if err != nil {
|
|
return State{}, fmt.Errorf("error in genesis doc: %w", err)
|
|
}
|
|
|
|
var validatorSet, nextValidatorSet *types.ValidatorSet
|
|
if genDoc.Validators == nil {
|
|
validatorSet = types.NewValidatorSet(nil)
|
|
nextValidatorSet = types.NewValidatorSet(nil)
|
|
} else {
|
|
validators := make([]*types.Validator, len(genDoc.Validators))
|
|
for i, val := range genDoc.Validators {
|
|
validators[i] = types.NewValidator(val.PubKey, val.Power)
|
|
}
|
|
validatorSet = types.NewValidatorSet(validators)
|
|
nextValidatorSet = types.NewValidatorSet(validators).CopyIncrementProposerPriority(1)
|
|
}
|
|
|
|
return State{
|
|
Version: InitStateVersion,
|
|
ChainID: genDoc.ChainID,
|
|
InitialHeight: genDoc.InitialHeight,
|
|
|
|
LastBlockHeight: 0,
|
|
LastBlockID: types.BlockID{},
|
|
LastBlockTime: genDoc.GenesisTime,
|
|
|
|
NextValidators: nextValidatorSet,
|
|
Validators: validatorSet,
|
|
LastValidators: types.NewValidatorSet(nil),
|
|
LastHeightValidatorsChanged: genDoc.InitialHeight,
|
|
|
|
ConsensusParams: *genDoc.ConsensusParams,
|
|
LastHeightConsensusParamsChanged: genDoc.InitialHeight,
|
|
|
|
AppHash: genDoc.AppHash,
|
|
}, nil
|
|
}
|