Skip to content

Commit 6ef531e

Browse files
JonathanOppenheimeralarso16StephenButtolph
authored
style: enable errcheck linter (#1203)
Signed-off-by: Jonathan Oppenheimer <[email protected]> Signed-off-by: Jonathan Oppenheimer <[email protected]> Co-authored-by: Austin Larson <[email protected]> Co-authored-by: Stephen Buttolph <[email protected]>
1 parent c06f5df commit 6ef531e

File tree

19 files changed

+86
-48
lines changed

19 files changed

+86
-48
lines changed

.avalanche-golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ linters:
5555
- bodyclose
5656
- copyloopvar
5757
# - depguard
58-
# - errcheck
58+
- errcheck
5959
- errorlint
6060
- forbidigo
6161
- goconst

core/fifo_cache.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ func (f *BufferFIFOCache[K, V]) Put(key K, val V) {
4444
f.l.Lock()
4545
defer f.l.Unlock()
4646

47-
f.buffer.Insert(key) // Insert will remove the oldest [K] if we are at the [limit]
47+
// Insert will remove the oldest [K] if we are at the [limit] -
48+
// remove always returns nil, so it is safe to ignore this error.
49+
_ = f.buffer.Insert(key)
4850
f.m[key] = val
4951
}
5052

core/state_manager_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ func TestCappedMemoryTrieWriter(t *testing.T) {
5656
require.NoError(w.InsertTrie(block))
5757
require.Zero(m.LastDereference, "should not have dereferenced block on insert")
5858
require.Zero(m.LastCommit, "should not have committed block on insert")
59+
require.NoError(w.AcceptTrie(block))
5960

60-
w.AcceptTrie(block)
6161
if i <= tipBufferSize {
6262
require.Zero(m.LastDereference, "should not have dereferenced block on accept")
6363
} else {
@@ -71,7 +71,7 @@ func TestCappedMemoryTrieWriter(t *testing.T) {
7171
m.LastCommit = common.Hash{}
7272
}
7373

74-
w.RejectTrie(block)
74+
require.NoError(w.RejectTrie(block))
7575
require.Equal(block.Root(), m.LastDereference, "should have dereferenced block on reject")
7676
require.Zero(m.LastCommit, "should not have committed block on reject")
7777
m.LastDereference = common.Hash{}
@@ -96,12 +96,12 @@ func TestNoPruningTrieWriter(t *testing.T) {
9696
require.Zero(m.LastDereference, "should not have dereferenced block on insert")
9797
require.Zero(m.LastCommit, "should not have committed block on insert")
9898

99-
w.AcceptTrie(block)
99+
require.NoError(w.AcceptTrie(block))
100100
require.Zero(m.LastDereference, "should not have dereferenced block on accept")
101101
require.Equal(block.Root(), m.LastCommit, "should have committed block on accept")
102102
m.LastCommit = common.Hash{}
103103

104-
w.RejectTrie(block)
104+
require.NoError(w.RejectTrie(block))
105105
require.Equal(block.Root(), m.LastDereference, "should have dereferenced block on reject")
106106
require.Zero(m.LastCommit, "should not have committed block on reject")
107107
m.LastDereference = common.Hash{}

plugin/evm/atomic/state/atomic_trie.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,9 @@ func (a *AtomicTrie) InsertTrie(nodes *trienode.NodeSet, root common.Hash) error
244244
return err
245245
}
246246
}
247-
a.trieDB.Reference(root, common.Hash{})
247+
if err := a.trieDB.Reference(root, common.Hash{}); err != nil {
248+
return err
249+
}
248250

249251
// The use of [Cap] in [insertTrie] prevents exceeding the configured memory
250252
// limit (and OOM) in case there is a large backlog of processing (unaccepted) blocks.
@@ -285,12 +287,13 @@ func (a *AtomicTrie) AcceptTrie(height uint64, root common.Hash) (bool, error) {
285287
// - not committted, in which case the current root we are inserting contains
286288
// references to all the relevant data from the previous root, so the previous
287289
// root can be dereferenced.
288-
a.trieDB.Dereference(a.lastAcceptedRoot)
290+
if err := a.trieDB.Dereference(a.lastAcceptedRoot); err != nil {
291+
return false, err
292+
}
289293
a.lastAcceptedRoot = root
290294
return hasCommitted, nil
291295
}
292296

293297
func (a *AtomicTrie) RejectTrie(root common.Hash) error {
294-
a.trieDB.Dereference(root)
295-
return nil
298+
return a.trieDB.Dereference(root)
296299
}

plugin/evm/atomic/state/atomic_trie_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ func TestAtomicTrie_AcceptTrie(t *testing.T) {
604604
_, storageSize, _ := atomicTrie.trieDB.Size()
605605
require.NotZero(t, storageSize, "there should be a dirty node taking up storage space")
606606
}
607-
atomicTrie.updateLastCommitted(testCase.lastCommittedRoot, testCase.lastCommittedHeight)
607+
require.NoError(t, atomicTrie.updateLastCommitted(testCase.lastCommittedRoot, testCase.lastCommittedHeight))
608608

609609
hasCommitted, err := atomicTrie.AcceptTrie(testCase.height, testCase.root)
610610
require.NoError(t, err)

plugin/evm/atomic/vm/block_extension.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,13 @@ func (be *blockExtension) Reject() error {
229229
// CleanupVerified is called when the block is cleaned up after a failed insertion.
230230
func (be *blockExtension) CleanupVerified() {
231231
vm := be.blockExtender.vm
232-
if atomicState, err := vm.AtomicBackend.GetVerifiedAtomicState(be.block.GetEthBlock().Hash()); err == nil {
233-
atomicState.Reject()
232+
// If the state isn't found, no need to reject (it was never verified).
233+
atomicState, err := vm.AtomicBackend.GetVerifiedAtomicState(be.block.GetEthBlock().Hash())
234+
if err != nil {
235+
return
234236
}
237+
// atomicState.Reject() never returns an error in practice.
238+
atomicState.Reject() //nolint:errcheck // Reject never returns an error
235239
}
236240

237241
// AtomicTxs returns the atomic transactions in this block.

plugin/evm/atomic/vm/vm_test.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,13 @@ func testIssueAtomicTxs(t *testing.T, scheme string) {
197197
utxos := map[ids.ShortID]uint64{
198198
vmtest.TestShortIDAddrs[0]: importAmount,
199199
}
200-
addUTXOs(tvm.AtomicMemory, vm.Ctx, utxos)
200+
require.NoError(addUTXOs(tvm.AtomicMemory, vm.Ctx, utxos))
201201
defer func() {
202202
require.NoError(vm.Shutdown(t.Context()))
203203
}()
204204

205205
importTx, err := vm.newImportTx(vm.Ctx.XChainID, vmtest.TestEthAddrs[0], vmtest.InitialBaseFee, vmtest.TestKeys[0:1])
206206
require.NoError(err)
207-
208207
require.NoError(vm.AtomicMempool.AddLocalTx(importTx))
209208

210209
msg, err := vm.WaitForEvent(t.Context())
@@ -213,11 +212,8 @@ func testIssueAtomicTxs(t *testing.T, scheme string) {
213212

214213
blk, err := vm.BuildBlock(t.Context())
215214
require.NoError(err)
216-
217215
require.NoError(blk.Verify(t.Context()))
218-
219216
require.NoError(vm.SetPreference(t.Context(), blk.ID()))
220-
221217
require.NoError(blk.Accept(t.Context()))
222218

223219
lastAcceptedID, err := vm.LastAccepted(t.Context())
@@ -231,7 +227,6 @@ func testIssueAtomicTxs(t *testing.T, scheme string) {
231227
wrappedState := extstate.New(state)
232228
exportTx, err := atomic.NewExportTx(vm.Ctx, vm.CurrentRules(), wrappedState, vm.Ctx.AVAXAssetID, importAmount-(2*ap0.AtomicTxFee), vm.Ctx.XChainID, vmtest.TestShortIDAddrs[0], vmtest.InitialBaseFee, vmtest.TestKeys[0:1])
233229
require.NoError(err)
234-
235230
require.NoError(vm.AtomicMempool.AddLocalTx(exportTx))
236231

237232
msg, err = vm.WaitForEvent(t.Context())
@@ -240,9 +235,7 @@ func testIssueAtomicTxs(t *testing.T, scheme string) {
240235

241236
blk2, err := vm.BuildBlock(t.Context())
242237
require.NoError(err)
243-
244238
require.NoError(blk2.Verify(t.Context()))
245-
246239
require.NoError(blk2.Accept(t.Context()))
247240

248241
lastAcceptedID, err = vm.LastAccepted(t.Context())
@@ -842,18 +835,17 @@ func TestConsecutiveAtomicTransactionsRevertSnapshot(t *testing.T) {
842835
blk, err := vm.BuildBlock(t.Context())
843836
require.NoError(err)
844837
require.NoError(blk.Verify(t.Context()))
845-
846838
require.NoError(vm.SetPreference(t.Context(), blk.ID()))
847-
848839
require.NoError(blk.Accept(t.Context()))
849840

850841
newHead := <-newTxPoolHeadChan
851842
require.Equal(common.Hash(blk.ID()), newHead.Head.Hash())
852843

853844
// Add the two conflicting transactions directly to the mempool, so that two consecutive transactions
854845
// will fail verification when build block is called.
855-
vm.AtomicMempool.AddRemoteTx(importTxs[1])
856-
vm.AtomicMempool.AddRemoteTx(importTxs[2])
846+
require.NoError(vm.AtomicMempool.ForceAddTx(importTxs[1]))
847+
require.NoError(vm.AtomicMempool.ForceAddTx(importTxs[2]))
848+
require.Equal(2, vm.AtomicMempool.Txs.PendingLen())
857849

858850
_, err = vm.BuildBlock(t.Context())
859851
require.ErrorIs(err, ErrEmptyBlock)
@@ -890,7 +882,7 @@ func TestAtomicTxBuildBlockDropsConflicts(t *testing.T) {
890882
err = vm.AtomicMempool.AddLocalTx(conflictTx)
891883
require.ErrorIs(err, txpool.ErrConflict)
892884
// force add the tx
893-
vm.AtomicMempool.ForceAddTx(conflictTx)
885+
require.NoError(vm.AtomicMempool.ForceAddTx(conflictTx))
894886
conflictSets[index].Add(conflictTx.ID())
895887
}
896888
msg, err := vm.WaitForEvent(t.Context())
@@ -1637,7 +1629,7 @@ func TestWaitForEvent(t *testing.T) {
16371629
},
16381630
}}}}))
16391631
testCase.testCase(t, vm, address, key)
1640-
vm.Shutdown(t.Context())
1632+
require.NoError(t, vm.Shutdown(t.Context()))
16411633
})
16421634
}
16431635
}

plugin/evm/extension/config.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ type BlockExtension interface {
101101
// it can be implemented to extend inner block verification
102102
SemanticVerify() error
103103
// CleanupVerified is called when a block has passed SemanticVerify and SynctacticVerify,
104-
// and should be cleaned up due to error or verification runs under non-write mode. This
105-
// does not return an error because the block has already been verified.
104+
// and should be cleaned up due to error or verification runs under non-write mode.
106105
CleanupVerified()
107106
// Accept is called when a block is accepted by the block manager. Accept takes a
108107
// database.Batch that contains the changes that were made to the database as a result

plugin/evm/message/codec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func init() {
3838
// See https://github.com/ava-labs/coreth/pull/999
3939
c.SkipRegistrations(3)
4040

41-
Codec.RegisterCodec(Version, c)
41+
errs.Add(Codec.RegisterCodec(Version, c))
4242

4343
if errs.Errored() {
4444
panic(errs.Err)

plugin/evm/vm.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,9 @@ func (vm *VM) Initialize(
467467

468468
// Add p2p warp message warpHandler
469469
warpHandler := acp118.NewCachedHandler(meteredCache, vm.warpBackend, vm.ctx.WarpSigner)
470-
vm.Network.AddHandler(p2p.SignatureRequestHandlerID, warpHandler)
470+
if err = vm.Network.AddHandler(p2p.SignatureRequestHandlerID, warpHandler); err != nil {
471+
return err
472+
}
471473

472474
vm.stateSyncDone = make(chan struct{})
473475

@@ -896,7 +898,9 @@ func (vm *VM) Shutdown(context.Context) error {
896898
for _, handler := range vm.rpcHandlers {
897899
handler.Stop()
898900
}
899-
vm.eth.Stop()
901+
if err := vm.eth.Stop(); err != nil {
902+
log.Error("error stopping eth", "err", err)
903+
}
900904
vm.shutdownWg.Wait()
901905
return nil
902906
}

0 commit comments

Comments
 (0)