2024-08-07 15:35:17 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
|
|
|
# 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 Tasklets: Packer, VM execute and compact txs
|
|
|
|
## =============================================================
|
|
|
|
##
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
|
|
|
stew/sorted_set,
|
2024-11-06 02:01:25 +00:00
|
|
|
stew/byteutils,
|
2024-10-18 23:39:33 +00:00
|
|
|
../../db/ledger,
|
2024-08-07 15:35:17 +00:00
|
|
|
../../common/common,
|
|
|
|
../../utils/utils,
|
|
|
|
../../constants,
|
|
|
|
".."/[executor, validate, casper],
|
|
|
|
../../transaction/call_evm,
|
|
|
|
../../transaction,
|
|
|
|
../../evm/state,
|
|
|
|
../../evm/types,
|
|
|
|
../eip4844,
|
2024-10-26 11:10:54 +00:00
|
|
|
../eip6110,
|
2024-08-07 15:35:17 +00:00
|
|
|
"."/[tx_desc, tx_item, tx_tabs, tx_tabs/tx_status, tx_info],
|
|
|
|
tx_tasks/[tx_bucket]
|
|
|
|
|
|
|
|
type
|
|
|
|
TxPacker = object
|
|
|
|
# Packer state
|
|
|
|
vmState: BaseVMState
|
|
|
|
txDB: TxTabsRef
|
|
|
|
cleanState: bool
|
|
|
|
numBlobPerBlock: int
|
|
|
|
|
|
|
|
# Packer results
|
|
|
|
blockValue: UInt256
|
2024-10-16 01:34:12 +00:00
|
|
|
stateRoot: Hash32
|
|
|
|
receiptsRoot: Hash32
|
|
|
|
logsBloom: Bloom
|
2024-10-26 11:10:54 +00:00
|
|
|
withdrawalReqs: seq[byte]
|
|
|
|
consolidationReqs: seq[byte]
|
|
|
|
depositReqs: seq[byte]
|
2024-08-07 15:35:17 +00:00
|
|
|
|
|
|
|
GrabResult = enum
|
|
|
|
FetchNextItem
|
|
|
|
ContinueWithNextAccount
|
|
|
|
StopCollecting
|
|
|
|
|
|
|
|
const
|
|
|
|
receiptsExtensionSize = ##\
|
|
|
|
## Number of slots to extend the `receipts[]` at the same time.
|
|
|
|
20
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc persist(pst: var TxPacker)
|
|
|
|
{.gcsafe,raises: [].} =
|
|
|
|
## Smart wrapper
|
|
|
|
let vmState = pst.vmState
|
|
|
|
if not pst.cleanState:
|
|
|
|
let clearEmptyAccount = vmState.fork >= FkSpurious
|
|
|
|
vmState.stateDB.persist(clearEmptyAccount)
|
|
|
|
pst.cleanState = true
|
|
|
|
|
|
|
|
proc classifyValidatePacked(vmState: BaseVMState; item: TxItemRef): bool =
|
|
|
|
## Verify the argument `item` against the accounts database. This function
|
|
|
|
## is a wrapper around the `verifyTransaction()` call to be used in a similar
|
|
|
|
## fashion as in `asyncProcessTransactionImpl()`.
|
|
|
|
let
|
|
|
|
roDB = vmState.readOnlyStateDB
|
|
|
|
baseFee = vmState.blockCtx.baseFeePerGas.get(0.u256)
|
|
|
|
fork = vmState.fork
|
|
|
|
gasLimit = vmState.blockCtx.gasLimit
|
|
|
|
tx = item.tx.eip1559TxNormalization(baseFee.truncate(GasInt))
|
|
|
|
excessBlobGas = calcExcessBlobGas(vmState.parent)
|
|
|
|
|
|
|
|
roDB.validateTransaction(
|
|
|
|
tx, item.sender, gasLimit, baseFee, excessBlobGas, fork).isOk
|
|
|
|
|
|
|
|
proc classifyPacked(vmState: BaseVMState; moreBurned: GasInt): bool =
|
|
|
|
## Classifier for *packing* (i.e. adding up `gasUsed` values after executing
|
|
|
|
## in the VM.) This function checks whether the sum of the arguments
|
|
|
|
## `gasBurned` and `moreGasBurned` is within acceptable constraints.
|
|
|
|
let totalGasUsed = vmState.cumulativeGasUsed + moreBurned
|
|
|
|
totalGasUsed < vmState.blockCtx.gasLimit
|
|
|
|
|
|
|
|
proc classifyPackedNext(vmState: BaseVMState): bool =
|
|
|
|
## Classifier for *packing* (i.e. adding up `gasUsed` values after executing
|
|
|
|
## in the VM.) This function returns `true` if the packing level is still
|
|
|
|
## low enough to proceed trying to accumulate more items.
|
|
|
|
##
|
|
|
|
## This function is typically called as a follow up after a `false` return of
|
|
|
|
## `classifyPack()`.
|
|
|
|
vmState.cumulativeGasUsed < vmState.blockCtx.gasLimit
|
|
|
|
|
|
|
|
func baseFee(pst: TxPacker): GasInt =
|
|
|
|
## Getter, baseFee for the next bock header. This value is auto-generated
|
|
|
|
## when a new insertion point is set via `head=`.
|
|
|
|
if pst.vmState.blockCtx.baseFeePerGas.isSome:
|
|
|
|
pst.vmState.blockCtx.baseFeePerGas.get.truncate(GasInt)
|
|
|
|
else:
|
|
|
|
0.GasInt
|
|
|
|
|
2024-10-16 06:51:38 +00:00
|
|
|
func feeRecipient(pst: TxPacker): Address =
|
2024-08-07 15:35:17 +00:00
|
|
|
pst.vmState.com.pos.feeRecipient
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc runTx(pst: var TxPacker; item: TxItemRef): GasInt =
|
|
|
|
## Execute item transaction and update `vmState` book keeping. Returns the
|
|
|
|
## `gasUsed` after executing the transaction.
|
|
|
|
let
|
|
|
|
baseFee = pst.baseFee
|
|
|
|
|
2024-08-19 07:42:07 +00:00
|
|
|
let gasUsed = item.tx.txCallEvm(item.sender, pst.vmState, baseFee)
|
2024-08-07 15:35:17 +00:00
|
|
|
pst.cleanState = false
|
|
|
|
doAssert 0 <= gasUsed
|
|
|
|
gasUsed
|
|
|
|
|
|
|
|
proc runTxCommit(pst: var TxPacker; item: TxItemRef; gasBurned: GasInt)
|
|
|
|
{.gcsafe,raises: [CatchableError].} =
|
|
|
|
## Book keeping after executing argument `item` transaction in the VM. The
|
|
|
|
## function returns the next number of items `nItems+1`.
|
|
|
|
let
|
|
|
|
vmState = pst.vmState
|
|
|
|
inx = pst.txDB.byStatus.eq(txItemPacked).nItems
|
|
|
|
gasTip = item.tx.effectiveGasTip(pst.baseFee)
|
|
|
|
|
|
|
|
# The gas tip cannot get negative as all items in the `staged` bucket
|
|
|
|
# are vetted for profitability before entering that bucket.
|
|
|
|
assert 0 <= gasTip
|
|
|
|
let reward = gasBurned.u256 * gasTip.u256
|
|
|
|
vmState.stateDB.addBalance(pst.feeRecipient, reward)
|
|
|
|
pst.blockValue += reward
|
|
|
|
|
|
|
|
# Save accounts via persist() is not needed unless the fork is smaller
|
2024-10-27 18:56:28 +00:00
|
|
|
# than `FkByzantium` in which case, the `getStateRoot()` function is called
|
|
|
|
# by `makeReceipt()`. As the `getStateRoot()` function asserts unconditionally
|
2024-08-07 15:35:17 +00:00
|
|
|
# that the account cache has been saved, the `persist()` call is
|
|
|
|
# obligatory here.
|
|
|
|
if vmState.fork < FkByzantium:
|
|
|
|
pst.persist()
|
|
|
|
|
|
|
|
# Update receipts sequence
|
|
|
|
if vmState.receipts.len <= inx:
|
|
|
|
vmState.receipts.setLen(inx + receiptsExtensionSize)
|
|
|
|
|
|
|
|
# Return remaining gas to the block gas counter so it is
|
|
|
|
# available for the next transaction.
|
|
|
|
vmState.gasPool += item.tx.gasLimit - gasBurned
|
|
|
|
|
|
|
|
# gasUsed accounting
|
|
|
|
vmState.cumulativeGasUsed += gasBurned
|
|
|
|
vmState.receipts[inx] = vmState.makeReceipt(item.tx.txType)
|
|
|
|
|
|
|
|
# Add the item to the `packed` bucket. This implicitely increases the
|
|
|
|
# receipts index `inx` at the next visit of this function.
|
|
|
|
discard pst.txDB.reassign(item,txItemPacked)
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions: packer packerVmExec() helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc vmExecInit(xp: TxPoolRef): Result[TxPacker, string]
|
|
|
|
{.gcsafe,raises: [CatchableError].} =
|
|
|
|
|
|
|
|
# Flush `packed` bucket
|
|
|
|
xp.bucketFlushPacked
|
|
|
|
|
|
|
|
let packer = TxPacker(
|
|
|
|
vmState: xp.vmState,
|
|
|
|
txDB: xp.txDB,
|
|
|
|
numBlobPerBlock: 0,
|
|
|
|
blockValue: 0.u256,
|
|
|
|
stateRoot: xp.vmState.parent.stateRoot,
|
|
|
|
)
|
|
|
|
|
|
|
|
# EIP-4788
|
|
|
|
if xp.nextFork >= FkCancun:
|
|
|
|
let beaconRoot = xp.vmState.com.pos.parentBeaconBlockRoot
|
|
|
|
xp.vmState.processBeaconBlockRoot(beaconRoot).isOkOr:
|
|
|
|
return err(error)
|
|
|
|
|
2024-10-26 11:10:54 +00:00
|
|
|
# EIP-2935
|
|
|
|
if xp.nextFork >= FkPrague:
|
|
|
|
xp.vmState.processParentBlockHash(xp.vmState.blockCtx.parentHash).isOkOr:
|
|
|
|
return err(error)
|
|
|
|
|
2024-08-07 15:35:17 +00:00
|
|
|
ok(packer)
|
|
|
|
|
|
|
|
proc vmExecGrabItem(pst: var TxPacker; item: TxItemRef): GrabResult
|
|
|
|
{.gcsafe,raises: [CatchableError].} =
|
|
|
|
## Greedily collect & compact items as long as the accumulated `gasLimit`
|
|
|
|
## values are below the maximum block size.
|
|
|
|
let vmState = pst.vmState
|
|
|
|
|
|
|
|
if not item.tx.validateChainId(vmState.com.chainId):
|
|
|
|
discard pst.txDB.dispose(item, txInfoChainIdMismatch)
|
|
|
|
return ContinueWithNextAccount
|
|
|
|
|
|
|
|
# EIP-4844
|
|
|
|
if pst.numBlobPerBlock + item.tx.versionedHashes.len > MAX_BLOBS_PER_BLOCK:
|
|
|
|
return ContinueWithNextAccount
|
|
|
|
pst.numBlobPerBlock += item.tx.versionedHashes.len
|
2024-10-26 11:10:54 +00:00
|
|
|
|
2024-10-22 02:07:43 +00:00
|
|
|
let blobGasUsed = item.tx.getTotalBlobGas
|
|
|
|
if vmState.blobGasUsed + blobGasUsed > MAX_BLOB_GAS_PER_BLOCK:
|
|
|
|
return ContinueWithNextAccount
|
|
|
|
vmState.blobGasUsed += blobGasUsed
|
2024-08-07 15:35:17 +00:00
|
|
|
|
|
|
|
# Verify we have enough gas in gasPool
|
|
|
|
if vmState.gasPool < item.tx.gasLimit:
|
|
|
|
# skip this transaction and
|
|
|
|
# continue with next account
|
|
|
|
# if we don't have enough gas
|
|
|
|
return ContinueWithNextAccount
|
|
|
|
vmState.gasPool -= item.tx.gasLimit
|
|
|
|
|
|
|
|
# Validate transaction relative to the current vmState
|
|
|
|
if not vmState.classifyValidatePacked(item):
|
|
|
|
return ContinueWithNextAccount
|
|
|
|
|
|
|
|
# EIP-1153
|
|
|
|
vmState.stateDB.clearTransientStorage()
|
|
|
|
|
|
|
|
let
|
|
|
|
accTx = vmState.stateDB.beginSavepoint
|
|
|
|
gasUsed = pst.runTx(item) # this is the crucial part, running the tx
|
|
|
|
|
|
|
|
# Find out what to do next: accepting this tx or trying the next account
|
|
|
|
if not vmState.classifyPacked(gasUsed):
|
|
|
|
vmState.stateDB.rollback(accTx)
|
|
|
|
if vmState.classifyPackedNext():
|
|
|
|
return ContinueWithNextAccount
|
|
|
|
return StopCollecting
|
|
|
|
|
|
|
|
# Commit account state DB
|
|
|
|
vmState.stateDB.commit(accTx)
|
|
|
|
|
|
|
|
vmState.stateDB.persist(clearEmptyAccount = vmState.fork >= FkSpurious)
|
|
|
|
|
|
|
|
# Finish book-keeping and move item to `packed` bucket
|
|
|
|
pst.runTxCommit(item, gasUsed)
|
|
|
|
|
|
|
|
FetchNextItem
|
|
|
|
|
2024-10-26 11:10:54 +00:00
|
|
|
proc vmExecCommit(pst: var TxPacker): Result[void, string] =
|
2024-08-07 15:35:17 +00:00
|
|
|
let
|
|
|
|
vmState = pst.vmState
|
|
|
|
stateDB = vmState.stateDB
|
|
|
|
|
|
|
|
# EIP-4895
|
|
|
|
if vmState.fork >= FkShanghai:
|
|
|
|
for withdrawal in vmState.com.pos.withdrawals:
|
|
|
|
stateDB.addBalance(withdrawal.address, withdrawal.weiAmount)
|
|
|
|
|
2024-10-26 11:10:54 +00:00
|
|
|
# EIP-6110, EIP-7002, EIP-7251
|
|
|
|
if vmState.fork >= FkPrague:
|
|
|
|
pst.withdrawalReqs = processDequeueWithdrawalRequests(vmState)
|
|
|
|
pst.consolidationReqs = processDequeueConsolidationRequests(vmState)
|
2024-11-08 03:47:07 +00:00
|
|
|
pst.depositReqs = ?parseDepositLogs(vmState.allLogs, vmState.com.depositContractAddress)
|
2024-10-26 11:10:54 +00:00
|
|
|
|
2024-10-27 18:56:28 +00:00
|
|
|
# Finish up, then vmState.stateDB.stateRoot may be accessed
|
2024-08-07 15:35:17 +00:00
|
|
|
stateDB.persist(clearEmptyAccount = vmState.fork >= FkSpurious)
|
|
|
|
|
|
|
|
# Update flexi-array, set proper length
|
|
|
|
let nItems = pst.txDB.byStatus.eq(txItemPacked).nItems
|
|
|
|
vmState.receipts.setLen(nItems)
|
|
|
|
|
|
|
|
pst.receiptsRoot = vmState.receipts.calcReceiptsRoot
|
|
|
|
pst.logsBloom = vmState.receipts.createBloom
|
2024-10-27 18:56:28 +00:00
|
|
|
pst.stateRoot = vmState.stateDB.getStateRoot()
|
2024-10-26 11:10:54 +00:00
|
|
|
ok()
|
2024-08-07 15:35:17 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc packerVmExec*(xp: TxPoolRef): Result[TxPacker, string]
|
|
|
|
{.gcsafe,raises: [CatchableError].} =
|
|
|
|
## Rebuild `packed` bucket by selection items from the `staged` bucket
|
|
|
|
## after executing them in the VM.
|
|
|
|
let db = xp.vmState.com.db
|
2024-12-18 16:03:51 +00:00
|
|
|
let dbTx = db.ctx.txFrameBegin()
|
2024-08-07 15:35:17 +00:00
|
|
|
defer: dbTx.dispose()
|
|
|
|
|
|
|
|
var pst = xp.vmExecInit.valueOr:
|
|
|
|
return err(error)
|
|
|
|
|
|
|
|
block loop:
|
|
|
|
for (_,nonceList) in xp.txDB.packingOrderAccounts(txItemStaged):
|
|
|
|
|
|
|
|
block account:
|
|
|
|
for item in nonceList.incNonce:
|
|
|
|
let rc = pst.vmExecGrabItem(item)
|
|
|
|
if rc == StopCollecting:
|
|
|
|
break loop # stop
|
|
|
|
if rc == ContinueWithNextAccount:
|
|
|
|
break account # continue with next account
|
|
|
|
|
2024-10-26 11:10:54 +00:00
|
|
|
?pst.vmExecCommit()
|
2024-08-07 15:35:17 +00:00
|
|
|
ok(pst)
|
|
|
|
# Block chain will roll back automatically
|
|
|
|
|
2024-11-06 02:01:25 +00:00
|
|
|
func getExtraData(com: CommonRef): seq[byte] =
|
|
|
|
if com.extraData.len > 32:
|
|
|
|
com.extraData.toBytes[0..<32]
|
|
|
|
else:
|
|
|
|
com.extraData.toBytes
|
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc assembleHeader*(pst: TxPacker): Header =
|
2024-08-07 15:35:17 +00:00
|
|
|
## Generate a new header, a child of the cached `head`
|
|
|
|
let
|
|
|
|
vmState = pst.vmState
|
|
|
|
com = vmState.com
|
|
|
|
pos = com.pos
|
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
result = Header(
|
2024-10-26 11:10:54 +00:00
|
|
|
parentHash: vmState.blockCtx.parentHash,
|
2024-08-07 15:35:17 +00:00
|
|
|
ommersHash: EMPTY_UNCLE_HASH,
|
|
|
|
coinbase: pos.feeRecipient,
|
|
|
|
stateRoot: pst.stateRoot,
|
|
|
|
receiptsRoot: pst.receiptsRoot,
|
|
|
|
logsBloom: pst.logsBloom,
|
|
|
|
difficulty: UInt256.zero(),
|
|
|
|
number: vmState.blockNumber,
|
|
|
|
gasLimit: vmState.blockCtx.gasLimit,
|
|
|
|
gasUsed: vmState.cumulativeGasUsed,
|
|
|
|
timestamp: pos.timestamp,
|
2024-11-06 02:01:25 +00:00
|
|
|
extraData: getExtraData(com),
|
2024-08-07 15:35:17 +00:00
|
|
|
mixHash: pos.prevRandao,
|
2024-10-16 01:34:12 +00:00
|
|
|
nonce: default(Bytes8),
|
2024-08-07 15:35:17 +00:00
|
|
|
baseFeePerGas: vmState.blockCtx.baseFeePerGas,
|
|
|
|
)
|
|
|
|
|
|
|
|
if com.isShanghaiOrLater(pos.timestamp):
|
|
|
|
result.withdrawalsRoot = Opt.some(calcWithdrawalsRoot(pos.withdrawals))
|
|
|
|
|
|
|
|
if com.isCancunOrLater(pos.timestamp):
|
|
|
|
result.parentBeaconBlockRoot = Opt.some(pos.parentBeaconBlockRoot)
|
|
|
|
result.blobGasUsed = Opt.some vmState.blobGasUsed
|
|
|
|
result.excessBlobGas = Opt.some vmState.blockCtx.excessBlobGas
|
|
|
|
|
2024-10-26 11:10:54 +00:00
|
|
|
if com.isPragueOrLater(pos.timestamp):
|
2024-10-29 05:01:59 +00:00
|
|
|
let requestsHash = calcRequestsHash(pst.depositReqs,
|
2024-10-26 11:10:54 +00:00
|
|
|
pst.withdrawalReqs, pst.consolidationReqs)
|
|
|
|
result.requestsHash = Opt.some(requestsHash)
|
|
|
|
|
2024-08-08 23:05:18 +00:00
|
|
|
func blockValue*(pst: TxPacker): UInt256 =
|
|
|
|
pst.blockValue
|
|
|
|
|
2024-10-29 05:01:59 +00:00
|
|
|
func executionRequests*(pst: var TxPacker): array[3, seq[byte]] =
|
|
|
|
result[0] = move(pst.depositReqs)
|
|
|
|
result[1] = move(pst.withdrawalReqs)
|
|
|
|
result[2] = move(pst.consolidationReqs)
|
2024-10-26 11:10:54 +00:00
|
|
|
|
2024-08-07 15:35:17 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|