2023-08-27 01:23:45 +00:00
|
|
|
# Nimbus
|
2024-06-14 07:31:08 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-08-27 01:23:45 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
|
|
|
|
|
|
|
import
|
|
|
|
std/[options, typetraits],
|
|
|
|
eth/common,
|
|
|
|
../web3_eth_conv,
|
|
|
|
../beacon_engine,
|
2023-12-08 09:35:50 +00:00
|
|
|
web3/execution_types,
|
2023-08-27 01:23:45 +00:00
|
|
|
../../db/core_db,
|
|
|
|
./api_utils
|
|
|
|
|
|
|
|
{.push gcsafe, raises:[CatchableError].}
|
|
|
|
|
|
|
|
const
|
|
|
|
maxBodyRequest = 32
|
|
|
|
|
|
|
|
proc getPayloadBodyByHeader(db: CoreDbRef,
|
|
|
|
header: common.BlockHeader,
|
2024-06-14 07:31:08 +00:00
|
|
|
output: var seq[Opt[ExecutionPayloadBodyV1]]) =
|
2023-08-27 01:23:45 +00:00
|
|
|
|
|
|
|
var body: common.BlockBody
|
|
|
|
if not db.getBlockBody(header, body):
|
2024-06-14 07:31:08 +00:00
|
|
|
output.add Opt.none(ExecutionPayloadBodyV1)
|
2023-08-27 01:23:45 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
let txs = w3Txs body.transactions
|
|
|
|
var wds: seq[WithdrawalV1]
|
|
|
|
if body.withdrawals.isSome:
|
|
|
|
for w in body.withdrawals.get:
|
|
|
|
wds.add w3Withdrawal(w)
|
|
|
|
|
|
|
|
output.add(
|
2024-06-14 07:31:08 +00:00
|
|
|
Opt.some(ExecutionPayloadBodyV1(
|
2023-08-27 01:23:45 +00:00
|
|
|
transactions: txs,
|
|
|
|
# pre Shanghai block return null withdrawals
|
|
|
|
# post Shanghai block return at least empty slice
|
|
|
|
withdrawals: if header.withdrawalsRoot.isSome:
|
2024-06-14 07:31:08 +00:00
|
|
|
Opt.some(wds)
|
2023-08-27 01:23:45 +00:00
|
|
|
else:
|
2024-06-14 07:31:08 +00:00
|
|
|
Opt.none(seq[WithdrawalV1])
|
2023-08-27 01:23:45 +00:00
|
|
|
))
|
|
|
|
)
|
|
|
|
|
|
|
|
proc getPayloadBodiesByHash*(ben: BeaconEngineRef,
|
|
|
|
hashes: seq[Web3Hash]):
|
2024-06-14 07:31:08 +00:00
|
|
|
seq[Opt[ExecutionPayloadBodyV1]] =
|
2023-08-27 01:23:45 +00:00
|
|
|
if hashes.len > maxBodyRequest:
|
|
|
|
raise tooLargeRequest("request exceeds max allowed " & $maxBodyRequest)
|
|
|
|
|
|
|
|
let db = ben.com.db
|
|
|
|
var header: common.BlockHeader
|
|
|
|
for h in hashes:
|
|
|
|
if not db.getBlockHeader(ethHash h, header):
|
2024-06-14 07:31:08 +00:00
|
|
|
result.add Opt.none(ExecutionPayloadBodyV1)
|
2023-08-27 01:23:45 +00:00
|
|
|
continue
|
|
|
|
db.getPayloadBodyByHeader(header, result)
|
|
|
|
|
|
|
|
proc getPayloadBodiesByRange*(ben: BeaconEngineRef,
|
|
|
|
start: uint64, count: uint64):
|
2024-06-14 07:31:08 +00:00
|
|
|
seq[Opt[ExecutionPayloadBodyV1]] =
|
2023-08-27 01:23:45 +00:00
|
|
|
if start == 0:
|
|
|
|
raise invalidParams("start block should greater than zero")
|
|
|
|
|
|
|
|
if count == 0:
|
|
|
|
raise invalidParams("blocks count should greater than zero")
|
|
|
|
|
|
|
|
if count > maxBodyRequest:
|
|
|
|
raise tooLargeRequest("request exceeds max allowed " & $maxBodyRequest)
|
|
|
|
|
|
|
|
let
|
|
|
|
com = ben.com
|
|
|
|
db = com.db
|
2024-06-14 07:31:08 +00:00
|
|
|
current = com.syncCurrent
|
2023-08-27 01:23:45 +00:00
|
|
|
|
|
|
|
var
|
|
|
|
header: common.BlockHeader
|
|
|
|
last = start+count-1
|
|
|
|
|
|
|
|
if last > current:
|
|
|
|
last = current
|
|
|
|
|
|
|
|
for bn in start..last:
|
2024-06-14 07:31:08 +00:00
|
|
|
if not db.getBlockHeader(bn, header):
|
|
|
|
result.add Opt.none(ExecutionPayloadBodyV1)
|
2023-08-27 01:23:45 +00:00
|
|
|
continue
|
|
|
|
db.getPayloadBodyByHeader(header, result)
|