Skip to content

Commit b737385

Browse files
committed
chore: improve error handling, add tests
1 parent d7cdcaf commit b737385

18 files changed

Lines changed: 398 additions & 79 deletions

internal/blocksync/reactor_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ func newReactor(
139139

140140
lastExtCommit := seenExtCommit.Clone()
141141

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

144145
thisParts, err := thisBlock.MakePartSet(types.BlockPartSizeBytes)
145146
require.NoError(t, err)

internal/consensus/replay_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,10 @@ func makeBlocks(n int, state sm.State, privVals []types.PrivValidator) ([]*types
880880
if err != nil {
881881
return nil, err
882882
}
883-
block := state.MakeBlock(height, test.MakeNTxs(height, 10), lastCommit, nil, state.LastValidators.Proposer.Address)
883+
block, err := state.MakeBlock(height, test.MakeNTxs(height, 10), lastCommit, nil, state.LastValidators.Proposer.Address)
884+
if err != nil {
885+
return nil, err
886+
}
884887
blocks[i] = block
885888
state.LastBlockID = blockID
886889
state.LastBlockHeight = height

internal/consensus/state_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3270,13 +3270,14 @@ func findBlockSizeLimit(t *testing.T, height, maxBytes int64, cs *State, partSiz
32703270
}
32713271
softMaxDataBytes := int(types.MaxDataBytes(maxBytes, 0, 0))
32723272
for i := softMaxDataBytes; i < softMaxDataBytes*2; i++ {
3273-
propBlock := cs.state.MakeBlock(
3273+
propBlock, err := cs.state.MakeBlock(
32743274
height,
32753275
[]types.Tx{[]byte("a=" + strings.Repeat("o", i-2))},
32763276
&types.Commit{},
32773277
nil,
32783278
cs.privValidatorPubKey.Address(),
32793279
)
3280+
require.NoError(t, err)
32803281

32813282
propBlockParts, err := propBlock.MakePartSet(partSize)
32823283
require.NoError(t, err)

internal/evidence/pool_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,10 @@ func initializeBlockStore(db dbm.DB, state sm.State, valAddr []byte) (*store.Blo
415415

416416
for i := int64(1); i <= state.LastBlockHeight; i++ {
417417
lastCommit := makeExtCommit(i-1, valAddr)
418-
block := state.MakeBlock(i, test.MakeNTxs(i, 1), lastCommit.ToCommit(), nil, state.Validators.Proposer.Address)
418+
block, err := state.MakeBlock(i, test.MakeNTxs(i, 1), lastCommit.ToCommit(), nil, state.Validators.Proposer.Address)
419+
if err != nil {
420+
return nil, err
421+
}
419422
block.Header.Time = defaultEvidenceTime.Add(time.Duration(i) * time.Minute)
420423
block.Header.Version = cmtversion.Consensus{Block: version.BlockProtocol, App: 1}
421424
partSet, err := block.MakePartSet(types.BlockPartSizeBytes)

state/execution.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,10 @@ func (blockExec *BlockExecutor) CreateProposalBlock(
131131

132132
txs := blockExec.mempool.ReapMaxBytesMaxGas(maxReapBytes, maxGas)
133133
commit := lastExtCommit.ToCommit()
134-
block := state.MakeBlock(height, txs, commit, evidence, proposerAddr)
134+
block, err := state.MakeBlock(height, txs, commit, evidence, proposerAddr)
135+
if err != nil {
136+
return nil, err
137+
}
135138
rpp, err := blockExec.proxyApp.PrepareProposal(
136139
ctx,
137140
&abci.PrepareProposalRequest{
@@ -162,7 +165,7 @@ func (blockExec *BlockExecutor) CreateProposalBlock(
162165
return nil, err
163166
}
164167

165-
return state.MakeBlock(height, txl, commit, evidence, proposerAddr), nil
168+
return state.MakeBlock(height, txl, commit, evidence, proposerAddr)
166169
}
167170

168171
func (blockExec *BlockExecutor) ProcessProposal(

state/execution_test.go

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ func TestApplyBlock(t *testing.T) {
6666
blockExec := sm.NewBlockExecutor(stateStore, log.TestingLogger(), proxyApp.Consensus(),
6767
mp, sm.EmptyEvidencePool{}, blockStore)
6868

69-
block := makeBlock(state, 1, new(types.Commit))
69+
block, err := makeBlock(state, 1, new(types.Commit))
70+
require.NoError(t, err)
7071
bps, err := block.MakePartSet(testPartSize)
7172
require.NoError(t, err)
7273
blockID := types.BlockID{Hash: block.Hash(), PartSetHeader: bps.Header()}
@@ -141,7 +142,8 @@ func TestFinalizeBlockDecidedLastCommit(t *testing.T) {
141142
}
142143

143144
// block for height 2
144-
block := makeBlock(state, 2, lastCommit.ToCommit())
145+
block, err := makeBlock(state, 2, lastCommit.ToCommit())
146+
require.NoError(t, err)
145147
bps, err := block.MakePartSet(testPartSize)
146148
require.NoError(t, err)
147149
blockID := types.BlockID{Hash: block.Hash(), PartSetHeader: bps.Header()}
@@ -221,7 +223,8 @@ func TestFinalizeBlockValidators(t *testing.T) {
221223
}
222224

223225
// block for height 2
224-
block := makeBlock(state, 2, lastCommit.ToCommit())
226+
block, err := makeBlock(state, 2, lastCommit.ToCommit())
227+
require.NoError(t, err)
225228

226229
_, err = sm.ExecCommitBlock(proxyApp.Consensus(), block, log.TestingLogger(), stateStore, 1, 2)
227230
require.NoError(t, err, tc.desc)
@@ -347,7 +350,8 @@ func TestFinalizeBlockMisbehavior(t *testing.T) {
347350
blockExec := sm.NewBlockExecutor(stateStore, log.TestingLogger(), proxyApp.Consensus(),
348351
mp, evpool, blockStore)
349352

350-
block := makeBlock(state, 1, new(types.Commit))
353+
block, err := makeBlock(state, 1, new(types.Commit))
354+
require.NoError(t, err)
351355
block.Evidence = types.EvidenceData{Evidence: ev}
352356
block.Header.EvidenceHash = block.Evidence.Hash()
353357
bps, err := block.MakePartSet(testPartSize)
@@ -394,7 +398,8 @@ func TestProcessProposal(t *testing.T) {
394398
blockStore,
395399
)
396400

397-
block0 := makeBlock(state, height-1, new(types.Commit))
401+
block0, err := makeBlock(state, height-1, new(types.Commit))
402+
require.NoError(t, err)
398403
lastCommitSig := []types.CommitSig{}
399404
partSet, err := block0.MakePartSet(types.BlockPartSizeBytes)
400405
require.NoError(t, err)
@@ -417,10 +422,11 @@ func TestProcessProposal(t *testing.T) {
417422
lastCommitSig = append(lastCommitSig, vote.CommitSig())
418423
}
419424

420-
block1 := makeBlock(state, height, &types.Commit{
425+
block1, err := makeBlock(state, height, &types.Commit{
421426
Height: height - 1,
422427
Signatures: lastCommitSig,
423428
})
429+
require.NoError(t, err)
424430

425431
block1.Txs = txs
426432

@@ -615,7 +621,8 @@ func TestFinalizeBlockValidatorUpdates(t *testing.T) {
615621
)
616622
require.NoError(t, err)
617623

618-
block := makeBlock(state, 1, new(types.Commit))
624+
block, err := makeBlock(state, 1, new(types.Commit))
625+
require.NoError(t, err)
619626
bps, err := block.MakePartSet(testPartSize)
620627
require.NoError(t, err)
621628
blockID := types.BlockID{Hash: block.Hash(), PartSetHeader: bps.Header()}
@@ -675,7 +682,8 @@ func TestFinalizeBlockValidatorUpdatesResultingInEmptySet(t *testing.T) {
675682
blockStore,
676683
)
677684

678-
block := makeBlock(state, 1, new(types.Commit))
685+
block, err := makeBlock(state, 1, new(types.Commit))
686+
require.NoError(t, err)
679687
bps, err := block.MakePartSet(testPartSize)
680688
require.NoError(t, err)
681689
blockID := types.BlockID{Hash: block.Hash(), PartSetHeader: bps.Header()}
@@ -1024,26 +1032,26 @@ func TestCreateProposalAbsentVoteExtensions(t *testing.T) {
10241032
}{
10251033
{
10261034
name: "missing extension data on first required height",
1027-
height: 2,
1028-
extensionEnableHeight: 1,
1035+
height: 3,
1036+
extensionEnableHeight: 2,
10291037
expectPanic: true,
10301038
},
10311039
{
10321040
name: "missing extension during before required height",
1033-
height: 2,
1034-
extensionEnableHeight: 2,
1041+
height: 3,
1042+
extensionEnableHeight: 3,
10351043
expectPanic: false,
10361044
},
10371045
{
10381046
name: "missing extension data and not required",
1039-
height: 2,
1047+
height: 3,
10401048
extensionEnableHeight: 0,
10411049
expectPanic: false,
10421050
},
10431051
{
10441052
name: "missing extension data and required in two heights",
1045-
height: 2,
1046-
extensionEnableHeight: 3,
1053+
height: 3,
1054+
extensionEnableHeight: 4,
10471055
expectPanic: false,
10481056
},
10491057
} {
@@ -1087,7 +1095,8 @@ func TestCreateProposalAbsentVoteExtensions(t *testing.T) {
10871095
sm.EmptyEvidencePool{},
10881096
blockStore,
10891097
)
1090-
block := makeBlock(state, testCase.height, new(types.Commit))
1098+
block, err := makeBlock(state, testCase.height, new(types.Commit))
1099+
require.NoError(t, err)
10911100

10921101
bps, err := block.MakePartSet(testPartSize)
10931102
require.NoError(t, err)
@@ -1097,7 +1106,8 @@ func TestCreateProposalAbsentVoteExtensions(t *testing.T) {
10971106
stripSignatures(lastCommit)
10981107
if testCase.expectPanic {
10991108
require.Panics(t, func() {
1100-
blockExec.CreateProposalBlock(ctx, testCase.height, state, lastCommit, pa) //nolint:errcheck
1109+
_, err := blockExec.CreateProposalBlock(ctx, testCase.height, state, lastCommit, pa)
1110+
require.NoError(t, err)
11011111
})
11021112
} else {
11031113
_, err = blockExec.CreateProposalBlock(ctx, testCase.height, state, lastCommit, pa)

state/helpers_test.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ func makeAndCommitGoodBlock(
5454
func makeAndApplyGoodBlock(state sm.State, height int64, lastCommit *types.Commit, proposerAddr []byte,
5555
blockExec *sm.BlockExecutor, evidence []types.Evidence,
5656
) (sm.State, types.BlockID, error) {
57-
block := state.MakeBlock(height, test.MakeNTxs(height, 10), lastCommit, evidence, proposerAddr)
57+
block, err := state.MakeBlock(height, test.MakeNTxs(height, 10), lastCommit, evidence, proposerAddr)
58+
if err != nil {
59+
return state, types.BlockID{}, err
60+
}
5861
partSet, err := block.MakePartSet(types.BlockPartSizeBytes)
5962
if err != nil {
6063
return state, types.BlockID{}, err
@@ -74,7 +77,7 @@ func makeAndApplyGoodBlock(state sm.State, height int64, lastCommit *types.Commi
7477
return state, blockID, nil
7578
}
7679

77-
func makeBlock(state sm.State, height int64, c *types.Commit) *types.Block {
80+
func makeBlock(state sm.State, height int64, c *types.Commit) (*types.Block, error) {
7881
return state.MakeBlock(
7982
height,
8083
test.MakeNTxs(state.LastBlockHeight, 10),
@@ -129,7 +132,10 @@ func makeHeaderPartsResponsesValPubKeyChange(
129132
state sm.State,
130133
pubkey crypto.PubKey,
131134
) (types.Header, types.BlockID, *abci.FinalizeBlockResponse) {
132-
block := makeBlock(state, state.LastBlockHeight+1, new(types.Commit))
135+
block, err := makeBlock(state, state.LastBlockHeight+1, new(types.Commit))
136+
if err != nil {
137+
return types.Header{}, types.BlockID{}, nil
138+
}
133139
abciResponses := &abci.FinalizeBlockResponse{}
134140
// If the pubkey is new, remove the old and add the new.
135141
_, val := state.NextValidators.GetByIndex(0)
@@ -147,7 +153,10 @@ func makeHeaderPartsResponsesValPowerChange(
147153
state sm.State,
148154
power int64,
149155
) (types.Header, types.BlockID, *abci.FinalizeBlockResponse) {
150-
block := makeBlock(state, state.LastBlockHeight+1, new(types.Commit))
156+
block, err := makeBlock(state, state.LastBlockHeight+1, new(types.Commit))
157+
if err != nil {
158+
return types.Header{}, types.BlockID{}, nil
159+
}
151160
abciResponses := &abci.FinalizeBlockResponse{}
152161

153162
// If the pubkey is new, remove the old and add the new.
@@ -163,7 +172,10 @@ func makeHeaderPartsResponsesParams(
163172
state sm.State,
164173
params cmtproto.ConsensusParams,
165174
) (types.Header, types.BlockID, *abci.FinalizeBlockResponse) {
166-
block := makeBlock(state, state.LastBlockHeight+1, new(types.Commit))
175+
block, err := makeBlock(state, state.LastBlockHeight+1, new(types.Commit))
176+
if err != nil {
177+
return types.Header{}, types.BlockID{}, nil
178+
}
167179
abciResponses := &abci.FinalizeBlockResponse{
168180
ConsensusParamUpdates: &params,
169181
}

state/pruner_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ func TestPruningWithHeight1(t *testing.T) {
257257
err = pruner.SetApplicationBlockRetainHeight(0)
258258
require.NoError(t, err)
259259

260-
block := state.MakeBlock(1, test.MakeNTxs(1, 10), new(types.Commit), nil, state.Validators.GetProposer().Address)
260+
block, err := state.MakeBlock(1, test.MakeNTxs(1, 10), new(types.Commit), nil, state.Validators.GetProposer().Address)
261+
require.NoError(t, err)
261262
partSet, err := block.MakePartSet(2)
262263
require.NoError(t, err)
263264

state/state.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func (state State) MakeBlock(
236236
lastCommit *types.Commit,
237237
evidence []types.Evidence,
238238
proposerAddress []byte,
239-
) *types.Block {
239+
) (*types.Block, error) {
240240
// Build base block with block data.
241241
block := types.MakeBlock(height, txs, lastCommit, evidence)
242242

@@ -248,7 +248,11 @@ func (state State) MakeBlock(
248248
case height == state.InitialHeight:
249249
timestamp = state.LastBlockTime // genesis time
250250
default:
251-
timestamp = lastCommit.MedianTime(state.LastValidators)
251+
ts, err := lastCommit.MedianTime(state.LastValidators)
252+
if err != nil {
253+
return nil, fmt.Errorf("error making block while calculating median time: %w", err)
254+
}
255+
timestamp = ts
252256
}
253257

254258
// Fill rest of header with state data.
@@ -260,7 +264,7 @@ func (state State) MakeBlock(
260264
proposerAddress,
261265
)
262266

263-
return block
267+
return block, nil
264268
}
265269

266270
// ------------------------------------------------------------------------

0 commit comments

Comments
 (0)