Skip to content

Commit f3599dc

Browse files
committed
Enable extra default linters for our code
- enable errcheck, gosimple, govet, staticcheck - only scans plugin directory and _ext.go files - fix lint errors
1 parent 1630968 commit f3599dc

13 files changed

+104
-53
lines changed

.golangci.yml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ linters:
1414
- unconvert
1515
- typecheck
1616
- unused
17-
# - staticcheck
17+
- staticcheck
1818
- bidichk
1919
- durationcheck
2020
- copyloopvar
@@ -25,3 +25,18 @@ linters:
2525
- reassign
2626
- mirror
2727
- tenv
28+
- errcheck
29+
30+
issues:
31+
exclude-rules:
32+
- path: "_test\\.go"
33+
text: "SA1019: rand\\.(Seed|Read) has been deprecated since Go 1\\.20"
34+
linters:
35+
- staticcheck
36+
37+
- path-except: "(plugin\\/.+\\.go|.+_ext\\.go)"
38+
linters:
39+
- errcheck
40+
- gosimple
41+
- govet
42+
- staticcheck

plugin/evm/atomic/export_tx.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,16 @@ func (utx *UnsignedExportTx) Verify(
133133
func (utx *UnsignedExportTx) GasUsed(fixedFee bool) (uint64, error) {
134134
byteCost := calcBytesCost(len(utx.Bytes()))
135135
numSigs := uint64(len(utx.Ins))
136-
sigCost, err := math.Mul64(numSigs, secp256k1fx.CostPerSignature)
136+
sigCost, err := math.Mul(numSigs, secp256k1fx.CostPerSignature)
137137
if err != nil {
138138
return 0, err
139139
}
140-
cost, err := math.Add64(byteCost, sigCost)
140+
cost, err := math.Add(byteCost, sigCost)
141141
if err != nil {
142142
return 0, err
143143
}
144144
if fixedFee {
145-
cost, err = math.Add64(cost, params.AtomicTxBaseCost)
145+
cost, err = math.Add(cost, params.AtomicTxBaseCost)
146146
if err != nil {
147147
return 0, err
148148
}
@@ -160,15 +160,15 @@ func (utx *UnsignedExportTx) Burned(assetID ids.ID) (uint64, error) {
160160
)
161161
for _, out := range utx.ExportedOutputs {
162162
if out.AssetID() == assetID {
163-
spent, err = math.Add64(spent, out.Output().Amount())
163+
spent, err = math.Add(spent, out.Output().Amount())
164164
if err != nil {
165165
return 0, err
166166
}
167167
}
168168
}
169169
for _, in := range utx.Ins {
170170
if in.AssetID == assetID {
171-
input, err = math.Add64(input, in.Amount)
171+
input, err = math.Add(input, in.Amount)
172172
if err != nil {
173173
return 0, err
174174
}
@@ -346,7 +346,7 @@ func NewExportTx(
346346
avaxIns, avaxSigners, err = GetSpendableAVAXWithFee(ctx, state, keys, avaxNeeded, cost, baseFee)
347347
default:
348348
var newAvaxNeeded uint64
349-
newAvaxNeeded, err = math.Add64(avaxNeeded, params.AvalancheAtomicTxFee)
349+
newAvaxNeeded, err = math.Add(avaxNeeded, params.AvalancheAtomicTxFee)
350350
if err != nil {
351351
return nil, errOverflowExport
352352
}
@@ -486,7 +486,7 @@ func GetSpendableAVAXWithFee(
486486
return nil, nil, err
487487
}
488488

489-
newAmount, err := math.Add64(amount, initialFee)
489+
newAmount, err := math.Add(amount, initialFee)
490490
if err != nil {
491491
return nil, nil, err
492492
}
@@ -527,7 +527,7 @@ func GetSpendableAVAXWithFee(
527527
// Update the cost for the next iteration
528528
cost = newCost
529529

530-
newAmount, err := math.Add64(amount, additionalFee)
530+
newAmount, err := math.Add(amount, additionalFee)
531531
if err != nil {
532532
return nil, nil, err
533533
}

plugin/evm/atomic/import_tx.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,13 @@ func (utx *UnsignedImportTx) GasUsed(fixedFee bool) (uint64, error) {
144144
if err != nil {
145145
return 0, err
146146
}
147-
cost, err = math.Add64(cost, inCost)
147+
cost, err = math.Add(cost, inCost)
148148
if err != nil {
149149
return 0, err
150150
}
151151
}
152152
if fixedFee {
153-
cost, err = math.Add64(cost, params.AtomicTxBaseCost)
153+
cost, err = math.Add(cost, params.AtomicTxBaseCost)
154154
if err != nil {
155155
return 0, err
156156
}
@@ -167,15 +167,15 @@ func (utx *UnsignedImportTx) Burned(assetID ids.ID) (uint64, error) {
167167
)
168168
for _, out := range utx.Outs {
169169
if out.AssetID == assetID {
170-
spent, err = math.Add64(spent, out.Amount)
170+
spent, err = math.Add(spent, out.Amount)
171171
if err != nil {
172172
return 0, err
173173
}
174174
}
175175
}
176176
for _, in := range utx.ImportedInputs {
177177
if in.AssetID() == assetID {
178-
input, err = math.Add64(input, in.Input().Amount())
178+
input, err = math.Add(input, in.Input().Amount())
179179
if err != nil {
180180
return 0, err
181181
}
@@ -311,7 +311,7 @@ func NewImportTx(
311311
continue
312312
}
313313
aid := utxo.AssetID()
314-
importedAmount[aid], err = math.Add64(importedAmount[aid], input.Amount())
314+
importedAmount[aid], err = math.Add(importedAmount[aid], input.Amount())
315315
if err != nil {
316316
return nil, err
317317
}

plugin/evm/atomic_syncer_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import (
77
"bytes"
88
"context"
99
"fmt"
10-
"math/rand"
1110
"testing"
1211

1312
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
1414

1515
"github.com/ava-labs/avalanchego/database"
1616
"github.com/ava-labs/avalanchego/database/memdb"
@@ -80,7 +80,9 @@ func testAtomicSyncer(t *testing.T, serverTrieDB *triedb.Database, targetHeight
8080
return leafsResponse, nil
8181
}
8282

83-
syncer.Start(ctx)
83+
err = syncer.Start(ctx)
84+
require.NoError(t, err)
85+
8486
if err := <-syncer.Done(); err == nil {
8587
t.Fatalf("Expected syncer to fail at checkpoint with numLeaves %d", numLeaves)
8688
}
@@ -104,7 +106,9 @@ func testAtomicSyncer(t *testing.T, serverTrieDB *triedb.Database, targetHeight
104106
return leafsResponse, nil
105107
}
106108

107-
syncer.Start(ctx)
109+
err = syncer.Start(ctx)
110+
require.NoError(t, err)
111+
108112
if err := <-syncer.Done(); err != nil {
109113
t.Fatalf("Expected syncer to finish successfully but failed due to %s", err)
110114
}
@@ -153,7 +157,6 @@ func testAtomicSyncer(t *testing.T, serverTrieDB *triedb.Database, targetHeight
153157
}
154158

155159
func TestAtomicSyncer(t *testing.T) {
156-
rand.Seed(1)
157160
targetHeight := 10 * uint64(commitInterval)
158161
serverTrieDB := triedb.NewDatabase(rawdb.NewMemoryDatabase(), nil)
159162
root, _, _ := syncutils.GenerateTrie(t, serverTrieDB, int(targetHeight), atomicKeyLength)
@@ -162,7 +165,6 @@ func TestAtomicSyncer(t *testing.T) {
162165
}
163166

164167
func TestAtomicSyncerResume(t *testing.T) {
165-
rand.Seed(1)
166168
targetHeight := 10 * uint64(commitInterval)
167169
serverTrieDB := triedb.NewDatabase(rawdb.NewMemoryDatabase(), nil)
168170
numTrieKeys := int(targetHeight) - 1 // no atomic ops for genesis
@@ -179,7 +181,6 @@ func TestAtomicSyncerResume(t *testing.T) {
179181
}
180182

181183
func TestAtomicSyncerResumeNewRootCheckpoint(t *testing.T) {
182-
rand.Seed(1)
183184
targetHeight1 := 10 * uint64(commitInterval)
184185
serverTrieDB := triedb.NewDatabase(rawdb.NewMemoryDatabase(), nil)
185186
numTrieKeys1 := int(targetHeight1) - 1 // no atomic ops for genesis

plugin/evm/atomic_trie.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,11 @@ func (a *atomicTrie) InsertTrie(nodes *trienode.NodeSet, root common.Hash) error
323323
return err
324324
}
325325
}
326-
a.trieDB.Reference(root, common.Hash{})
326+
327+
err := a.trieDB.Reference(root, common.Hash{})
328+
if err != nil {
329+
return fmt.Errorf("referencing root: %w", err)
330+
}
327331

328332
// The use of [Cap] in [insertTrie] prevents exceeding the configured memory
329333
// limit (and OOM) in case there is a large backlog of processing (unaccepted) blocks.
@@ -364,12 +368,19 @@ func (a *atomicTrie) AcceptTrie(height uint64, root common.Hash) (bool, error) {
364368
// - not committted, in which case the current root we are inserting contains
365369
// references to all the relevant data from the previous root, so the previous
366370
// root can be dereferenced.
367-
a.trieDB.Dereference(a.lastAcceptedRoot)
371+
err := a.trieDB.Dereference(a.lastAcceptedRoot)
372+
if err != nil {
373+
return false, fmt.Errorf("dereferencing last accepted root: %w", err)
374+
}
375+
368376
a.lastAcceptedRoot = root
369377
return hasCommitted, nil
370378
}
371379

372380
func (a *atomicTrie) RejectTrie(root common.Hash) error {
373-
a.trieDB.Dereference(root)
381+
err := a.trieDB.Dereference(root)
382+
if err != nil {
383+
return fmt.Errorf("dereferencing root from trie database: %w", err)
384+
}
374385
return nil
375386
}

plugin/evm/atomic_trie_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,6 @@ func TestIndexingNilShouldNotImpactTrie(t *testing.T) {
387387
if err := indexAtomicTxs(a1, i, ops[i]); err != nil {
388388
t.Fatal(err)
389389
}
390-
} else {
391-
// do nothing
392390
}
393391
}
394392

@@ -575,7 +573,7 @@ func TestApplyToSharedMemory(t *testing.T) {
575573
assert.NoError(t, err)
576574
assert.False(t, hasMarker)
577575
// reinitialize the atomic trie
578-
backend, err = NewAtomicBackend(
576+
_, err = NewAtomicBackend(
579577
db, sharedMemories.thisChain, nil, repo, test.lastAcceptedHeight, common.Hash{}, test.commitInterval,
580578
)
581579
assert.NoError(t, err)
@@ -736,7 +734,8 @@ func TestAtomicTrie_AcceptTrie(t *testing.T) {
736734
_, storageSize, _ := atomicTrie.trieDB.Size()
737735
require.NotZero(t, storageSize, "there should be a dirty node taking up storage space")
738736
}
739-
atomicTrie.updateLastCommitted(testCase.lastCommittedRoot, testCase.lastCommittedHeight)
737+
err = atomicTrie.updateLastCommitted(testCase.lastCommittedRoot, testCase.lastCommittedHeight)
738+
require.NoError(t, err)
740739

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

plugin/evm/block_verification.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func (v blockValidator) SyntacticVerify(b *Block, rules params.Rules) error {
215215
if err != nil {
216216
return err
217217
}
218-
totalGasUsed, err = safemath.Add64(totalGasUsed, gasUsed)
218+
totalGasUsed, err = safemath.Add(totalGasUsed, gasUsed)
219219
if err != nil {
220220
return err
221221
}

plugin/evm/export_tx_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,7 +1704,6 @@ func TestNewExportTx(t *testing.T) {
17041704
}
17051705
}()
17061706

1707-
parent := vm.LastAcceptedBlockInternal().(*Block)
17081707
importAmount := uint64(50000000)
17091708
utxoID := avax.UTXOID{TxID: ids.GenerateTestID()}
17101709

@@ -1764,7 +1763,7 @@ func TestNewExportTx(t *testing.T) {
17641763
t.Fatal(err)
17651764
}
17661765

1767-
parent = vm.LastAcceptedBlockInternal().(*Block)
1766+
parent := vm.LastAcceptedBlockInternal().(*Block)
17681767
exportAmount := uint64(5000000)
17691768

17701769
state, err := vm.blockChain.State()
@@ -1877,7 +1876,6 @@ func TestNewExportTxMulticoin(t *testing.T) {
18771876
}
18781877
}()
18791878

1880-
parent := vm.LastAcceptedBlockInternal().(*Block)
18811879
importAmount := uint64(50000000)
18821880
utxoID := avax.UTXOID{TxID: ids.GenerateTestID()}
18831881

@@ -1967,7 +1965,7 @@ func TestNewExportTxMulticoin(t *testing.T) {
19671965
t.Fatal(err)
19681966
}
19691967

1970-
parent = vm.LastAcceptedBlockInternal().(*Block)
1968+
parent := vm.LastAcceptedBlockInternal().(*Block)
19711969
exportAmount := uint64(5000000)
19721970

19731971
testKeys0Addr := testKeys[0].EthAddress()

plugin/evm/gossiper_atomic_gossiping_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/ava-labs/avalanchego/proto/pb/sdk"
1616
"github.com/ava-labs/avalanchego/utils/set"
1717
"github.com/stretchr/testify/assert"
18+
"github.com/stretchr/testify/require"
1819
"google.golang.org/protobuf/proto"
1920

2021
commonEng "github.com/ava-labs/avalanchego/snow/engine/common"
@@ -147,7 +148,9 @@ func TestMempoolAtmTxsAppGossipHandlingDiscardedTx(t *testing.T) {
147148
tx, conflictingTx := importTxs[0], importTxs[1]
148149
txID := tx.ID()
149150

150-
mempool.AddRemoteTx(tx)
151+
err := mempool.AddRemoteTx(tx)
152+
require.NoError(t, err)
153+
151154
mempool.NextTx()
152155
mempool.DiscardCurrentTx(txID)
153156

@@ -184,7 +187,9 @@ func TestMempoolAtmTxsAppGossipHandlingDiscardedTx(t *testing.T) {
184187
// Conflicting tx must be submitted over the API to be included in push gossip.
185188
// (i.e., txs received via p2p are not included in push gossip)
186189
// This test adds it directly to the mempool + gossiper to simulate that.
187-
vm.mempool.AddRemoteTx(conflictingTx)
190+
err = vm.mempool.AddRemoteTx(conflictingTx)
191+
require.NoError(t, err)
192+
188193
vm.atomicTxPushGossiper.Add(&atomic.GossipAtomicTx{Tx: conflictingTx})
189194
time.Sleep(500 * time.Millisecond)
190195

0 commit comments

Comments
 (0)