-
Notifications
You must be signed in to change notification settings - Fork 39
chore(op-alloy): Update to use op-alloy flashblock types #328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,20 +16,24 @@ use alloy_consensus::{ | |
| BlockBody, EMPTY_OMMER_ROOT_HASH, Header, constants::EMPTY_WITHDRAWALS, proofs, | ||
| }; | ||
| use alloy_eips::{Encodable2718, eip7685::EMPTY_REQUESTS_HASH, merge::BEACON_NONCE}; | ||
| use alloy_primitives::{Address, B256, U256, map::foldhash::HashMap}; | ||
| use alloy_primitives::{Address, B256, U256}; | ||
| use core::time::Duration; | ||
| use either::Either; | ||
| use eyre::WrapErr as _; | ||
| use op_alloy_rpc_types_engine::{ | ||
| OpFlashblockPayload, OpFlashblockPayloadBase, OpFlashblockPayloadDelta, | ||
| OpFlashblockPayloadMetadata, | ||
| }; | ||
| use reth::payload::PayloadBuilderAttributes; | ||
| use reth_basic_payload_builder::BuildOutcome; | ||
| use reth_chainspec::EthChainSpec; | ||
| use reth_evm::{ConfigureEvm, execute::BlockBuilder}; | ||
| use reth_node_api::{Block, NodePrimitives, PayloadBuilderError}; | ||
| use reth_node_api::{Block, PayloadBuilderError}; | ||
| use reth_optimism_consensus::{calculate_receipt_root_no_memo_optimism, isthmus}; | ||
| use reth_optimism_evm::{OpEvmConfig, OpNextBlockEnvAttributes}; | ||
| use reth_optimism_forks::OpHardforks; | ||
| use reth_optimism_node::{OpBuiltPayload, OpPayloadBuilderAttributes}; | ||
| use reth_optimism_primitives::{OpPrimitives, OpReceipt, OpTransactionSigned}; | ||
| use reth_optimism_primitives::{OpReceipt, OpTransactionSigned}; | ||
| use reth_payload_primitives::BuiltPayloadExecutedBlock; | ||
| use reth_payload_util::BestPayloadTransactions; | ||
| use reth_primitives_traits::RecoveredBlock; | ||
|
|
@@ -43,11 +47,8 @@ use reth_revm::{ | |
| use reth_transaction_pool::TransactionPool; | ||
| use reth_trie::{HashedPostState, updates::TrieUpdates}; | ||
| use revm::Database; | ||
| use rollup_boost::{ | ||
| ExecutionPayloadBaseV1, ExecutionPayloadFlashblockDeltaV1, FlashblocksPayloadV1, | ||
| }; | ||
| use serde::{Deserialize, Serialize}; | ||
| use std::{ | ||
| collections::BTreeMap, | ||
| ops::{Div, Rem}, | ||
| sync::Arc, | ||
| time::Instant, | ||
|
|
@@ -56,6 +57,24 @@ use tokio::sync::mpsc; | |
| use tokio_util::sync::CancellationToken; | ||
| use tracing::{debug, error, info, metadata::Level, span, warn}; | ||
|
|
||
| /// Converts a reth OpReceipt to an op-alloy OpReceipt | ||
| /// TODO: remove this once reth updates to use the op-alloy defined type as well. | ||
| fn convert_receipt(receipt: &OpReceipt) -> op_alloy_consensus::OpReceipt { | ||
| match receipt { | ||
| OpReceipt::Legacy(r) => op_alloy_consensus::OpReceipt::Legacy(r.clone()), | ||
| OpReceipt::Eip2930(r) => op_alloy_consensus::OpReceipt::Eip2930(r.clone()), | ||
| OpReceipt::Eip1559(r) => op_alloy_consensus::OpReceipt::Eip1559(r.clone()), | ||
| OpReceipt::Eip7702(r) => op_alloy_consensus::OpReceipt::Eip7702(r.clone()), | ||
| OpReceipt::Deposit(r) => { | ||
| op_alloy_consensus::OpReceipt::Deposit(op_alloy_consensus::OpDepositReceipt { | ||
| inner: r.inner.clone(), | ||
| deposit_nonce: r.deposit_nonce, | ||
| deposit_receipt_version: r.deposit_receipt_version, | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| type NextBestFlashblocksTxs<Pool> = BestFlashblocksTxs< | ||
| <Pool as TransactionPool>::Transaction, | ||
| Box< | ||
|
|
@@ -916,13 +935,6 @@ where | |
| } | ||
| } | ||
|
|
||
| #[derive(Debug, Serialize, Deserialize)] | ||
| struct FlashblocksMetadata { | ||
| receipts: HashMap<B256, <OpPrimitives as NodePrimitives>::Receipt>, | ||
| new_account_balances: HashMap<Address, U256>, | ||
| block_number: u64, | ||
| } | ||
|
|
||
| fn execute_pre_steps<DB, ExtraCtx>( | ||
| state: &mut State<DB>, | ||
| ctx: &OpPayloadBuilderCtx<ExtraCtx>, | ||
|
|
@@ -948,7 +960,7 @@ pub(super) fn build_block<DB, P, ExtraCtx>( | |
| ctx: &OpPayloadBuilderCtx<ExtraCtx>, | ||
| info: &mut ExecutionInfo<FlashblocksExecutionInfo>, | ||
| calculate_state_root: bool, | ||
| ) -> Result<(OpBuiltPayload, FlashblocksPayloadV1), PayloadBuilderError> | ||
| ) -> Result<(OpBuiltPayload, OpFlashblockPayload), PayloadBuilderError> | ||
| where | ||
| DB: Database<Error = ProviderError> + AsRef<P>, | ||
| P: StateRootProvider + HashedPostStateProvider + StorageRootProvider, | ||
|
|
@@ -1115,16 +1127,16 @@ where | |
| let receipts_with_hash = new_transactions | ||
| .iter() | ||
| .zip(new_receipts.iter()) | ||
| .map(|(tx, receipt)| (tx.tx_hash(), receipt.clone())) | ||
| .collect::<HashMap<B256, OpReceipt>>(); | ||
| .map(|(tx, receipt)| (tx.tx_hash(), convert_receipt(receipt))) | ||
| .collect::<BTreeMap<B256, op_alloy_consensus::OpReceipt>>(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why would we use BTreeMap with B256?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To help with cases where you need ordered traversal of receipts, perf wise should be ok since this list is not super big |
||
| let new_account_balances = state | ||
| .bundle_state | ||
| .state | ||
| .iter() | ||
| .filter_map(|(address, account)| account.info.as_ref().map(|info| (*address, info.balance))) | ||
| .collect::<HashMap<Address, U256>>(); | ||
| .collect::<BTreeMap<Address, U256>>(); | ||
|
Comment on lines
+1130
to
+1137
|
||
|
|
||
| let metadata: FlashblocksMetadata = FlashblocksMetadata { | ||
| let metadata = OpFlashblockPayloadMetadata { | ||
| receipts: receipts_with_hash, | ||
| new_account_balances, | ||
| block_number: ctx.parent().number + 1, | ||
|
|
@@ -1133,10 +1145,10 @@ where | |
| let (_, blob_gas_used) = ctx.blob_fields(info); | ||
|
|
||
| // Prepare the flashblocks message | ||
| let fb_payload = FlashblocksPayloadV1 { | ||
| let fb_payload = OpFlashblockPayload { | ||
| payload_id: ctx.payload_id(), | ||
| index: 0, | ||
| base: Some(ExecutionPayloadBaseV1 { | ||
| base: Some(OpFlashblockPayloadBase { | ||
| parent_beacon_block_root: ctx | ||
| .attributes() | ||
| .payload_attributes | ||
|
|
@@ -1149,9 +1161,9 @@ where | |
| gas_limit: ctx.block_gas_limit(), | ||
| timestamp: ctx.attributes().payload_attributes.timestamp, | ||
| extra_data: ctx.extra_data()?, | ||
| base_fee_per_gas: ctx.base_fee().try_into().unwrap(), | ||
| base_fee_per_gas: U256::from(ctx.base_fee()), | ||
| }), | ||
| diff: ExecutionPayloadFlashblockDeltaV1 { | ||
| diff: OpFlashblockPayloadDelta { | ||
| state_root, | ||
| receipts_root, | ||
| logs_bloom, | ||
|
|
@@ -1162,7 +1174,7 @@ where | |
| withdrawals_root: withdrawals_root.unwrap_or_default(), | ||
| blob_gas_used, | ||
| }, | ||
| metadata: serde_json::to_value(&metadata).unwrap_or_default(), | ||
| metadata, | ||
| }; | ||
|
|
||
| // We clean bundle and place initial state transaction back | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
convert_receiptfunction performs a deep clone of each receipt variant. This could impact performance in scenarios with many transactions. Consider whether a reference-based approach or shared ownership (Arc) could be used instead, especially since receipts are being collected into a BTreeMap at line 1131.