Skip to content

Commit c56d64e

Browse files
authored
Merge commit from fork
v0.38
2 parents 01d5ea5 + eeb4a59 commit c56d64e

15 files changed

Lines changed: 375 additions & 76 deletions

blocksync/reactor_test.go

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

139139
lastExtCommit := seenExtCommit.Clone()
140140

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

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

consensus/replay_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,10 @@ func makeBlocks(n int, state sm.State, privVals []types.PrivValidator) ([]*types
898898
if err != nil {
899899
return nil, err
900900
}
901-
block := state.MakeBlock(height, test.MakeNTxs(height, 10), lastCommit, nil, state.LastValidators.Proposer.Address)
901+
block, err := state.MakeBlock(height, test.MakeNTxs(height, 10), lastCommit, nil, state.LastValidators.Proposer.Address)
902+
if err != nil {
903+
return nil, err
904+
}
902905
blocks[i] = block
903906
state.LastBlockID = blockID
904907
state.LastBlockHeight = height

consensus/state_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2596,13 +2596,14 @@ func findBlockSizeLimit(t *testing.T, height, maxBytes int64, cs *State, partSiz
25962596
}
25972597
softMaxDataBytes := int(types.MaxDataBytes(maxBytes, 0, 0))
25982598
for i := softMaxDataBytes; i < softMaxDataBytes*2; i++ {
2599-
propBlock := cs.state.MakeBlock(
2599+
propBlock, err := cs.state.MakeBlock(
26002600
height,
26012601
[]types.Tx{[]byte("a=" + strings.Repeat("o", i-2))},
26022602
&types.Commit{},
26032603
nil,
26042604
cs.privValidatorPubKey.Address(),
26052605
)
2606+
require.NoError(t, err)
26062607

26072608
propBlockParts, err := propBlock.MakePartSet(partSize)
26082609
require.NoError(t, err)

evidence/pool_test.go

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

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

state/execution.go

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

126126
txs := blockExec.mempool.ReapMaxBytesMaxGas(maxReapBytes, maxGas)
127127
commit := lastExtCommit.ToCommit()
128-
block := state.MakeBlock(height, txs, commit, evidence, proposerAddr)
128+
block, err := state.MakeBlock(height, txs, commit, evidence, proposerAddr)
129+
if err != nil {
130+
return nil, err
131+
}
129132
rpp, err := blockExec.proxyApp.PrepareProposal(
130133
ctx,
131134
&abci.RequestPrepareProposal{
@@ -156,7 +159,7 @@ func (blockExec *BlockExecutor) CreateProposalBlock(
156159
return nil, err
157160
}
158161

159-
return state.MakeBlock(height, txl, commit, evidence, proposerAddr), nil
162+
return state.MakeBlock(height, txl, commit, evidence, proposerAddr)
160163
}
161164

162165
func (blockExec *BlockExecutor) ProcessProposal(

state/execution_test.go

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

70-
block := makeBlock(state, 1, new(types.Commit))
70+
block, err := makeBlock(state, 1, new(types.Commit))
71+
require.NoError(t, err)
7172
bps, err := block.MakePartSet(testPartSize)
7273
require.NoError(t, err)
7374
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)
227230
require.NoError(t, err, tc.desc)
@@ -346,7 +349,8 @@ func TestFinalizeBlockMisbehavior(t *testing.T) {
346349
blockExec := sm.NewBlockExecutor(stateStore, log.TestingLogger(), proxyApp.Consensus(),
347350
mp, evpool, blockStore)
348351

349-
block := makeBlock(state, 1, new(types.Commit))
352+
block, err := makeBlock(state, 1, new(types.Commit))
353+
require.NoError(t, err)
350354
block.Evidence = types.EvidenceData{Evidence: ev}
351355
block.EvidenceHash = block.Evidence.Hash()
352356
bps, err := block.MakePartSet(testPartSize)
@@ -393,7 +397,8 @@ func TestProcessProposal(t *testing.T) {
393397
blockStore,
394398
)
395399

396-
block0 := makeBlock(state, height-1, new(types.Commit))
400+
block0, err := makeBlock(state, height-1, new(types.Commit))
401+
require.NoError(t, err)
397402
lastCommitSig := []types.CommitSig{}
398403
partSet, err := block0.MakePartSet(types.BlockPartSizeBytes)
399404
require.NoError(t, err)
@@ -416,10 +421,11 @@ func TestProcessProposal(t *testing.T) {
416421
lastCommitSig = append(lastCommitSig, vote.CommitSig())
417422
}
418423

419-
block1 := makeBlock(state, height, &types.Commit{
424+
block1, err := makeBlock(state, height, &types.Commit{
420425
Height: height - 1,
421426
Signatures: lastCommitSig,
422427
})
428+
require.NoError(t, err)
423429

424430
block1.Txs = txs
425431

@@ -624,7 +630,8 @@ func TestFinalizeBlockValidatorUpdates(t *testing.T) {
624630
)
625631
require.NoError(t, err)
626632

627-
block := makeBlock(state, 1, new(types.Commit))
633+
block, err := makeBlock(state, 1, new(types.Commit))
634+
require.NoError(t, err)
628635
bps, err := block.MakePartSet(testPartSize)
629636
require.NoError(t, err)
630637
blockID := types.BlockID{Hash: block.Hash(), PartSetHeader: bps.Header()}
@@ -686,7 +693,8 @@ func TestFinalizeBlockValidatorUpdatesResultingInEmptySet(t *testing.T) {
686693
blockStore,
687694
)
688695

689-
block := makeBlock(state, 1, new(types.Commit))
696+
block, err := makeBlock(state, 1, new(types.Commit))
697+
require.NoError(t, err)
690698
bps, err := block.MakePartSet(testPartSize)
691699
require.NoError(t, err)
692700
blockID := types.BlockID{Hash: block.Hash(), PartSetHeader: bps.Header()}
@@ -1031,26 +1039,26 @@ func TestCreateProposalAbsentVoteExtensions(t *testing.T) {
10311039
}{
10321040
{
10331041
name: "missing extension data on first required height",
1034-
height: 2,
1035-
extensionEnableHeight: 1,
1042+
height: 3,
1043+
extensionEnableHeight: 2,
10361044
expectPanic: true,
10371045
},
10381046
{
10391047
name: "missing extension during before required height",
1040-
height: 2,
1041-
extensionEnableHeight: 2,
1048+
height: 3,
1049+
extensionEnableHeight: 3,
10421050
expectPanic: false,
10431051
},
10441052
{
10451053
name: "missing extension data and not required",
1046-
height: 2,
1054+
height: 3,
10471055
extensionEnableHeight: 0,
10481056
expectPanic: false,
10491057
},
10501058
{
10511059
name: "missing extension data and required in two heights",
1052-
height: 2,
1053-
extensionEnableHeight: 3,
1060+
height: 3,
1061+
extensionEnableHeight: 4,
10541062
expectPanic: false,
10551063
},
10561064
} {
@@ -1094,7 +1102,8 @@ func TestCreateProposalAbsentVoteExtensions(t *testing.T) {
10941102
sm.EmptyEvidencePool{},
10951103
blockStore,
10961104
)
1097-
block := makeBlock(state, testCase.height, new(types.Commit))
1105+
block, err := makeBlock(state, testCase.height, new(types.Commit))
1106+
require.NoError(t, err)
10981107

10991108
bps, err := block.MakePartSet(testPartSize)
11001109
require.NoError(t, err)
@@ -1104,7 +1113,8 @@ func TestCreateProposalAbsentVoteExtensions(t *testing.T) {
11041113
stripSignatures(lastCommit)
11051114
if testCase.expectPanic {
11061115
require.Panics(t, func() {
1107-
blockExec.CreateProposalBlock(ctx, testCase.height, state, lastCommit, pa) //nolint:errcheck
1116+
_, err := blockExec.CreateProposalBlock(ctx, testCase.height, state, lastCommit, pa)
1117+
require.NoError(t, err)
11081118
})
11091119
} else {
11101120
_, 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
@@ -56,7 +56,10 @@ func makeAndCommitGoodBlock(
5656
func makeAndApplyGoodBlock(state sm.State, height int64, lastCommit *types.Commit, proposerAddr []byte,
5757
blockExec *sm.BlockExecutor, evidence []types.Evidence,
5858
) (sm.State, types.BlockID, error) {
59-
block := state.MakeBlock(height, test.MakeNTxs(height, 10), lastCommit, evidence, proposerAddr)
59+
block, err := state.MakeBlock(height, test.MakeNTxs(height, 10), lastCommit, evidence, proposerAddr)
60+
if err != nil {
61+
return state, types.BlockID{}, nil
62+
}
6063
partSet, err := block.MakePartSet(types.BlockPartSizeBytes)
6164
if err != nil {
6265
return state, types.BlockID{}, err
@@ -76,7 +79,7 @@ func makeAndApplyGoodBlock(state sm.State, height int64, lastCommit *types.Commi
7679
return state, blockID, nil
7780
}
7881

79-
func makeBlock(state sm.State, height int64, c *types.Commit) *types.Block {
82+
func makeBlock(state sm.State, height int64, c *types.Commit) (*types.Block, error) {
8083
return state.MakeBlock(
8184
height,
8285
test.MakeNTxs(state.LastBlockHeight, 10),
@@ -171,7 +174,10 @@ func makeHeaderPartsResponsesValPubKeyChange(
171174
state sm.State,
172175
pubkey crypto.PubKey,
173176
) (types.Header, types.BlockID, *abci.ResponseFinalizeBlock) {
174-
block := makeBlock(state, state.LastBlockHeight+1, new(types.Commit))
177+
block, err := makeBlock(state, state.LastBlockHeight+1, new(types.Commit))
178+
if err != nil {
179+
return types.Header{}, types.BlockID{}, nil
180+
}
175181
abciResponses := &abci.ResponseFinalizeBlock{}
176182
// If the pubkey is new, remove the old and add the new.
177183
_, val := state.NextValidators.GetByIndex(0)
@@ -189,7 +195,10 @@ func makeHeaderPartsResponsesValPowerChange(
189195
state sm.State,
190196
power int64,
191197
) (types.Header, types.BlockID, *abci.ResponseFinalizeBlock) {
192-
block := makeBlock(state, state.LastBlockHeight+1, new(types.Commit))
198+
block, err := makeBlock(state, state.LastBlockHeight+1, new(types.Commit))
199+
if err != nil {
200+
return types.Header{}, types.BlockID{}, nil
201+
}
193202
abciResponses := &abci.ResponseFinalizeBlock{}
194203

195204
// If the pubkey is new, remove the old and add the new.
@@ -207,7 +216,10 @@ func makeHeaderPartsResponsesParams(
207216
state sm.State,
208217
params cmtproto.ConsensusParams,
209218
) (types.Header, types.BlockID, *abci.ResponseFinalizeBlock) {
210-
block := makeBlock(state, state.LastBlockHeight+1, new(types.Commit))
219+
block, err := makeBlock(state, state.LastBlockHeight+1, new(types.Commit))
220+
if err != nil {
221+
return types.Header{}, types.BlockID{}, nil
222+
}
211223
abciResponses := &abci.ResponseFinalizeBlock{
212224
ConsensusParamUpdates: &params,
213225
}

state/state.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ func (state State) MakeBlock(
237237
lastCommit *types.Commit,
238238
evidence []types.Evidence,
239239
proposerAddress []byte,
240-
) *types.Block {
240+
) (*types.Block, error) {
241241

242242
// Build base block with block data.
243243
block := types.MakeBlock(height, txs, lastCommit, evidence)
@@ -247,7 +247,11 @@ func (state State) MakeBlock(
247247
if height == state.InitialHeight {
248248
timestamp = state.LastBlockTime // genesis time
249249
} else {
250-
timestamp = MedianTime(lastCommit, state.LastValidators)
250+
ts, err := MedianTime(lastCommit, state.LastValidators)
251+
if err != nil {
252+
return nil, fmt.Errorf("error making block while calculating median time: %w", err)
253+
}
254+
timestamp = ts
251255
}
252256

253257
// Fill rest of header with state data.
@@ -259,14 +263,14 @@ func (state State) MakeBlock(
259263
proposerAddress,
260264
)
261265

262-
return block
266+
return block, nil
263267
}
264268

265269
// MedianTime computes a median time for a given Commit (based on Timestamp field of votes messages) and the
266270
// corresponding validator set. The computed time is always between timestamps of
267271
// the votes sent by honest processes, i.e., a faulty processes can not arbitrarily increase or decrease the
268272
// computed value.
269-
func MedianTime(commit *types.Commit, validators *types.ValidatorSet) time.Time {
273+
func MedianTime(commit *types.Commit, validators *types.ValidatorSet) (time.Time, error) {
270274
weightedTimes := make([]*cmttime.WeightedTime, len(commit.Signatures))
271275
totalVotingPower := int64(0)
272276

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

285-
return cmttime.WeightedMedian(weightedTimes, totalVotingPower)
291+
return cmttime.WeightedMedian(weightedTimes, totalVotingPower), nil
286292
}
287293

288294
//------------------------------------------------------------------------

0 commit comments

Comments
 (0)