reth_optimism_node/
utils.rs

1use crate::{OpBuiltPayload, OpNode as OtherOpNode, OpPayloadBuilderAttributes};
2use alloy_genesis::Genesis;
3use alloy_primitives::{Address, B256};
4use alloy_rpc_types_engine::PayloadAttributes;
5use reth_e2e_test_utils::{
6    transaction::TransactionTestContext, wallet::Wallet, NodeHelperType, TmpDB,
7};
8use reth_node_api::NodeTypesWithDBAdapter;
9use reth_optimism_chainspec::OpChainSpecBuilder;
10use reth_payload_builder::EthPayloadBuilderAttributes;
11use reth_provider::providers::BlockchainProvider;
12use reth_tasks::TaskManager;
13use std::sync::Arc;
14use tokio::sync::Mutex;
15
16/// Optimism Node Helper type
17pub(crate) type OpNode =
18    NodeHelperType<OtherOpNode, BlockchainProvider<NodeTypesWithDBAdapter<OtherOpNode, TmpDB>>>;
19
20/// Creates the initial setup with `num_nodes` of the node config, started and connected.
21pub async fn setup(num_nodes: usize) -> eyre::Result<(Vec<OpNode>, TaskManager, Wallet)> {
22    let genesis: Genesis =
23        serde_json::from_str(include_str!("../tests/assets/genesis.json")).unwrap();
24    reth_e2e_test_utils::setup_engine(
25        num_nodes,
26        Arc::new(OpChainSpecBuilder::base_mainnet().genesis(genesis).ecotone_activated().build()),
27        false,
28        optimism_payload_attributes,
29    )
30    .await
31}
32
33/// Advance the chain with sequential payloads returning them in the end.
34pub async fn advance_chain(
35    length: usize,
36    node: &mut OpNode,
37    wallet: Arc<Mutex<Wallet>>,
38) -> eyre::Result<Vec<OpBuiltPayload>> {
39    node.advance(length as u64, |_| {
40        let wallet = wallet.clone();
41        Box::pin(async move {
42            let mut wallet = wallet.lock().await;
43            let tx_fut = TransactionTestContext::optimism_l1_block_info_tx(
44                wallet.chain_id,
45                wallet.inner.clone(),
46                wallet.inner_nonce,
47            );
48            wallet.inner_nonce += 1;
49            tx_fut.await
50        })
51    })
52    .await
53}
54
55/// Helper function to create a new eth payload attributes
56pub fn optimism_payload_attributes<T>(timestamp: u64) -> OpPayloadBuilderAttributes<T> {
57    let attributes = PayloadAttributes {
58        timestamp,
59        prev_randao: B256::ZERO,
60        suggested_fee_recipient: Address::ZERO,
61        withdrawals: Some(vec![]),
62        parent_beacon_block_root: Some(B256::ZERO),
63    };
64
65    OpPayloadBuilderAttributes {
66        payload_attributes: EthPayloadBuilderAttributes::new(B256::ZERO, attributes),
67        transactions: vec![],
68        no_tx_pool: false,
69        gas_limit: Some(30_000_000),
70        eip_1559_params: None,
71    }
72}