2022-01-18 14:40:02 +00:00
|
|
|
# Nimbus
|
2024-05-28 18:26:51 +00:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2022-01-18 14:40:02 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except
|
|
|
|
# according to those terms.
|
|
|
|
|
|
|
|
## Transaction Pool Descriptor
|
|
|
|
## ===========================
|
|
|
|
##
|
|
|
|
|
|
|
|
import
|
|
|
|
std/[times],
|
2022-12-02 04:35:41 +00:00
|
|
|
../../common/common,
|
2022-01-18 14:40:02 +00:00
|
|
|
./tx_chain,
|
|
|
|
./tx_info,
|
|
|
|
./tx_item,
|
|
|
|
./tx_tabs,
|
|
|
|
./tx_tabs/tx_sender, # for verify()
|
2022-12-02 04:35:41 +00:00
|
|
|
eth/keys
|
2022-01-18 14:40:02 +00:00
|
|
|
|
2023-01-30 22:10:23 +00:00
|
|
|
{.push raises: [].}
|
2022-01-18 14:40:02 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
TxPoolCallBackRecursion* = object of Defect
|
|
|
|
## Attempt to recurse a call back function
|
|
|
|
|
|
|
|
TxPoolFlags* = enum ##\
|
|
|
|
## Processing strategy selector symbols
|
|
|
|
|
|
|
|
stageItems1559MinFee ##\
|
|
|
|
## Stage tx items with `tx.maxFee` at least `minFeePrice`. Other items
|
|
|
|
## are left or set pending. This symbol affects post-London tx items,
|
|
|
|
## only.
|
|
|
|
|
|
|
|
stageItems1559MinTip ##\
|
|
|
|
## Stage tx items with `tx.effectiveGasTip(baseFee)` at least
|
|
|
|
## `minTipPrice`. Other items are considered underpriced and left
|
|
|
|
## or set pending. This symbol affects post-London tx items, only.
|
|
|
|
|
|
|
|
stageItemsPlMinPrice ##\
|
|
|
|
## Stage tx items with `tx.gasPrice` at least `minPreLondonGasPrice`.
|
|
|
|
## Other items are considered underpriced and left or set pending.
|
|
|
|
## This symbol affects pre-London tx items, only.
|
|
|
|
|
|
|
|
# -----------
|
|
|
|
|
|
|
|
packItemsMaxGasLimit ##\
|
|
|
|
## It set, the *packer* will execute and collect additional items from
|
|
|
|
## the `staged` bucket while accumulating `gasUsed` as long as
|
|
|
|
## `maxGasLimit` is not exceeded. If `packItemsTryHarder` flag is also
|
|
|
|
## set, the *packer* will not stop until at least `hwmGasLimit` is
|
|
|
|
## reached.
|
|
|
|
##
|
|
|
|
## Otherwise the *packer* will accumulate up until `trgGasLimit` is
|
|
|
|
## not exceeded, and not stop until at least `lwmGasLimit` is reached
|
|
|
|
## in case `packItemsTryHarder` is also set,
|
|
|
|
|
|
|
|
packItemsTryHarder ##\
|
|
|
|
## It set, the *packer* will *not* stop accumulaing transactions up until
|
|
|
|
## the `lwmGasLimit` or `hwmGasLimit` is reached, depending on whether
|
|
|
|
## the `packItemsMaxGasLimit` is set. Otherwise, accumulating stops
|
|
|
|
## immediately before the next transaction exceeds `trgGasLimit`, or
|
|
|
|
## `maxGasLimit` depending on `packItemsMaxGasLimit`.
|
|
|
|
|
|
|
|
# -----------
|
|
|
|
|
|
|
|
autoUpdateBucketsDB ##\
|
|
|
|
## Automatically update the state buckets after running batch jobs if
|
|
|
|
## the `dirtyBuckets` flag is also set.
|
|
|
|
|
|
|
|
autoZombifyUnpacked ##\
|
|
|
|
## Automatically dispose *pending* or *staged* txs that were queued
|
|
|
|
## at least `lifeTime` ago.
|
|
|
|
|
|
|
|
autoZombifyPacked ##\
|
|
|
|
## Automatically dispose *packed* txs that were queued
|
|
|
|
## at least `lifeTime` ago.
|
|
|
|
|
|
|
|
TxPoolParam* = tuple ## Getter/setter accessible parameters
|
|
|
|
minFeePrice: GasPrice ## Gas price enforced by the pool, `gasFeeCap`
|
|
|
|
minTipPrice: GasPrice ## Desired tip-per-tx target, `effectiveGasTip`
|
|
|
|
minPlGasPrice: GasPrice ## Desired pre-London min `gasPrice`
|
|
|
|
dirtyBuckets: bool ## Buckets need to be updated
|
|
|
|
doubleCheck: seq[TxItemRef] ## Check items after moving block chain head
|
|
|
|
flags: set[TxPoolFlags] ## Processing strategy symbols
|
|
|
|
|
|
|
|
TxPoolRef* = ref object of RootObj ##\
|
|
|
|
## Transaction pool descriptor
|
|
|
|
startDate: Time ## Start date (read-only)
|
|
|
|
|
|
|
|
chain: TxChainRef ## block chain state
|
|
|
|
txDB: TxTabsRef ## Transaction lists & tables
|
|
|
|
|
|
|
|
lifeTime*: times.Duration ## Maximum life time of a tx in the system
|
|
|
|
priceBump*: uint ## Min precentage price when superseding
|
2023-08-18 07:25:11 +00:00
|
|
|
blockValue*: UInt256 ## Sum of reward received by feeRecipient
|
2022-01-18 14:40:02 +00:00
|
|
|
|
|
|
|
param: TxPoolParam ## Getter/Setter parameters
|
|
|
|
|
|
|
|
const
|
|
|
|
txItemLifeTime = ##\
|
|
|
|
## Maximum amount of time transactions can be held in the database\
|
|
|
|
## unless they are packed already for a block. This default is chosen\
|
|
|
|
## as found in core/tx_pool.go(184) of the geth implementation.
|
|
|
|
initDuration(hours = 3)
|
|
|
|
|
|
|
|
txPriceBump = ##\
|
|
|
|
## Minimum price bump percentage to replace an already existing\
|
|
|
|
## transaction (nonce). This default is chosen as found in\
|
|
|
|
## core/tx_pool.go(177) of the geth implementation.
|
|
|
|
10u
|
|
|
|
|
|
|
|
txMinFeePrice = 1.GasPrice
|
|
|
|
txMinTipPrice = 1.GasPrice
|
|
|
|
txPoolFlags = {stageItems1559MinTip,
|
|
|
|
stageItems1559MinFee,
|
|
|
|
stageItemsPlMinPrice,
|
|
|
|
packItemsTryHarder,
|
|
|
|
autoUpdateBucketsDB,
|
|
|
|
autoZombifyUnpacked}
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions, constructor
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-05-28 18:26:51 +00:00
|
|
|
proc init*(xp: TxPoolRef; com: CommonRef)
|
2023-01-30 22:10:23 +00:00
|
|
|
{.gcsafe,raises: [CatchableError].} =
|
2024-05-28 18:26:51 +00:00
|
|
|
## Constructor, returns new tx-pool descriptor.
|
2022-01-18 14:40:02 +00:00
|
|
|
xp.startDate = getTime().utc.toTime
|
|
|
|
|
2024-05-28 18:26:51 +00:00
|
|
|
xp.chain = TxChainRef.new(com)
|
2022-01-18 14:40:02 +00:00
|
|
|
xp.txDB = TxTabsRef.new
|
|
|
|
|
|
|
|
xp.lifeTime = txItemLifeTime
|
|
|
|
xp.priceBump = txPriceBump
|
|
|
|
|
|
|
|
xp.param.reset
|
|
|
|
xp.param.minFeePrice = txMinFeePrice
|
|
|
|
xp.param.minTipPrice = txMinTipPrice
|
|
|
|
xp.param.flags = txPoolFlags
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions, getters
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-05-28 18:26:51 +00:00
|
|
|
func chain*(xp: TxPoolRef): TxChainRef =
|
2022-01-18 14:40:02 +00:00
|
|
|
## Getter, block chain DB
|
|
|
|
xp.chain
|
|
|
|
|
2024-05-28 18:26:51 +00:00
|
|
|
func pFlags*(xp: TxPoolRef): set[TxPoolFlags] =
|
2022-01-18 14:40:02 +00:00
|
|
|
## Returns the set of algorithm strategy symbols for labelling items
|
|
|
|
## as`packed`
|
|
|
|
xp.param.flags
|
|
|
|
|
2024-05-28 18:26:51 +00:00
|
|
|
func pDirtyBuckets*(xp: TxPoolRef): bool =
|
2022-01-18 14:40:02 +00:00
|
|
|
## Getter, buckets need update
|
|
|
|
xp.param.dirtyBuckets
|
|
|
|
|
2024-05-28 18:26:51 +00:00
|
|
|
func pDoubleCheck*(xp: TxPoolRef): seq[TxItemRef] =
|
2022-01-18 14:40:02 +00:00
|
|
|
## Getter, cached block chain head was moved back
|
|
|
|
xp.param.doubleCheck
|
|
|
|
|
2024-05-28 18:26:51 +00:00
|
|
|
func pMinFeePrice*(xp: TxPoolRef): GasPrice =
|
2022-01-18 14:40:02 +00:00
|
|
|
## Getter
|
|
|
|
xp.param.minFeePrice
|
|
|
|
|
2024-05-28 18:26:51 +00:00
|
|
|
func pMinTipPrice*(xp: TxPoolRef): GasPrice =
|
2022-01-18 14:40:02 +00:00
|
|
|
## Getter
|
|
|
|
xp.param.minTipPrice
|
|
|
|
|
2024-05-28 18:26:51 +00:00
|
|
|
func pMinPlGasPrice*(xp: TxPoolRef): GasPrice =
|
2022-01-18 14:40:02 +00:00
|
|
|
## Getter
|
|
|
|
xp.param.minPlGasPrice
|
|
|
|
|
2024-05-28 18:26:51 +00:00
|
|
|
func startDate*(xp: TxPoolRef): Time =
|
2022-01-18 14:40:02 +00:00
|
|
|
## Getter
|
|
|
|
xp.startDate
|
|
|
|
|
2024-05-28 18:26:51 +00:00
|
|
|
func txDB*(xp: TxPoolRef): TxTabsRef =
|
2022-01-18 14:40:02 +00:00
|
|
|
## Getter, pool database
|
|
|
|
xp.txDB
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions, setters
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-05-28 18:26:51 +00:00
|
|
|
func `pDirtyBuckets=`*(xp: TxPoolRef; val: bool) =
|
2022-01-18 14:40:02 +00:00
|
|
|
## Setter
|
|
|
|
xp.param.dirtyBuckets = val
|
|
|
|
|
2024-05-28 18:26:51 +00:00
|
|
|
func pDoubleCheckAdd*(xp: TxPoolRef; val: seq[TxItemRef]) =
|
2022-01-18 14:40:02 +00:00
|
|
|
## Pseudo setter
|
|
|
|
xp.param.doubleCheck.add val
|
|
|
|
|
2024-05-28 18:26:51 +00:00
|
|
|
func pDoubleCheckFlush*(xp: TxPoolRef) =
|
2022-01-18 14:40:02 +00:00
|
|
|
## Pseudo setter
|
|
|
|
xp.param.doubleCheck.setLen(0)
|
|
|
|
|
2024-05-28 18:26:51 +00:00
|
|
|
func `pFlags=`*(xp: TxPoolRef; val: set[TxPoolFlags]) =
|
2022-01-18 14:40:02 +00:00
|
|
|
## Install a set of algorithm strategy symbols for labelling items as`packed`
|
|
|
|
xp.param.flags = val
|
|
|
|
|
2024-05-28 18:26:51 +00:00
|
|
|
func `pMinFeePrice=`*(xp: TxPoolRef; val: GasPrice) =
|
2022-01-18 14:40:02 +00:00
|
|
|
## Setter
|
|
|
|
xp.param.minFeePrice = val
|
|
|
|
|
2024-05-28 18:26:51 +00:00
|
|
|
func `pMinTipPrice=`*(xp: TxPoolRef; val: GasPrice) =
|
2022-01-18 14:40:02 +00:00
|
|
|
## Setter
|
|
|
|
xp.param.minTipPrice = val
|
|
|
|
|
2024-05-28 18:26:51 +00:00
|
|
|
func `pMinPlGasPrice=`*(xp: TxPoolRef; val: GasPrice) =
|
2022-01-18 14:40:02 +00:00
|
|
|
## Setter
|
|
|
|
xp.param.minPlGasPrice = val
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions, heplers (debugging only)
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc verify*(xp: TxPoolRef): Result[void,TxInfo]
|
2023-01-30 22:10:23 +00:00
|
|
|
{.gcsafe, raises: [CatchableError].} =
|
2022-01-18 14:40:02 +00:00
|
|
|
## Verify descriptor and subsequent data structures.
|
|
|
|
|
|
|
|
block:
|
|
|
|
let rc = xp.txDB.verify
|
|
|
|
if rc.isErr:
|
|
|
|
return rc
|
|
|
|
|
|
|
|
# verify consecutive nonces per sender
|
|
|
|
var
|
|
|
|
initOk = false
|
|
|
|
lastSender: EthAddress
|
|
|
|
lastNonce: AccountNonce
|
|
|
|
lastSublist: TxSenderSchedRef
|
|
|
|
|
|
|
|
for (_,nonceList) in xp.txDB.incAccount:
|
|
|
|
for item in nonceList.incNonce:
|
|
|
|
if not initOk or lastSender != item.sender:
|
|
|
|
initOk = true
|
|
|
|
lastSender = item.sender
|
|
|
|
lastNonce = item.tx.nonce
|
|
|
|
lastSublist = xp.txDB.bySender.eq(item.sender).value.data
|
|
|
|
elif lastNonce + 1 == item.tx.nonce:
|
|
|
|
lastNonce = item.tx.nonce
|
|
|
|
else:
|
|
|
|
return err(txInfoVfyNonceChain)
|
|
|
|
|
|
|
|
# verify bucket boundary conditions
|
|
|
|
case item.status:
|
|
|
|
of txItemPending:
|
|
|
|
discard
|
|
|
|
of txItemStaged:
|
|
|
|
if lastSublist.eq(txItemPending).eq(item.tx.nonce - 1).isOk:
|
|
|
|
return err(txInfoVfyNonceChain)
|
|
|
|
of txItemPacked:
|
|
|
|
if lastSublist.eq(txItemPending).eq(item.tx.nonce - 1).isOk:
|
|
|
|
return err(txInfoVfyNonceChain)
|
|
|
|
if lastSublist.eq(txItemStaged).eq(item.tx.nonce - 1).isOk:
|
|
|
|
return err(txInfoVfyNonceChain)
|
|
|
|
|
|
|
|
ok()
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|