Skip to content

Commit

Permalink
enable more linters #954 (#7179)
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlexSharov authored Mar 25, 2023
1 parent e0dd48c commit 201572c
Show file tree
Hide file tree
Showing 34 changed files with 131 additions and 169 deletions.
37 changes: 25 additions & 12 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,35 @@ run:
deadline: 10m

linters:
disable-all: true
presets:
- bugs
- error
- unused
- performance
disable:
- exhaustive
- musttag
- contextcheck
- wrapcheck
- goerr113
- unparam
- makezero #TODO: enable me
- noctx #TODO: enable me
- nilerr #TODO: enable me
- errorlint #TODO: enable me
- errchkjson #TODO: enable me
- unused #TODO: enable me
- gocheckcompilerdirectives
enable:
- unconvert
# - predeclared #TODO: enable me
# - thelper #TODO: enable me
# - wastedassign
- gofmt
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
# - structcheck # 1.18
# - unused # 1.18
- gocritic
- bodyclose
- gosec
# - revive
# - forcetypeassert
- prealloc
- unconvert
# - stylecheck

linters-settings:
gocritic:
Expand Down
15 changes: 8 additions & 7 deletions accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ import (
"github.com/ledgerwatch/erigon/ethdb/olddb"
"github.com/ledgerwatch/erigon/event"
"github.com/ledgerwatch/erigon/params"
"github.com/ledgerwatch/erigon/rpc"
"github.com/ledgerwatch/erigon/turbo/snapshotsync"
"github.com/ledgerwatch/erigon/turbo/stages"
)
Expand Down Expand Up @@ -570,9 +569,9 @@ func (b *SimulatedBackend) EstimateGas(ctx context.Context, call ethereum.CallMs

// Determine the lowest and highest possible gas limits to binary search in between
var (
lo = params.TxGas - 1
hi uint64
cap uint64
lo = params.TxGas - 1
hi uint64
gasCap uint64
)
if call.Gas >= params.TxGas {
hi = call.Gas
Expand Down Expand Up @@ -600,7 +599,7 @@ func (b *SimulatedBackend) EstimateGas(ctx context.Context, call ethereum.CallMs
hi = allowance.Uint64()
}
}
cap = hi
gasCap = hi
b.pendingState.Prepare(libcommon.Hash{}, libcommon.Hash{}, len(b.pendingBlock.Transactions()))

// Create a helper to check if a gas allowance results in an executable transaction
Expand Down Expand Up @@ -637,7 +636,7 @@ func (b *SimulatedBackend) EstimateGas(ctx context.Context, call ethereum.CallMs
}
}
// Reject the transaction as invalid if it still fails at the highest allowance
if hi == cap {
if hi == gasCap {
failed, result, err := executable(hi)
if err != nil {
return 0, err
Expand All @@ -650,7 +649,7 @@ func (b *SimulatedBackend) EstimateGas(ctx context.Context, call ethereum.CallMs
return 0, result.Err
}
// Otherwise, the specified gas cap is too low
return 0, fmt.Errorf("gas required exceeds allowance (%d)", cap)
return 0, fmt.Errorf("gas required exceeds allowance (%d)", gasCap)
}
}
return hi, nil
Expand Down Expand Up @@ -799,6 +798,7 @@ func (m callMsg) Data() []byte { return m.CallMsg.Data }
func (m callMsg) AccessList() types2.AccessList { return m.CallMsg.AccessList }
func (m callMsg) IsFree() bool { return false }

/*
// filterBackend implements filters.Backend to support filtering for logs without
// taking bloom-bits acceleration structures into account.
type filterBackend struct {
Expand Down Expand Up @@ -895,3 +895,4 @@ func nullSubscription() event.Subscription {
return nil
})
}
*/
4 changes: 2 additions & 2 deletions accounts/abi/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (
// packBytesSlice packs the given bytes as [L, V] as the canonical representation
// bytes slice.
func packBytesSlice(bytes []byte, l int) []byte {
len := packNum(reflect.ValueOf(l))
return append(len, common.RightPadBytes(bytes, (l+31)/32*32)...)
packedLen := packNum(reflect.ValueOf(l))
return append(packedLen, common.RightPadBytes(bytes, (l+31)/32*32)...)
}

// packElement packs the given reflect value according to the abi specification in
Expand Down
4 changes: 2 additions & 2 deletions accounts/abi/unpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@ func TestMethodMultiReturn(t *testing.T) {
Int *big.Int
}

newInterfaceSlice := func(len int) interface{} {
slice := make([]interface{}, len)
newInterfaceSlice := func(l int) interface{} {
slice := make([]interface{}, l)
return &slice
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/erigon-cl/core/rawdb/accessors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
)

func TestBytes2(t *testing.T) {
len := 1000
buf := rawdb.Bytes2FromLength(len)
require.Equal(t, len, rawdb.LengthFromBytes2(buf))
l := 1000
buf := rawdb.Bytes2FromLength(l)
require.Equal(t, l, rawdb.LengthFromBytes2(buf))
}

var emptyBlock = &cltypes.Eth1Block{}
Expand Down
12 changes: 6 additions & 6 deletions cmd/rpcdaemon/commands/eth_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ func (api *APIImpl) EstimateGas(ctx context.Context, argsOrNil *ethapi2.CallArgs

// Binary search the gas requirement, as it may be higher than the amount used
var (
lo = params.TxGas - 1
hi uint64
cap uint64
lo = params.TxGas - 1
hi uint64
gasCap uint64
)
// Use zero address if sender unspecified.
if args.From == nil {
Expand Down Expand Up @@ -217,7 +217,7 @@ func (api *APIImpl) EstimateGas(ctx context.Context, argsOrNil *ethapi2.CallArgs
log.Warn("Caller gas above allowance, capping", "requested", hi, "cap", api.GasCap)
hi = api.GasCap
}
cap = hi
gasCap = hi

chainConfig, err := api.chainConfig(dbtx)
if err != nil {
Expand Down Expand Up @@ -286,7 +286,7 @@ func (api *APIImpl) EstimateGas(ctx context.Context, argsOrNil *ethapi2.CallArgs
}

// Reject the transaction as invalid if it still fails at the highest allowance
if hi == cap {
if hi == gasCap {
failed, result, err := executable(hi)
if err != nil {
return 0, err
Expand All @@ -299,7 +299,7 @@ func (api *APIImpl) EstimateGas(ctx context.Context, argsOrNil *ethapi2.CallArgs
return 0, result.Err
}
// Otherwise, the specified gas cap is too low
return 0, fmt.Errorf("gas required exceeds allowance (%d)", cap)
return 0, fmt.Errorf("gas required exceeds allowance (%d)", gasCap)
}
}
return hexutil.Uint64(hi), nil
Expand Down
38 changes: 0 additions & 38 deletions cmd/rpcdaemon/commands/eth_receipts.go
Original file line number Diff line number Diff line change
Expand Up @@ -765,44 +765,6 @@ func marshalReceipt(receipt *types.Receipt, txn types.Transaction, chainConfig *
return fields
}

func includes(addresses []common.Address, a common.Address) bool {
for _, addr := range addresses {
if addr == a {
return true
}
}
return false
}

// filterLogs creates a slice of logs matching the given criteria.
func filterLogsOld(logs []*types.Log, addresses []common.Address, topics [][]common.Hash) []*types.Log {
result := make(types.Logs, 0, len(logs))
Logs:
for _, log := range logs {
if len(addresses) > 0 && !includes(addresses, log.Address) {
continue
}
// If the to filtered topics is greater than the amount of topics in logs, skip.
if len(topics) > len(log.Topics) {
continue Logs
}
for i, sub := range topics {
match := len(sub) == 0 // empty rule set == wildcard
for _, topic := range sub {
if log.Topics[i] == topic {
match = true
break
}
}
if !match {
continue Logs
}
}
result = append(result, log)
}
return result
}

// MapTxNum2BlockNumIter - enrich iterator by TxNumbers, adding more info:
// - blockNum
// - txIndex in block: -1 means first system tx
Expand Down
10 changes: 5 additions & 5 deletions cmd/rpcdaemon/commands/send_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ func (api *APIImpl) SendTransaction(_ context.Context, txObject interface{}) (co

// checkTxFee is an internal function used to check whether the fee of
// the given transaction is _reasonable_(under the cap).
func checkTxFee(gasPrice *big.Int, gas uint64, cap float64) error {
// Short circuit if there is no cap for transaction fee at all.
if cap == 0 {
func checkTxFee(gasPrice *big.Int, gas uint64, gasCap float64) error {
// Short circuit if there is no gasCap for transaction fee at all.
if gasCap == 0 {
return nil
}
feeEth := new(big.Float).Quo(new(big.Float).SetInt(new(big.Int).Mul(gasPrice, new(big.Int).SetUint64(gas))), new(big.Float).SetInt(big.NewInt(params.Ether)))
feeFloat, _ := feeEth.Float64()
if feeFloat > cap {
return fmt.Errorf("tx fee (%.2f ether) exceeds the configured cap (%.2f ether)", feeFloat, cap)
if feeFloat > gasCap {
return fmt.Errorf("tx fee (%.2f ether) exceeds the configured cap (%.2f ether)", feeFloat, gasCap)
}
return nil
}
9 changes: 4 additions & 5 deletions cmd/state/exec3/state_recon.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ import (
)

type ScanWorker struct {
txNum uint64
as *libstate.AggregatorStep
fromKey, toKey []byte
currentKey []byte
bitmap roaring64.Bitmap
txNum uint64
as *libstate.AggregatorStep
toKey []byte
bitmap roaring64.Bitmap
}

func NewScanWorker(txNum uint64, as *libstate.AggregatorStep) *ScanWorker {
Expand Down
4 changes: 0 additions & 4 deletions cmd/verkle/verkletrie/verkle_tree_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ import (
"github.com/ledgerwatch/erigon/turbo/trie/vtree"
)

func identityFuncForVerkleTree(k []byte, value []byte, _ etl.CurrentTableReader, next etl.LoadNextFunc) error {
return next(k, k, value)
}

func int256ToVerkleFormat(x *uint256.Int, buffer []byte) {
bbytes := x.ToBig().Bytes()
if len(bbytes) > 0 {
Expand Down
4 changes: 2 additions & 2 deletions common/math/big_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ func BenchmarkByteAtOld(b *testing.B) {
func TestReadBits(t *testing.T) {
check := func(input string) {
want, _ := hex.DecodeString(input)
int, _ := new(big.Int).SetString(input, 16)
in, _ := new(big.Int).SetString(input, 16)
buf := make([]byte, len(want))
ReadBits(int, buf)
ReadBits(in, buf)
if !bytes.Equal(buf, want) {
t.Errorf("have: %x\nwant: %x", buf, want)
}
Expand Down
4 changes: 2 additions & 2 deletions common/math/integer.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ type HexOrDecimal64 uint64

// UnmarshalText implements encoding.TextUnmarshaler.
func (i *HexOrDecimal64) UnmarshalText(input []byte) error {
int, ok := ParseUint64(string(input))
in, ok := ParseUint64(string(input))
if !ok {
return fmt.Errorf("invalid hex or decimal integer %q", input)
}
*i = HexOrDecimal64(int)
*i = HexOrDecimal64(in)
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion consensus/clique/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func lastSnapshot(db kv.RwDB) (uint64, error) {

lastEnc, err := tx.GetOne(kv.CliqueLastSnapshot, LastSnapshotKey())
if err != nil {
return 0, fmt.Errorf("failed check last clique snapshot: %d", err)
return 0, fmt.Errorf("failed check last clique snapshot: %w", err)
}
if len(lastEnc) == 0 {
return 0, ErrNotFound
Expand Down
6 changes: 3 additions & 3 deletions core/asm/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ func Lex(source []byte, debug bool) <-chan token {
}

// next returns the next rune in the program's source.
func (l *lexer) next() (rune rune) {
func (l *lexer) next() (runeVal rune) {
if l.pos >= len(l.input) {
l.width = 0
return 0
}
rune, l.width = utf8.DecodeRuneInString(l.input[l.pos:])
runeVal, l.width = utf8.DecodeRuneInString(l.input[l.pos:])
l.pos += l.width
return rune
return runeVal
}

// backup backsup the last parsed element (multi-character)
Expand Down
4 changes: 2 additions & 2 deletions core/state/intra_block_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,11 @@ func (sdb *IntraBlockState) GetCodeSize(addr libcommon.Address) int {
if stateObject.code != nil {
return len(stateObject.code)
}
len, err := sdb.stateReader.ReadAccountCodeSize(addr, stateObject.data.Incarnation, stateObject.data.CodeHash)
l, err := sdb.stateReader.ReadAccountCodeSize(addr, stateObject.data.Incarnation, stateObject.data.CodeHash)
if err != nil {
sdb.setErrorUnsafe(err)
}
return len
return l
}

// DESCRIBED: docs/programmers_guide/guide.md#address---identifier-of-an-account
Expand Down
11 changes: 5 additions & 6 deletions core/state/recon_writer_inc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ import (
)

type StateReconWriterInc struct {
as *libstate.AggregatorStep
rs *ReconState
txNum uint64
tx kv.Tx
chainTx kv.Tx
composite []byte
as *libstate.AggregatorStep
rs *ReconState
txNum uint64
tx kv.Tx
chainTx kv.Tx
}

func NewStateReconWriterInc(as *libstate.AggregatorStep, rs *ReconState) *StateReconWriterInc {
Expand Down
7 changes: 4 additions & 3 deletions core/state/temporal/kv_temporal.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func New(db kv.RwDB, agg *state.AggregatorV3, cb1 tConvertV3toV2, cb2 tRestoreCo

return &DB{RwDB: db, agg: agg, convertV3toV2: cb1, restoreCodeHash: cb2, parseInc: cb3, systemContractLookup: systemContractLookup}, nil
}

func (db *DB) BeginTemporalRo(ctx context.Context) (kv.TemporalTx, error) {
kvTx, err := db.RwDB.BeginRo(ctx)
if err != nil {
Expand Down Expand Up @@ -103,7 +104,7 @@ func (db *DB) View(ctx context.Context, f func(tx kv.Tx) error) error {
}

func (db *DB) BeginTemporalRw(ctx context.Context) (kv.RwTx, error) {
kvTx, err := db.RwDB.BeginRw(ctx)
kvTx, err := db.RwDB.BeginRw(ctx) //nolint:gocritic
if err != nil {
return nil, err
}
Expand All @@ -125,7 +126,7 @@ func (db *DB) Update(ctx context.Context, f func(tx kv.RwTx) error) error {
}

func (db *DB) BeginTemporalRwNosync(ctx context.Context) (kv.RwTx, error) {
kvTx, err := db.RwDB.BeginRwNosync(ctx)
kvTx, err := db.RwDB.BeginRwNosync(ctx) //nolint:gocritic
if err != nil {
return nil, err
}
Expand All @@ -135,7 +136,7 @@ func (db *DB) BeginTemporalRwNosync(ctx context.Context) (kv.RwTx, error) {
return tx, nil
}
func (db *DB) BeginRwNosync(ctx context.Context) (kv.RwTx, error) {
return db.BeginTemporalRwNosync(ctx)
return db.BeginTemporalRwNosync(ctx) //nolint:gocritic
}
func (db *DB) UpdateNosync(ctx context.Context, f func(tx kv.RwTx) error) error {
tx, err := db.BeginTemporalRwNosync(ctx)
Expand Down
Loading

0 comments on commit 201572c

Please sign in to comment.