Engine-api: fix getPayloadBodiesByHashV1 and getPayloadBodiesByRangeV1 bugs

This commit is contained in:
jangko 2023-08-17 11:33:35 +07:00
parent 53d7165f6d
commit 7c2e1ad3f3
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
1 changed files with 43 additions and 42 deletions

View File

@ -479,33 +479,51 @@ func toHash(value: array[32, byte]): Hash256 =
const
maxBodyRequest = 32
proc handle_getPayloadBodiesByHash(sealingEngine: SealingEngineRef,
proc getPayloadBodyByHeader(db: CoreDbRef,
header: BlockHeader,
output: var seq[Option[ExecutionPayloadBodyV1]]) {.raises: [CatchableError].} =
var body: BlockBody
if not db.getBlockBody(header, body):
output.add none(ExecutionPayloadBodyV1)
return
var typedTransactions: seq[TypedTransaction]
for tx in body.transactions:
typedTransactions.add(tx.toTypedTransaction)
var withdrawals: seq[WithdrawalV1]
if body.withdrawals.isSome:
for w in body.withdrawals.get:
withdrawals.add(w.toWithdrawalV1)
output.add(
some(ExecutionPayloadBodyV1(
transactions: typedTransactions,
# pre Shanghai block return null withdrawals
# post Shanghai block return at least empty slice
withdrawals: if header.withdrawalsRoot.isSome:
some(withdrawals)
else:
none(seq[WithdrawalV1])
))
)
proc handle_getPayloadBodiesByHash(com: CommonRef,
hashes: seq[BlockHash]):
seq[Option[ExecutionPayloadBodyV1]] {.raises: [CatchableError].} =
if hashes.len > maxBodyRequest:
raise tooLargeRequest("request exceeds max allowed " & $maxBodyRequest)
let db = sealingEngine.chain.db
var body: BlockBody
let db = com.db
var header: BlockHeader
for h in hashes:
if db.getBlockBody(toHash(distinctBase(h)), body):
var typedTransactions: seq[TypedTransaction]
for tx in body.transactions:
typedTransactions.add(tx.toTypedTransaction)
var withdrawals: seq[WithdrawalV1]
if body.withdrawals.isSome:
for w in body.withdrawals.get:
withdrawals.add(w.toWithdrawalV1)
result.add(
some(ExecutionPayloadBodyV1(
transactions: typedTransactions,
withdrawals: withdrawals
))
)
else:
result.add(none[ExecutionPayloadBodyV1]())
if not db.getBlockHeader(toHash(distinctBase(h)), header):
result.add none(ExecutionPayloadBodyV1)
continue
db.getPayloadBodyByHeader(header, result)
proc handle_getPayloadBodiesByRange(sealingEngine: SealingEngineRef,
proc handle_getPayloadBodiesByRange(com: CommonRef,
start: uint64, count: uint64):
seq[Option[ExecutionPayloadBodyV1]] {.raises: [CatchableError].} =
if start == 0:
@ -517,30 +535,13 @@ proc handle_getPayloadBodiesByRange(sealingEngine: SealingEngineRef,
if count > maxBodyRequest:
raise tooLargeRequest("request exceeds max allowed " & $maxBodyRequest)
let db = sealingEngine.chain.db
var body: BlockBody
let db = com.db
var header: BlockHeader
for bn in start..<start+count:
if not db.getBlockHeader(bn.toBlockNumber, header):
result.add(none[ExecutionPayloadBodyV1]())
result.add none(ExecutionPayloadBodyV1)
continue
if db.getBlockBody(header, body):
var typedTransactions: seq[TypedTransaction]
for tx in body.transactions:
typedTransactions.add(tx.toTypedTransaction)
var withdrawals: seq[WithdrawalV1]
if body.withdrawals.isSome:
for w in body.withdrawals.get:
withdrawals.add(w.toWithdrawalV1)
result.add(
some(ExecutionPayloadBodyV1(
transactions: typedTransactions,
withdrawals: withdrawals
))
)
else:
result.add(none[ExecutionPayloadBodyV1]())
db.getPayloadBodyByHeader(header, result)
const supportedMethods: HashSet[string] =
toHashSet([
@ -651,8 +652,8 @@ proc setupEngineAPI*(
server.rpc("engine_getPayloadBodiesByHashV1") do(
hashes: seq[BlockHash]) -> seq[Option[ExecutionPayloadBodyV1]]:
return handle_getPayloadBodiesByHash(sealingEngine, hashes)
return handle_getPayloadBodiesByHash(com, hashes)
server.rpc("engine_getPayloadBodiesByRangeV1") do(
start: Quantity, count: Quantity) -> seq[Option[ExecutionPayloadBodyV1]]:
return handle_getPayloadBodiesByRange(sealingEngine, start.uint64, count.uint64)
return handle_getPayloadBodiesByRange(com, start.uint64, count.uint64)