Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion blocksync/reactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ func newReactor(
for blockHeight := int64(1); blockHeight <= maxBlockHeight; blockHeight++ {
lastExtCommit := seenExtCommit.Clone()

thisBlock := state.MakeBlock(blockHeight, nil, lastExtCommit.ToCommit(), nil, state.Validators.Proposer.Address)
thisBlock, err := state.MakeBlock(blockHeight, nil, lastExtCommit.ToCommit(), nil, state.Validators.Proposer.Address)
require.NoError(t, err)

thisParts, err := thisBlock.MakePartSet(types.BlockPartSizeBytes)
require.NoError(t, err)
Expand Down
5 changes: 4 additions & 1 deletion consensus/replay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,10 @@ func makeBlocks(n int, state sm.State, privVals []types.PrivValidator) ([]*types
if err != nil {
return nil, err
}
block := state.MakeBlock(height, test.MakeNTxs(height, 10), lastCommit, nil, state.LastValidators.Proposer.Address)
block, err := state.MakeBlock(height, test.MakeNTxs(height, 10), lastCommit, nil, state.LastValidators.Proposer.Address)
if err != nil {
return nil, err
}
blocks[i] = block
state.LastBlockID = blockID
state.LastBlockHeight = height
Expand Down
3 changes: 2 additions & 1 deletion consensus/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3205,13 +3205,14 @@ func findBlockSizeLimit(t *testing.T, height, maxBytes int64, cs *State, partSiz
}
softMaxDataBytes := int(types.MaxDataBytes(maxBytes, 0, 0))
for i := softMaxDataBytes; i < softMaxDataBytes*2; i++ {
propBlock := cs.state.MakeBlock(
propBlock, err := cs.state.MakeBlock(
height,
[]types.Tx{[]byte("a=" + strings.Repeat("o", i-2))},
&types.Commit{},
nil,
cs.privValidatorPubKey.Address(),
)
require.NoError(t, err)

propBlockParts, err := propBlock.MakePartSet(partSize)
require.NoError(t, err)
Expand Down
9 changes: 6 additions & 3 deletions evidence/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,12 @@ func initializeBlockStore(db dbm.DB, state sm.State, valAddr []byte) (*store.Blo

for i := int64(1); i <= state.LastBlockHeight; i++ {
lastCommit := makeExtCommit(i-1, valAddr)
block := state.MakeBlock(i, test.MakeNTxs(i, 1), lastCommit.ToCommit(), nil, state.Validators.Proposer.Address)
block.Header.Time = defaultEvidenceTime.Add(time.Duration(i) * time.Minute)
block.Header.Version = cmtversion.Consensus{Block: version.BlockProtocol, App: 1}
block, err := state.MakeBlock(i, test.MakeNTxs(i, 1), lastCommit.ToCommit(), nil, state.Validators.Proposer.Address)
if err != nil {
return nil, err
}
block.Time = defaultEvidenceTime.Add(time.Duration(i) * time.Minute)
block.Version = cmtversion.Consensus{Block: version.BlockProtocol, App: 1}
partSet, err := block.MakePartSet(types.BlockPartSizeBytes)
if err != nil {
return nil, err
Expand Down
7 changes: 5 additions & 2 deletions state/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ func (blockExec *BlockExecutor) CreateProposalBlock(

txs := blockExec.mempool.ReapMaxBytesMaxGas(maxReapBytes, maxGas)
commit := lastExtCommit.ToCommit()
block := state.MakeBlock(height, txs, commit, evidence, proposerAddr)
block, err := state.MakeBlock(height, txs, commit, evidence, proposerAddr)
if err != nil {
return nil, err
}
rpp, err := blockExec.proxyApp.PrepareProposal(
ctx,
&abci.RequestPrepareProposal{
Expand Down Expand Up @@ -157,7 +160,7 @@ func (blockExec *BlockExecutor) CreateProposalBlock(
return nil, err
}

return state.MakeBlock(height, txl, commit, evidence, proposerAddr), nil
return state.MakeBlock(height, txl, commit, evidence, proposerAddr)
}

func (blockExec *BlockExecutor) ProcessProposal(
Expand Down
44 changes: 27 additions & 17 deletions state/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ func TestApplyBlock(t *testing.T) {
blockExec := sm.NewBlockExecutor(stateStore, log.TestingLogger(), proxyApp.Consensus(),
mp, sm.EmptyEvidencePool{}, blockStore)

block := makeBlock(state, 1, new(types.Commit))
block, err := makeBlock(state, 1, new(types.Commit))
require.NoError(t, err)
bps, err := block.MakePartSet(testPartSize)
require.NoError(t, err)
blockID := types.BlockID{Hash: block.Hash(), PartSetHeader: bps.Header()}
Expand Down Expand Up @@ -141,7 +142,8 @@ func TestFinalizeBlockDecidedLastCommit(t *testing.T) {
}

// block for height 2
block := makeBlock(state, 2, lastCommit.ToCommit())
block, err := makeBlock(state, 2, lastCommit.ToCommit())
require.NoError(t, err)
bps, err := block.MakePartSet(testPartSize)
require.NoError(t, err)
blockID := types.BlockID{Hash: block.Hash(), PartSetHeader: bps.Header()}
Expand Down Expand Up @@ -221,7 +223,8 @@ func TestFinalizeBlockValidators(t *testing.T) {
}

// block for height 2
block := makeBlock(state, 2, lastCommit.ToCommit())
block, err := makeBlock(state, 2, lastCommit.ToCommit())
require.NoError(t, err)

_, err = sm.ExecCommitBlock(proxyApp.Consensus(), block, log.TestingLogger(), stateStore, 1)
require.NoError(t, err, tc.desc)
Expand Down Expand Up @@ -348,7 +351,8 @@ func TestFinalizeBlockMisbehavior(t *testing.T) {
blockExec := sm.NewBlockExecutor(stateStore, log.TestingLogger(), proxyApp.Consensus(),
mp, evpool, blockStore)

block := makeBlock(state, 1, new(types.Commit))
block, err := makeBlock(state, 1, new(types.Commit))
require.NoError(t, err)
block.Evidence = types.EvidenceData{Evidence: ev}
block.Header.EvidenceHash = block.Evidence.Hash()
bps, err := block.MakePartSet(testPartSize)
Expand Down Expand Up @@ -395,7 +399,8 @@ func TestProcessProposal(t *testing.T) {
blockStore,
)

block0 := makeBlock(state, height-1, new(types.Commit))
block0, err := makeBlock(state, height-1, new(types.Commit))
require.NoError(t, err)
lastCommitSig := []types.CommitSig{}
partSet, err := block0.MakePartSet(types.BlockPartSizeBytes)
require.NoError(t, err)
Expand All @@ -419,10 +424,11 @@ func TestProcessProposal(t *testing.T) {
lastCommitSig = append(lastCommitSig, vote.CommitSig())
}

block1 := makeBlock(state, height, &types.Commit{
block1, err := makeBlock(state, height, &types.Commit{
Height: height - 1,
Signatures: lastCommitSig,
})
require.NoError(t, err)

block1.Txs = txs

Expand Down Expand Up @@ -653,7 +659,8 @@ func TestFinalizeBlockValidatorUpdates(t *testing.T) {
)
require.NoError(t, err)

block := makeBlock(state, 1, new(types.Commit))
block, err := makeBlock(state, 1, new(types.Commit))
require.NoError(t, err)
bps, err := block.MakePartSet(testPartSize)
require.NoError(t, err)
blockID := types.BlockID{Hash: block.Hash(), PartSetHeader: bps.Header()}
Expand Down Expand Up @@ -721,7 +728,8 @@ func TestFinalizeBlockValidatorUpdatesResultingInEmptySet(t *testing.T) {
blockStore,
)

block := makeBlock(state, 1, new(types.Commit))
block, err := makeBlock(state, 1, new(types.Commit))
require.NoError(t, err)
bps, err := block.MakePartSet(testPartSize)
require.NoError(t, err)
blockID := types.BlockID{Hash: block.Hash(), PartSetHeader: bps.Header()}
Expand Down Expand Up @@ -1066,26 +1074,26 @@ func TestCreateProposalAbsentVoteExtensions(t *testing.T) {
}{
{
name: "missing extension data on first required height",
height: 2,
extensionEnableHeight: 1,
height: 3,
extensionEnableHeight: 2,
expectPanic: true,
},
{
name: "missing extension during before required height",
height: 2,
extensionEnableHeight: 2,
height: 3,
extensionEnableHeight: 3,
expectPanic: false,
},
{
name: "missing extension data and not required",
height: 2,
height: 3,
extensionEnableHeight: 0,
expectPanic: false,
},
{
name: "missing extension data and required in two heights",
height: 2,
extensionEnableHeight: 3,
height: 3,
extensionEnableHeight: 4,
expectPanic: false,
},
} {
Expand Down Expand Up @@ -1129,7 +1137,8 @@ func TestCreateProposalAbsentVoteExtensions(t *testing.T) {
sm.EmptyEvidencePool{},
blockStore,
)
block := makeBlock(state, testCase.height, new(types.Commit))
block, err := makeBlock(state, testCase.height, new(types.Commit))
require.NoError(t, err)

bps, err := block.MakePartSet(testPartSize)
require.NoError(t, err)
Expand All @@ -1139,7 +1148,8 @@ func TestCreateProposalAbsentVoteExtensions(t *testing.T) {
stripSignatures(lastCommit)
if testCase.expectPanic {
require.Panics(t, func() {
blockExec.CreateProposalBlock(ctx, testCase.height, state, lastCommit, pa) //nolint:errcheck
_, err := blockExec.CreateProposalBlock(ctx, testCase.height, state, lastCommit, pa)
require.NoError(t, err)
})
} else {
_, err = blockExec.CreateProposalBlock(ctx, testCase.height, state, lastCommit, pa)
Expand Down
22 changes: 17 additions & 5 deletions state/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ func makeAndCommitGoodBlock(
func makeAndApplyGoodBlock(state sm.State, height int64, lastCommit *types.Commit, proposerAddr []byte,
blockExec *sm.BlockExecutor, evidence []types.Evidence,
) (sm.State, types.BlockID, error) {
block := state.MakeBlock(height, test.MakeNTxs(height, 10), lastCommit, evidence, proposerAddr)
block, err := state.MakeBlock(height, test.MakeNTxs(height, 10), lastCommit, evidence, proposerAddr)
if err != nil {
return state, types.BlockID{}, nil
}
partSet, err := block.MakePartSet(types.BlockPartSizeBytes)
if err != nil {
return state, types.BlockID{}, err
Expand All @@ -76,7 +79,7 @@ func makeAndApplyGoodBlock(state sm.State, height int64, lastCommit *types.Commi
return state, blockID, nil
}

func makeBlock(state sm.State, height int64, c *types.Commit) *types.Block {
func makeBlock(state sm.State, height int64, c *types.Commit) (*types.Block, error) {
return state.MakeBlock(
height,
test.MakeNTxs(state.LastBlockHeight, 10),
Expand Down Expand Up @@ -171,7 +174,10 @@ func makeHeaderPartsResponsesValPubKeyChange(
state sm.State,
pubkey crypto.PubKey,
) (types.Header, types.BlockID, *abci.ResponseFinalizeBlock) {
block := makeBlock(state, state.LastBlockHeight+1, new(types.Commit))
block, err := makeBlock(state, state.LastBlockHeight+1, new(types.Commit))
if err != nil {
return types.Header{}, types.BlockID{}, nil
}
abciResponses := &abci.ResponseFinalizeBlock{}
// If the pubkey is new, remove the old and add the new.
_, val := state.NextValidators.GetByIndex(0)
Expand All @@ -189,7 +195,10 @@ func makeHeaderPartsResponsesValPowerChange(
state sm.State,
power int64,
) (types.Header, types.BlockID, *abci.ResponseFinalizeBlock) {
block := makeBlock(state, state.LastBlockHeight+1, new(types.Commit))
block, err := makeBlock(state, state.LastBlockHeight+1, new(types.Commit))
if err != nil {
return types.Header{}, types.BlockID{}, nil
}
abciResponses := &abci.ResponseFinalizeBlock{}

// If the pubkey is new, remove the old and add the new.
Expand All @@ -207,7 +216,10 @@ func makeHeaderPartsResponsesParams(
state sm.State,
params cmtproto.ConsensusParams,
) (types.Header, types.BlockID, *abci.ResponseFinalizeBlock) {
block := makeBlock(state, state.LastBlockHeight+1, new(types.Commit))
block, err := makeBlock(state, state.LastBlockHeight+1, new(types.Commit))
if err != nil {
return types.Header{}, types.BlockID{}, nil
}
abciResponses := &abci.ResponseFinalizeBlock{
ConsensusParamUpdates: &params,
}
Expand Down
22 changes: 14 additions & 8 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func (state State) MakeBlock(
lastCommit *types.Commit,
evidence []types.Evidence,
proposerAddress []byte,
) *types.Block {
) (*types.Block, error) {

// Build base block with block data.
block := types.MakeBlock(height, txs, lastCommit, evidence)
Expand All @@ -255,7 +255,11 @@ func (state State) MakeBlock(
if height == state.InitialHeight {
timestamp = state.LastBlockTime // genesis time
} else {
timestamp = MedianTime(lastCommit, state.LastValidators)
ts, err := MedianTime(lastCommit, state.LastValidators)
if err != nil {
return nil, fmt.Errorf("error making block while calculating median time: %w", err)
}
timestamp = ts
}

// Fill rest of header with state data.
Expand All @@ -267,14 +271,14 @@ func (state State) MakeBlock(
proposerAddress,
)

return block
return block, nil
}

// 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 {
func MedianTime(commit *types.Commit, validators *types.ValidatorSet) (time.Time, error) {
weightedTimes := make([]*cmttime.WeightedTime, len(commit.Signatures))
totalVotingPower := int64(0)

Expand All @@ -284,13 +288,15 @@ func MedianTime(commit *types.Commit, validators *types.ValidatorSet) time.Time
}
_, validator := validators.GetByAddress(commitSig.ValidatorAddress)
// If there's no condition, TestValidateBlockCommit panics; not needed normally.
if validator != nil {
totalVotingPower += validator.VotingPower
weightedTimes[i] = cmttime.NewWeightedTime(commitSig.Timestamp, validator.VotingPower)
if validator == nil {
return time.Time{}, fmt.Errorf("commit validator not found in validator set: %X",
commitSig.ValidatorAddress)
}
totalVotingPower += validator.VotingPower
weightedTimes[i] = cmttime.NewWeightedTime(commitSig.Timestamp, validator.VotingPower)
}

return cmttime.WeightedMedian(weightedTimes, totalVotingPower)
return cmttime.WeightedMedian(weightedTimes, totalVotingPower), nil
}

//------------------------------------------------------------------------
Expand Down
Loading
Loading