Skip to content

Commit 6902345

Browse files
committed
Extract FlashblocksHarness module
1 parent 83390a4 commit 6902345

File tree

6 files changed

+96
-84
lines changed

6 files changed

+96
-84
lines changed

crates/flashblocks-rpc/tests/rpc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use alloy_rpc_types::simulate::{SimBlock, SimulatePayload};
1313
use alloy_rpc_types_engine::PayloadId;
1414
use alloy_rpc_types_eth::{TransactionInput, error::EthRpcErrorCode};
1515
use base_reth_flashblocks_rpc::subscription::{Flashblock, Metadata};
16-
use base_reth_test_utils::harness::FlashblocksHarness;
16+
use base_reth_test_utils::flashblocks_harness::FlashblocksHarness;
1717
use common::{BLOCK_INFO_TXN, BLOCK_INFO_TXN_HASH};
1818
use eyre::Result;
1919
use op_alloy_consensus::OpDepositReceipt;

crates/flashblocks-rpc/tests/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use base_reth_flashblocks_rpc::{
1212
subscription::{Flashblock, Metadata},
1313
};
1414
use base_reth_test_utils::{
15-
accounts::TestAccounts, harness::FlashblocksHarness, node::LocalNodeProvider,
15+
accounts::TestAccounts, flashblocks_harness::FlashblocksHarness, node::LocalNodeProvider,
1616
};
1717
use common::{BLOCK_INFO_TXN, BLOCK_INFO_TXN_HASH};
1818
use op_alloy_consensus::OpDepositReceipt;

crates/test-utils/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ Each account includes:
211211
Use `FlashblocksHarness` when you need `send_flashblock` and access to the in-memory pending state.
212212

213213
```rust
214-
use base_reth_test_utils::harness::FlashblocksHarness;
214+
use base_reth_test_utils::flashblocks_harness::FlashblocksHarness;
215215

216216
#[tokio::test]
217217
async fn test_flashblocks() -> eyre::Result<()> {
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
use std::{ops::Deref, sync::Arc};
2+
3+
use base_reth_flashblocks_rpc::subscription::Flashblock;
4+
use eyre::Result;
5+
use futures_util::Future;
6+
use reth::builder::NodeHandle;
7+
use reth_e2e_test_utils::Adapter;
8+
use reth_optimism_node::OpNode;
9+
10+
use crate::{
11+
harness::TestHarness,
12+
node::{
13+
FlashblocksLocalNode, FlashblocksParts, LocalFlashblocksState, OpAddOns, OpBuilder,
14+
default_launcher,
15+
},
16+
tracing::init_silenced_tracing,
17+
};
18+
19+
pub struct FlashblocksHarness {
20+
inner: TestHarness,
21+
parts: FlashblocksParts,
22+
}
23+
24+
impl FlashblocksHarness {
25+
pub async fn new() -> Result<Self> {
26+
Self::with_launcher(default_launcher).await
27+
}
28+
29+
pub async fn manual_canonical() -> Result<Self> {
30+
Self::manual_canonical_with_launcher(default_launcher).await
31+
}
32+
33+
pub async fn with_launcher<L, LRet>(launcher: L) -> Result<Self>
34+
where
35+
L: FnOnce(OpBuilder) -> LRet,
36+
LRet: Future<Output = eyre::Result<NodeHandle<Adapter<OpNode>, OpAddOns>>>,
37+
{
38+
init_silenced_tracing();
39+
let flash_node = FlashblocksLocalNode::with_launcher(launcher).await?;
40+
Self::from_flashblocks_node(flash_node).await
41+
}
42+
43+
pub async fn manual_canonical_with_launcher<L, LRet>(launcher: L) -> Result<Self>
44+
where
45+
L: FnOnce(OpBuilder) -> LRet,
46+
LRet: Future<Output = eyre::Result<NodeHandle<Adapter<OpNode>, OpAddOns>>>,
47+
{
48+
init_silenced_tracing();
49+
let flash_node = FlashblocksLocalNode::with_manual_canonical_launcher(launcher).await?;
50+
Self::from_flashblocks_node(flash_node).await
51+
}
52+
53+
pub fn flashblocks_state(&self) -> Arc<LocalFlashblocksState> {
54+
self.parts.state()
55+
}
56+
57+
pub async fn send_flashblock(&self, flashblock: Flashblock) -> Result<()> {
58+
self.parts.send(flashblock).await
59+
}
60+
61+
pub async fn send_flashblocks<I>(&self, flashblocks: I) -> Result<()>
62+
where
63+
I: IntoIterator<Item = Flashblock>,
64+
{
65+
for flashblock in flashblocks {
66+
self.send_flashblock(flashblock).await?;
67+
}
68+
Ok(())
69+
}
70+
71+
pub fn into_inner(self) -> TestHarness {
72+
self.inner
73+
}
74+
75+
async fn from_flashblocks_node(flash_node: FlashblocksLocalNode) -> Result<Self> {
76+
let (node, parts) = flash_node.into_parts();
77+
let inner = TestHarness::from_node(node).await?;
78+
Ok(Self { inner, parts })
79+
}
80+
}
81+
82+
impl Deref for FlashblocksHarness {
83+
type Target = TestHarness;
84+
85+
fn deref(&self) -> &Self::Target {
86+
&self.inner
87+
}
88+
}

crates/test-utils/src/harness.rs

Lines changed: 4 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
//! Unified test harness combining node and engine helpers, plus an optional flashblocks adapter.
1+
//! Unified test harness combining node and engine helpers, plus optional flashblocks adapter.
22
3-
use std::{ops::Deref, sync::Arc, time::Duration};
3+
use std::time::Duration;
44

55
use alloy_eips::{BlockHashOrNumber, eip7685::Requests};
66
use alloy_primitives::{B64, B256, Bytes, bytes};
77
use alloy_provider::{Provider, RootProvider};
88
use alloy_rpc_types::BlockNumberOrTag;
99
use alloy_rpc_types_engine::PayloadAttributes;
10-
use base_reth_flashblocks_rpc::subscription::Flashblock;
1110
use eyre::{Result, eyre};
1211
use futures_util::Future;
1312
use op_alloy_network::Optimism;
@@ -25,10 +24,7 @@ use tokio::time::sleep;
2524
use crate::{
2625
accounts::TestAccounts,
2726
engine::{EngineApi, IpcEngine},
28-
node::{
29-
FlashblocksLocalNode, FlashblocksParts, LocalFlashblocksState, LocalNode,
30-
LocalNodeProvider, OpAddOns, OpBuilder, default_launcher,
31-
},
27+
node::{LocalNode, LocalNodeProvider, OpAddOns, OpBuilder, default_launcher},
3228
tracing::init_silenced_tracing,
3329
};
3430

@@ -62,7 +58,7 @@ impl TestHarness {
6258
Self::from_node(node).await
6359
}
6460

65-
async fn from_node(node: LocalNode) -> Result<Self> {
61+
pub(crate) async fn from_node(node: LocalNode) -> Result<Self> {
6662
let engine = node.engine_api()?;
6763
let accounts = TestAccounts::new();
6864

@@ -185,79 +181,6 @@ impl TestHarness {
185181
}
186182
}
187183

188-
pub struct FlashblocksHarness {
189-
inner: TestHarness,
190-
parts: FlashblocksParts,
191-
}
192-
193-
impl FlashblocksHarness {
194-
pub async fn new() -> Result<Self> {
195-
Self::with_launcher(default_launcher).await
196-
}
197-
198-
/// Same as `new` but canonical block processing is left to the test, which is useful when
199-
/// reproducing races or reorg scenarios that require deterministic sequencing.
200-
pub async fn manual_canonical() -> Result<Self> {
201-
Self::manual_canonical_with_launcher(default_launcher).await
202-
}
203-
204-
pub async fn with_launcher<L, LRet>(launcher: L) -> Result<Self>
205-
where
206-
L: FnOnce(OpBuilder) -> LRet,
207-
LRet: Future<Output = eyre::Result<NodeHandle<Adapter<OpNode>, OpAddOns>>>,
208-
{
209-
init_silenced_tracing();
210-
let flash_node = FlashblocksLocalNode::with_launcher(launcher).await?;
211-
Self::from_flashblocks_node(flash_node).await
212-
}
213-
214-
pub async fn manual_canonical_with_launcher<L, LRet>(launcher: L) -> Result<Self>
215-
where
216-
L: FnOnce(OpBuilder) -> LRet,
217-
LRet: Future<Output = eyre::Result<NodeHandle<Adapter<OpNode>, OpAddOns>>>,
218-
{
219-
init_silenced_tracing();
220-
let flash_node = FlashblocksLocalNode::with_manual_canonical_launcher(launcher).await?;
221-
Self::from_flashblocks_node(flash_node).await
222-
}
223-
224-
pub fn flashblocks_state(&self) -> Arc<LocalFlashblocksState> {
225-
self.parts.state()
226-
}
227-
228-
pub async fn send_flashblock(&self, flashblock: Flashblock) -> Result<()> {
229-
self.parts.send(flashblock).await
230-
}
231-
232-
pub async fn send_flashblocks<I>(&self, flashblocks: I) -> Result<()>
233-
where
234-
I: IntoIterator<Item = Flashblock>,
235-
{
236-
for flashblock in flashblocks {
237-
self.send_flashblock(flashblock).await?;
238-
}
239-
Ok(())
240-
}
241-
242-
pub fn into_inner(self) -> TestHarness {
243-
self.inner
244-
}
245-
246-
async fn from_flashblocks_node(flash_node: FlashblocksLocalNode) -> Result<Self> {
247-
let (node, parts) = flash_node.into_parts();
248-
let inner = TestHarness::from_node(node).await?;
249-
Ok(Self { inner, parts })
250-
}
251-
}
252-
253-
impl Deref for FlashblocksHarness {
254-
type Target = TestHarness;
255-
256-
fn deref(&self) -> &Self::Target {
257-
&self.inner
258-
}
259-
}
260-
261184
#[cfg(test)]
262185
mod tests {
263186
use alloy_primitives::U256;

crates/test-utils/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod accounts;
22
pub mod engine;
3+
pub mod flashblocks_harness;
34
pub mod harness;
45
pub mod node;
56
pub mod tracing;

0 commit comments

Comments
 (0)