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        Default::default(),
29        optimism_payload_attributes,
30    )
31    .await
32}
33
34/// Advance the chain with sequential payloads returning them in the end.
35pub async fn advance_chain(
36    length: usize,
37    node: &mut OpNode,
38    wallet: Arc<Mutex<Wallet>>,
39) -> eyre::Result<Vec<OpBuiltPayload>> {
40    node.advance(length as u64, |_| {
41        let wallet = wallet.clone();
42        Box::pin(async move {
43            let mut wallet = wallet.lock().await;
44            let tx_fut = TransactionTestContext::optimism_l1_block_info_tx(
45                wallet.chain_id,
46                wallet.inner.clone(),
47                wallet.inner_nonce,
48            );
49            wallet.inner_nonce += 1;
50            tx_fut.await
51        })
52    })
53    .await
54}
55
56/// Helper function to create a new eth payload attributes
57pub fn optimism_payload_attributes<T>(timestamp: u64) -> OpPayloadBuilderAttributes<T> {
58    let attributes = PayloadAttributes {
59        timestamp,
60        prev_randao: B256::ZERO,
61        suggested_fee_recipient: Address::ZERO,
62        withdrawals: Some(vec![]),
63        parent_beacon_block_root: Some(B256::ZERO),
64    };
65
66    OpPayloadBuilderAttributes {
67        payload_attributes: EthPayloadBuilderAttributes::new(B256::ZERO, attributes),
68        transactions: vec![],
69        no_tx_pool: false,
70        gas_limit: Some(30_000_000),
71        eip_1559_params: None,
72    }
73}