From 58c3e590b48924d6361770267b16b62938a7d8e3 Mon Sep 17 00:00:00 2001 From: Marko Date: Thu, 25 Jul 2019 10:13:19 +0200 Subject: [PATCH] types: move MakeVote / MakeBlock functions (#3819) to the types package Paritally Fixes #3584 --- blockchain/v0/reactor_test.go | 26 ++++++----------------- consensus/replay_test.go | 24 +++------------------ state/helpers_test.go | 20 +----------------- state/validation_test.go | 4 ++-- types/block.go | 19 ----------------- types/test_util.go | 40 +++++++++++++++++++++++++++++++++-- 6 files changed, 50 insertions(+), 83 deletions(-) diff --git a/blockchain/v0/reactor_test.go b/blockchain/v0/reactor_test.go index 29de5b193..08ec66cda 100644 --- a/blockchain/v0/reactor_test.go +++ b/blockchain/v0/reactor_test.go @@ -45,24 +45,6 @@ func randGenesisDoc(numValidators int, randPower bool, minPower int64) (*types.G }, privValidators } -func makeVote(header *types.Header, blockID types.BlockID, valset *types.ValidatorSet, privVal types.PrivValidator) *types.Vote { - addr := privVal.GetPubKey().Address() - idx, _ := valset.GetByAddress(addr) - vote := &types.Vote{ - ValidatorAddress: addr, - ValidatorIndex: idx, - Height: header.Height, - Round: 1, - Timestamp: tmtime.Now(), - Type: types.PrecommitType, - BlockID: blockID, - } - - privVal.SignVote(header.ChainID, vote) - - return vote -} - type BlockchainReactorPair struct { reactor *BlockchainReactor app proxy.AppConns @@ -106,8 +88,12 @@ func newBlockchainReactor(logger log.Logger, genDoc *types.GenesisDoc, privVals lastBlockMeta := blockStore.LoadBlockMeta(blockHeight - 1) lastBlock := blockStore.LoadBlock(blockHeight - 1) - vote := makeVote(&lastBlock.Header, lastBlockMeta.BlockID, state.Validators, privVals[0]).CommitSig() - lastCommit = types.NewCommit(lastBlockMeta.BlockID, []*types.CommitSig{vote}) + vote, err := types.MakeVote(lastBlock.Header.Height, lastBlockMeta.BlockID, state.Validators, privVals[0], lastBlock.Header.ChainID) + if err != nil { + panic(err) + } + voteCommitSig := vote.CommitSig() + lastCommit = types.NewCommit(lastBlockMeta.BlockID, []*types.CommitSig{voteCommitSig}) } thisBlock := makeBlock(blockHeight, state, lastCommit) diff --git a/consensus/replay_test.go b/consensus/replay_test.go index 3a0f9024a..c3ded97cb 100644 --- a/consensus/replay_test.go +++ b/consensus/replay_test.go @@ -28,7 +28,6 @@ import ( "github.com/tendermint/tendermint/proxy" sm "github.com/tendermint/tendermint/state" "github.com/tendermint/tendermint/types" - tmtime "github.com/tendermint/tendermint/types/time" "github.com/tendermint/tendermint/version" dbm "github.com/tendermint/tm-cmn/db" ) @@ -849,31 +848,14 @@ func makeBlocks(n int, state *sm.State, privVal types.PrivValidator) []*types.Bl return blocks } -func makeVote(header *types.Header, blockID types.BlockID, valset *types.ValidatorSet, privVal types.PrivValidator) *types.Vote { - addr := privVal.GetPubKey().Address() - idx, _ := valset.GetByAddress(addr) - vote := &types.Vote{ - ValidatorAddress: addr, - ValidatorIndex: idx, - Height: header.Height, - Round: 1, - Timestamp: tmtime.Now(), - Type: types.PrecommitType, - BlockID: blockID, - } - - privVal.SignVote(header.ChainID, vote) - - return vote -} - func makeBlock(state sm.State, lastBlock *types.Block, lastBlockMeta *types.BlockMeta, privVal types.PrivValidator, height int64) (*types.Block, *types.PartSet) { lastCommit := types.NewCommit(types.BlockID{}, nil) if height > 1 { - vote := makeVote(&lastBlock.Header, lastBlockMeta.BlockID, state.Validators, privVal).CommitSig() - lastCommit = types.NewCommit(lastBlockMeta.BlockID, []*types.CommitSig{vote}) + vote, _ := types.MakeVote(lastBlock.Header.Height, lastBlockMeta.BlockID, state.Validators, privVal, lastBlock.Header.ChainID) + voteCommitSig := vote.CommitSig() + lastCommit = types.NewCommit(lastBlockMeta.BlockID, []*types.CommitSig{voteCommitSig}) } return state.MakeBlock(height, []types.Tx{}, lastCommit, nil, state.Validators.GetProposer().Address) diff --git a/state/helpers_test.go b/state/helpers_test.go index bd2a4e5ec..7e26f039b 100644 --- a/state/helpers_test.go +++ b/state/helpers_test.go @@ -69,29 +69,11 @@ func makeAndApplyGoodBlock(state sm.State, height int64, lastCommit *types.Commi return state, blockID, nil } -func makeVote(height int64, blockID types.BlockID, valSet *types.ValidatorSet, privVal types.PrivValidator) (*types.Vote, error) { - addr := privVal.GetPubKey().Address() - idx, _ := valSet.GetByAddress(addr) - vote := &types.Vote{ - ValidatorAddress: addr, - ValidatorIndex: idx, - Height: height, - Round: 0, - Timestamp: tmtime.Now(), - Type: types.PrecommitType, - BlockID: blockID, - } - if err := privVal.SignVote(chainID, vote); err != nil { - return nil, err - } - return vote, nil -} - func makeValidCommit(height int64, blockID types.BlockID, vals *types.ValidatorSet, privVals map[string]types.PrivValidator) (*types.Commit, error) { sigs := make([]*types.CommitSig, 0) for i := 0; i < vals.Size(); i++ { _, val := vals.GetByIndex(i) - vote, err := makeVote(height, blockID, vals, privVals[val.Address.String()]) + vote, err := types.MakeVote(height, blockID, vals, privVals[val.Address.String()], chainID) if err != nil { return nil, err } diff --git a/state/validation_test.go b/state/validation_test.go index c53cf0102..c0dd6e569 100644 --- a/state/validation_test.go +++ b/state/validation_test.go @@ -101,7 +101,7 @@ func TestValidateBlockCommit(t *testing.T) { #2589: ensure state.LastValidators.VerifyCommit fails here */ // should be height-1 instead of height - wrongHeightVote, err := makeVote(height, state.LastBlockID, state.Validators, privVals[proposerAddr.String()]) + wrongHeightVote, err := types.MakeVote(height, state.LastBlockID, state.Validators, privVals[proposerAddr.String()], chainID) require.NoError(t, err, "height %d", height) wrongHeightCommit := types.NewCommit(state.LastBlockID, []*types.CommitSig{wrongHeightVote.CommitSig()}) block, _ := state.MakeBlock(height, makeTxs(height), wrongHeightCommit, nil, proposerAddr) @@ -129,7 +129,7 @@ func TestValidateBlockCommit(t *testing.T) { /* wrongPrecommitsCommit is fine except for the extra bad precommit */ - goodVote, err := makeVote(height, blockID, state.Validators, privVals[proposerAddr.String()]) + goodVote, err := types.MakeVote(height, blockID, state.Validators, privVals[proposerAddr.String()], chainID) require.NoError(t, err, "height %d", height) badVote := &types.Vote{ ValidatorAddress: badPrivVal.GetPubKey().Address(), diff --git a/types/block.go b/types/block.go index 55709ad60..5dc0ff6a7 100644 --- a/types/block.go +++ b/types/block.go @@ -41,25 +41,6 @@ type Block struct { LastCommit *Commit `json:"last_commit"` } -// MakeBlock returns a new block with an empty header, except what can be -// computed from itself. -// It populates the same set of fields validated by ValidateBasic. -func MakeBlock(height int64, txs []Tx, lastCommit *Commit, evidence []Evidence) *Block { - block := &Block{ - Header: Header{ - Height: height, - NumTxs: int64(len(txs)), - }, - Data: Data{ - Txs: txs, - }, - Evidence: EvidenceData{Evidence: evidence}, - LastCommit: lastCommit, - } - block.fillHeader() - return block -} - // ValidateBasic performs basic validation that doesn't involve state data. // It checks the internal consistency of the block. // Further validation is done using state#ValidateBlock. diff --git a/types/test_util.go b/types/test_util.go index 18e472148..d226fd99e 100644 --- a/types/test_util.go +++ b/types/test_util.go @@ -5,8 +5,7 @@ import ( ) func MakeCommit(blockID BlockID, height int64, round int, - voteSet *VoteSet, - validators []PrivValidator) (*Commit, error) { + voteSet *VoteSet, validators []PrivValidator) (*Commit, error) { // all sign for i := 0; i < len(validators); i++ { @@ -37,3 +36,40 @@ func signAddVote(privVal PrivValidator, vote *Vote, voteSet *VoteSet) (signed bo } return voteSet.AddVote(vote) } + +func MakeVote(height int64, blockID BlockID, valSet *ValidatorSet, privVal PrivValidator, chainID string) (*Vote, error) { + addr := privVal.GetPubKey().Address() + idx, _ := valSet.GetByAddress(addr) + vote := &Vote{ + ValidatorAddress: addr, + ValidatorIndex: idx, + Height: height, + Round: 0, + Timestamp: tmtime.Now(), + Type: PrecommitType, + BlockID: blockID, + } + if err := privVal.SignVote(chainID, vote); err != nil { + return nil, err + } + return vote, nil +} + +// MakeBlock returns a new block with an empty header, except what can be +// computed from itself. +// It populates the same set of fields validated by ValidateBasic. +func MakeBlock(height int64, txs []Tx, lastCommit *Commit, evidence []Evidence) *Block { + block := &Block{ + Header: Header{ + Height: height, + NumTxs: int64(len(txs)), + }, + Data: Data{ + Txs: txs, + }, + Evidence: EvidenceData{Evidence: evidence}, + LastCommit: lastCommit, + } + block.fillHeader() + return block +}