-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmod.rs
More file actions
287 lines (270 loc) · 10.7 KB
/
Copy pathmod.rs
File metadata and controls
287 lines (270 loc) · 10.7 KB
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
//! Test utilities for the Scroll rollup node.
//!
//! This module provides a high-level test framework for creating and managing
//! test nodes, building blocks, managing L1 interactions, and asserting on events.
//!
//! # Quick Start
//!
//! ```rust,ignore
//! use rollup_node::test_utils::TestFixture;
//!
//! #[tokio::test]
//! async fn test_basic_block_production() -> eyre::Result<()> {
//! let mut fixture = TestFixture::sequencer().build().await?;
//!
//! // Inject a transaction
//! let tx_hash = fixture.inject_transfer().await?;
//!
//! // Build a block
//! let block = fixture.build_block()
//! .expect_tx(tx_hash)
//! .await_block()
//! .await?;
//!
//! // Get the current block
//! let current_block = fixture.get_sequencer_block().await?;
//!
//! Ok(())
//! }
//! ```
//!
//! # Event Assertions
//!
//! The framework provides powerful event assertion capabilities:
//!
//! ```rust,ignore
//! // Wait for events on a single node
//! fixture.expect_event_on(1).chain_extended().await?;
//!
//! // Wait for the same event on multiple nodes
//! fixture.expect_event_on_followers().new_block_received().await?;
//!
//! // Wait for events on all nodes (including sequencer)
//! fixture.expect_event_on_all_nodes().chain_extended().await?;
//!
//! // Custom event predicates - just check if event matches
//! fixture.expect_event()
//! .where_event(|e| matches!(e, ChainOrchestratorEvent::BlockSequenced(_)))
//! .await?;
//!
//! // Extract values from events
//! let block_numbers = fixture.expect_event_on_nodes(vec![1, 2])
//! .extract(|e| {
//! if let ChainOrchestratorEvent::NewL1Block(num) = e {
//! Some(*num)
//! } else {
//! None
//! }
//! })
//! .await?;
//! ```
// Module declarations
pub mod block_builder;
pub mod event_utils;
pub mod fixture;
pub mod l1_helpers;
pub mod network_helpers;
pub mod tx_helpers;
// Re-export main types for convenience
pub use event_utils::{EventAssertions, EventWaiter};
pub use fixture::{NodeHandle, TestFixture, TestFixtureBuilder};
pub use network_helpers::{
NetworkHelper, NetworkHelperProvider, ReputationChecker, ReputationChecks,
};
// Legacy utilities - keep existing functions for backward compatibility
use crate::{
BlobProviderArgs, ChainOrchestratorArgs, ConsensusArgs, EngineDriverArgs, L1ProviderArgs,
PprofArgs, RollupNodeDatabaseArgs, RollupNodeNetworkArgs, RpcArgs, ScrollRollupNode,
ScrollRollupNodeConfig, SequencerArgs, constants,
};
use alloy_primitives::Bytes;
use reth_chainspec::EthChainSpec;
use reth_e2e_test_utils::{
node::NodeTestContext, transaction::TransactionTestContext, wallet::Wallet, Adapter,
NodeHelperType, TmpDB, TmpNodeAddOnsHandle, TmpNodeEthApi,
};
use reth_engine_local::LocalPayloadAttributesBuilder;
use reth_node_builder::{
rpc::RpcHandleProvider, EngineNodeLauncher, Node, NodeBuilder, NodeConfig,
NodeHandle as RethNodeHandle, NodeTypes, NodeTypesWithDBAdapter, PayloadAttributesBuilder,
PayloadTypes, TreeConfig,
};
use reth_node_core::args::{DiscoveryArgs, NetworkArgs, RpcServerArgs, TxPoolArgs};
use reth_provider::providers::BlockchainProvider;
use reth_rpc_server_types::RpcModuleSelection;
use reth_tasks::TaskManager;
use rollup_node_sequencer::L1MessageInclusionMode;
use std::{path::PathBuf, sync::Arc};
use tokio::sync::Mutex;
use tracing::{span, Level};
/// Creates the initial setup with `num_nodes` started and interconnected.
///
/// This is the legacy setup function that's used by existing tests.
/// For new tests, consider using the `TestFixture` API instead.
pub async fn setup_engine(
mut scroll_node_config: ScrollRollupNodeConfig,
num_nodes: usize,
chain_spec: Arc<<ScrollRollupNode as NodeTypes>::ChainSpec>,
is_dev: bool,
no_local_transactions_propagation: bool,
) -> eyre::Result<(
Vec<
NodeHelperType<
ScrollRollupNode,
BlockchainProvider<NodeTypesWithDBAdapter<ScrollRollupNode, TmpDB>>,
>,
>,
TaskManager,
Wallet,
)>
where
LocalPayloadAttributesBuilder<<ScrollRollupNode as NodeTypes>::ChainSpec>:
PayloadAttributesBuilder<
<<ScrollRollupNode as NodeTypes>::Payload as PayloadTypes>::PayloadAttributes,
>,
TmpNodeAddOnsHandle<ScrollRollupNode>:
RpcHandleProvider<Adapter<ScrollRollupNode>, TmpNodeEthApi<ScrollRollupNode>>,
{
let tasks = TaskManager::current();
let exec = tasks.executor();
let network_config = NetworkArgs {
discovery: DiscoveryArgs { disable_discovery: true, ..DiscoveryArgs::default() },
..NetworkArgs::default()
};
// Create nodes and peer them
let mut nodes: Vec<NodeTestContext<_, _>> = Vec::with_capacity(num_nodes);
for idx in 0..num_nodes {
// disable sequencer nodes after the first one
if idx != 0 {
scroll_node_config.sequencer_args.sequencer_enabled = false;
}
let node_config = NodeConfig::new(chain_spec.clone())
.with_network(network_config.clone())
.with_unused_ports()
.with_rpc(
RpcServerArgs::default()
.with_unused_ports()
.with_http()
.with_http_api(RpcModuleSelection::All),
)
.set_dev(is_dev)
.with_txpool(TxPoolArgs { no_local_transactions_propagation, ..Default::default() });
let span = span!(Level::INFO, "node", idx);
let _enter = span.enter();
let testing_node = NodeBuilder::new(node_config.clone()).testing_node(exec.clone());
let testing_config = testing_node.config().clone();
let node = ScrollRollupNode::new(scroll_node_config.clone(), testing_config).await;
let RethNodeHandle { node, node_exit_future: _ } = testing_node
.with_types_and_provider::<ScrollRollupNode, BlockchainProvider<_>>()
.with_components(node.components_builder())
.with_add_ons(node.add_ons())
.launch_with_fn(|builder| {
let tree_config = TreeConfig::default()
.with_always_process_payload_attributes_on_canonical_head(true)
.with_unwind_canonical_header(true)
.with_persistence_threshold(0);
let launcher = EngineNodeLauncher::new(
builder.task_executor().clone(),
builder.config().datadir(),
tree_config,
);
builder.launch_with(launcher)
})
.await?;
let mut node =
NodeTestContext::new(node, |_| panic!("should not build payloads using this method"))
.await?;
let genesis = node.block_hash(0);
node.update_forkchoice(genesis, genesis).await?;
// Connect each node in a chain.
if let Some(previous_node) = nodes.last_mut() {
previous_node.connect(&mut node).await;
}
// Connect last node with the first if there are more than two
if idx + 1 == num_nodes && num_nodes > 2 {
if let Some(first_node) = nodes.first_mut() {
node.connect(first_node).await;
}
}
nodes.push(node);
}
Ok((nodes, tasks, Wallet::default().with_chain_id(chain_spec.chain().into())))
}
/// Generate a transfer transaction with the given wallet.
pub async fn generate_tx(wallet: Arc<Mutex<Wallet>>) -> Bytes {
let mut wallet = wallet.lock().await;
let tx_fut = TransactionTestContext::transfer_tx_nonce_bytes(
wallet.chain_id,
wallet.inner.clone(),
wallet.inner_nonce,
);
wallet.inner_nonce += 1;
tx_fut.await
}
/// Returns a default [`ScrollRollupNodeConfig`] preconfigured for testing.
pub fn default_test_scroll_rollup_node_config() -> ScrollRollupNodeConfig {
ScrollRollupNodeConfig {
test: true,
network_args: RollupNodeNetworkArgs::default(),
database_args: RollupNodeDatabaseArgs::default(),
l1_provider_args: L1ProviderArgs::default(),
engine_driver_args: EngineDriverArgs { sync_at_startup: true },
chain_orchestrator_args: ChainOrchestratorArgs {
optimistic_sync_trigger: 100,
chain_buffer_size: 100,
},
sequencer_args: SequencerArgs {
payload_building_duration: 1000,
allow_empty_blocks: true,
..Default::default()
},
blob_provider_args: BlobProviderArgs { mock: true, ..Default::default() },
signer_args: Default::default(),
gas_price_oracle_args: crate::RollupNodeGasPriceOracleArgs::default(),
consensus_args: ConsensusArgs::noop(),
database: None,
pprof_args: PprofArgs::default(),
rpc_args: RpcArgs { basic_enabled: true, admin_enabled: true },
}
}
/// Returns a default [`ScrollRollupNodeConfig`] preconfigured for testing with sequencer.
/// It sets `sequencer_args.block_time = 0` so that no blocks are produced automatically.
/// To produce blocks the `build_block` method needs to be invoked.
/// This is so that block production and test scenarios remain predictable.
///
/// In case this behavior is not wanted, `block_time` can be adjusted to any value > 0 after
/// obtaining the config so that the sequencer node will produce blocks automatically in this
/// interval.
pub fn default_sequencer_test_scroll_rollup_node_config() -> ScrollRollupNodeConfig {
ScrollRollupNodeConfig {
test: true,
network_args: RollupNodeNetworkArgs::default(),
database_args: RollupNodeDatabaseArgs {
rn_db_path: Some(PathBuf::from("sqlite::memory:")),
},
l1_provider_args: L1ProviderArgs::default(),
engine_driver_args: EngineDriverArgs { sync_at_startup: true },
chain_orchestrator_args: ChainOrchestratorArgs {
optimistic_sync_trigger: 100,
chain_buffer_size: 100,
},
sequencer_args: SequencerArgs {
sequencer_enabled: true,
auto_start: false,
block_time: 100,
payload_building_duration: 40,
fee_recipient: Default::default(),
l1_message_inclusion_mode: L1MessageInclusionMode::BlockDepth(0),
allow_empty_blocks: true,
max_l1_messages: None,
payload_size_limit: constants::DEFAULT_PAYLOAD_SIZE_LIMIT,
},
blob_provider_args: BlobProviderArgs { mock: true, ..Default::default() },
signer_args: Default::default(),
gas_price_oracle_args: crate::RollupNodeGasPriceOracleArgs::default(),
consensus_args: ConsensusArgs::noop(),
database: None,
pprof_args: PprofArgs::default(),
rpc_args: RpcArgs { basic_enabled: true, admin_enabled: true },
}
}