reth_e2e_test_utils/
node.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
use crate::{
    engine_api::EngineApiTestContext, network::NetworkTestContext, payload::PayloadTestContext,
    rpc::RpcTestContext, traits::PayloadEnvelopeExt,
};
use alloy_consensus::BlockHeader;
use alloy_eips::BlockId;
use alloy_primitives::{BlockHash, BlockNumber, Bytes, B256};
use alloy_rpc_types_engine::PayloadStatusEnum;
use alloy_rpc_types_eth::BlockNumberOrTag;
use eyre::Ok;
use futures_util::Future;
use reth_chainspec::EthereumHardforks;
use reth_network_api::test_utils::PeersHandleProvider;
use reth_node_api::{Block, EngineTypes, FullNodeComponents};
use reth_node_builder::{rpc::RethRpcAddOns, FullNode, NodeTypes, NodeTypesWithEngine};
use reth_payload_primitives::{BuiltPayload, PayloadBuilderAttributes};
use reth_primitives::EthPrimitives;
use reth_provider::{
    BlockReader, BlockReaderIdExt, CanonStateSubscriptions, StageCheckpointReader,
};
use reth_rpc_eth_api::helpers::{EthApiSpec, EthTransactions, TraceExt};
use reth_stages_types::StageId;
use std::{marker::PhantomData, pin::Pin};
use tokio_stream::StreamExt;
use url::Url;

/// An helper struct to handle node actions
#[allow(missing_debug_implementations)]
pub struct NodeTestContext<Node, AddOns>
where
    Node: FullNodeComponents,
    AddOns: RethRpcAddOns<Node>,
{
    /// The core structure representing the full node.
    pub inner: FullNode<Node, AddOns>,
    /// Context for testing payload-related features.
    pub payload: PayloadTestContext<<Node::Types as NodeTypesWithEngine>::Engine>,
    /// Context for testing network functionalities.
    pub network: NetworkTestContext<Node::Network>,
    /// Context for testing the Engine API.
    pub engine_api: EngineApiTestContext<
        <Node::Types as NodeTypesWithEngine>::Engine,
        <Node::Types as NodeTypes>::ChainSpec,
    >,
    /// Context for testing RPC features.
    pub rpc: RpcTestContext<Node, AddOns::EthApi>,
}

impl<Node, Engine, AddOns> NodeTestContext<Node, AddOns>
where
    Engine: EngineTypes,
    Node: FullNodeComponents,
    Node::Types: NodeTypesWithEngine<
        ChainSpec: EthereumHardforks,
        Engine = Engine,
        Primitives = EthPrimitives,
    >,
    Node::Network: PeersHandleProvider,
    AddOns: RethRpcAddOns<Node>,
{
    /// Creates a new test node
    pub async fn new(
        node: FullNode<Node, AddOns>,
        attributes_generator: impl Fn(u64) -> Engine::PayloadBuilderAttributes + 'static,
    ) -> eyre::Result<Self> {
        let builder = node.payload_builder.clone();

        Ok(Self {
            inner: node.clone(),
            payload: PayloadTestContext::new(builder, attributes_generator).await?,
            network: NetworkTestContext::new(node.network.clone()),
            engine_api: EngineApiTestContext {
                chain_spec: node.chain_spec(),
                engine_api_client: node.auth_server_handle().http_client(),
                canonical_stream: node.provider.canonical_state_stream(),
                _marker: PhantomData::<Engine>,
            },
            rpc: RpcTestContext { inner: node.add_ons_handle.rpc_registry },
        })
    }

    /// Establish a connection to the node
    pub async fn connect(&mut self, node: &mut Self) {
        self.network.add_peer(node.network.record()).await;
        node.network.next_session_established().await;
        self.network.next_session_established().await;
    }

    /// Advances the chain `length` blocks.
    ///
    /// Returns the added chain as a Vec of block hashes.
    pub async fn advance(
        &mut self,
        length: u64,
        tx_generator: impl Fn(u64) -> Pin<Box<dyn Future<Output = Bytes>>>,
    ) -> eyre::Result<Vec<(Engine::BuiltPayload, Engine::PayloadBuilderAttributes)>>
    where
        Engine::ExecutionPayloadEnvelopeV3: From<Engine::BuiltPayload> + PayloadEnvelopeExt,
        Engine::ExecutionPayloadEnvelopeV4: From<Engine::BuiltPayload> + PayloadEnvelopeExt,
        AddOns::EthApi: EthApiSpec<Provider: BlockReader<Block = reth_primitives::Block>>
            + EthTransactions
            + TraceExt,
    {
        let mut chain = Vec::with_capacity(length as usize);
        for i in 0..length {
            let raw_tx = tx_generator(i).await;
            let tx_hash = self.rpc.inject_tx(raw_tx).await?;
            let (payload, eth_attr) = self.advance_block().await?;
            let block_hash = payload.block().hash();
            let block_number = payload.block().number;
            self.assert_new_block(tx_hash, block_hash, block_number).await?;
            chain.push((payload, eth_attr));
        }
        Ok(chain)
    }

    /// Creates a new payload from given attributes generator
    /// expects a payload attribute event and waits until the payload is built.
    ///
    /// It triggers the resolve payload via engine api and expects the built payload event.
    pub async fn new_payload(
        &mut self,
    ) -> eyre::Result<(Engine::BuiltPayload, Engine::PayloadBuilderAttributes)>
    where
        <Engine as EngineTypes>::ExecutionPayloadEnvelopeV3:
            From<Engine::BuiltPayload> + PayloadEnvelopeExt,
    {
        // trigger new payload building draining the pool
        let eth_attr = self.payload.new_payload().await.unwrap();
        // first event is the payload attributes
        self.payload.expect_attr_event(eth_attr.clone()).await?;
        // wait for the payload builder to have finished building
        self.payload.wait_for_built_payload(eth_attr.payload_id()).await;
        // trigger resolve payload via engine api
        self.engine_api.get_payload_v3_value(eth_attr.payload_id()).await?;
        // ensure we're also receiving the built payload as event
        Ok((self.payload.expect_built_payload().await?, eth_attr))
    }

    /// Triggers payload building job and submits it to the engine.
    pub async fn build_and_submit_payload(
        &mut self,
    ) -> eyre::Result<(Engine::BuiltPayload, Engine::PayloadBuilderAttributes)>
    where
        <Engine as EngineTypes>::ExecutionPayloadEnvelopeV3:
            From<Engine::BuiltPayload> + PayloadEnvelopeExt,
        <Engine as EngineTypes>::ExecutionPayloadEnvelopeV4:
            From<Engine::BuiltPayload> + PayloadEnvelopeExt,
    {
        let (payload, eth_attr) = self.new_payload().await?;

        self.engine_api
            .submit_payload(payload.clone(), eth_attr.clone(), PayloadStatusEnum::Valid)
            .await?;

        Ok((payload, eth_attr))
    }

    /// Advances the node forward one block
    pub async fn advance_block(
        &mut self,
    ) -> eyre::Result<(Engine::BuiltPayload, Engine::PayloadBuilderAttributes)>
    where
        <Engine as EngineTypes>::ExecutionPayloadEnvelopeV3:
            From<Engine::BuiltPayload> + PayloadEnvelopeExt,
        <Engine as EngineTypes>::ExecutionPayloadEnvelopeV4:
            From<Engine::BuiltPayload> + PayloadEnvelopeExt,
    {
        let (payload, eth_attr) = self.build_and_submit_payload().await?;

        // trigger forkchoice update via engine api to commit the block to the blockchain
        self.engine_api.update_forkchoice(payload.block().hash(), payload.block().hash()).await?;

        Ok((payload, eth_attr))
    }

    /// Waits for block to be available on node.
    pub async fn wait_block(
        &self,
        number: BlockNumber,
        expected_block_hash: BlockHash,
        wait_finish_checkpoint: bool,
    ) -> eyre::Result<()> {
        let mut check = !wait_finish_checkpoint;
        loop {
            tokio::time::sleep(std::time::Duration::from_millis(20)).await;

            if !check && wait_finish_checkpoint {
                if let Some(checkpoint) =
                    self.inner.provider.get_stage_checkpoint(StageId::Finish)?
                {
                    if checkpoint.block_number >= number {
                        check = true
                    }
                }
            }

            if check {
                if let Some(latest_block) = self.inner.provider.block_by_number(number)? {
                    assert_eq!(latest_block.header().hash_slow(), expected_block_hash);
                    break
                }
                assert!(
                    !wait_finish_checkpoint,
                    "Finish checkpoint matches, but could not fetch block."
                );
            }
        }
        Ok(())
    }

    /// Waits for the node to unwind to the given block number
    pub async fn wait_unwind(&self, number: BlockNumber) -> eyre::Result<()> {
        loop {
            tokio::time::sleep(std::time::Duration::from_millis(10)).await;
            if let Some(checkpoint) = self.inner.provider.get_stage_checkpoint(StageId::Headers)? {
                if checkpoint.block_number == number {
                    break
                }
            }
        }
        Ok(())
    }

    /// Asserts that a new block has been added to the blockchain
    /// and the tx has been included in the block.
    ///
    /// Does NOT work for pipeline since there's no stream notification!
    pub async fn assert_new_block(
        &mut self,
        tip_tx_hash: B256,
        block_hash: B256,
        block_number: BlockNumber,
    ) -> eyre::Result<()> {
        // get head block from notifications stream and verify the tx has been pushed to the
        // pool is actually present in the canonical block
        let head = self.engine_api.canonical_stream.next().await.unwrap();
        let tx = head.tip().transactions().first();
        assert_eq!(tx.unwrap().hash().as_slice(), tip_tx_hash.as_slice());

        loop {
            // wait for the block to commit
            tokio::time::sleep(std::time::Duration::from_millis(20)).await;
            if let Some(latest_block) =
                self.inner.provider.block_by_number_or_tag(BlockNumberOrTag::Latest)?
            {
                if latest_block.header().number() == block_number {
                    // make sure the block hash we submitted via FCU engine api is the new latest
                    // block using an RPC call
                    assert_eq!(latest_block.header().hash_slow(), block_hash);
                    break
                }
            }
        }
        Ok(())
    }

    /// Gets block hash by number.
    pub fn block_hash(&self, number: u64) -> BlockHash {
        self.inner
            .provider
            .sealed_header_by_number_or_tag(BlockNumberOrTag::Number(number))
            .unwrap()
            .unwrap()
            .hash()
    }

    /// Sends FCU and waits for the node to sync to the given block.
    pub async fn sync_to(&self, block: BlockHash) -> eyre::Result<()> {
        self.engine_api.update_forkchoice(block, block).await?;

        let start = std::time::Instant::now();

        while self
            .inner
            .provider
            .sealed_header_by_id(BlockId::Number(BlockNumberOrTag::Latest))?
            .is_none_or(|h| h.hash() != block)
        {
            tokio::time::sleep(std::time::Duration::from_millis(100)).await;

            assert!(start.elapsed() <= std::time::Duration::from_secs(10), "timed out");
        }

        // Hack to make sure that all components have time to process canonical state update.
        // Otherwise, this might result in e.g "nonce too low" errors when advancing chain further,
        // making tests flaky.
        tokio::time::sleep(std::time::Duration::from_millis(1000)).await;

        Ok(())
    }

    /// Returns the RPC URL.
    pub fn rpc_url(&self) -> Url {
        let addr = self.inner.rpc_server_handle().http_local_addr().unwrap();
        format!("http://{}", addr).parse().unwrap()
    }
}