diff --git a/.golangci.yml b/.golangci.yml index 1466bdec11..0efaab1974 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -49,7 +49,7 @@ linters: - tparallel - unconvert - unparam - - wsl + # - wsl - asasalint #- errorlint causes stack overflow. TODO: recheck after each golangci update diff --git a/consensus/ethash/consensus_test.go b/consensus/ethash/consensus_test.go index 2d8d3f6e55..59db41c49b 100644 --- a/consensus/ethash/consensus_test.go +++ b/consensus/ethash/consensus_test.go @@ -91,11 +91,11 @@ func TestCalcDifficulty(t *testing.T) { func randSlice(min, max uint32) []byte { var b = make([]byte, 4) - crand.Read(b) + _ , _ = crand.Read(b) a := binary.LittleEndian.Uint32(b) size := min + a%(max-min) out := make([]byte, size) - crand.Read(out) + _ , _ = crand.Read(out) return out } diff --git a/core/rawdb/chain_freezer.go b/core/rawdb/chain_freezer.go index 22dbda4a21..5a9badcd09 100644 --- a/core/rawdb/chain_freezer.go +++ b/core/rawdb/chain_freezer.go @@ -82,6 +82,7 @@ func (f *chainFreezer) Close() error { // // This functionality is deliberately broken off from block importing to avoid // incurring additional data shuffling delays on block propagation. +// nolint:gocognit func (f *chainFreezer) freeze(db ethdb.KeyValueStore) { var ( backoff bool @@ -250,6 +251,7 @@ func (f *chainFreezer) freeze(db ethdb.KeyValueStore) { } } +// nolint:gocognit func (f *chainFreezer) freezeRange(nfdb *nofreezedb, number, limit uint64) (hashes []common.Hash, err error) { hashes = make([]common.Hash, 0, limit-number) diff --git a/core/rawdb/database.go b/core/rawdb/database.go index 5d3bc1f3de..5f1322062e 100644 --- a/core/rawdb/database.go +++ b/core/rawdb/database.go @@ -197,6 +197,7 @@ func resolveChainFreezerDir(ancient string) string { // value data store with a freezer moving immutable chain segments into cold // storage. The passed ancient indicates the path of root ancient directory // where the chain freezer can be opened. +// nolint:gocognit func NewDatabaseWithFreezer(db ethdb.KeyValueStore, ancient string, namespace string, readonly bool) (ethdb.Database, error) { // Create the idle freezer instance frdb, err := newChainFreezer(resolveChainFreezerDir(ancient), namespace, readonly) diff --git a/core/rawdb/freezer.go b/core/rawdb/freezer.go index 7cc3477ec9..909327a22e 100644 --- a/core/rawdb/freezer.go +++ b/core/rawdb/freezer.go @@ -409,6 +409,7 @@ type convertLegacyFn = func([]byte) ([]byte, error) // MigrateTable processes the entries in a given table in sequence // converting them to a new format if they're of an old format. +// nolint:gocognit func (f *Freezer) MigrateTable(kind string, convert convertLegacyFn) error { if f.readonly { return errReadOnly diff --git a/core/rawdb/freezer_table.go b/core/rawdb/freezer_table.go index 4d4598138b..fadacd9739 100644 --- a/core/rawdb/freezer_table.go +++ b/core/rawdb/freezer_table.go @@ -555,13 +555,13 @@ func (t *freezerTable) Close() error { defer t.lock.Unlock() var errs []error - doClose := func(f *os.File, sync bool, close bool) { + doClose := func(f *os.File, sync bool, closed bool) { if sync && !t.readonly { if err := f.Sync(); err != nil { errs = append(errs, err) } } - if close { + if closed { if err := f.Close(); err != nil { errs = append(errs, err) } diff --git a/core/state/iterator_test.go b/core/state/iterator_test.go index 5327f8ee8b..7465073ad8 100644 --- a/core/state/iterator_test.go +++ b/core/state/iterator_test.go @@ -89,7 +89,7 @@ func TestNodeIteratorCoverage(t *testing.T) { // isTrieNode is a helper function which reports if the provided // database entry belongs to a trie node or not. -func isTrieNode(scheme string, key, val []byte) (bool, common.Hash) { +func isTrieNode(scheme string, key, _ []byte) (bool, common.Hash) { if scheme == rawdb.HashScheme { if len(key) == common.HashLength { return true, common.BytesToHash(key) diff --git a/core/state/pruner/pruner.go b/core/state/pruner/pruner.go index a251a13896..f654bea3f8 100644 --- a/core/state/pruner/pruner.go +++ b/core/state/pruner/pruner.go @@ -235,6 +235,7 @@ func prune(snaptree *snapshot.Tree, root common.Hash, maindb ethdb.Database, sta // Prune deletes all historical state nodes except the nodes belong to the // specified state version. If user doesn't specify the state version, use // the bottom-most snapshot diff layer as the target. +// nolint:nestif func (p *Pruner) Prune(root common.Hash) error { // If the state bloom filter is already committed previously, // reuse it for pruning instead of generating a new one. It's diff --git a/core/state/snapshot/conversion.go b/core/state/snapshot/conversion.go index a814ceedd7..91bcb176c0 100644 --- a/core/state/snapshot/conversion.go +++ b/core/state/snapshot/conversion.go @@ -244,6 +244,7 @@ func runReport(stats *generateStats, stop chan bool) { // generateTrieRoot generates the trie hash based on the snapshot iterator. // It can be used for generating account trie, storage trie or even the // whole state which connects the accounts and the corresponding storages. +// nolint:gocognit func generateTrieRoot(db ethdb.KeyValueWriter, scheme string, it Iterator, account common.Hash, generatorFn trieGeneratorFn, leafCallback leafCallbackFn, stats *generateStats, report bool) (common.Hash, error) { var ( in = make(chan trieKV) // chan to pass leaves diff --git a/core/state/snapshot/generate.go b/core/state/snapshot/generate.go index 74cc53842c..ef6cefaf04 100644 --- a/core/state/snapshot/generate.go +++ b/core/state/snapshot/generate.go @@ -159,6 +159,7 @@ func (result *proofResult) forEach(callback func(key []byte, val []byte) error) // // The proof result will be returned if the range proving is finished, otherwise // the error will be returned to abort the entire procedure. +// nolint:gocognit func (dl *diskLayer) proveRange(ctx *generatorContext, trieId *trie.ID, prefix []byte, kind string, origin []byte, max int, valueConvertFn func([]byte) ([]byte, error)) (*proofResult, error) { var ( keys [][]byte @@ -307,6 +308,7 @@ type onStateCallback func(key []byte, val []byte, write bool, delete bool) error // generateRange generates the state segment with particular prefix. Generation can // either verify the correctness of existing state through range-proof and skip // generation, or iterate trie to regenerate state on demand. +// nolint:gocognit func (dl *diskLayer) generateRange(ctx *generatorContext, trieId *trie.ID, prefix []byte, kind string, origin []byte, max int, onState onStateCallback, valueConvertFn func([]byte) ([]byte, error)) (bool, []byte, error) { // Use range prover to check the validity of the flat state in the range result, err := dl.proveRange(ctx, trieId, prefix, kind, origin, max, valueConvertFn) @@ -573,6 +575,7 @@ func generateStorages(ctx *generatorContext, dl *diskLayer, stateRoot common.Has // generateAccounts generates the missing snapshot accounts as well as their // storage slots in the main trie. It's supposed to restart the generation // from the given origin position. +// nolint:nestif,gocognit func generateAccounts(ctx *generatorContext, dl *diskLayer, accMarker []byte) error { onAccount := func(key []byte, val []byte, write bool, delete bool) error { // Make sure to clear all dangling storages before this account @@ -636,7 +639,7 @@ func generateAccounts(ctx *generatorContext, dl *diskLayer, accMarker []byte) er // If the iterated account is the contract, create a further loop to // verify or regenerate the contract storage. if acc.Root == types.EmptyRootHash { - ctx.removeStorageAt(account) + _ = ctx.removeStorageAt(account) } else { var storeMarker []byte if accMarker != nil && bytes.Equal(account[:], accMarker) && len(dl.genMarker) > common.HashLength { diff --git a/core/state/snapshot/generate_test.go b/core/state/snapshot/generate_test.go index 1819247802..41c14aed35 100644 --- a/core/state/snapshot/generate_test.go +++ b/core/state/snapshot/generate_test.go @@ -200,7 +200,7 @@ func (t *testHelper) makeStorageTrie(stateRoot, owner common.Hash, keys []string root, nodes := stTrie.Commit(false) if nodes != nil { - t.nodes.Merge(nodes) + _ = t.nodes.Merge(nodes) } return root.Bytes() } @@ -208,10 +208,10 @@ func (t *testHelper) makeStorageTrie(stateRoot, owner common.Hash, keys []string func (t *testHelper) Commit() common.Hash { root, nodes := t.accTrie.Commit(true) if nodes != nil { - t.nodes.Merge(nodes) + _ = t.nodes.Merge(nodes) } - t.triedb.Update(t.nodes) - t.triedb.Commit(root, false) + _ = t.triedb.Update(t.nodes) + _ = t.triedb.Commit(root, false) return root } @@ -400,8 +400,8 @@ func TestGenerateCorruptAccountTrie(t *testing.T) { root := helper.Commit() // Root: 0xa04693ea110a31037fb5ee814308a6f1d76bdab0b11676bdf4541d2de55ba978 // Delete an account trie leaf and ensure the generator chokes - helper.triedb.Commit(root, false) - helper.diskdb.Delete(common.HexToHash("0x65145f923027566669a1ae5ccac66f945b55ff6eaeb17d2ea8e048b7d381f2d7").Bytes()) + _ = helper.triedb.Commit(root, false) + _ = helper.diskdb.Delete(common.HexToHash("0x65145f923027566669a1ae5ccac66f945b55ff6eaeb17d2ea8e048b7d381f2d7").Bytes()) snap := generateSnapshot(helper.diskdb, helper.triedb, 16, root) select { @@ -436,7 +436,7 @@ func TestGenerateMissingStorageTrie(t *testing.T) { root := helper.Commit() // Delete a storage trie root and ensure the generator chokes - helper.diskdb.Delete(stRoot) + _ = helper.diskdb.Delete(stRoot) snap := generateSnapshot(helper.diskdb, helper.triedb, 16, root) select { @@ -470,7 +470,7 @@ func TestGenerateCorruptStorageTrie(t *testing.T) { root := helper.Commit() // Delete a storage trie leaf and ensure the generator chokes - helper.diskdb.Delete(common.HexToHash("0x18a0f4d79cff4459642dd7604f303886ad9d77c30cf3d7d7cedb3a693ab6d371").Bytes()) + _ = helper.diskdb.Delete(common.HexToHash("0x18a0f4d79cff4459642dd7604f303886ad9d77c30cf3d7d7cedb3a693ab6d371").Bytes()) snap := generateSnapshot(helper.diskdb, helper.triedb, 16, root) select { diff --git a/core/state/snapshot/iterator_test.go b/core/state/snapshot/iterator_test.go index 54614427a5..35bb0ee3e0 100644 --- a/core/state/snapshot/iterator_test.go +++ b/core/state/snapshot/iterator_test.go @@ -48,7 +48,7 @@ func TestAccountIteratorBasics(t *testing.T) { if rand.Intn(2) == 0 { accStorage := make(map[common.Hash][]byte) value := make([]byte, 32) - crand.Read(value) + _ , _ = crand.Read(value) accStorage[randomHash()] = value storage[h] = accStorage } @@ -80,7 +80,7 @@ func TestStorageIteratorBasics(t *testing.T) { var nilstorage int for i := 0; i < 100; i++ { - crand.Read(value) + _ , _ = crand.Read(value) if rand.Intn(2) == 0 { accStorage[randomHash()] = common.CopyBytes(value) } else { diff --git a/core/state/snapshot/journal.go b/core/state/snapshot/journal.go index f344d475dc..7ebcf7df0f 100644 --- a/core/state/snapshot/journal.go +++ b/core/state/snapshot/journal.go @@ -281,6 +281,7 @@ type journalCallback = func(parent common.Hash, root common.Hash, destructs map[ // the most recent layer. // This method returns error either if there was some error reading from disk, // OR if the callback returns an error when invoked. +// nolint:gocognit func iterateJournal(db ethdb.KeyValueReader, callback journalCallback) error { journal := rawdb.ReadSnapshotJournal(db) if len(journal) == 0 { diff --git a/core/state/sync.go b/core/state/sync.go index 122b10265f..f3b70f08c4 100644 --- a/core/state/sync.go +++ b/core/state/sync.go @@ -31,6 +31,7 @@ func NewStateSync(root common.Hash, database ethdb.KeyValueReader, onLeaf func(k // Register the storage slot callback if the external callback is specified. var onSlot func(keys [][]byte, path []byte, leaf []byte, parent common.Hash, parentPath []byte) error if onLeaf != nil { + // nolint:unparam onSlot = func(keys [][]byte, path []byte, leaf []byte, parent common.Hash, parentPath []byte) error { return onLeaf(keys, leaf) } diff --git a/core/state/sync_test.go b/core/state/sync_test.go index 90696a598b..e8c686c7e2 100644 --- a/core/state/sync_test.go +++ b/core/state/sync_test.go @@ -173,6 +173,7 @@ type stateElement struct { syncPath trie.SyncPath } +// nolint:nestif func testIterativeStateSync(t *testing.T, count int, commit bool, bypath bool) { // Create a random state to copy _, srcDb, srcRoot, srcAccounts := makeTestState() @@ -298,6 +299,7 @@ func testIterativeStateSync(t *testing.T, count int, commit bool, bypath bool) { // Tests that the trie scheduler can correctly reconstruct the state even if only // partial results are returned, and the others sent only later. +// nolint:prealloc func TestIterativeDelayedStateSync(t *testing.T) { // Create a random state to copy _, srcDb, srcRoot, srcAccounts := makeTestState() diff --git a/core/types/transaction_marshalling.go b/core/types/transaction_marshalling.go index 2566d0b8d6..41f99942a0 100644 --- a/core/types/transaction_marshalling.go +++ b/core/types/transaction_marshalling.go @@ -99,6 +99,7 @@ func (tx *Transaction) MarshalJSON() ([]byte, error) { } // UnmarshalJSON unmarshals from JSON. +// nolint:gocognit func (tx *Transaction) UnmarshalJSON(input []byte) error { var dec txJSON if err := json.Unmarshal(input, &dec); err != nil { diff --git a/core/vm/instructions_test.go b/core/vm/instructions_test.go index bc1d382c1f..0534d27263 100644 --- a/core/vm/instructions_test.go +++ b/core/vm/instructions_test.go @@ -314,7 +314,7 @@ func opBenchmark(bench *testing.B, op executionFunc, args ...string) { for _, arg := range intArgs { stack.push(arg) } - op(&pc, evmInterpreter, scope) + _ , _ = op(&pc, evmInterpreter, scope) stack.pop() } bench.StopTimer() @@ -610,14 +610,14 @@ func TestOpTstore(t *testing.T) { stack.push(new(uint256.Int).SetBytes(value)) // push the location to the stack stack.push(new(uint256.Int)) - opTstore(&pc, evmInterpreter, &scopeContext) + _ , _ = opTstore(&pc, evmInterpreter, &scopeContext) // there should be no elements on the stack after TSTORE if stack.len() != 0 { t.Fatal("stack wrong size") } // push the location to the stack stack.push(new(uint256.Int)) - opTload(&pc, evmInterpreter, &scopeContext) + _ , _ = opTload(&pc, evmInterpreter, &scopeContext) // there should be one element on the stack after TLOAD if stack.len() != 1 { t.Fatal("stack wrong size") diff --git a/eth/tracers/logger/logger_json.go b/eth/tracers/logger/logger_json.go index a2cb4cd9fc..d3d20d82d6 100644 --- a/eth/tracers/logger/logger_json.go +++ b/eth/tracers/logger/logger_json.go @@ -89,7 +89,7 @@ func (l *JSONLogger) CaptureEnd(output []byte, gasUsed uint64, err error) { if err != nil { errMsg = err.Error() } - l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), errMsg}) + _ = l.encoder.Encode(endLog{common.Bytes2Hex(output), math.HexOrDecimal64(gasUsed), errMsg}) } func (l *JSONLogger) CaptureEnter(typ vm.OpCode, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) { diff --git a/internal/debug/flags.go b/internal/debug/flags.go index 02602786e7..7c13251eaf 100644 --- a/internal/debug/flags.go +++ b/internal/debug/flags.go @@ -191,6 +191,7 @@ func init() { // Setup initializes profiling and logging based on the CLI flags. // It should be called as early as possible in the program. +// nolint:nestif func Setup(ctx *cli.Context) error { var ( logfmt log.Format diff --git a/p2p/dnsdisc/client_test.go b/p2p/dnsdisc/client_test.go index e29b636b50..30b6b12c2a 100644 --- a/p2p/dnsdisc/client_test.go +++ b/p2p/dnsdisc/client_test.go @@ -471,8 +471,9 @@ func (mr mapResolver) LookupTXT(ctx context.Context, name string) ([]string, err return nil, errors.New("not found") } +// nolint:prealloc func parseNodes(rec []string) []*enode.Node { - ns := make([]*enode.Node, len(rec)) + var ns []*enode.Node for _, r := range rec { var n enode.Node diff --git a/trie/database.go b/trie/database.go index 200ed3674b..bb93edad43 100644 --- a/trie/database.go +++ b/trie/database.go @@ -770,6 +770,7 @@ func (c *cleaner) Delete(key []byte) error { // Update inserts the dirty nodes in provided nodeset into database and // link the account trie with multiple storage tries if necessary. +// nolint:prealloc func (db *Database) Update(nodes *MergedNodeSet) error { db.lock.Lock() defer db.lock.Unlock() diff --git a/trie/nodeset.go b/trie/nodeset.go index 99e4a80fa8..c87f901912 100644 --- a/trie/nodeset.go +++ b/trie/nodeset.go @@ -154,11 +154,13 @@ func (set *NodeSet) Size() (int, int) { // Hashes returns the hashes of all updated nodes. TODO(rjl493456442) how can // we get rid of it? +// nolint:makezero func (set *NodeSet) Hashes() []common.Hash { - var ret []common.Hash + ret := make([]common.Hash, len(set.nodes)) for _, node := range set.nodes { ret = append(ret, node.hash) } + return ret } diff --git a/trie/sync.go b/trie/sync.go index a928c55224..a1f4ef3847 100644 --- a/trie/sync.go +++ b/trie/sync.go @@ -422,6 +422,7 @@ func (s *Sync) scheduleCodeRequest(req *codeRequest) { // children retrieves all the missing children of a state trie entry for future // retrieval scheduling. +// nolint:gocognit func (s *Sync) children(req *nodeRequest, object node) ([]*nodeRequest, error) { // Gather all the children of the node, irrelevant whether known or not type childNode struct { diff --git a/trie/sync_test.go b/trie/sync_test.go index 9a0a04c4d6..3c9c24902a 100644 --- a/trie/sync_test.go +++ b/trie/sync_test.go @@ -124,6 +124,7 @@ func TestIterativeSyncBatched(t *testing.T) { testIterativeSync(t, 100, func TestIterativeSyncIndividualByPath(t *testing.T) { testIterativeSync(t, 1, true) } func TestIterativeSyncBatchedByPath(t *testing.T) { testIterativeSync(t, 100, true) } +// nolint:prealloc func testIterativeSync(t *testing.T, count int, bypath bool) { // Create a random trie to copy srcDb, srcTrie, srcData := makeTestTrie() @@ -195,6 +196,7 @@ func testIterativeSync(t *testing.T, count int, bypath bool) { // Tests that the trie scheduler can correctly reconstruct the state even if only // partial results are returned, and the others sent only later. +// nolint:prealloc func TestIterativeDelayedSync(t *testing.T) { // Create a random trie to copy srcDb, srcTrie, srcData := makeTestTrie() @@ -384,12 +386,12 @@ func TestIterativeRandomDelayedSync(t *testing.T) { } } } - // Cross check that the two tries are in sync + // Cross-check that the two tries are in sync checkTrieContents(t, triedb, srcTrie.Hash().Bytes(), srcData) } -// Tests that a trie sync will not request nodes multiple times, even if they -// have such references. +// Tests that a trie sync will not request nodes multiple times, even if they have such references. +// nolint:prealloc func TestDuplicateAvoidanceSync(t *testing.T) { // Create a random trie to copy srcDb, srcTrie, srcData := makeTestTrie() @@ -456,8 +458,8 @@ func TestDuplicateAvoidanceSync(t *testing.T) { checkTrieContents(t, triedb, srcTrie.Hash().Bytes(), srcData) } -// Tests that at any point in time during a sync, only complete sub-tries are in -// the database. +// Tests that at any point in time during a sync, only complete sub-tries are in the database. +// nolint:prealloc func TestIncompleteSync(t *testing.T) { t.Parallel() // Create a random trie to copy @@ -543,8 +545,8 @@ func TestIncompleteSync(t *testing.T) { } } -// Tests that trie nodes get scheduled lexicographically when having the same -// depth. +// Tests that trie nodes get scheduled lexicographically when having the same depth. +// nolint:prealloc func TestSyncOrdering(t *testing.T) { // Create a random trie to copy srcDb, srcTrie, srcData := makeTestTrie() diff --git a/trie/tracer_test.go b/trie/tracer_test.go index 786313acc9..c670efff35 100644 --- a/trie/tracer_test.go +++ b/trie/tracer_test.go @@ -179,6 +179,7 @@ func testAccessList(t *testing.T, vals []struct{ k, v string }) { // Add more new nodes trie, _ = New(TrieID(root), db) orig = trie.Copy() + // nolint:prealloc var keys []string for i := 0; i < 30; i++ { diff --git a/trie/trie_test.go b/trie/trie_test.go index 58c177ab10..edd875eec5 100644 --- a/trie/trie_test.go +++ b/trie/trie_test.go @@ -470,6 +470,7 @@ func verifyAccessList(oldTrie *Trie, newTrie *Trie, set *NodeSet) error { return nil } +// nolint:gocognit func runRandTest(rt randTest) bool { var ( triedb = NewDatabase(rawdb.NewMemoryDatabase())