Skip to content

Commit

Permalink
purge routine for txpool
Browse files Browse the repository at this point in the history
  • Loading branch information
hexoscott committed Nov 14, 2024
1 parent 70f9b98 commit b126eda
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 3 deletions.
8 changes: 7 additions & 1 deletion cmd/txpool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ var (

noTxGossip bool

commitEvery time.Duration
commitEvery time.Duration
purgeEvery time.Duration
purgeDistance time.Duration
)

func init() {
Expand All @@ -85,6 +87,8 @@ func init() {
rootCmd.PersistentFlags().Uint64Var(&priceBump, "txpool.pricebump", txpoolcfg.DefaultConfig.PriceBump, "Price bump percentage to replace an already existing transaction")
rootCmd.PersistentFlags().Uint64Var(&blobPriceBump, "txpool.blobpricebump", txpoolcfg.DefaultConfig.BlobPriceBump, "Price bump percentage to replace an existing blob (type-3) transaction")
rootCmd.PersistentFlags().DurationVar(&commitEvery, utils.TxPoolCommitEveryFlag.Name, utils.TxPoolCommitEveryFlag.Value, utils.TxPoolCommitEveryFlag.Usage)
rootCmd.PersistentFlags().DurationVar(&purgeEvery, utils.TxpoolPurgeEveryFlag.Name, utils.TxpoolPurgeEveryFlag.Value, utils.TxpoolPurgeEveryFlag.Usage)
rootCmd.PersistentFlags().DurationVar(&purgeDistance, utils.TxpoolPurgeDistanceFlag.Name, utils.TxpoolPurgeDistanceFlag.Value, utils.TxpoolPurgeDistanceFlag.Usage)
rootCmd.PersistentFlags().BoolVar(&noTxGossip, utils.TxPoolGossipDisableFlag.Name, utils.TxPoolGossipDisableFlag.Value, utils.TxPoolGossipDisableFlag.Usage)
rootCmd.Flags().StringSliceVar(&traceSenders, utils.TxPoolTraceSendersFlag.Name, []string{}, utils.TxPoolTraceSendersFlag.Usage)
}
Expand Down Expand Up @@ -144,6 +148,8 @@ func doTxpool(ctx context.Context, logger log.Logger) error {
cfg.DBDir = dirs.TxPool

cfg.CommitEvery = common2.RandomizeDuration(commitEvery)
cfg.PurgeEvery = common2.RandomizeDuration(purgeEvery)
cfg.PurgeDistance = purgeDistance
cfg.PendingSubPoolLimit = pendingPoolLimit
cfg.BaseFeeSubPoolLimit = baseFeePoolLimit
cfg.QueuedSubPoolLimit = queuedPoolLimit
Expand Down
16 changes: 16 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,16 @@ var (
Usage: "How often transactions should be committed to the storage",
Value: txpoolcfg.DefaultConfig.CommitEvery,
}
TxpoolPurgeEveryFlag = cli.DurationFlag{
Name: "txpool.purge.every",
Usage: "How often transactions should be purged from the storage",
Value: txpoolcfg.DefaultConfig.PurgeEvery,
}
TxpoolPurgeDistanceFlag = cli.DurationFlag{
Name: "txpool.purge.distance",
Usage: "Transactions older than this distance will be purged",
Value: txpoolcfg.DefaultConfig.PurgeDistance,
}
// Miner settings
MiningEnabledFlag = cli.BoolFlag{
Name: "mine",
Expand Down Expand Up @@ -1914,6 +1924,12 @@ func setTxPool(ctx *cli.Context, fullCfg *ethconfig.Config) {
fullCfg.TxPool.BlobPriceBump = ctx.Uint64(TxPoolBlobPriceBumpFlag.Name)
}
cfg.CommitEvery = common2.RandomizeDuration(ctx.Duration(TxPoolCommitEveryFlag.Name))

purgeEvery := ctx.Duration(TxpoolPurgeEveryFlag.Name)
purgeDistance := ctx.Duration(TxpoolPurgeDistanceFlag.Name)

fullCfg.TxPool.PurgeEvery = common2.RandomizeDuration(purgeEvery)
fullCfg.TxPool.PurgeDistance = purgeDistance
}

func setEthash(ctx *cli.Context, datadir string, cfg *ethconfig.Config) {
Expand Down
3 changes: 2 additions & 1 deletion core/vm/contracts_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ var PrecompiledContractsForkID8Elderberry = map[libcommon.Address]PrecompiledCon
libcommon.BytesToAddress([]byte{2}): &sha256hash_zkevm{enabled: true},
libcommon.BytesToAddress([]byte{3}): &ripemd160hash_zkevm{enabled: false},
libcommon.BytesToAddress([]byte{4}): &dataCopy_zkevm{enabled: true},
libcommon.BytesToAddress([]byte{5}): &bigModExp_zkevm{enabled: false, eip2565: true},
libcommon.BytesToAddress([]byte{5}): &bigModExp_zkevm{enabled: true, eip2565: true},
libcommon.BytesToAddress([]byte{6}): &bn256AddIstanbul_zkevm{enabled: true},
libcommon.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul_zkevm{enabled: true},
libcommon.BytesToAddress([]byte{8}): &bn256PairingIstanbul_zkevm{enabled: true},
Expand Down Expand Up @@ -316,6 +316,7 @@ func (c *bigModExp_zkevm) RequiredGas(input []byte) uint64 {
// - if neither of the above are true we check for reverts and return 0 gas fee

if modBitLen == 0 {
return 0
// consume as normal - will return 0
} else if baseBitLen == 0 {
if modBitLen > 8192 {
Expand Down
5 changes: 5 additions & 0 deletions erigon-lib/txpool/txpoolcfg/txpoolcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,25 @@ type Config struct {
ProcessRemoteTxsEvery time.Duration
CommitEvery time.Duration
LogEvery time.Duration
PurgeEvery time.Duration

//txpool db
MdbxPageSize datasize.ByteSize
MdbxDBSizeLimit datasize.ByteSize
MdbxGrowthStep datasize.ByteSize

NoGossip bool // this mode doesn't broadcast any txs, and if receive remote-txn - skip it

PurgeDistance time.Duration
}

var DefaultConfig = Config{
SyncToNewPeersEvery: 5 * time.Second,
ProcessRemoteTxsEvery: 100 * time.Millisecond,
CommitEvery: 15 * time.Second,
LogEvery: 30 * time.Second,
PurgeEvery: 1 * time.Minute,
PurgeDistance: 24 * time.Hour,

PendingSubPoolLimit: 10_000,
BaseFeeSubPoolLimit: 10_000,
Expand Down
4 changes: 4 additions & 0 deletions eth/ethconfig/tx_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,13 @@ var DefaultTxPool2Config = func(fullCfg *Config) txpoolcfg.Config {
cfg.BlobSlots = fullCfg.TxPool.BlobSlots
cfg.TotalBlobPoolLimit = fullCfg.TxPool.TotalBlobPoolLimit
cfg.LogEvery = 3 * time.Minute
cfg.PurgeEvery = 1 * time.Minute
cfg.PurgeDistance = 24 * time.Hour
cfg.CommitEvery = 5 * time.Minute
cfg.TracedSenders = pool1Cfg.TracedSenders
cfg.CommitEvery = pool1Cfg.CommitEvery
cfg.PurgeEvery = fullCfg.TxPool.PurgeEvery
cfg.PurgeDistance = fullCfg.TxPool.PurgeDistance

return cfg
}
2 changes: 2 additions & 0 deletions turbo/cli/default_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ var DefaultFlags = []cli.Flag{
&utils.TxPoolLifetimeFlag,
&utils.TxPoolTraceSendersFlag,
&utils.TxPoolCommitEveryFlag,
&utils.TxpoolPurgeEveryFlag,
&utils.TxpoolPurgeDistanceFlag,
&PruneFlag,
&PruneHistoryFlag,
&PruneReceiptFlag,
Expand Down
43 changes: 42 additions & 1 deletion zk/txpool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ const (
DiscardByLimbo DiscardReason = 27
SmartContractDeploymentDisabled DiscardReason = 28 // to == null not allowed, config set to block smart contract deployment
GasLimitTooHigh DiscardReason = 29 // gas limit is too high
Expired DiscardReason = 30 // used when a transaction is purged from the pool
)

func (r DiscardReason) String() string {
Expand Down Expand Up @@ -226,13 +227,14 @@ type metaTx struct {
bestIndex int
worstIndex int
timestamp uint64 // when it was added to pool
created uint64 // unix timestamp of creation
subPool SubPoolMarker
currentSubPool SubPoolType
alreadyYielded bool
}

func newMetaTx(slot *types.TxSlot, isLocal bool, timestmap uint64) *metaTx {
mt := &metaTx{Tx: slot, worstIndex: -1, bestIndex: -1, timestamp: timestmap}
mt := &metaTx{Tx: slot, worstIndex: -1, bestIndex: -1, timestamp: timestmap, created: uint64(time.Now().Unix())}
if isLocal {
mt.subPool = IsLocal
}
Expand Down Expand Up @@ -1391,6 +1393,8 @@ func MainLoop(ctx context.Context, db kv.RwDB, coreDB kv.RoDB, p *TxPool, newTxs
defer commitEvery.Stop()
logEvery := time.NewTicker(p.cfg.LogEvery)
defer logEvery.Stop()
purgeEvery := time.NewTicker(p.cfg.PurgeEvery)
defer purgeEvery.Stop()

for {
select {
Expand Down Expand Up @@ -1520,6 +1524,8 @@ func MainLoop(ctx context.Context, db kv.RwDB, coreDB kv.RoDB, p *TxPool, newTxs
types, sizes, hashes = p.AppendAllAnnouncements(types, sizes, hashes[:0])
go send.PropagatePooledTxsToPeersList(newPeers, types, sizes, hashes)
propagateToNewPeerTimer.UpdateDuration(t)
case <-purgeEvery.C:
p.purge()
}
}
}
Expand Down Expand Up @@ -1833,6 +1839,41 @@ func (p *TxPool) deprecatedForEach(_ context.Context, f func(rlp []byte, sender
})
}

func (p *TxPool) purge() {
p.lock.Lock()
defer p.lock.Unlock()

// go through all transactions and remove the ones that have a timestamp older than the purge time in config
cutOff := uint64(time.Now().Add(-p.cfg.PurgeDistance).Unix())
log.Debug("[txpool] purging", "cutOff", cutOff)

toDelete := make([]*metaTx, 0)

p.all.ascendAll(func(mt *metaTx) bool {
if mt.created < cutOff {
toDelete = append(toDelete, mt)
}

return false
})

for _, mt := range toDelete {
p.discardLocked(mt, Expired)
// do not hold on to the discard reason as we're purging it completely from the pool
p.discardReasonsLRU.Remove(string(mt.Tx.IDHash[:]))

// get the address of the sender
addr := common.Address{}
if checkAddr, ok := p.senders.senderID2Addr[mt.Tx.SenderID]; ok {
addr = checkAddr
}
log.Debug("[txpool] purge",
"sender", addr,
"hash", hex.EncodeToString(mt.Tx.IDHash[:]),
"ts", mt.created)
}
}

// CalcIntrinsicGas computes the 'intrinsic gas' for a message with the given data.
func CalcIntrinsicGas(dataLen, dataNonZeroLen uint64, accessList types.AccessList, isContractCreation, isHomestead, isEIP2028, isShanghai bool) (uint64, DiscardReason) {
// Set the starting gas for the raw transaction
Expand Down

0 comments on commit b126eda

Please sign in to comment.