diff --git a/cmd/txpool/main.go b/cmd/txpool/main.go index 61558a4095e..727cd8213ea 100644 --- a/cmd/txpool/main.go +++ b/cmd/txpool/main.go @@ -2,10 +2,8 @@ package main import ( "context" - "crypto/rand" "errors" "fmt" - "math/big" "os" "path/filepath" "time" @@ -24,6 +22,7 @@ import ( "github.com/ledgerwatch/erigon-lib/txpool/txpooluitl" "github.com/ledgerwatch/erigon-lib/types" "github.com/ledgerwatch/erigon/cmd/rpcdaemon/rpcdaemontest" + common2 "github.com/ledgerwatch/erigon/common" "github.com/ledgerwatch/erigon/ethdb/privateapi" "github.com/ledgerwatch/log/v3" "github.com/spf13/cobra" @@ -137,11 +136,7 @@ func doTxpool(ctx context.Context) error { cfg.DBDir = dirs.TxPool - randDuration, err := rand.Int(rand.Reader, big.NewInt(int64(2*time.Second))) - if err != nil { - return fmt.Errorf("generating random additional value for --txpool.commit.every: %w", err) - } - cfg.CommitEvery = commitEvery + time.Duration(randDuration.Int64()) + cfg.CommitEvery = common2.RandomizeDuration(commitEvery) cfg.PendingSubPoolLimit = pendingPoolLimit cfg.BaseFeeSubPoolLimit = baseFeePoolLimit cfg.QueuedSubPoolLimit = queuedPoolLimit diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index ba81ab85bc5..8fa2a610bf2 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -19,14 +19,12 @@ package utils import ( "crypto/ecdsa" - "crypto/rand" "fmt" "math/big" "path/filepath" "runtime" "strconv" "strings" - "time" "github.com/c2h5oh/datasize" "github.com/ledgerwatch/erigon-lib/chain" @@ -37,6 +35,7 @@ import ( downloadercfg2 "github.com/ledgerwatch/erigon-lib/downloader/downloadercfg" "github.com/ledgerwatch/erigon-lib/kv" "github.com/ledgerwatch/erigon-lib/txpool" + common2 "github.com/ledgerwatch/erigon/common" "github.com/ledgerwatch/log/v3" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -1269,14 +1268,8 @@ func setTxPool(ctx *cli.Context, cfg *core.TxPoolConfig) { cfg.TracedSenders[i] = string(sender[:]) } } - if ctx.IsSet(TxPoolCommitEveryFlag.Name) { - cfg.CommitEvery = ctx.Duration(TxPoolCommitEveryFlag.Name) - randDuration, err := rand.Int(rand.Reader, big.NewInt(int64(2*time.Second))) - if err != nil { - Fatalf("Generating random additional value for --txpool.commit.every: %s", err) - } - cfg.CommitEvery += time.Duration(randDuration.Int64()) - } + + cfg.CommitEvery = common2.RandomizeDuration(ctx.Duration(TxPoolCommitEveryFlag.Name)) } func setEthash(ctx *cli.Context, datadir string, cfg *ethconfig.Config) { diff --git a/common/debug.go b/common/debug.go index 767a34bc95d..8bd0f99a6be 100644 --- a/common/debug.go +++ b/common/debug.go @@ -17,11 +17,14 @@ package common import ( + "crypto/rand" "fmt" + "math/big" "os" "runtime" "runtime/debug" "strings" + "time" ) // Report gives off a warning requesting the user to submit an issue to the github tracker. @@ -50,3 +53,13 @@ func PrintDepricationWarning(str string) { `, line, emptyLine, str, emptyLine, line) } + +// RandomizeDuration - periodic parallel actions may interfere and resonance. +// Use this func to add small randomness to period +func RandomizeDuration(in time.Duration) time.Duration { + randDuration, err := rand.Int(rand.Reader, big.NewInt(int64(time.Second))) + if err != nil { + panic(err) + } + return in + time.Duration(randDuration.Uint64()) +}