Skip to content

Commit

Permalink
Rewire blockValue from Txpool to EngineAPI (#2554)
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko committed Aug 8, 2024
1 parent 3dc3019 commit b8e1282
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 14 deletions.
2 changes: 1 addition & 1 deletion nimbus/beacon/api_handler/api_forkchoice.nim
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ proc forkchoiceUpdated*(ben: BeaconEngineRef,
raise invalidAttr(error)

let id = computePayloadId(blockHash, attrs)
ben.put(id, ben.blockValue, bundle.executionPayload, bundle.blobsBundle)
ben.put(id, bundle.blockValue, bundle.executionPayload, bundle.blobsBundle)

info "Created payload for sealing",
id = id.toHex,
Expand Down
9 changes: 3 additions & 6 deletions nimbus/beacon/beacon_engine.nim
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,6 @@ func posFinalized*(ben: BeaconEngineRef): bool =
## PoSFinalized reports whether the chain has entered the PoS stage.
ben.merge.posFinalized

func blockValue*(ben: BeaconEngineRef): UInt256 =
## return sum of reward for feeRecipient for each
## tx included in a block
ben.txPool.blockValue

proc get*(ben: BeaconEngineRef, hash: common.Hash256,
header: var common.BlockHeader): bool =
ben.queue.get(hash, header)
Expand Down Expand Up @@ -208,6 +203,7 @@ proc get*(ben: BeaconEngineRef, id: PayloadID,
type ExecutionPayloadAndBlobsBundle* = object
executionPayload*: ExecutionPayload
blobsBundle*: Opt[BlobsBundleV1]
blockValue*: UInt256

proc generatePayload*(ben: BeaconEngineRef,
attrs: PayloadAttributes):
Expand Down Expand Up @@ -253,7 +249,8 @@ proc generatePayload*(ben: BeaconEngineRef,

ok ExecutionPayloadAndBlobsBundle(
executionPayload: executionPayload(bundle.blk),
blobsBundle: blobsBundle)
blobsBundle: blobsBundle,
blockValue: bundle.blockValue)

proc setInvalidAncestor*(ben: BeaconEngineRef, header: common.BlockHeader, blockHash: common.Hash256) =
ben.invalidBlocksHits[blockHash] = 1
Expand Down
4 changes: 3 additions & 1 deletion nimbus/core/tx_pool.nim
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ func com*(xp: TxPoolRef): CommonRef =
type EthBlockAndBlobsBundle* = object
blk*: EthBlock
blobsBundle*: Opt[BlobsBundle]
blockValue*: UInt256

proc assembleBlock*(
xp: TxPoolRef,
Expand Down Expand Up @@ -529,7 +530,8 @@ proc assembleBlock*(

ok EthBlockAndBlobsBundle(
blk: blk,
blobsBundle: blobsBundleOpt)
blobsBundle: blobsBundleOpt,
blockValue: pst.blockValue)

# core/tx_pool.go(474): func (pool SetGasPrice,*TxPool) Stats() (int, int) {
# core/tx_pool.go(1728): func (t *txLookup) Count() int {
Expand Down
1 change: 0 additions & 1 deletion nimbus/core/tx_pool/tx_desc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ type

lifeTime*: times.Duration ## Maximum life time of a tx in the system
priceBump*: uint ## Min precentage price when superseding
blockValue*: UInt256 ## Sum of reward received by feeRecipient

const
txItemLifeTime = ##\
Expand Down
6 changes: 3 additions & 3 deletions nimbus/core/tx_pool/tx_packer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,6 @@ proc vmExecInit(xp: TxPoolRef): Result[TxPacker, string]
# Flush `packed` bucket
xp.bucketFlushPacked

# reset blockValue before adding any tx
xp.blockValue = 0.u256

let packer = TxPacker(
vmState: xp.vmState,
txDB: xp.txDB,
Expand Down Expand Up @@ -341,6 +338,9 @@ proc assembleHeader*(pst: TxPacker): BlockHeader =
result.blobGasUsed = Opt.some vmState.blobGasUsed
result.excessBlobGas = Opt.some vmState.blockCtx.excessBlobGas

func blockValue*(pst: TxPacker): UInt256 =
pst.blockValue

# ------------------------------------------------------------------------------
# End
# ------------------------------------------------------------------------------
15 changes: 13 additions & 2 deletions tests/test_txpool2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import
../nimbus/core/chain,
../nimbus/[config, transaction, constants],
../nimbus/core/tx_pool,
../nimbus/core/tx_pool/tx_desc,
../nimbus/core/casper,
../nimbus/common/common,
../nimbus/utils/utils,
../nimbus/evm/types,
./test_txpool/helpers,
./macro_assembler

Expand All @@ -38,7 +40,7 @@ type
xp : TxPoolRef

const
signerKeyHex = "9c647b8b7c4e7c3490668fb6c11473619db80c93704c70893d3813af4090c39c"
# signerKeyHex = "9c647b8b7c4e7c3490668fb6c11473619db80c93704c70893d3813af4090c39c"
vaultKeyHex = "63b508a03c3b5937ceb903af8b1b0c191012ef6eb7e9c3fb7afa94e5d214d376"
recipient = hexToByteArray[20]("0000000000000000000000000000000000000318")
feeRecipient = hexToByteArray[20]("0000000000000000000000000000000000000212")
Expand Down Expand Up @@ -210,7 +212,8 @@ proc runTxPoolBlobhashTest() =
check false
return

blk = r.get.blk
let bundle = r.get
blk = bundle.blk
check com.isBlockAfterTtd(blk.header)

body = BlockBody(
Expand All @@ -220,6 +223,14 @@ proc runTxPoolBlobhashTest() =
)
check blk.txs.len == 2

let
gasUsed1 = xp.vmState.receipts[0].cumulativeGasUsed
gasUsed2 = xp.vmState.receipts[1].cumulativeGasUsed - gasUsed1
blockValue = gasUsed1.u256 * tx1.effectiveGasTip(blk.header.baseFeePerGas).u256 +
gasUsed2.u256 * tx2.effectiveGasTip(blk.header.baseFeePerGas).u256

check blockValue == bundle.blockValue

test "Blobhash persistBlocks":
let rr = chain.persistBlocks([EthBlock.init(blk.header, body)])
check rr.isOk()
Expand Down

0 comments on commit b8e1282

Please sign in to comment.