Browse Source

types: move MakeVote / MakeBlock functions (#3819)

to the types package

Paritally Fixes #3584
pull/3926/head
Marko 5 years ago
committed by Jack Zampolin
parent
commit
58c3e590b4
6 changed files with 50 additions and 83 deletions
  1. +6
    -20
      blockchain/v0/reactor_test.go
  2. +3
    -21
      consensus/replay_test.go
  3. +1
    -19
      state/helpers_test.go
  4. +2
    -2
      state/validation_test.go
  5. +0
    -19
      types/block.go
  6. +38
    -2
      types/test_util.go

+ 6
- 20
blockchain/v0/reactor_test.go View File

@ -45,24 +45,6 @@ func randGenesisDoc(numValidators int, randPower bool, minPower int64) (*types.G
}, privValidators }, 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 { type BlockchainReactorPair struct {
reactor *BlockchainReactor reactor *BlockchainReactor
app proxy.AppConns app proxy.AppConns
@ -106,8 +88,12 @@ func newBlockchainReactor(logger log.Logger, genDoc *types.GenesisDoc, privVals
lastBlockMeta := blockStore.LoadBlockMeta(blockHeight - 1) lastBlockMeta := blockStore.LoadBlockMeta(blockHeight - 1)
lastBlock := blockStore.LoadBlock(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) thisBlock := makeBlock(blockHeight, state, lastCommit)


+ 3
- 21
consensus/replay_test.go View File

@ -28,7 +28,6 @@ import (
"github.com/tendermint/tendermint/proxy" "github.com/tendermint/tendermint/proxy"
sm "github.com/tendermint/tendermint/state" sm "github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/types" "github.com/tendermint/tendermint/types"
tmtime "github.com/tendermint/tendermint/types/time"
"github.com/tendermint/tendermint/version" "github.com/tendermint/tendermint/version"
dbm "github.com/tendermint/tm-cmn/db" 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 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, func makeBlock(state sm.State, lastBlock *types.Block, lastBlockMeta *types.BlockMeta,
privVal types.PrivValidator, height int64) (*types.Block, *types.PartSet) { privVal types.PrivValidator, height int64) (*types.Block, *types.PartSet) {
lastCommit := types.NewCommit(types.BlockID{}, nil) lastCommit := types.NewCommit(types.BlockID{}, nil)
if height > 1 { 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) return state.MakeBlock(height, []types.Tx{}, lastCommit, nil, state.Validators.GetProposer().Address)


+ 1
- 19
state/helpers_test.go View File

@ -69,29 +69,11 @@ func makeAndApplyGoodBlock(state sm.State, height int64, lastCommit *types.Commi
return state, blockID, nil 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) { func makeValidCommit(height int64, blockID types.BlockID, vals *types.ValidatorSet, privVals map[string]types.PrivValidator) (*types.Commit, error) {
sigs := make([]*types.CommitSig, 0) sigs := make([]*types.CommitSig, 0)
for i := 0; i < vals.Size(); i++ { for i := 0; i < vals.Size(); i++ {
_, val := vals.GetByIndex(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 { if err != nil {
return nil, err return nil, err
} }


+ 2
- 2
state/validation_test.go View File

@ -101,7 +101,7 @@ func TestValidateBlockCommit(t *testing.T) {
#2589: ensure state.LastValidators.VerifyCommit fails here #2589: ensure state.LastValidators.VerifyCommit fails here
*/ */
// should be height-1 instead of height // 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) require.NoError(t, err, "height %d", height)
wrongHeightCommit := types.NewCommit(state.LastBlockID, []*types.CommitSig{wrongHeightVote.CommitSig()}) wrongHeightCommit := types.NewCommit(state.LastBlockID, []*types.CommitSig{wrongHeightVote.CommitSig()})
block, _ := state.MakeBlock(height, makeTxs(height), wrongHeightCommit, nil, proposerAddr) 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 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) require.NoError(t, err, "height %d", height)
badVote := &types.Vote{ badVote := &types.Vote{
ValidatorAddress: badPrivVal.GetPubKey().Address(), ValidatorAddress: badPrivVal.GetPubKey().Address(),


+ 0
- 19
types/block.go View File

@ -41,25 +41,6 @@ type Block struct {
LastCommit *Commit `json:"last_commit"` 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. // ValidateBasic performs basic validation that doesn't involve state data.
// It checks the internal consistency of the block. // It checks the internal consistency of the block.
// Further validation is done using state#ValidateBlock. // Further validation is done using state#ValidateBlock.


+ 38
- 2
types/test_util.go View File

@ -5,8 +5,7 @@ import (
) )
func MakeCommit(blockID BlockID, height int64, round int, func MakeCommit(blockID BlockID, height int64, round int,
voteSet *VoteSet,
validators []PrivValidator) (*Commit, error) {
voteSet *VoteSet, validators []PrivValidator) (*Commit, error) {
// all sign // all sign
for i := 0; i < len(validators); i++ { 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) 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
}

Loading…
Cancel
Save