From 4e5dce13654bd40ec6b5dcd675b0fcaccc6dbe63 Mon Sep 17 00:00:00 2001 From: Temirlan Date: Wed, 22 Mar 2023 09:53:47 +0600 Subject: [PATCH] add flag txpool.commit_every (#7062) Adds flag _--txpool.commit_every_ to control how often transactions should be committed to the storage [Related Issue](https://github.com/ledgerwatch/erigon/issues/7002) --- cmd/txpool/main.go | 12 +++++++++++- cmd/utils/flags.go | 15 +++++++++++++++ core/tx_pool.go | 2 ++ turbo/cli/default_flags.go | 1 + turbo/cli/flags.go | 7 +++++++ 5 files changed, 36 insertions(+), 1 deletion(-) diff --git a/cmd/txpool/main.go b/cmd/txpool/main.go index 7ca936f37..61558a409 100644 --- a/cmd/txpool/main.go +++ b/cmd/txpool/main.go @@ -2,8 +2,10 @@ package main import ( "context" + "crypto/rand" "errors" "fmt" + "math/big" "os" "path/filepath" "time" @@ -50,6 +52,8 @@ var ( priceLimit uint64 accountSlots uint64 priceBump uint64 + + commitEvery time.Duration ) func init() { @@ -71,6 +75,7 @@ func init() { rootCmd.PersistentFlags().Uint64Var(&priceLimit, "txpool.pricelimit", txpool.DefaultConfig.MinFeeCap, "Minimum gas price (fee cap) limit to enforce for acceptance into the pool") rootCmd.PersistentFlags().Uint64Var(&accountSlots, "txpool.accountslots", txpool.DefaultConfig.AccountSlots, "Minimum number of executable transaction slots guaranteed per account") rootCmd.PersistentFlags().Uint64Var(&priceBump, "txpool.pricebump", txpool.DefaultConfig.PriceBump, "Price bump percentage to replace an already existing transaction") + rootCmd.PersistentFlags().DurationVar(&commitEvery, utils.TxPoolCommitEveryFlag.Name, utils.TxPoolCommitEveryFlag.Value, utils.TxPoolCommitEveryFlag.Usage) rootCmd.Flags().StringSliceVar(&traceSenders, utils.TxPoolTraceSendersFlag.Name, []string{}, utils.TxPoolTraceSendersFlag.Usage) } @@ -131,7 +136,12 @@ func doTxpool(ctx context.Context) error { dirs := datadir.New(datadirCli) cfg.DBDir = dirs.TxPool - cfg.CommitEvery = 30 * time.Second + + 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.PendingSubPoolLimit = pendingPoolLimit cfg.BaseFeeSubPoolLimit = baseFeePoolLimit cfg.QueuedSubPoolLimit = queuedPoolLimit diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 0c43334e5..ba81ab85b 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -19,12 +19,14 @@ 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" @@ -193,6 +195,11 @@ var ( Usage: "Comma separared list of addresses, whoes transactions will traced in transaction pool with debug printing", Value: "", } + TxPoolCommitEveryFlag = cli.DurationFlag{ + Name: "txpool.commit.every", + Usage: "How often transactions should be committed to the storage", + Value: txpool.DefaultConfig.CommitEvery, + } // Miner settings MiningEnabledFlag = cli.BoolFlag{ Name: "mine", @@ -1262,6 +1269,14 @@ 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()) + } } func setEthash(ctx *cli.Context, datadir string, cfg *ethconfig.Config) { diff --git a/core/tx_pool.go b/core/tx_pool.go index 9319cf521..e7811229a 100644 --- a/core/tx_pool.go +++ b/core/tx_pool.go @@ -77,6 +77,7 @@ type TxPoolConfig struct { Lifetime time.Duration // Maximum amount of time non-executable transaction are queued StartOnInit bool TracedSenders []string // List of senders for which tx pool should print out debugging info + CommitEvery time.Duration } // DeprecatedDefaultTxPoolConfig contains the default configurations for the transaction @@ -105,6 +106,7 @@ var DefaultTxPool2Config = func(pool1Cfg TxPoolConfig) txpool.Config { cfg.LogEvery = 1 * time.Minute cfg.CommitEvery = 5 * time.Minute cfg.TracedSenders = pool1Cfg.TracedSenders + cfg.CommitEvery = pool1Cfg.CommitEvery return cfg } diff --git a/turbo/cli/default_flags.go b/turbo/cli/default_flags.go index 100cde2b3..6a69a67f1 100644 --- a/turbo/cli/default_flags.go +++ b/turbo/cli/default_flags.go @@ -25,6 +25,7 @@ var DefaultFlags = []cli.Flag{ &utils.TxPoolGlobalQueueFlag, &utils.TxPoolLifetimeFlag, &utils.TxPoolTraceSendersFlag, + &utils.TxPoolCommitEveryFlag, &PruneFlag, &PruneHistoryFlag, &PruneReceiptFlag, diff --git a/turbo/cli/flags.go b/turbo/cli/flags.go index d8a944aa4..09acebc0f 100644 --- a/turbo/cli/flags.go +++ b/turbo/cli/flags.go @@ -2,6 +2,7 @@ package cli import ( "fmt" + "github.com/ledgerwatch/erigon-lib/txpool" "strings" "time" @@ -194,6 +195,12 @@ var ( Usage: "Maximum amount of time to wait for the answer from EVM call.", Value: rpccfg.DefaultEvmCallTimeout, } + + TxPoolCommitEvery = cli.DurationFlag{ + Name: "txpool.commit.every", + Usage: "How often transactions should be committed to the storage", + Value: txpool.DefaultConfig.CommitEvery, + } ) func ApplyFlagsForEthConfig(ctx *cli.Context, cfg *ethconfig.Config) {