2023-08-27 01:23:45 +00:00
|
|
|
# Nimbus
|
2024-02-20 03:07:38 +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
|
|
|
|
./web3_eth_conv,
|
2023-12-08 09:35:50 +00:00
|
|
|
web3/execution_types,
|
2023-08-27 01:23:45 +00:00
|
|
|
../utils/utils,
|
|
|
|
eth/common
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private helpers
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func wdRoot(list: openArray[WithdrawalV1]): common.Hash256
|
2023-10-05 03:04:12 +00:00
|
|
|
{.gcsafe, raises:[].} =
|
2024-02-20 03:07:38 +00:00
|
|
|
{.noSideEffect.}:
|
2023-08-27 01:23:45 +00:00
|
|
|
calcWithdrawalsRoot(ethWithdrawals list)
|
|
|
|
|
|
|
|
func wdRoot(x: Option[seq[WithdrawalV1]]): Option[common.Hash256]
|
2023-10-05 03:04:12 +00:00
|
|
|
{.gcsafe, raises:[].} =
|
2024-02-20 03:07:38 +00:00
|
|
|
{.noSideEffect.}:
|
2023-08-27 01:23:45 +00:00
|
|
|
if x.isNone: none(common.Hash256)
|
|
|
|
else: some(wdRoot x.get)
|
|
|
|
|
2024-04-21 14:44:05 +00:00
|
|
|
func txRoot(list: openArray[Web3Tx], removeBlobs: bool): common.Hash256
|
2023-10-05 03:04:12 +00:00
|
|
|
{.gcsafe, raises:[RlpError].} =
|
2024-02-20 03:07:38 +00:00
|
|
|
{.noSideEffect.}:
|
2024-04-21 14:44:05 +00:00
|
|
|
calcTxRoot(ethTxs(list, removeBlobs))
|
2023-08-27 01:23:45 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
{.push gcsafe, raises:[].}
|
|
|
|
|
|
|
|
func executionPayload*(blk: EthBlock): ExecutionPayload =
|
|
|
|
ExecutionPayload(
|
|
|
|
parentHash : w3Hash blk.header.parentHash,
|
|
|
|
feeRecipient : w3Addr blk.header.coinbase,
|
|
|
|
stateRoot : w3Hash blk.header.stateRoot,
|
|
|
|
receiptsRoot : w3Hash blk.header.receiptRoot,
|
|
|
|
logsBloom : w3Bloom blk.header.bloom,
|
|
|
|
prevRandao : w3PrevRandao blk.header.prevRandao,
|
|
|
|
blockNumber : w3Qty blk.header.blockNumber,
|
|
|
|
gasLimit : w3Qty blk.header.gasLimit,
|
|
|
|
gasUsed : w3Qty blk.header.gasUsed,
|
|
|
|
timestamp : w3Qty blk.header.timestamp,
|
|
|
|
extraData : w3ExtraData blk.header.extraData,
|
|
|
|
baseFeePerGas: blk.header.fee.get(0.u256),
|
|
|
|
blockHash : w3Hash blk.header,
|
|
|
|
transactions : w3Txs blk.txs,
|
|
|
|
withdrawals : w3Withdrawals blk.withdrawals,
|
|
|
|
blobGasUsed : w3Qty blk.header.blobGasUsed,
|
|
|
|
excessBlobGas: w3Qty blk.header.excessBlobGas
|
|
|
|
)
|
|
|
|
|
|
|
|
func executionPayloadV1V2*(blk: EthBlock): ExecutionPayloadV1OrV2 =
|
|
|
|
ExecutionPayloadV1OrV2(
|
|
|
|
parentHash : w3Hash blk.header.parentHash,
|
|
|
|
feeRecipient : w3Addr blk.header.coinbase,
|
|
|
|
stateRoot : w3Hash blk.header.stateRoot,
|
|
|
|
receiptsRoot : w3Hash blk.header.receiptRoot,
|
|
|
|
logsBloom : w3Bloom blk.header.bloom,
|
|
|
|
prevRandao : w3PrevRandao blk.header.prevRandao,
|
|
|
|
blockNumber : w3Qty blk.header.blockNumber,
|
|
|
|
gasLimit : w3Qty blk.header.gasLimit,
|
|
|
|
gasUsed : w3Qty blk.header.gasUsed,
|
|
|
|
timestamp : w3Qty blk.header.timestamp,
|
|
|
|
extraData : w3ExtraData blk.header.extraData,
|
|
|
|
baseFeePerGas: blk.header.fee.get(0.u256),
|
|
|
|
blockHash : w3Hash blk.header,
|
|
|
|
transactions : w3Txs blk.txs,
|
|
|
|
withdrawals : w3Withdrawals blk.withdrawals,
|
|
|
|
)
|
|
|
|
|
|
|
|
func blockHeader*(p: ExecutionPayload,
|
2024-04-21 14:44:05 +00:00
|
|
|
removeBlobs: bool,
|
|
|
|
beaconRoot: Option[common.Hash256]):
|
2023-09-18 20:20:28 +00:00
|
|
|
common.BlockHeader {.gcsafe, raises:[CatchableError].} =
|
2023-08-27 01:23:45 +00:00
|
|
|
common.BlockHeader(
|
|
|
|
parentHash : ethHash p.parentHash,
|
|
|
|
ommersHash : EMPTY_UNCLE_HASH,
|
|
|
|
coinbase : ethAddr p.feeRecipient,
|
|
|
|
stateRoot : ethHash p.stateRoot,
|
2024-04-21 14:44:05 +00:00
|
|
|
txRoot : txRoot(p.transactions, removeBlobs),
|
2023-08-27 01:23:45 +00:00
|
|
|
receiptRoot : ethHash p.receiptsRoot,
|
|
|
|
bloom : ethBloom p.logsBloom,
|
|
|
|
difficulty : 0.u256,
|
|
|
|
blockNumber : u256 p.blockNumber,
|
|
|
|
gasLimit : ethGasInt p.gasLimit,
|
|
|
|
gasUsed : ethGasInt p.gasUsed,
|
|
|
|
timestamp : ethTime p.timestamp,
|
|
|
|
extraData : ethBlob p.extraData,
|
|
|
|
mixDigest : ethHash p.prevRandao,
|
|
|
|
nonce : default(BlockNonce),
|
|
|
|
fee : some(p.baseFeePerGas),
|
|
|
|
withdrawalsRoot: wdRoot p.withdrawals,
|
|
|
|
blobGasUsed : u64(p.blobGasUsed),
|
|
|
|
excessBlobGas : u64(p.excessBlobGas),
|
|
|
|
parentBeaconBlockRoot: beaconRoot
|
|
|
|
)
|
|
|
|
|
2023-10-20 14:02:22 +00:00
|
|
|
func blockBody*(p: ExecutionPayload, removeBlobs: bool):
|
2023-08-27 01:23:45 +00:00
|
|
|
common.BlockBody {.gcsafe, raises:[RlpError].} =
|
|
|
|
common.BlockBody(
|
|
|
|
uncles : @[],
|
2023-10-20 14:02:22 +00:00
|
|
|
transactions: ethTxs(p.transactions, removeBlobs),
|
2023-08-27 01:23:45 +00:00
|
|
|
withdrawals : ethWithdrawals p.withdrawals,
|
|
|
|
)
|
|
|
|
|
|
|
|
func ethBlock*(p: ExecutionPayload,
|
2023-10-20 14:02:22 +00:00
|
|
|
removeBlobs: bool,
|
2023-08-27 01:23:45 +00:00
|
|
|
beaconRoot: Option[common.Hash256]):
|
2023-09-18 20:20:28 +00:00
|
|
|
common.EthBlock {.gcsafe, raises:[CatchableError].} =
|
2023-08-27 01:23:45 +00:00
|
|
|
common.Ethblock(
|
2024-04-21 14:44:05 +00:00
|
|
|
header : blockHeader(p, removeBlobs, beaconRoot),
|
2023-08-27 01:23:45 +00:00
|
|
|
uncles : @[],
|
2023-10-20 14:02:22 +00:00
|
|
|
txs : ethTxs(p.transactions, removeBlobs),
|
2023-08-27 01:23:45 +00:00
|
|
|
withdrawals: ethWithdrawals p.withdrawals,
|
|
|
|
)
|