Fix engine api: getPayload V2 and V3 now returns correct blockValue

This commit is contained in:
jangko 2023-08-18 14:25:11 +07:00
parent 467e6fffa6
commit 4fd53156f5
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
5 changed files with 43 additions and 31 deletions

View File

@ -232,6 +232,11 @@ proc generateExecutionPayload*(engine: SealingEngineRef,
excessBlobGas: excessBlobGas excessBlobGas: excessBlobGas
)) ))
proc blockValue*(engine: SealingEngineRef): UInt256 =
# return sum of reward for feeRecipient for each
# tx included in a block
engine.txPool.blockValue
proc new*(_: type SealingEngineRef, proc new*(_: type SealingEngineRef,
chain: ChainRef, chain: ChainRef,
ctx: EthContext, ctx: EthContext,

View File

@ -97,6 +97,7 @@ type
lifeTime*: times.Duration ## Maximum life time of a tx in the system lifeTime*: times.Duration ## Maximum life time of a tx in the system
priceBump*: uint ## Min precentage price when superseding priceBump*: uint ## Min precentage price when superseding
blockValue*: UInt256 ## Sum of reward received by feeRecipient
param: TxPoolParam ## Getter/Setter parameters param: TxPoolParam ## Getter/Setter parameters

View File

@ -108,6 +108,7 @@ proc runTxCommit(pst: TxPackerStateRef; item: TxItemRef; gasBurned: GasInt)
assert 0 <= gasTip assert 0 <= gasTip
let reward = gasBurned.u256 * gasTip.uint64.u256 let reward = gasBurned.u256 * gasTip.uint64.u256
vmState.stateDB.addBalance(xp.chain.feeRecipient, reward) vmState.stateDB.addBalance(xp.chain.feeRecipient, reward)
xp.blockValue += reward
if vmState.generateWitness: if vmState.generateWitness:
vmState.stateDB.collectWitnessData() vmState.stateDB.collectWitnessData()
@ -153,6 +154,9 @@ proc vmExecInit(xp: TxPoolRef): TxPackerStateRef
# Flush `packed` bucket # Flush `packed` bucket
xp.bucketFlushPacked xp.bucketFlushPacked
# reset blockValue before adding any tx
xp.blockValue = 0.u256
xp.chain.maxMode = (packItemsMaxGasLimit in xp.pFlags) xp.chain.maxMode = (packItemsMaxGasLimit in xp.pFlags)
if xp.chain.com.daoForkSupport and if xp.chain.com.daoForkSupport and

View File

@ -58,17 +58,6 @@ proc invalidFCU(com: CommonRef, header: EthBlockHeader): ForkchoiceUpdatedRespon
let blockHash = latestValidHash(com.db, parent, com.ttd.get(high(common.BlockNumber))) let blockHash = latestValidHash(com.db, parent, com.ttd.get(high(common.BlockNumber)))
invalidFCU(blockHash) invalidFCU(blockHash)
proc txPriorityFee(ttx: TypedTransaction): UInt256 =
try:
let tx = rlp.decode(distinctBase(ttx), Transaction)
return u256(tx.gasPrice * tx.maxPriorityFee)
except RlpError:
doAssert(false, "found TypedTransaction that RLP failed to decode")
# AARDVARK: make sure I have the right units (wei/gwei)
proc sumOfBlockPriorityFees(payload: ExecutionPayloadV1OrV2): UInt256 =
payload.transactions.foldl(a + txPriorityFee(b), UInt256.zero)
template unsafeQuantityToInt64(q: Quantity): int64 = template unsafeQuantityToInt64(q: Quantity): int64 =
int64 q int64 q
@ -201,11 +190,10 @@ proc handle_getPayload(api: EngineApiRef, payloadId: PayloadID): GetPayloadV2Res
meth = "GetPayload", id = payloadId.toHex meth = "GetPayload", id = payloadId.toHex
var payload: ExecutionPayloadV1OrV2 var payload: ExecutionPayloadV1OrV2
if not api.get(payloadId, payload): var blockValue: UInt256
if not api.get(payloadId, blockValue, payload):
raise unknownPayload("Unknown payload") raise unknownPayload("Unknown payload")
let blockValue = sumOfBlockPriorityFees(payload)
return GetPayloadV2Response( return GetPayloadV2Response(
executionPayload: payload, executionPayload: payload,
blockValue: blockValue blockValue: blockValue
@ -216,20 +204,19 @@ proc handle_getPayloadV3(api: EngineApiRef, com: CommonRef, payloadId: PayloadID
meth = "GetPayload", id = payloadId.toHex meth = "GetPayload", id = payloadId.toHex
var payload: ExecutionPayloadV3 var payload: ExecutionPayloadV3
if not api.get(payloadId, payload): var blockValue: UInt256
if not api.get(payloadId, blockValue, payload):
raise unknownPayload("Unknown payload") raise unknownPayload("Unknown payload")
if not com.isCancunOrLater(fromUnix(payload.timestamp.unsafeQuantityToInt64)): if not com.isCancunOrLater(fromUnix(payload.timestamp.unsafeQuantityToInt64)):
raise unsupportedFork("payload timestamp is less than Cancun activation") raise unsupportedFork("payload timestamp is less than Cancun activation")
var var
blockValue: UInt256
blobsBundle: BlobsBundleV1 blobsBundle: BlobsBundleV1
try: try:
for ttx in payload.transactions: for ttx in payload.transactions:
let tx = rlp.decode(distinctBase(ttx), Transaction) let tx = rlp.decode(distinctBase(ttx), Transaction)
blockValue += u256(tx.gasPrice * tx.maxPriorityFee)
if tx.networkPayload.isNil.not: if tx.networkPayload.isNil.not:
for blob in tx.networkPayload.blobs: for blob in tx.networkPayload.blobs:
blobsBundle.blobs.add Web3Blob(blob) blobsBundle.blobs.add Web3Blob(blob)
@ -462,7 +449,7 @@ proc handle_forkchoiceUpdated(sealingEngine: SealingEngineRef,
let payload = res.get let payload = res.get
let id = computePayloadId(blockHash, payloadAttrs) let id = computePayloadId(blockHash, payloadAttrs)
api.put(id, payload) api.put(id, sealingEngine.blockValue, payload)
info "Created payload for sealing", info "Created payload for sealing",
id = id.toHex, id = id.toHex,

View File

@ -43,6 +43,7 @@ type
PayloadItem = object PayloadItem = object
id: PayloadID id: PayloadID
payload: ExecutionPayload payload: ExecutionPayload
blockValue: UInt256
HeaderItem = object HeaderItem = object
hash: Hash256 hash: Hash256
@ -83,43 +84,57 @@ proc get*(api: EngineApiRef, hash: Hash256, header: var EthBlockHeader): bool =
return true return true
false false
proc put*(api: EngineApiRef, id: PayloadID, payload: ExecutionPayload) = proc put*(api: EngineApiRef, id: PayloadID,
api.payloadQueue.put(PayloadItem(id: id, payload: payload)) blockValue: UInt256, payload: ExecutionPayload) =
api.payloadQueue.put(PayloadItem(id: id,
payload: payload, blockValue: blockValue))
proc put*(api: EngineApiRef, id: PayloadID, payload: SomeExecutionPayload) = proc put*(api: EngineApiRef, id: PayloadID,
api.put(id, payload.executionPayload) blockValue: UInt256, payload: SomeExecutionPayload) =
api.put(id, blockValue, payload.executionPayload)
proc get*(api: EngineApiRef, id: PayloadID, payload: var ExecutionPayload): bool = proc get*(api: EngineApiRef, id: PayloadID,
blockValue: var UInt256,
payload: var ExecutionPayload): bool =
for x in api.payloadQueue: for x in api.payloadQueue:
if x.id == id: if x.id == id:
payload = x.payload payload = x.payload
blockValue = x.blockValue
return true return true
false false
proc get*(api: EngineApiRef, id: PayloadID, payload: var ExecutionPayloadV1): bool = proc get*(api: EngineApiRef, id: PayloadID,
blockValue: var UInt256,
payload: var ExecutionPayloadV1): bool =
var p: ExecutionPayload var p: ExecutionPayload
let found = api.get(id, p) let found = api.get(id, blockValue, p)
doAssert(p.version == Version.V1) doAssert(p.version == Version.V1)
payload = p.V1 payload = p.V1
return found return found
proc get*(api: EngineApiRef, id: PayloadID, payload: var ExecutionPayloadV2): bool = proc get*(api: EngineApiRef, id: PayloadID,
blockValue: var UInt256,
payload: var ExecutionPayloadV2): bool =
var p: ExecutionPayload var p: ExecutionPayload
let found = api.get(id, p) let found = api.get(id, blockValue, p)
doAssert(p.version == Version.V2) doAssert(p.version == Version.V2)
payload = p.V2 payload = p.V2
return found return found
proc get*(api: EngineApiRef, id: PayloadID, payload: var ExecutionPayloadV3): bool = proc get*(api: EngineApiRef, id: PayloadID,
blockValue: var UInt256,
payload: var ExecutionPayloadV3): bool =
var p: ExecutionPayload var p: ExecutionPayload
let found = api.get(id, p) let found = api.get(id, blockValue, p)
doAssert(p.version == Version.V3) doAssert(p.version == Version.V3)
payload = p.V3 payload = p.V3
return found return found
proc get*(api: EngineApiRef, id: PayloadID, payload: var ExecutionPayloadV1OrV2): bool = proc get*(api: EngineApiRef, id: PayloadID,
blockValue: var UInt256,
payload: var ExecutionPayloadV1OrV2): bool =
var p: ExecutionPayload var p: ExecutionPayload
let found = api.get(id, p) let found = api.get(id, blockValue, p)
doAssert(p.version in {Version.V1, Version.V2}) doAssert(p.version in {Version.V1, Version.V2})
payload = p.V1V2 payload = p.V1V2
return found return found