Skip to content

Commit

Permalink
chore: enable errcheck linter (cosmos#16406)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruthishvitwit authored Jul 12, 2023
1 parent c5df6a3 commit fd7e549
Show file tree
Hide file tree
Showing 140 changed files with 1,019 additions and 617 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ run:
linters:
disable-all: true
enable:
- errcheck
- dogsled
- exportloopref
- goconst
Expand Down
43 changes: 24 additions & 19 deletions baseapp/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func TestABCI_First_block_Height(t *testing.T) {
InitialHeight: 1,
})
require.NoError(t, err)

_, err = app.Commit()
require.NoError(t, err)

Expand Down Expand Up @@ -185,9 +186,9 @@ func TestABCI_InitChain_WithInitialHeight(t *testing.T) {
},
)
require.NoError(t, err)

_, err = app.Commit()
require.NoError(t, err)

require.Equal(t, int64(3), app.LastBlockHeight())
}

Expand All @@ -210,7 +211,6 @@ func TestABCI_FinalizeBlock_WithInitialHeight(t *testing.T) {
require.NoError(t, err)
_, err = app.Commit()
require.NoError(t, err)

require.Equal(t, int64(3), app.LastBlockHeight())
}

Expand Down Expand Up @@ -391,6 +391,7 @@ func TestABCI_GRPCQuery(t *testing.T) {

_, err = suite.baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{Height: suite.baseApp.LastBlockHeight() + 1})
require.NoError(t, err)

_, err = suite.baseApp.Commit()
require.NoError(t, err)

Expand Down Expand Up @@ -596,6 +597,7 @@ func TestABCI_FinalizeBlock_DeliverTx(t *testing.T) {

_, err = suite.baseApp.Commit()
require.NoError(t, err)

}
}

Expand Down Expand Up @@ -646,7 +648,9 @@ func TestABCI_FinalizeBlock_MultiMsg(t *testing.T) {
msgs = append(msgs, &baseapptestutil.MsgCounter2{Counter: 0, Signer: addr.String()})
msgs = append(msgs, &baseapptestutil.MsgCounter2{Counter: 1, Signer: addr.String()})

builder.SetMsgs(msgs...)
err = builder.SetMsgs(msgs...)
require.NoError(t, err)

builder.SetMemo(tx.GetMemo())
setTxSignature(t, builder, 0)

Expand Down Expand Up @@ -750,12 +754,10 @@ func TestABCI_InvalidTransaction(t *testing.T) {
ConsensusParams: &cmtproto.ConsensusParams{},
})
require.NoError(t, err)

_, err = suite.baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{
Height: 1,
})
require.NoError(t, err)

// malformed transaction bytes
{
bz := []byte("example vote extension")
Expand Down Expand Up @@ -818,7 +820,8 @@ func TestABCI_InvalidTransaction(t *testing.T) {
{
txBuilder := suite.txConfig.NewTxBuilder()
_, _, addr := testdata.KeyTestPubAddr()
txBuilder.SetMsgs(&baseapptestutil.MsgCounter2{Signer: addr.String()})
err = txBuilder.SetMsgs(&baseapptestutil.MsgCounter2{Signer: addr.String()})
require.NoError(t, err)
setTxSignature(t, txBuilder, 0)
unknownRouteTx := txBuilder.GetTx()

Expand All @@ -831,10 +834,11 @@ func TestABCI_InvalidTransaction(t *testing.T) {
require.EqualValues(t, sdkerrors.ErrUnknownRequest.ABCICode(), code, err)

txBuilder = suite.txConfig.NewTxBuilder()
txBuilder.SetMsgs(
err = txBuilder.SetMsgs(
&baseapptestutil.MsgCounter{Signer: addr.String()},
&baseapptestutil.MsgCounter2{Signer: addr.String()},
)
require.NoError(t, err)
setTxSignature(t, txBuilder, 0)
unknownRouteTx = txBuilder.GetTx()

Expand All @@ -850,11 +854,11 @@ func TestABCI_InvalidTransaction(t *testing.T) {
// Transaction with an unregistered message
{
txBuilder := suite.txConfig.NewTxBuilder()
txBuilder.SetMsgs(&testdata.MsgCreateDog{})
err = txBuilder.SetMsgs(&testdata.MsgCreateDog{})
require.NoError(t, err)
tx := txBuilder.GetTx()

_, _, err := suite.baseApp.SimDeliver(suite.txConfig.TxEncoder(), tx)
require.Error(t, err)
space, code, _ := errorsmod.ABCIInfo(err, false)
require.EqualValues(t, sdkerrors.ErrTxDecode.ABCICode(), code)
require.EqualValues(t, sdkerrors.ErrTxDecode.Codespace(), space)
Expand Down Expand Up @@ -1019,7 +1023,7 @@ func TestABCI_MaxBlockGasLimits(t *testing.T) {
tx := tc.tx

// reset block gas
_, err = suite.baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{Height: suite.baseApp.LastBlockHeight() + 1})
_, err := suite.baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{Height: suite.baseApp.LastBlockHeight() + 1})
require.NoError(t, err)

// execute the transaction multiple times
Expand Down Expand Up @@ -1269,7 +1273,7 @@ func TestABCI_GetBlockRetentionHeight(t *testing.T) {
tc := tc

tc.bapp.SetParamStore(&paramStore{db: dbm.NewMemDB()})
_, err = tc.bapp.InitChain(&abci.RequestInitChain{
_, err := tc.bapp.InitChain(&abci.RequestInitChain{
ConsensusParams: &cmtproto.ConsensusParams{
Evidence: &cmtproto.EvidenceParams{
MaxAgeNumBlocks: tc.maxAgeBlocks,
Expand Down Expand Up @@ -1474,11 +1478,11 @@ func TestABCI_Proposals_WithVE(t *testing.T) {

suite := NewBaseAppSuite(t, setInitChainerOpt, prepareOpt)

suite.baseApp.InitChain(&abci.RequestInitChain{
_, err := suite.baseApp.InitChain(&abci.RequestInitChain{
InitialHeight: 1,
ConsensusParams: &cmtproto.ConsensusParams{},
})

require.NoError(t, err)
reqPrepareProposal := abci.RequestPrepareProposal{
MaxTxBytes: 100000,
Height: 1, // this value can't be 0
Expand Down Expand Up @@ -1573,20 +1577,21 @@ func TestABCI_PrepareProposal_MaxGas(t *testing.T) {
baseapptestutil.RegisterCounterServer(suite.baseApp.MsgServiceRouter(), NoopCounterServerImpl{})

// set max block gas limit to 100
suite.baseApp.InitChain(&abci.RequestInitChain{
_, err := suite.baseApp.InitChain(&abci.RequestInitChain{
ConsensusParams: &cmtproto.ConsensusParams{
Block: &cmtproto.BlockParams{MaxGas: 100},
},
})

require.NoError(t, err)
// insert 100 txs, each with a gas limit of 10
_, _, addr := testdata.KeyTestPubAddr()
for i := int64(0); i < 100; i++ {
msg := &baseapptestutil.MsgCounter{Counter: i, FailOnHandler: false, Signer: addr.String()}
msgs := []sdk.Msg{msg}

builder := suite.txConfig.NewTxBuilder()
builder.SetMsgs(msgs...)
err = builder.SetMsgs(msgs...)
require.NoError(t, err)
builder.SetMemo("counter=" + strconv.FormatInt(i, 10) + "&failOnAnte=false")
builder.SetGasLimit(10)
setTxSignature(t, builder, uint64(i))
Expand Down Expand Up @@ -1884,13 +1889,13 @@ func TestABCI_HaltChain(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
suite := NewBaseAppSuite(t, baseapp.SetHaltHeight(tc.haltHeight), baseapp.SetHaltTime(tc.haltTime))
suite.baseApp.InitChain(&abci.RequestInitChain{
_, err := suite.baseApp.InitChain(&abci.RequestInitChain{
ConsensusParams: &cmtproto.ConsensusParams{},
InitialHeight: tc.blockHeight,
})

require.NoError(t, err)
app := suite.baseApp
_, err := app.FinalizeBlock(&abci.RequestFinalizeBlock{
_, err = app.FinalizeBlock(&abci.RequestFinalizeBlock{
Height: tc.blockHeight,
Time: time.Unix(tc.blockTime, 0),
})
Expand Down
25 changes: 15 additions & 10 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ func NewBaseAppSuiteWithSnapshots(t *testing.T, cfg SnapshotsConfig, opts ...fun
}

builder := suite.txConfig.NewTxBuilder()
builder.SetMsgs(msgs...)
err := builder.SetMsgs(msgs...)
require.NoError(t, err)
setTxSignature(t, builder, 0)

txBytes, err := suite.txConfig.TxEncoder()(builder.GetTx())
Expand Down Expand Up @@ -488,7 +489,8 @@ func TestCustomRunTxPanicHandler(t *testing.T) {
require.PanicsWithValue(t, customPanicMsg, func() {
bz, err := suite.txConfig.TxEncoder()(tx)
require.NoError(t, err)
suite.baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{Height: 1, Txs: [][]byte{bz}})
_, err = suite.baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{Height: 1, Txs: [][]byte{bz}})
require.NoError(t, err)
})
}
}
Expand Down Expand Up @@ -530,7 +532,7 @@ func TestBaseAppAnteHandler(t *testing.T) {
// execute at tx that will pass the ante handler (the checkTx state should
// mutate) but will fail the message handler
tx = newTxCounter(t, suite.txConfig, 0, 0)
tx = setFailOnHandler(suite.txConfig, tx, true)
tx = setFailOnHandler(t, suite.txConfig, tx, true)

txBytes, err = suite.txConfig.TxEncoder()(tx)
require.NoError(t, err)
Expand Down Expand Up @@ -562,7 +564,8 @@ func TestBaseAppAnteHandler(t *testing.T) {
require.Equal(t, int64(2), getIntFromStore(t, store, anteKey))
require.Equal(t, int64(1), getIntFromStore(t, store, deliverKey))

suite.baseApp.Commit()
_, err = suite.baseApp.Commit()
require.NoError(t, err)
}

// Test and ensure that invalid block heights always cause errors.
Expand All @@ -585,7 +588,6 @@ func TestABCI_CreateQueryContext(t *testing.T) {
require.NoError(t, err)
_, err = app.Commit()
require.NoError(t, err)

testCases := []struct {
name string
height int64
Expand Down Expand Up @@ -622,19 +624,22 @@ func TestGetMaximumBlockGas(t *testing.T) {
suite := NewBaseAppSuite(t)
_, err := suite.baseApp.InitChain(&abci.RequestInitChain{})
require.NoError(t, err)

ctx := suite.baseApp.NewContext(true)

suite.baseApp.StoreConsensusParams(ctx, cmtproto.ConsensusParams{Block: &cmtproto.BlockParams{MaxGas: 0}})
err = suite.baseApp.StoreConsensusParams(ctx, cmtproto.ConsensusParams{Block: &cmtproto.BlockParams{MaxGas: 0}})
require.NoError(t, err)
require.Equal(t, uint64(0), suite.baseApp.GetMaximumBlockGas(ctx))

suite.baseApp.StoreConsensusParams(ctx, cmtproto.ConsensusParams{Block: &cmtproto.BlockParams{MaxGas: -1}})
err = suite.baseApp.StoreConsensusParams(ctx, cmtproto.ConsensusParams{Block: &cmtproto.BlockParams{MaxGas: -1}})
require.NoError(t, err)
require.Equal(t, uint64(0), suite.baseApp.GetMaximumBlockGas(ctx))

suite.baseApp.StoreConsensusParams(ctx, cmtproto.ConsensusParams{Block: &cmtproto.BlockParams{MaxGas: 5000000}})
err = suite.baseApp.StoreConsensusParams(ctx, cmtproto.ConsensusParams{Block: &cmtproto.BlockParams{MaxGas: 5000000}})
require.NoError(t, err)
require.Equal(t, uint64(5000000), suite.baseApp.GetMaximumBlockGas(ctx))

suite.baseApp.StoreConsensusParams(ctx, cmtproto.ConsensusParams{Block: &cmtproto.BlockParams{MaxGas: -5000000}})
err = suite.baseApp.StoreConsensusParams(ctx, cmtproto.ConsensusParams{Block: &cmtproto.BlockParams{MaxGas: -5000000}})
require.NoError(t, err)
require.Panics(t, func() { suite.baseApp.GetMaximumBlockGas(ctx) })
}

Expand Down
3 changes: 2 additions & 1 deletion baseapp/block_gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,13 @@ func TestBaseApp_BlockGas(t *testing.T) {
genState := GenesisStateWithSingleValidator(t, cdc, appBuilder)
stateBytes, err := cmtjson.MarshalIndent(genState, "", " ")
require.NoError(t, err)
bapp.InitChain(&abci.RequestInitChain{
_, err = bapp.InitChain(&abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
ConsensusParams: simtestutil.DefaultConsensusParams,
AppStateBytes: stateBytes,
})

require.NoError(t, err)
ctx := bapp.NewContext(false)

// tx fee
Expand Down
18 changes: 10 additions & 8 deletions baseapp/streaming_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ func TestABCI_MultiListener_StateChanges(t *testing.T) {
addListenerOpt := func(bapp *baseapp.BaseApp) { bapp.CommitMultiStore().AddListeners([]storetypes.StoreKey{distKey1}) }
suite := NewBaseAppSuite(t, anteOpt, distOpt, streamingManagerOpt, addListenerOpt)

suite.baseApp.InitChain(
_, err := suite.baseApp.InitChain(
&abci.RequestInitChain{
ConsensusParams: &tmproto.ConsensusParams{},
},
)

require.NoError(t, err)
deliverKey := []byte("deliver-key")
baseapptestutil.RegisterCounterServer(suite.baseApp.MsgServiceRouter(), CounterServerImpl{t, capKey1, deliverKey})

Expand Down Expand Up @@ -103,8 +103,8 @@ func TestABCI_MultiListener_StateChanges(t *testing.T) {
// require.Equal(t, sdk.MarkEventsToIndex(counterEvent(sdk.EventTypeMessage, counter).ToABCIEvents(), map[string]struct{}{})[0], events[2], "msg handler update counter event")
}

suite.baseApp.Commit()

_, err = suite.baseApp.Commit()
require.NoError(t, err)
require.Equal(t, expectedChangeSet, mockListener1.ChangeSet, "should contain the same changeSet")
require.Equal(t, expectedChangeSet, mockListener2.ChangeSet, "should contain the same changeSet")
}
Expand All @@ -119,9 +119,10 @@ func Test_Ctx_with_StreamingManager(t *testing.T) {
addListenerOpt := func(bapp *baseapp.BaseApp) { bapp.CommitMultiStore().AddListeners([]storetypes.StoreKey{distKey1}) }
suite := NewBaseAppSuite(t, streamingManagerOpt, addListenerOpt)

suite.baseApp.InitChain(&abci.RequestInitChain{
_, err := suite.baseApp.InitChain(&abci.RequestInitChain{
ConsensusParams: &tmproto.ConsensusParams{},
})
require.NoError(t, err)

ctx := getFinalizeBlockStateCtx(suite.baseApp)
sm := ctx.StreamingManager()
Expand All @@ -133,14 +134,15 @@ func Test_Ctx_with_StreamingManager(t *testing.T) {

for blockN := 0; blockN < nBlocks; blockN++ {

suite.baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{Height: int64(blockN) + 1})

_, err = suite.baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{Height: int64(blockN) + 1})
require.NoError(t, err)
ctx := getFinalizeBlockStateCtx(suite.baseApp)
sm := ctx.StreamingManager()
require.NotNil(t, sm, fmt.Sprintf("nil StreamingManager: %v", sm))
require.Equal(t, listeners, sm.ABCIListeners, fmt.Sprintf("should contain same listeners: %v", listeners))
require.Equal(t, true, sm.StopNodeOnErr, "should contain StopNodeOnErr = true")

suite.baseApp.Commit()
_, err = suite.baseApp.Commit()
require.NoError(t, err)
}
}
13 changes: 8 additions & 5 deletions baseapp/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,8 @@ func newTxCounter(t *testing.T, cfg client.TxConfig, counter int64, msgCounters
}

builder := cfg.NewTxBuilder()
builder.SetMsgs(msgs...)
err := builder.SetMsgs(msgs...)
require.NoError(t, err)
builder.SetMemo("counter=" + strconv.FormatInt(counter, 10) + "&failOnAnte=false")
setTxSignature(t, builder, uint64(counter))

Expand All @@ -362,8 +363,8 @@ func getIntFromStore(t *testing.T, store storetypes.KVStore, key []byte) int64 {
func setFailOnAnte(t *testing.T, cfg client.TxConfig, tx signing.Tx, failOnAnte bool) signing.Tx {
t.Helper()
builder := cfg.NewTxBuilder()
builder.SetMsgs(tx.GetMsgs()...)

err := builder.SetMsgs(tx.GetMsgs()...)
require.NoError(t, err)
memo := tx.GetMemo()
vals, err := url.ParseQuery(memo)
require.NoError(t, err)
Expand All @@ -376,7 +377,8 @@ func setFailOnAnte(t *testing.T, cfg client.TxConfig, tx signing.Tx, failOnAnte
return builder.GetTx()
}

func setFailOnHandler(cfg client.TxConfig, tx signing.Tx, fail bool) signing.Tx {
func setFailOnHandler(t *testing.T, cfg client.TxConfig, tx signing.Tx, fail bool) signing.Tx {
t.Helper()
builder := cfg.NewTxBuilder()
builder.SetMemo(tx.GetMemo())

Expand All @@ -388,6 +390,7 @@ func setFailOnHandler(cfg client.TxConfig, tx signing.Tx, fail bool) signing.Tx
}
}

builder.SetMsgs(msgs...)
err := builder.SetMsgs(msgs...)
require.NoError(t, err)
return builder.GetTx()
}
6 changes: 4 additions & 2 deletions client/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,20 @@ func (s *IntegrationTestSuite) SetupSuite() {
s.NoError(err)

// init chain will set the validator set and initialize the genesis accounts
app.InitChain(&abci.RequestInitChain{
_, err = app.InitChain(&abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
ConsensusParams: sims.DefaultConsensusParams,
AppStateBytes: stateBytes,
},
)
s.NoError(err)

app.FinalizeBlock(&abci.RequestFinalizeBlock{
_, err = app.FinalizeBlock(&abci.RequestFinalizeBlock{
Height: app.LastBlockHeight() + 1,
Hash: app.LastCommitID().Hash,
NextValidatorsHash: valSet.Hash(),
})
s.NoError(err)

// end of app init

Expand Down
Loading

0 comments on commit fd7e549

Please sign in to comment.