From 3e4fb5dd497aad79eae3947a7dcd12f5558e6d6e Mon Sep 17 00:00:00 2001
From: Alex Sharov <AskAlexSharov@gmail.com>
Date: Tue, 10 May 2022 10:14:09 +0700
Subject: [PATCH] up linter version (#4108)

* save

* save

* save
---
 .github/workflows/ci.yml                |  2 +-
 .golangci.yml                           |  2 +-
 Makefile                                |  2 +-
 cmd/downloader/downloader/downloader.go |  2 +-
 cmd/integration/commands/stages.go      | 11 ++-----
 common/fdlimit/fdlimit_test.go          |  3 +-
 consensus/parlia/parlia.go              |  9 ++---
 core/rawdb/accessors_chain.go           | 10 ++----
 core/rawdb/bor_receipts.go              |  2 +-
 core/vm/contracts_test.go               |  2 +-
 eth/protocols/eth/peer.go               | 23 +++++--------
 eth/stagedsync/stage_execute.go         | 44 +++++++++++--------------
 eth/stagedsync/stage_headers.go         |  9 ++---
 eth/stagedsync/stage_senders.go         |  3 +-
 eth/stagedsync/stage_tevm.go            |  3 +-
 eth/stagedsync/stage_txlookup.go        |  3 +-
 go.mod                                  |  3 +-
 go.sum                                  |  6 ++--
 p2p/discover/common.go                  | 10 ++----
 p2p/discover/lookup_util_test.go        |  4 +--
 p2p/discover/v5_udp.go                  |  5 +--
 turbo/shards/trie_cache.go              | 20 +++--------
 turbo/snapshotsync/block_snapshots.go   | 18 ++++------
 turbo/trie/trie.go                      | 10 ++----
 24 files changed, 76 insertions(+), 130 deletions(-)

diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index eac1b3c235..5af6f2cae7 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -53,7 +53,7 @@ jobs:
         if: runner.os == 'Linux'
         uses: golangci/golangci-lint-action@v3
         with:
-          version: v1.45
+          version: v1.46
           skip-pkg-cache: true
           skip-build-cache: true
 
diff --git a/.golangci.yml b/.golangci.yml
index c776341cb1..8455481591 100644
--- a/.golangci.yml
+++ b/.golangci.yml
@@ -14,7 +14,7 @@ linters:
     - structcheck
     - unused
     - varcheck
-    - gocritic
+#    - gocritic
     - bodyclose
 #    - gosec
 #    - forcetypeassert
diff --git a/Makefile b/Makefile
index 19d423cc60..1488c7928b 100644
--- a/Makefile
+++ b/Makefile
@@ -105,7 +105,7 @@ lintci:
 
 lintci-deps:
 	rm -f ./build/bin/golangci-lint
-	curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b ./build/bin v1.45.2
+	curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b ./build/bin v1.46.0
 
 clean:
 	go clean -cache
diff --git a/cmd/downloader/downloader/downloader.go b/cmd/downloader/downloader/downloader.go
index 6664b6896c..35e46f976b 100644
--- a/cmd/downloader/downloader/downloader.go
+++ b/cmd/downloader/downloader/downloader.go
@@ -142,7 +142,7 @@ func (d *Downloader) ReCalcStats(interval time.Duration) {
 	stats.PeersUnique = int32(len(peers))
 	stats.FilesTotal = int32(len(torrents))
 
-	if prevStats.Completed == false && stats.Completed == true {
+	if !prevStats.Completed && stats.Completed {
 		d.onComplete()
 	}
 
diff --git a/cmd/integration/commands/stages.go b/cmd/integration/commands/stages.go
index fa0f84df82..9814a0a5f2 100644
--- a/cmd/integration/commands/stages.go
+++ b/cmd/integration/commands/stages.go
@@ -12,6 +12,7 @@ import (
 
 	"github.com/c2h5oh/datasize"
 	common2 "github.com/ledgerwatch/erigon-lib/common"
+	"github.com/ledgerwatch/erigon-lib/common/cmp"
 	"github.com/ledgerwatch/erigon-lib/common/dir"
 	"github.com/ledgerwatch/erigon-lib/etl"
 	"github.com/ledgerwatch/erigon-lib/kv"
@@ -441,14 +442,6 @@ func init() {
 	rootCmd.AddCommand(cmdSetPrune)
 }
 
-// max is a helper function which returns the larger of the two given integers.
-func max(a, b uint64) uint64 { //nolint:unparam
-	if a > b {
-		return a
-	}
-	return b
-}
-
 func stageHeaders(db kv.RwDB, ctx context.Context) error {
 	return db.Update(ctx, func(tx kv.RwTx) error {
 		if !(unwind > 0 || reset) {
@@ -471,7 +464,7 @@ func stageHeaders(db kv.RwDB, ctx context.Context) error {
 		if unwind > progress {
 			unwindTo = 1 // keep genesis
 		} else {
-			unwindTo = max(1, progress-unwind)
+			unwindTo = cmp.Max(1, progress-unwind)
 		}
 
 		if err = stages.SaveStageProgress(tx, stages.Headers, unwindTo); err != nil {
diff --git a/common/fdlimit/fdlimit_test.go b/common/fdlimit/fdlimit_test.go
index 21362b8463..9fd5e9fc3c 100644
--- a/common/fdlimit/fdlimit_test.go
+++ b/common/fdlimit/fdlimit_test.go
@@ -17,7 +17,6 @@
 package fdlimit
 
 import (
-	"fmt"
 	"testing"
 )
 
@@ -30,7 +29,7 @@ func TestFileDescriptorLimits(t *testing.T) {
 		t.Fatal(err)
 	}
 	if hardlimit < target {
-		t.Skip(fmt.Sprintf("system limit is less than desired test target: %d < %d", hardlimit, target))
+		t.Skipf("system limit is less than desired test target: %d < %d", hardlimit, target)
 	}
 
 	if limit, err := Current(); err != nil || limit <= 0 {
diff --git a/consensus/parlia/parlia.go b/consensus/parlia/parlia.go
index d1ca6701cc..e6ec98ab3e 100644
--- a/consensus/parlia/parlia.go
+++ b/consensus/parlia/parlia.go
@@ -998,9 +998,10 @@ func (p *Parlia) getCurrentValidators(header *types.Header, ibs *state.IntraBloc
 		return nil, err
 	}
 	valz := make([]common.Address, len(*ret0))
-	for i, a := range *ret0 {
-		valz[i] = a
-	}
+	copy(valz, *ret0)
+	//for i, a := range *ret0 {
+	//	valz[i] = a
+	//}
 	return valz, nil
 }
 
@@ -1146,7 +1147,7 @@ func (p *Parlia) applyTransaction(from common.Address, to common.Address, value
 			return nil, nil, nil, err
 		}
 	} else {
-		if systemTxs == nil || len(systemTxs) == 0 || systemTxs[0] == nil {
+		if len(systemTxs) == 0 || systemTxs[0] == nil {
 			return nil, nil, nil, fmt.Errorf("supposed to get a actual transaction, but get none")
 		}
 		actualTx := systemTxs[0]
diff --git a/core/rawdb/accessors_chain.go b/core/rawdb/accessors_chain.go
index ae7cd22b3c..9f6dcb0fe4 100644
--- a/core/rawdb/accessors_chain.go
+++ b/core/rawdb/accessors_chain.go
@@ -25,6 +25,7 @@ import (
 	"math/big"
 	"time"
 
+	libcommon "github.com/ledgerwatch/erigon-lib/common/cmp"
 	"github.com/ledgerwatch/erigon-lib/kv"
 	"github.com/ledgerwatch/erigon/cmd/rpcdaemon/interfaces"
 	"github.com/ledgerwatch/erigon/common"
@@ -1068,13 +1069,6 @@ func WriteBlock(db kv.RwTx, block *types.Block) error {
 	return nil
 }
 
-func min(a, b uint64) uint64 {
-	if a < b {
-		return a
-	}
-	return b
-}
-
 // DeleteAncientBlocks - delete [1, to) old blocks after moving it to snapshots.
 // keeps genesis in db: [1, to)
 // doesn't change sequnces of kv.EthTx and kv.NonCanonicalTxs
@@ -1095,7 +1089,7 @@ func DeleteAncientBlocks(db kv.RwTx, blockTo uint64, blocksDeleteLimit int) erro
 		return nil
 	}
 	blockFrom := binary.BigEndian.Uint64(k)
-	stopAtBlock := min(blockTo, blockFrom+uint64(blocksDeleteLimit))
+	stopAtBlock := libcommon.Min(blockTo, blockFrom+uint64(blocksDeleteLimit))
 
 	for k, _, err := c.Current(); k != nil; k, _, err = c.Next() {
 		if err != nil {
diff --git a/core/rawdb/bor_receipts.go b/core/rawdb/bor_receipts.go
index ba186a0af7..123edabe76 100644
--- a/core/rawdb/bor_receipts.go
+++ b/core/rawdb/bor_receipts.go
@@ -40,7 +40,7 @@ func ReadBorReceiptRLP(db kv.Getter, hash common.Hash, number uint64) rlp.RawVal
 func ReadRawBorReceipt(db kv.Tx, hash common.Hash, number uint64) *types.Receipt {
 	// Retrieve the flattened receipt slice
 	data := ReadBorReceiptRLP(db, hash, number)
-	if data == nil || len(data) == 0 {
+	if len(data) == 0 {
 		return nil
 	}
 
diff --git a/core/vm/contracts_test.go b/core/vm/contracts_test.go
index 2e11fdab35..6b794da453 100644
--- a/core/vm/contracts_test.go
+++ b/core/vm/contracts_test.go
@@ -185,7 +185,7 @@ func benchmarkPrecompiled(addr string, test precompiledTest, bench *testing.B) {
 			return
 		}
 		if common.Bytes2Hex(res) != test.Expected {
-			bench.Error(fmt.Sprintf("Expected %v, got %v", test.Expected, common.Bytes2Hex(res)))
+			bench.Errorf("Expected %v, got %v", test.Expected, common.Bytes2Hex(res))
 			return
 		}
 	})
diff --git a/eth/protocols/eth/peer.go b/eth/protocols/eth/peer.go
index 257471b523..f9dc66fd21 100644
--- a/eth/protocols/eth/peer.go
+++ b/eth/protocols/eth/peer.go
@@ -22,6 +22,7 @@ import (
 	"sync"
 
 	mapset "github.com/deckarep/golang-set"
+	common2 "github.com/ledgerwatch/erigon-lib/common/cmp"
 	"github.com/ledgerwatch/erigon/common"
 	"github.com/ledgerwatch/erigon/core/types"
 	"github.com/ledgerwatch/erigon/p2p"
@@ -57,14 +58,6 @@ const (
 	maxQueuedBlockAnns = 4
 )
 
-// max is a helper function which returns the larger of the two given integers.
-func max(a, b int) int { //nolint:unparam
-	if a > b {
-		return a
-	}
-	return b
-}
-
 // Peer is a collection of relevant information we have about a `eth` peer.
 type Peer struct {
 	id enode.ID // Unique ID for the peer, cached
@@ -170,7 +163,7 @@ func (p *Peer) KnownTransaction(hash common.Hash) bool {
 // tests that directly send messages without having to do the asyn queueing.
 func (p *Peer) SendTransactions(txs types.Transactions) error {
 	// Mark all the transactions as known, but ensure we don't overflow our limits
-	for p.knownTxs.Cardinality() > max(0, maxKnownTxs-len(txs)) {
+	for p.knownTxs.Cardinality() > common2.Max(0, maxKnownTxs-len(txs)) {
 		p.knownTxs.Pop()
 	}
 	for _, tx := range txs {
@@ -186,7 +179,7 @@ func (p *Peer) AsyncSendTransactions(hashes []common.Hash) {
 	select {
 	case p.txBroadcast <- hashes:
 		// Mark all the transactions as known, but ensure we don't overflow our limits
-		for p.knownTxs.Cardinality() > max(0, maxKnownTxs-len(hashes)) {
+		for p.knownTxs.Cardinality() > common2.Max(0, maxKnownTxs-len(hashes)) {
 			p.knownTxs.Pop()
 		}
 		for _, hash := range hashes {
@@ -205,7 +198,7 @@ func (p *Peer) AsyncSendTransactions(hashes []common.Hash) {
 // not be managed directly.
 func (p *Peer) sendPooledTransactionHashes(hashes []common.Hash) error {
 	// Mark all the transactions as known, but ensure we don't overflow our limits
-	for p.knownTxs.Cardinality() > max(0, maxKnownTxs-len(hashes)) {
+	for p.knownTxs.Cardinality() > common2.Max(0, maxKnownTxs-len(hashes)) {
 		p.knownTxs.Pop()
 	}
 	for _, hash := range hashes {
@@ -221,7 +214,7 @@ func (p *Peer) AsyncSendPooledTransactionHashes(hashes []common.Hash) {
 	select {
 	case p.txAnnounce <- hashes:
 		// Mark all the transactions as known, but ensure we don't overflow our limits
-		for p.knownTxs.Cardinality() > max(0, maxKnownTxs-len(hashes)) {
+		for p.knownTxs.Cardinality() > common2.Max(0, maxKnownTxs-len(hashes)) {
 			p.knownTxs.Pop()
 		}
 		for _, hash := range hashes {
@@ -239,7 +232,7 @@ func (p *Peer) AsyncSendPooledTransactionHashes(hashes []common.Hash) {
 // transactions being sent.
 func (p *Peer) SendPooledTransactionsRLP(hashes []common.Hash, txs []rlp.RawValue) error {
 	// Mark all the transactions as known, but ensure we don't overflow our limits
-	for p.knownTxs.Cardinality() > max(0, maxKnownTxs-len(hashes)) {
+	for p.knownTxs.Cardinality() > common2.Max(0, maxKnownTxs-len(hashes)) {
 		p.knownTxs.Pop()
 	}
 	for _, hash := range hashes {
@@ -251,7 +244,7 @@ func (p *Peer) SendPooledTransactionsRLP(hashes []common.Hash, txs []rlp.RawValu
 // ReplyPooledTransactionsRLP is the eth/66 version of SendPooledTransactionsRLP.
 func (p *Peer) ReplyPooledTransactionsRLP(id uint64, hashes []common.Hash, txs []rlp.RawValue) error {
 	// Mark all the transactions as known, but ensure we don't overflow our limits
-	for p.knownTxs.Cardinality() > max(0, maxKnownTxs-len(hashes)) {
+	for p.knownTxs.Cardinality() > common2.Max(0, maxKnownTxs-len(hashes)) {
 		p.knownTxs.Pop()
 	}
 	for _, hash := range hashes {
@@ -268,7 +261,7 @@ func (p *Peer) ReplyPooledTransactionsRLP(id uint64, hashes []common.Hash, txs [
 // a hash notification.
 func (p *Peer) SendNewBlockHashes(hashes []common.Hash, numbers []uint64) error {
 	// Mark all the block hashes as known, but ensure we don't overflow our limits
-	for p.knownBlocks.Cardinality() > max(0, maxKnownBlocks-len(hashes)) {
+	for p.knownBlocks.Cardinality() > common2.Max(0, maxKnownBlocks-len(hashes)) {
 		p.knownBlocks.Pop()
 	}
 	for _, hash := range hashes {
diff --git a/eth/stagedsync/stage_execute.go b/eth/stagedsync/stage_execute.go
index 4f166536c1..6d6bbb7918 100644
--- a/eth/stagedsync/stage_execute.go
+++ b/eth/stagedsync/stage_execute.go
@@ -10,12 +10,13 @@ import (
 
 	"github.com/c2h5oh/datasize"
 	lru "github.com/hashicorp/golang-lru"
-	libcommon "github.com/ledgerwatch/erigon-lib/common"
+	"github.com/ledgerwatch/erigon-lib/common"
+	"github.com/ledgerwatch/erigon-lib/common/cmp"
 	"github.com/ledgerwatch/erigon-lib/common/length"
 	"github.com/ledgerwatch/erigon-lib/etl"
 	"github.com/ledgerwatch/erigon-lib/kv"
 	"github.com/ledgerwatch/erigon/cmd/rpcdaemon/interfaces"
-	"github.com/ledgerwatch/erigon/common"
+	commonold "github.com/ledgerwatch/erigon/common"
 	"github.com/ledgerwatch/erigon/common/changeset"
 	"github.com/ledgerwatch/erigon/common/dbutils"
 	"github.com/ledgerwatch/erigon/common/math"
@@ -98,7 +99,7 @@ func executeBlock(
 	writeChangesets bool,
 	writeReceipts bool,
 	writeCallTraces bool,
-	contractHasTEVM func(contractHash common.Hash) (bool, error),
+	contractHasTEVM func(contractHash commonold.Hash) (bool, error),
 	initialCycle bool,
 ) error {
 	blockNum := block.NumberU64()
@@ -108,7 +109,7 @@ func executeBlock(
 	}
 
 	// where the magic happens
-	getHeader := func(hash common.Hash, number uint64) *types.Header {
+	getHeader := func(hash commonold.Hash, number uint64) *types.Header {
 		h, _ := cfg.blockReader.Header(context.Background(), tx, hash, number)
 		return h
 	}
@@ -210,7 +211,7 @@ func SpawnExecuteBlocksStage(s *StageState, u Unwinder, tx kv.RwTx, toBlock uint
 	logPrefix := s.LogPrefix()
 	var to = prevStageProgress
 	if toBlock > 0 {
-		to = min(prevStageProgress, toBlock)
+		to = cmp.Min(prevStageProgress, toBlock)
 	}
 	if to <= s.BlockNumber {
 		return nil
@@ -256,7 +257,7 @@ func SpawnExecuteBlocksStage(s *StageState, u Unwinder, tx kv.RwTx, toBlock uint
 	var stoppedErr error
 Loop:
 	for blockNum := stageProgress + 1; blockNum <= to; blockNum++ {
-		if stoppedErr = libcommon.Stopped(quit); stoppedErr != nil {
+		if stoppedErr = common.Stopped(quit); stoppedErr != nil {
 			break
 		}
 
@@ -275,7 +276,7 @@ Loop:
 
 		lastLogTx += uint64(block.Transactions().Len())
 
-		var contractHasTEVM func(contractHash common.Hash) (bool, error)
+		var contractHasTEVM func(contractHash commonold.Hash) (bool, error)
 
 		if cfg.vmConfig.EnableTEMV {
 			contractHasTEVM = ethdb.GetHasTEVM(tx)
@@ -329,9 +330,9 @@ Loop:
 			totalGasTmp := new(big.Int).Set(totalGasUsed)
 			elapsed := time.Since(startTime)
 			estimateRatio := float64(cumulativeGas.Sub(cumulativeGas, startGasUsed).Uint64()) / float64(totalGasTmp.Sub(totalGasTmp, startGasUsed).Uint64())
-			var estimatedTime common.PrettyDuration
+			var estimatedTime commonold.PrettyDuration
 			if estimateRatio != 0 {
-				estimatedTime = common.PrettyDuration((elapsed.Seconds() / estimateRatio) * float64(time.Second))
+				estimatedTime = commonold.PrettyDuration((elapsed.Seconds() / estimateRatio) * float64(time.Second))
 			}
 			logBlock, logTx, logTime = logProgress(logPrefix, logBlock, logTime, blockNum, logTx, lastLogTx, gas, float64(currentStateGas)/float64(gasState), estimatedTime, batch)
 			gas = 0
@@ -357,7 +358,7 @@ Loop:
 	return stoppedErr
 }
 
-func logProgress(logPrefix string, prevBlock uint64, prevTime time.Time, currentBlock uint64, prevTx, currentTx uint64, gas uint64, gasState float64, estimatedTime common.PrettyDuration, batch ethdb.DbWithPendingMutations) (uint64, uint64, time.Time) {
+func logProgress(logPrefix string, prevBlock uint64, prevTime time.Time, currentBlock uint64, prevTx, currentTx uint64, gas uint64, gasState float64, estimatedTime commonold.PrettyDuration, batch ethdb.DbWithPendingMutations) (uint64, uint64, time.Time) {
 	currentTime := time.Now()
 	interval := currentTime.Sub(prevTime)
 	speed := float64(currentBlock-prevBlock) / (float64(interval) / float64(time.Second))
@@ -377,9 +378,9 @@ func logProgress(logPrefix string, prevBlock uint64, prevTime time.Time, current
 		logpairs = append(logpairs, "estimated duration", estimatedTime)
 	}
 	if batch != nil {
-		logpairs = append(logpairs, "batch", libcommon.ByteCount(uint64(batch.BatchSize())))
+		logpairs = append(logpairs, "batch", common.ByteCount(uint64(batch.BatchSize())))
 	}
-	logpairs = append(logpairs, "alloc", libcommon.ByteCount(m.Alloc), "sys", libcommon.ByteCount(m.Sys))
+	logpairs = append(logpairs, "alloc", common.ByteCount(m.Alloc), "sys", common.ByteCount(m.Sys))
 	log.Info(fmt.Sprintf("[%s] Executed blocks", logPrefix), logpairs...)
 
 	return currentBlock, currentTx, currentTime
@@ -453,7 +454,7 @@ func unwindExecutionStage(u *UnwindState, s *StageState, tx kv.RwTx, quit <-chan
 
 				// Fetch the code hash
 				recoverCodeHashPlain(&acc, tx, k)
-				var address common.Address
+				var address commonold.Address
 				copy(address[:], k)
 
 				// cleanup contract code bucket
@@ -481,7 +482,7 @@ func unwindExecutionStage(u *UnwindState, s *StageState, tx kv.RwTx, quit <-chan
 				}
 			} else {
 				if accumulator != nil {
-					var address common.Address
+					var address commonold.Address
 					copy(address[:], k)
 					accumulator.DeleteAccount(address)
 				}
@@ -492,13 +493,13 @@ func unwindExecutionStage(u *UnwindState, s *StageState, tx kv.RwTx, quit <-chan
 			return nil
 		}
 		if accumulator != nil {
-			var address common.Address
+			var address commonold.Address
 			var incarnation uint64
-			var location common.Hash
+			var location commonold.Hash
 			copy(address[:], k[:length.Addr])
 			incarnation = binary.BigEndian.Uint64(k[length.Addr:])
 			copy(location[:], k[length.Addr+length.Incarnation:])
-			accumulator.ChangeStorage(address, incarnation, location, libcommon.Copy(v))
+			accumulator.ChangeStorage(address, incarnation, location, common.Copy(v))
 		}
 		if len(v) > 0 {
 			if err := next(k, k[:storageKeyLength], v); err != nil {
@@ -550,7 +551,7 @@ func unwindExecutionStage(u *UnwindState, s *StageState, tx kv.RwTx, quit <-chan
 }
 
 func recoverCodeHashPlain(acc *accounts.Account, db kv.Tx, key []byte) {
-	var address common.Address
+	var address commonold.Address
 	copy(address[:], key)
 	if acc.Incarnation > 0 && acc.IsEmptyCodeHash() {
 		if codeHash, err2 := db.GetOne(kv.PlainContractCode, dbutils.PlainGenerateStoragePrefix(address[:], acc.Incarnation)); err2 == nil {
@@ -559,13 +560,6 @@ func recoverCodeHashPlain(acc *accounts.Account, db kv.Tx, key []byte) {
 	}
 }
 
-func min(a, b uint64) uint64 {
-	if a <= b {
-		return a
-	}
-	return b
-}
-
 func PruneExecutionStage(s *PruneState, tx kv.RwTx, cfg ExecuteBlockCfg, ctx context.Context, initialCycle bool) (err error) {
 	logPrefix := s.LogPrefix()
 	useExternalTx := tx != nil
diff --git a/eth/stagedsync/stage_headers.go b/eth/stagedsync/stage_headers.go
index ba3a9ffcdd..5c28a417c0 100644
--- a/eth/stagedsync/stage_headers.go
+++ b/eth/stagedsync/stage_headers.go
@@ -12,6 +12,7 @@ import (
 	"github.com/c2h5oh/datasize"
 	"github.com/holiman/uint256"
 	libcommon "github.com/ledgerwatch/erigon-lib/common"
+	"github.com/ledgerwatch/erigon-lib/common/cmp"
 	"github.com/ledgerwatch/erigon-lib/etl"
 	proto_downloader "github.com/ledgerwatch/erigon-lib/gointerfaces/downloader"
 	"github.com/ledgerwatch/erigon-lib/gointerfaces/remote"
@@ -1096,13 +1097,7 @@ func DownloadAndIndexSnapshotsIfNeed(s *StageState, ctx context.Context, tx kv.R
 			// wait for Downloader service to download all expected snapshots
 			if cfg.snapshots.IndicesAvailable() < cfg.snapshots.SegmentsAvailable() {
 				chainID, _ := uint256.FromBig(cfg.chainConfig.ChainID)
-				workers := runtime.GOMAXPROCS(-1) - 1
-				if workers < 1 {
-					workers = 1
-				}
-				if workers > 2 {
-					workers = 2 // 4 workers get killed on 16Gb RAM
-				}
+				workers := cmp.InRange(1, 2, runtime.GOMAXPROCS(-1)-1)
 				if err := snapshotsync.BuildIndices(ctx, cfg.snapshots, *chainID, cfg.tmpdir, cfg.snapshots.IndicesAvailable(), workers, log.LvlInfo); err != nil {
 					return err
 				}
diff --git a/eth/stagedsync/stage_senders.go b/eth/stagedsync/stage_senders.go
index a0fe3604ba..98b3f2a07f 100644
--- a/eth/stagedsync/stage_senders.go
+++ b/eth/stagedsync/stage_senders.go
@@ -11,6 +11,7 @@ import (
 
 	"github.com/holiman/uint256"
 	libcommon "github.com/ledgerwatch/erigon-lib/common"
+	"github.com/ledgerwatch/erigon-lib/common/cmp"
 	"github.com/ledgerwatch/erigon-lib/common/length"
 	"github.com/ledgerwatch/erigon-lib/etl"
 	"github.com/ledgerwatch/erigon-lib/kv"
@@ -80,7 +81,7 @@ func SpawnRecoverSendersStage(cfg SendersCfg, s *StageState, u Unwinder, tx kv.R
 
 	var to = prevStageProgress
 	if toBlock > 0 {
-		to = min(prevStageProgress, toBlock)
+		to = cmp.Min(prevStageProgress, toBlock)
 	}
 	if to <= s.BlockNumber {
 		return nil
diff --git a/eth/stagedsync/stage_tevm.go b/eth/stagedsync/stage_tevm.go
index 8d033a055d..d43ea4af8f 100644
--- a/eth/stagedsync/stage_tevm.go
+++ b/eth/stagedsync/stage_tevm.go
@@ -9,6 +9,7 @@ import (
 
 	"github.com/c2h5oh/datasize"
 	libcommon "github.com/ledgerwatch/erigon-lib/common"
+	"github.com/ledgerwatch/erigon-lib/common/cmp"
 	"github.com/ledgerwatch/erigon-lib/kv"
 	"github.com/ledgerwatch/erigon/common"
 	"github.com/ledgerwatch/erigon/common/dbutils"
@@ -58,7 +59,7 @@ func SpawnTranspileStage(s *StageState, tx kv.RwTx, toBlock uint64, cfg Transpil
 
 	var to = prevStageProgress
 	if toBlock > 0 {
-		to = min(prevStageProgress, toBlock)
+		to = cmp.Min(prevStageProgress, toBlock)
 	}
 
 	if to <= s.BlockNumber {
diff --git a/eth/stagedsync/stage_txlookup.go b/eth/stagedsync/stage_txlookup.go
index 14b3de740a..cf7241e0c8 100644
--- a/eth/stagedsync/stage_txlookup.go
+++ b/eth/stagedsync/stage_txlookup.go
@@ -6,6 +6,7 @@ import (
 	"fmt"
 	"math/big"
 
+	libcommon "github.com/ledgerwatch/erigon-lib/common/cmp"
 	"github.com/ledgerwatch/erigon-lib/etl"
 	"github.com/ledgerwatch/erigon-lib/kv"
 	"github.com/ledgerwatch/erigon/common"
@@ -57,7 +58,7 @@ func SpawnTxLookup(s *StageState, tx kv.RwTx, toBlock uint64, cfg TxLookupCfg, c
 		return err
 	}
 	if toBlock > 0 {
-		endBlock = min(endBlock, toBlock)
+		endBlock = libcommon.Min(endBlock, toBlock)
 	}
 
 	startBlock := s.BlockNumber
diff --git a/go.mod b/go.mod
index 130bb5fbc9..4ac6251887 100644
--- a/go.mod
+++ b/go.mod
@@ -35,7 +35,7 @@ require (
 	github.com/json-iterator/go v1.1.12
 	github.com/julienschmidt/httprouter v1.3.0
 	github.com/kevinburke/go-bindata v3.21.0+incompatible
-	github.com/ledgerwatch/erigon-lib v0.0.0-20220507081822-8bc26c0e011c
+	github.com/ledgerwatch/erigon-lib v0.0.0-20220510025207-2689ed9cf66d
 	github.com/ledgerwatch/log/v3 v3.4.1
 	github.com/ledgerwatch/secp256k1 v1.0.0
 	github.com/pelletier/go-toml v1.9.5
@@ -149,6 +149,7 @@ require (
 	github.com/valyala/fastrand v1.1.0 // indirect
 	github.com/valyala/histogram v1.2.0 // indirect
 	go.etcd.io/bbolt v1.3.6 // indirect
+	golang.org/x/exp v0.0.0-20220428152302-39d4317da171 // indirect
 	golang.org/x/image v0.0.0-20200927104501-e162460cd6b5 // indirect
 	golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 // indirect
 	golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 // indirect
diff --git a/go.sum b/go.sum
index b4cd327183..f0d5e93340 100644
--- a/go.sum
+++ b/go.sum
@@ -448,8 +448,8 @@ github.com/kylelemons/godebug v0.0.0-20170224010052-a616ab194758 h1:0D5M2HQSGD3P
 github.com/kylelemons/godebug v0.0.0-20170224010052-a616ab194758/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k=
 github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c=
 github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8=
-github.com/ledgerwatch/erigon-lib v0.0.0-20220507081822-8bc26c0e011c h1:rGatAdU/2VVVDBmcGT0UKlOI9rq1leQKUX++4K4Ab5M=
-github.com/ledgerwatch/erigon-lib v0.0.0-20220507081822-8bc26c0e011c/go.mod h1:WiZsC/i9wnQ6ymieYb0c1SSur4x5Mk4hbDR3fnVNLNQ=
+github.com/ledgerwatch/erigon-lib v0.0.0-20220510025207-2689ed9cf66d h1:cFFaMnIheahNQhbMAK/FtIDBZtkaEepuS38Rursznpc=
+github.com/ledgerwatch/erigon-lib v0.0.0-20220510025207-2689ed9cf66d/go.mod h1:oHHsHZR+xF3LSHzjBlJYW21ZnBXkwIZrVebCp6v9Iv0=
 github.com/ledgerwatch/log/v3 v3.4.1 h1:/xGwlVulXnsO9Uq+tzaExc8OWmXXHU0dnLalpbnY5Bc=
 github.com/ledgerwatch/log/v3 v3.4.1/go.mod h1:VXcz6Ssn6XEeU92dCMc39/g1F0OYAjw1Mt+dGP5DjXY=
 github.com/ledgerwatch/secp256k1 v1.0.0 h1:Usvz87YoTG0uePIV8woOof5cQnLXGYa162rFf3YnwaQ=
@@ -771,6 +771,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
 golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
 golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
 golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
+golang.org/x/exp v0.0.0-20220428152302-39d4317da171 h1:TfdoLivD44QwvssI9Sv1xwa5DcL5XQr4au4sZ2F2NV4=
+golang.org/x/exp v0.0.0-20220428152302-39d4317da171/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
 golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
 golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
 golang.org/x/image v0.0.0-20200927104501-e162460cd6b5 h1:QelT11PB4FXiDEXucrfNckHoFxwt8USGY1ajP1ZF5lM=
diff --git a/p2p/discover/common.go b/p2p/discover/common.go
index 52547f85f9..6e234ed8be 100644
--- a/p2p/discover/common.go
+++ b/p2p/discover/common.go
@@ -19,10 +19,11 @@ package discover
 import (
 	"context"
 	"crypto/ecdsa"
-	"github.com/ledgerwatch/erigon/crypto"
 	"net"
 	"time"
 
+	"github.com/ledgerwatch/erigon/crypto"
+
 	"github.com/ledgerwatch/erigon/common/mclock"
 	"github.com/ledgerwatch/erigon/p2p/enode"
 	"github.com/ledgerwatch/erigon/p2p/enr"
@@ -90,10 +91,3 @@ type ReadPacket struct {
 	Data []byte
 	Addr *net.UDPAddr
 }
-
-func min(x, y int) int {
-	if x > y {
-		return y
-	}
-	return x
-}
diff --git a/p2p/discover/lookup_util_test.go b/p2p/discover/lookup_util_test.go
index e12b1262e9..2aebde2be0 100644
--- a/p2p/discover/lookup_util_test.go
+++ b/p2p/discover/lookup_util_test.go
@@ -154,9 +154,7 @@ func (tn *preminedTestnet) closest(n int) (nodes []*enode.Node) {
 func (tn *preminedTestnet) privateKeys() []*ecdsa.PrivateKey {
 	var keys []*ecdsa.PrivateKey
 	for d := range tn.dists {
-		for _, key := range tn.dists[d] {
-			keys = append(keys, key)
-		}
+		keys = append(keys, tn.dists[d]...)
 	}
 	return keys
 }
diff --git a/p2p/discover/v5_udp.go b/p2p/discover/v5_udp.go
index d3b5806737..7ccbb08ce3 100644
--- a/p2p/discover/v5_udp.go
+++ b/p2p/discover/v5_udp.go
@@ -29,6 +29,7 @@ import (
 	"sync"
 	"time"
 
+	common2 "github.com/ledgerwatch/erigon-lib/common/cmp"
 	"github.com/ledgerwatch/erigon/common/debug"
 	"github.com/ledgerwatch/erigon/common/mclock"
 	"github.com/ledgerwatch/erigon/p2p/discover/v5wire"
@@ -388,7 +389,7 @@ func (t *UDPv5) waitForNodes(c *callV5, distances []uint) ([]*enode.Node, error)
 				nodes = append(nodes, node)
 			}
 			if total == -1 {
-				total = min(int(response.Total), totalNodesResponseLimit)
+				total = common2.Min(int(response.Total), totalNodesResponseLimit)
 			}
 			if received++; received == total {
 				return nodes, nil
@@ -831,7 +832,7 @@ func packNodes(reqid []byte, nodes []*enode.Node) []*v5wire.Nodes {
 	var resp []*v5wire.Nodes
 	for len(nodes) > 0 {
 		p := &v5wire.Nodes{ReqID: reqid, Total: total}
-		items := min(nodesResponseItemLimit, len(nodes))
+		items := common2.Min(nodesResponseItemLimit, len(nodes))
 		for i := 0; i < items; i++ {
 			p.Nodes = append(p.Nodes, nodes[i].Record())
 		}
diff --git a/turbo/shards/trie_cache.go b/turbo/shards/trie_cache.go
index 5aab732082..295d16ca88 100644
--- a/turbo/shards/trie_cache.go
+++ b/turbo/shards/trie_cache.go
@@ -212,9 +212,7 @@ func (sc *StateCache) SetAccountHashesRead(prefix []byte, hasState, hasTree, has
 	assertSubset(hasTree, hasState)
 	assertSubset(hasHash, hasState)
 	cpy := make([]common.Hash, len(hashes))
-	for i := 0; i < len(hashes); i++ {
-		cpy[i] = hashes[i]
-	}
+	copy(cpy, hashes)
 	ai := AccountHashItem{
 		addrHashPrefix: common.CopyBytes(prefix),
 		hasState:       hasState,
@@ -238,9 +236,7 @@ func (sc *StateCache) SetAccountHashWrite(prefix []byte, hasState, hasTree, hasH
 		hasHash:        hasHash,
 		hashes:         make([]common.Hash, len(hashes)),
 	}
-	for i := 0; i < len(hashes); i++ {
-		ai.hashes[i] = hashes[i]
-	}
+	copy(ai.hashes, hashes)
 	var awi AccountHashWriteItem
 	awi.ai = &ai
 	sc.setWrite(&ai, &awi, false /* delete */)
@@ -256,9 +252,7 @@ func (sc *StateCache) SetAccountHashDelete(prefix []byte) {
 
 func (sc *StateCache) SetStorageHashRead(addrHash common.Hash, incarnation uint64, locHashPrefix []byte, hasState, hasTree, hasHash uint16, hashes []common.Hash) {
 	cpy := make([]common.Hash, len(hashes))
-	for i := 0; i < len(hashes); i++ {
-		cpy[i] = hashes[i]
-	}
+	copy(cpy, hashes)
 	ai := StorageHashItem{
 		addrHash:      addrHash,
 		incarnation:   incarnation,
@@ -273,9 +267,7 @@ func (sc *StateCache) SetStorageHashRead(addrHash common.Hash, incarnation uint6
 
 func (sc *StateCache) SetStorageHashWrite(addrHash common.Hash, incarnation uint64, locHashPrefix []byte, hasState, hasTree, hasHash uint16, hashes []common.Hash) {
 	cpy := make([]common.Hash, len(hashes))
-	for i := 0; i < len(hashes); i++ {
-		cpy[i] = hashes[i]
-	}
+	copy(cpy, hashes)
 	ai := StorageHashItem{
 		addrHash:      addrHash,
 		incarnation:   incarnation,
@@ -292,9 +284,7 @@ func (sc *StateCache) SetStorageHashWrite(addrHash common.Hash, incarnation uint
 
 func (sc *StateCache) SetStorageHashDelete(addrHash common.Hash, incarnation uint64, locHashPrefix []byte, hasState, hasTree, hasHash uint16, hashes []common.Hash) {
 	cpy := make([]common.Hash, len(hashes))
-	for i := 0; i < len(hashes); i++ {
-		cpy[i] = hashes[i]
-	}
+	copy(cpy, hashes)
 	ai := StorageHashItem{
 		addrHash:      addrHash,
 		incarnation:   incarnation,
diff --git a/turbo/snapshotsync/block_snapshots.go b/turbo/snapshotsync/block_snapshots.go
index c4b01056a5..9005d649d0 100644
--- a/turbo/snapshotsync/block_snapshots.go
+++ b/turbo/snapshotsync/block_snapshots.go
@@ -17,6 +17,7 @@ import (
 
 	"github.com/holiman/uint256"
 	common2 "github.com/ledgerwatch/erigon-lib/common"
+	"github.com/ledgerwatch/erigon-lib/common/cmp"
 	"github.com/ledgerwatch/erigon-lib/compress"
 	proto_downloader "github.com/ledgerwatch/erigon-lib/gointerfaces/downloader"
 	"github.com/ledgerwatch/erigon-lib/kv"
@@ -295,7 +296,7 @@ func (s *RoSnapshots) IndicesReady() bool        { return s.indicesReady.Load()
 func (s *RoSnapshots) IndicesAvailable() uint64  { return s.idxAvailable.Load() }
 func (s *RoSnapshots) SegmentsAvailable() uint64 { return s.segmentsAvailable.Load() }
 func (s *RoSnapshots) BlocksAvailable() uint64 {
-	return min(s.segmentsAvailable.Load(), s.idxAvailable.Load())
+	return cmp.Min(s.segmentsAvailable.Load(), s.idxAvailable.Load())
 }
 func (s *RoSnapshots) EnsureExpectedBlocksAreAvailable(cfg *snapshothashes.Config) error {
 	if s.BlocksAvailable() < cfg.ExpectBlocks {
@@ -331,7 +332,7 @@ func (s *RoSnapshots) idxAvailability() uint64 {
 		txs = seg.To - 1
 		break
 	}
-	return min(headers, min(bodies, txs))
+	return cmp.Min(headers, cmp.Min(bodies, txs))
 }
 
 func (s *RoSnapshots) ReopenIndices() error {
@@ -829,17 +830,10 @@ func segments2(dir string) (res []snap.FileInfo, err error) {
 
 func chooseSegmentEnd(from, to, blocksPerFile uint64) uint64 {
 	next := (from/blocksPerFile + 1) * blocksPerFile
-	to = min(next, to)
+	to = cmp.Min(next, to)
 	return to - (to % snap.MIN_SEGMENT_SIZE) // round down to the nearest 1k
 }
 
-func min(a, b uint64) uint64 {
-	if a < b {
-		return a
-	}
-	return b
-}
-
 type BlockRetire struct {
 	working atomic.Bool
 	wg      *sync.WaitGroup
@@ -886,7 +880,7 @@ func canRetire(from, to uint64) (blockFrom, blockTo uint64, can bool) {
 		maxJump = 10_000
 	}
 	//roundedTo1K := (to / 1_000) * 1_000
-	jump := min(maxJump, roundedTo1K-blockFrom)
+	jump := cmp.Min(maxJump, roundedTo1K-blockFrom)
 	switch { // only next segment sizes are allowed
 	case jump >= 500_000:
 		blockTo = blockFrom + 500_000
@@ -903,7 +897,7 @@ func canRetire(from, to uint64) (blockFrom, blockTo uint64, can bool) {
 }
 func CanDeleteTo(curBlockNum uint64, snapshots *RoSnapshots) (blockTo uint64) {
 	hardLimit := (curBlockNum/1_000)*1_000 - params.FullImmutabilityThreshold
-	return min(hardLimit, snapshots.BlocksAvailable()+1)
+	return cmp.Min(hardLimit, snapshots.BlocksAvailable()+1)
 }
 func (br *BlockRetire) RetireBlocksInBackground(ctx context.Context, blockFrom, blockTo uint64, chainID uint256.Int, lvl log.Lvl) {
 	br.result = nil
diff --git a/turbo/trie/trie.go b/turbo/trie/trie.go
index c664870aee..afadc7bf62 100644
--- a/turbo/trie/trie.go
+++ b/turbo/trie/trie.go
@@ -23,6 +23,7 @@ import (
 	"fmt"
 
 	libcommon "github.com/ledgerwatch/erigon-lib/common"
+	"github.com/ledgerwatch/erigon-lib/common/cmp"
 	"github.com/ledgerwatch/erigon/common"
 	"github.com/ledgerwatch/erigon/common/debug"
 	"github.com/ledgerwatch/erigon/core/types/accounts"
@@ -357,13 +358,6 @@ func (t *Trie) NeedLoadCode(addrHash common.Hash, codeHash common.Hash, bytecode
 	return false, nil
 }
 
-func min(a, b int) int {
-	if a < b {
-		return a
-	}
-	return b
-}
-
 // FindSubTriesToLoad walks over the trie and creates the list of DB prefixes and
 // corresponding list of valid bits in the prefix (for the cases when prefix contains an
 // odd number of nibbles) that would allow loading the missing information from the database
@@ -852,7 +846,7 @@ func (t *Trie) deleteRecursive(origNode node, key []byte, keyStart int, preserve
 	switch n := origNode.(type) {
 	case *shortNode:
 		matchlen := prefixLen(key[keyStart:], n.Key)
-		if matchlen == min(len(n.Key), len(key[keyStart:])) || n.Key[matchlen] == 16 || key[keyStart+matchlen] == 16 {
+		if matchlen == cmp.Min(len(n.Key), len(key[keyStart:])) || n.Key[matchlen] == 16 || key[keyStart+matchlen] == 16 {
 			fullMatch := matchlen == len(key)-keyStart
 			removeNodeEntirely := fullMatch
 			if preserveAccountNode {
-- 
GitLab