2022-01-18 14:40:02 +00:00
|
|
|
# Nimbus
|
2024-02-20 10:07:38 +07: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.
|
|
|
|
|
2024-12-26 17:07:25 +07:00
|
|
|
{.push raises: [].}
|
2022-01-18 14:40:02 +00:00
|
|
|
|
|
|
|
import
|
2024-12-26 17:07:25 +07:00
|
|
|
eth/common/blocks,
|
2022-01-18 14:40:02 +00:00
|
|
|
./tx_pool/tx_tabs,
|
2024-12-26 17:07:25 +07:00
|
|
|
./tx_pool/tx_item,
|
|
|
|
./tx_pool/tx_desc,
|
|
|
|
./tx_pool/tx_packer,
|
2024-09-04 16:54:54 +07:00
|
|
|
./chain/forked_chain,
|
2023-10-19 07:50:07 +07:00
|
|
|
./casper
|
2022-01-18 14:40:02 +00:00
|
|
|
|
2024-12-26 17:07:25 +07:00
|
|
|
from eth/common/eth_types_rlp import rlpHash
|
2022-01-18 14:40:02 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2024-12-26 17:07:25 +07:00
|
|
|
# TxPoolRef public types
|
2022-01-18 14:40:02 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-12-26 17:07:25 +07:00
|
|
|
export
|
|
|
|
TxPoolRef,
|
|
|
|
TxError
|
2022-01-18 14:40:02 +00:00
|
|
|
|
2024-12-26 17:07:25 +07:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# TxItemRef public getters
|
|
|
|
# ------------------------------------------------------------------------------
|
2022-01-18 14:40:02 +00:00
|
|
|
|
2024-12-26 17:07:25 +07:00
|
|
|
export
|
|
|
|
tx, # : Transaction
|
|
|
|
pooledTx, # : PooledTransaction
|
|
|
|
id, # : Hash32
|
|
|
|
sender # : Address
|
2022-01-18 14:40:02 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2024-12-26 17:07:25 +07:00
|
|
|
# TxPoolRef constructor
|
2022-01-18 14:40:02 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-12-26 17:07:25 +07:00
|
|
|
proc new*(T: type TxPoolRef; chain: ForkedChainRef): T =
|
2024-05-28 18:26:51 +00:00
|
|
|
## Constructor, returns a new tx-pool descriptor.
|
2022-01-18 14:40:02 +00:00
|
|
|
new result
|
2024-12-13 13:21:20 +07:00
|
|
|
result.init(chain)
|
2022-01-18 14:40:02 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2024-12-26 17:07:25 +07:00
|
|
|
# TxPoolRef public getters
|
2022-01-18 14:40:02 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2024-12-26 17:07:25 +07:00
|
|
|
export
|
|
|
|
chain,
|
|
|
|
com,
|
|
|
|
len
|
2022-01-18 14:40:02 +00:00
|
|
|
|
2024-12-26 17:07:25 +07:00
|
|
|
# chain(xp: TxPoolRef): ForkedChainRef
|
|
|
|
# com(xp: TxPoolRef): CommonRef
|
|
|
|
# len(xp: TxPoolRef): int
|
2022-01-18 14:40:02 +00:00
|
|
|
|
2024-12-26 17:07:25 +07:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# TxPoolRef public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
2022-01-18 14:40:02 +00:00
|
|
|
|
2024-12-26 17:07:25 +07:00
|
|
|
export
|
|
|
|
addTx,
|
|
|
|
getItem,
|
|
|
|
removeTx,
|
|
|
|
removeExpiredTxs
|
2022-04-08 09:38:47 +01:00
|
|
|
|
2024-12-26 17:07:25 +07:00
|
|
|
# addTx(xp: TxPoolRef, ptx: PooledTransaction): Result[void, TxError]
|
|
|
|
# addTx(xp: TxPoolRef, tx: Transaction): Result[void, TxError]
|
|
|
|
# getItem(xp: TxPoolRef, id: Hash32): Result[TxItemRef, TxError]
|
|
|
|
# removeTx(xp: TxPoolRef, id: Hash32)
|
|
|
|
# removeExpiredTxs(xp: TxPoolRef, lifeTime: Duration)
|
2022-04-04 16:56:21 +01:00
|
|
|
|
2024-12-26 17:07:25 +07:00
|
|
|
proc removeNewBlockTxs*(xp: TxPoolRef, blk: Block, optHash = Opt.none(Hash32)) =
|
|
|
|
let fromHash = if optHash.isSome: optHash.get
|
|
|
|
else: blk.header.blockHash
|
2022-01-18 14:40:02 +00:00
|
|
|
|
2024-12-26 17:07:25 +07:00
|
|
|
# Up to date, no need for further actions
|
|
|
|
if fromHash == xp.rmHash:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Remove only the latest block transactions
|
|
|
|
if blk.header.parentHash == xp.rmHash:
|
|
|
|
for tx in blk.transactions:
|
|
|
|
let txHash = rlpHash(tx)
|
|
|
|
xp.removeTx(txHash)
|
|
|
|
|
|
|
|
xp.rmHash = fromHash
|
|
|
|
return
|
|
|
|
|
|
|
|
# Also remove transactions from older blocks
|
|
|
|
for txHash in xp.chain.txHashInRange(fromHash, xp.rmHash):
|
|
|
|
xp.removeTx(txHash)
|
2022-01-18 14:40:02 +00:00
|
|
|
|
2024-12-26 17:07:25 +07:00
|
|
|
xp.rmHash = fromHash
|
2022-01-18 14:40:02 +00:00
|
|
|
|
2024-10-26 18:10:54 +07:00
|
|
|
type AssembledBlock* = object
|
2024-05-15 06:07:59 +03:00
|
|
|
blk*: EthBlock
|
2024-06-14 14:31:08 +07:00
|
|
|
blobsBundle*: Opt[BlobsBundle]
|
2024-08-09 06:05:18 +07:00
|
|
|
blockValue*: UInt256
|
2024-12-21 12:59:30 +07:00
|
|
|
executionRequests*: Opt[seq[seq[byte]]]
|
2024-05-15 06:07:59 +03:00
|
|
|
|
|
|
|
proc assembleBlock*(
|
|
|
|
xp: TxPoolRef,
|
|
|
|
someBaseFee: bool = false
|
2024-12-26 17:07:25 +07:00
|
|
|
): Result[AssembledBlock, string] =
|
|
|
|
xp.updateVmState()
|
2022-12-02 11:35:41 +07:00
|
|
|
|
2024-12-26 17:07:25 +07:00
|
|
|
# Run EVM with most profitable transactions
|
|
|
|
var pst = xp.packerVmExec().valueOr:
|
2023-11-01 09:24:32 +07:00
|
|
|
return err(error)
|
|
|
|
|
|
|
|
var blk = EthBlock(
|
2024-12-26 17:07:25 +07:00
|
|
|
header: pst.assembleHeader
|
2023-11-01 09:24:32 +07:00
|
|
|
)
|
2024-05-15 06:07:59 +03:00
|
|
|
var blobsBundle: BlobsBundle
|
2024-12-26 17:07:25 +07:00
|
|
|
for item in pst.packedTxs:
|
|
|
|
let tx = item.pooledTx
|
|
|
|
blk.txs.add tx.tx
|
|
|
|
if tx.networkPayload != nil:
|
|
|
|
for k in tx.networkPayload.commitments:
|
|
|
|
blobsBundle.commitments.add k
|
|
|
|
for p in tx.networkPayload.proofs:
|
|
|
|
blobsBundle.proofs.add p
|
|
|
|
for blob in tx.networkPayload.blobs:
|
|
|
|
blobsBundle.blobs.add blob
|
2024-10-19 01:39:33 +02:00
|
|
|
blk.header.transactionsRoot = calcTxRoot(blk.txs)
|
2022-01-18 14:40:02 +00:00
|
|
|
|
2024-08-07 22:35:17 +07:00
|
|
|
let com = xp.vmState.com
|
2024-07-17 17:05:53 +07:00
|
|
|
if com.isShanghaiOrLater(blk.header.timestamp):
|
2024-06-14 14:31:08 +07:00
|
|
|
blk.withdrawals = Opt.some(com.pos.withdrawals)
|
2023-05-22 17:55:19 +07:00
|
|
|
|
2024-07-17 17:05:53 +07:00
|
|
|
if not com.isCancunOrLater(blk.header.timestamp) and blobsBundle.commitments.len > 0:
|
2024-05-15 06:07:59 +03:00
|
|
|
return err("PooledTransaction contains blobs prior to Cancun")
|
|
|
|
let blobsBundleOpt =
|
2024-07-17 17:05:53 +07:00
|
|
|
if com.isCancunOrLater(blk.header.timestamp):
|
2024-05-15 06:07:59 +03:00
|
|
|
doAssert blobsBundle.commitments.len == blobsBundle.blobs.len
|
|
|
|
doAssert blobsBundle.proofs.len == blobsBundle.blobs.len
|
2024-06-14 14:31:08 +07:00
|
|
|
Opt.some blobsBundle
|
2024-05-15 06:07:59 +03:00
|
|
|
else:
|
2024-06-14 14:31:08 +07:00
|
|
|
Opt.none BlobsBundle
|
2024-05-15 06:07:59 +03:00
|
|
|
|
2023-08-27 08:23:45 +07:00
|
|
|
if someBaseFee:
|
|
|
|
# make sure baseFee always has something
|
2024-06-14 14:31:08 +07:00
|
|
|
blk.header.baseFeePerGas = Opt.some(blk.header.baseFeePerGas.get(0.u256))
|
2023-11-01 09:24:32 +07:00
|
|
|
|
2024-10-26 18:10:54 +07:00
|
|
|
let executionRequestsOpt =
|
|
|
|
if com.isPragueOrLater(blk.header.timestamp):
|
|
|
|
Opt.some(pst.executionRequests)
|
|
|
|
else:
|
2024-12-21 12:59:30 +07:00
|
|
|
Opt.none(seq[seq[byte]])
|
2024-10-26 18:10:54 +07:00
|
|
|
|
|
|
|
ok AssembledBlock(
|
2024-05-15 06:07:59 +03:00
|
|
|
blk: blk,
|
2024-08-09 06:05:18 +07:00
|
|
|
blobsBundle: blobsBundleOpt,
|
2024-10-26 18:10:54 +07:00
|
|
|
blockValue: pst.blockValue,
|
|
|
|
executionRequests: executionRequestsOpt)
|