Skip to content

Commit dc41c2c

Browse files
aeddialbttx
authored andcommitted
refactor: remove useless code and fix a test (#3159)
- Commit 4b6219c move `ValidateBlock` method from `blockExec` to `state` since `blockExec.db` was an unused parameter. (simplify + remove misleading comment) - Commit 4f02bcd just removes useless `Sprintf` found in this package - Commit c34bfd5 improves `ValidateBasic` method (adding one more validation test that was marked with a `TODO` comment) - Commit df75b32 removes useless case testing the size of a fixed-sized array, see: https://github.com/gnolang/gno/blob/b3800b7dfb864396ac74dc20390e728bc0b2d88e/tm2/pkg/crypto/crypto.go#L24-L30 <details><summary>Contributors' checklist...</summary> - [x] Added new tests, or not needed, or not feasible - [x] Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory - [x] Updated the official documentation or not needed - [x] No breaking changes were made, or a `BREAKING CHANGE: xxx` message was included in the description - [x] Added references to related issues and PRs - [ ] Provided any useful hints for running manual tests </details>
1 parent 06746cf commit dc41c2c

File tree

11 files changed

+25
-47
lines changed

11 files changed

+25
-47
lines changed

tm2/pkg/bft/consensus/replay.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ func (h *Handshaker) Handshake(proxyApp appconn.AppConns) error {
237237
// Handshake is done via ABCI Info on the query conn.
238238
res, err := proxyApp.Query().InfoSync(abci.RequestInfo{})
239239
if err != nil {
240-
return fmt.Errorf("Error calling Info: %w", err)
240+
return fmt.Errorf("error calling Info: %w", err)
241241
}
242242

243243
blockHeight := res.LastBlockHeight

tm2/pkg/bft/consensus/state.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func (cs *ConsensusState) SetEventSwitch(evsw events.EventSwitch) {
203203
// String returns a string.
204204
func (cs *ConsensusState) String() string {
205205
// better not to access shared variables
206-
return fmt.Sprintf("ConsensusState") // (H:%v R:%v S:%v", cs.Height, cs.Round, cs.Step)
206+
return "ConsensusState" // (H:%v R:%v S:%v", cs.Height, cs.Round, cs.Step)
207207
}
208208

209209
// GetConfig returns a copy of the chain state.
@@ -1051,7 +1051,7 @@ func (cs *ConsensusState) defaultDoPrevote(height int64, round int) {
10511051
}
10521052

10531053
// Validate proposal block
1054-
err := cs.blockExec.ValidateBlock(cs.state, cs.ProposalBlock)
1054+
err := cs.state.ValidateBlock(cs.ProposalBlock)
10551055
if err != nil {
10561056
// ProposalBlock is invalid, prevote nil.
10571057
logger.Error("enterPrevote: ProposalBlock is invalid", "err", err)
@@ -1164,7 +1164,7 @@ func (cs *ConsensusState) enterPrecommit(height int64, round int) {
11641164
if cs.ProposalBlock.HashesTo(blockID.Hash) {
11651165
logger.Info("enterPrecommit: +2/3 prevoted proposal block. Locking", "hash", blockID.Hash)
11661166
// Validate the block.
1167-
if err := cs.blockExec.ValidateBlock(cs.state, cs.ProposalBlock); err != nil {
1167+
if err := cs.state.ValidateBlock(cs.ProposalBlock); err != nil {
11681168
panic(fmt.Sprintf("enterPrecommit: +2/3 prevoted for an invalid block: %v", err))
11691169
}
11701170
cs.LockedRound = round
@@ -1305,15 +1305,15 @@ func (cs *ConsensusState) finalizeCommit(height int64) {
13051305
block, blockParts := cs.ProposalBlock, cs.ProposalBlockParts
13061306

13071307
if !ok {
1308-
panic(fmt.Sprintf("Cannot finalizeCommit, commit does not have two thirds majority"))
1308+
panic("Cannot finalizeCommit, commit does not have two thirds majority")
13091309
}
13101310
if !blockParts.HasHeader(blockID.PartsHeader) {
1311-
panic(fmt.Sprintf("Expected ProposalBlockParts header to be commit header"))
1311+
panic("Expected ProposalBlockParts header to be commit header")
13121312
}
13131313
if !block.HashesTo(blockID.Hash) {
1314-
panic(fmt.Sprintf("Cannot finalizeCommit, ProposalBlock does not hash to commit hash"))
1314+
panic("Cannot finalizeCommit, ProposalBlock does not hash to commit hash")
13151315
}
1316-
if err := cs.blockExec.ValidateBlock(cs.state, block); err != nil {
1316+
if err := cs.state.ValidateBlock(block); err != nil {
13171317
panic(fmt.Sprintf("+2/3 committed an invalid block: %v", err))
13181318
}
13191319

tm2/pkg/bft/node/node_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ func TestCreateProposalBlock(t *testing.T) {
304304
proposerAddr,
305305
)
306306

307-
err = blockExec.ValidateBlock(state, block)
307+
err = state.ValidateBlock(block)
308308
assert.NoError(t, err)
309309
}
310310

tm2/pkg/bft/state/execution.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,13 @@ func (blockExec *BlockExecutor) CreateProposalBlock(
8282
return state.MakeBlock(height, txs, commit, proposerAddr)
8383
}
8484

85-
// ValidateBlock validates the given block against the given state.
86-
// If the block is invalid, it returns an error.
87-
// Validation does not mutate state, but does require historical information from the stateDB
88-
func (blockExec *BlockExecutor) ValidateBlock(state State, block *types.Block) error {
89-
return validateBlock(blockExec.db, state, block)
90-
}
91-
9285
// ApplyBlock validates the block against the state, executes it against the app,
9386
// fires the relevant events, commits the app, and saves the new state and responses.
9487
// It's the only function that needs to be called
9588
// from outside this package to process and commit an entire block.
9689
// It takes a blockID to avoid recomputing the parts hash.
9790
func (blockExec *BlockExecutor) ApplyBlock(state State, blockID types.BlockID, block *types.Block) (State, error) {
98-
if err := blockExec.ValidateBlock(state, block); err != nil {
91+
if err := state.ValidateBlock(block); err != nil {
9992
return state, InvalidBlockError(err)
10093
}
10194

tm2/pkg/bft/state/helpers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func makeAndApplyGoodBlock(state sm.State, height int64, lastCommit *types.Commi
5252
blockExec *sm.BlockExecutor,
5353
) (sm.State, types.BlockID, error) {
5454
block, _ := state.MakeBlock(height, makeTxs(height), lastCommit, proposerAddr)
55-
if err := blockExec.ValidateBlock(state, block); err != nil {
55+
if err := state.ValidateBlock(block); err != nil {
5656
return state, types.BlockID{}, err
5757
}
5858
blockID := types.BlockID{Hash: block.Hash(), PartsHeader: types.PartSetHeader{}}

tm2/pkg/bft/state/validation.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ import (
66
"fmt"
77

88
"github.com/gnolang/gno/tm2/pkg/bft/types"
9-
"github.com/gnolang/gno/tm2/pkg/crypto"
10-
dbm "github.com/gnolang/gno/tm2/pkg/db"
119
)
1210

1311
// -----------------------------------------------------
1412
// Validate block
1513

16-
func validateBlock(stateDB dbm.DB, state State, block *types.Block) error {
14+
func (state State) ValidateBlock(block *types.Block) error {
1715
// Validate internal consistency.
1816
if err := block.ValidateBasic(); err != nil {
1917
return err
@@ -137,9 +135,8 @@ func validateBlock(stateDB dbm.DB, state State, block *types.Block) error {
137135

138136
// NOTE: We can't actually verify it's the right proposer because we dont
139137
// know what round the block was first proposed. So just check that it's
140-
// a legit address and a known validator.
141-
if len(block.ProposerAddress) != crypto.AddressSize ||
142-
!state.Validators.HasAddress(block.ProposerAddress) {
138+
// a legit address from a known validator.
139+
if !state.Validators.HasAddress(block.ProposerAddress) {
143140
return fmt.Errorf("Block.Header.ProposerAddress, %X, is not a validator",
144141
block.ProposerAddress,
145142
)

tm2/pkg/bft/state/validation_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func TestValidateBlockHeader(t *testing.T) {
142142
for _, tc := range testCases {
143143
block, _ := state.MakeBlock(height, makeTxs(height), lastCommit, proposerAddr)
144144
tc.malleateBlock(block)
145-
err := blockExec.ValidateBlock(state, block)
145+
err := state.ValidateBlock(block)
146146
assert.ErrorContains(t, err, tc.expectedError, tc.name)
147147
}
148148

@@ -179,15 +179,15 @@ func TestValidateBlockCommit(t *testing.T) {
179179
require.NoError(t, err, "height %d", height)
180180
wrongHeightCommit := types.NewCommit(state.LastBlockID, []*types.CommitSig{wrongHeightVote.CommitSig()})
181181
block, _ := state.MakeBlock(height, makeTxs(height), wrongHeightCommit, proposerAddr)
182-
err = blockExec.ValidateBlock(state, block)
182+
err = state.ValidateBlock(block)
183183
_, isErrInvalidCommitHeight := err.(types.InvalidCommitHeightError)
184184
require.True(t, isErrInvalidCommitHeight, "expected InvalidCommitHeightError at height %d but got: %v", height, err)
185185

186186
/*
187187
#2589: test len(block.LastCommit.Precommits) == state.LastValidators.Size()
188188
*/
189189
block, _ = state.MakeBlock(height, makeTxs(height), wrongPrecommitsCommit, proposerAddr)
190-
err = blockExec.ValidateBlock(state, block)
190+
err = state.ValidateBlock(block)
191191
_, isErrInvalidCommitPrecommits := err.(types.InvalidCommitPrecommitsError)
192192
require.True(t, isErrInvalidCommitPrecommits, "expected InvalidCommitPrecommitsError at height %d but got: %v", height, err)
193193
}

tm2/pkg/bft/store/store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func (bs *BlockStore) SaveBlock(block *types.Block, blockParts *types.PartSet, s
152152
panic(fmt.Sprintf("BlockStore can only save contiguous blocks. Wanted %v, got %v", w, g))
153153
}
154154
if !blockParts.IsComplete() {
155-
panic(fmt.Sprintf("BlockStore can only save complete block part sets"))
155+
panic("BlockStore can only save complete block part sets")
156156
}
157157

158158
// Save block meta

tm2/pkg/bft/types/block.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/gnolang/gno/tm2/pkg/amino"
1111
typesver "github.com/gnolang/gno/tm2/pkg/bft/types/version"
1212
"github.com/gnolang/gno/tm2/pkg/bitarray"
13-
"github.com/gnolang/gno/tm2/pkg/crypto"
1413
"github.com/gnolang/gno/tm2/pkg/crypto/merkle"
1514
"github.com/gnolang/gno/tm2/pkg/crypto/tmhash"
1615
"github.com/gnolang/gno/tm2/pkg/errors"
@@ -54,10 +53,9 @@ func (b *Block) ValidateBasic() error {
5453
)
5554
}
5655

57-
// TODO: fix tests so we can do this
58-
/*if b.TotalTxs < b.NumTxs {
56+
if b.TotalTxs < b.NumTxs {
5957
return fmt.Errorf("Header.TotalTxs (%d) is less than Header.NumTxs (%d)", b.TotalTxs, b.NumTxs)
60-
}*/
58+
}
6159
if b.TotalTxs < 0 {
6260
return errors.New("Negative Header.TotalTxs")
6361
}
@@ -115,11 +113,6 @@ func (b *Block) ValidateBasic() error {
115113
return fmt.Errorf("wrong Header.LastResultsHash: %w", err)
116114
}
117115

118-
if len(b.ProposerAddress) != crypto.AddressSize {
119-
return fmt.Errorf("expected len(Header.ProposerAddress) to be %d, got %d",
120-
crypto.AddressSize, len(b.ProposerAddress))
121-
}
122-
123116
return nil
124117
}
125118

@@ -265,8 +258,9 @@ func (h *Header) GetTime() time.Time { return h.Time }
265258
func MakeBlock(height int64, txs []Tx, lastCommit *Commit) *Block {
266259
block := &Block{
267260
Header: Header{
268-
Height: height,
269-
NumTxs: int64(len(txs)),
261+
Height: height,
262+
NumTxs: int64(len(txs)),
263+
TotalTxs: int64(len(txs)),
270264
},
271265
Data: Data{
272266
Txs: txs,

tm2/pkg/bft/types/validator_set_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ func TestProposerSelection3(t *testing.T) {
249249
got := vset.GetProposer().Address
250250
expected := proposerOrder[j%4].Address
251251
if got != expected {
252-
t.Fatalf(fmt.Sprintf("vset.Proposer (%X) does not match expected proposer (%X) for (%d, %d)", got, expected, i, j))
252+
t.Fatalf("vset.Proposer (%X) does not match expected proposer (%X) for (%d, %d)", got, expected, i, j)
253253
}
254254

255255
// serialize, deserialize, check proposer
@@ -263,7 +263,7 @@ func TestProposerSelection3(t *testing.T) {
263263
computed := vset.GetProposer() // findGetProposer()
264264
if i != 0 {
265265
if got != computed.Address {
266-
t.Fatalf(fmt.Sprintf("vset.Proposer (%X) does not match computed proposer (%X) for (%d, %d)", got, computed.Address, i, j))
266+
t.Fatalf("vset.Proposer (%X) does not match computed proposer (%X) for (%d, %d)", got, computed.Address, i, j)
267267
}
268268
}
269269

0 commit comments

Comments
 (0)