Skip to content

Commit 18ef534

Browse files
committed
refactor for host simulation and add some test cases
1 parent 207e19e commit 18ef534

File tree

4 files changed

+221
-48
lines changed

4 files changed

+221
-48
lines changed

src/test_utils.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,18 @@ pub fn setup_test_config() -> Result<BuilderConfig> {
5858
Ok(pecorino_config)
5959
}
6060

61-
/// Returns a new signed test transaction with the provided nonce, value, and mpfpg.
62-
pub fn new_signed_tx(
61+
/// Returns a new signed test transaction with the provided nonce, value, mpfpg, and max fee.
62+
pub fn new_signed_tx_with_max_fee(
6363
wallet: &PrivateKeySigner,
6464
nonce: u64,
6565
value: U256,
6666
mpfpg: u128,
67+
max_fee_per_gas: u128,
6768
) -> Result<TxEnvelope> {
6869
let tx = TxEip1559 {
6970
chain_id: pecorino::RU_CHAIN_ID,
7071
nonce,
71-
max_fee_per_gas: 10_000_000,
72+
max_fee_per_gas,
7273
max_priority_fee_per_gas: mpfpg,
7374
to: TxKind::Call(Address::from_str("0x0000000000000000000000000000000000000000").unwrap()),
7475
value,

tests/block_builder_test.rs

Lines changed: 107 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ use alloy::{
1111
use builder::{
1212
tasks::{
1313
block::sim::Simulator,
14-
env::{EnvTask, Environment, SimEnv},
14+
env::{Environment, SimEnv},
1515
},
16-
test_utils::{new_signed_tx, setup_logging, setup_test_config, test_block_env},
16+
test_utils::{new_signed_tx_with_max_fee, setup_logging, setup_test_config, test_block_env},
1717
};
1818
use signet_sim::SimCache;
1919
use std::time::{Duration, Instant};
@@ -39,7 +39,7 @@ async fn test_handle_build() {
3939
// Create a rollup provider
4040
let ru_provider = RootProvider::<Ethereum>::new_http(anvil_instance.endpoint_url());
4141

42-
// Create a host provider
42+
// Create a host provider
4343
let host_provider = config.connect_host_provider().await.unwrap();
4444

4545
// Provide a dummy env receiver; this test calls handle_build directly and
@@ -51,10 +51,12 @@ async fn test_handle_build() {
5151
let sim_items = SimCache::new();
5252

5353
// Add two transactions from two senders to the sim cache
54-
let tx_1 = new_signed_tx(&test_key_0, 0, U256::from(1_u64), 11_000).unwrap();
54+
let tx_1 =
55+
new_signed_tx_with_max_fee(&test_key_0, 0, U256::from(1_u64), 11_000, 10_000_000).unwrap();
5556
sim_items.add_tx(tx_1, 0);
5657

57-
let tx_2 = new_signed_tx(&test_key_1, 0, U256::from(2_u64), 10_000).unwrap();
58+
let tx_2 =
59+
new_signed_tx_with_max_fee(&test_key_1, 0, U256::from(2_u64), 10_000, 10_000_000).unwrap();
5860
sim_items.add_tx(tx_2, 0);
5961

6062
// Setup the block envs
@@ -91,7 +93,7 @@ async fn test_harness_ticks_and_emits() {
9193
let test_key_0 = PrivateKeySigner::from_signing_key(keys[0].clone().into());
9294

9395
// Start simulator and tick a new SimEnv
94-
h.start();
96+
h.start().await;
9597

9698
// Add a transaction into the sim cache
9799
h.add_tx(&test_key_0, 0, U256::from(1_u64), 11_000);
@@ -123,7 +125,7 @@ async fn test_harness_simulates_full_flow() {
123125
h.add_tx(&test_key_1, 0, U256::from(2_u64), 10_000);
124126

125127
// Start simulator and tick a new SimEnv
126-
h.start();
128+
h.start().await;
127129

128130
h.mine_blocks(1).await.unwrap();
129131

@@ -154,6 +156,103 @@ async fn test_harness_stops() {
154156
setup_logging();
155157
let mut h = TestHarness::new().await.unwrap();
156158

157-
h.start();
159+
h.start().await;
160+
161+
h.stop().await;
162+
163+
assert_eq!(h.simulator_handle.is_none(), true);
164+
}
165+
166+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
167+
async fn test_harness_timeout_without_results() {
168+
setup_logging();
169+
let mut h = TestHarness::new().await.unwrap();
170+
171+
h.start().await;
172+
173+
let wait = Duration::from_millis(250);
174+
let got = h.recv_result(wait).await;
175+
176+
h.stop().await;
177+
178+
assert!(got.is_none(), "expected timeout when no blocks are mined");
179+
}
180+
181+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
182+
async fn test_harness_start_is_idempotent() {
183+
setup_logging();
184+
let mut h = TestHarness::new().await.unwrap();
185+
186+
h.start().await;
187+
let first_id = h.simulator_handle.as_ref().expect("simulator handle").id();
188+
189+
h.start().await;
190+
let second_id = h.simulator_handle.as_ref().expect("simulator handle").id();
191+
192+
h.stop().await;
193+
194+
assert_eq!(first_id, second_id, "second start should reuse existing task");
195+
}
196+
197+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
198+
async fn test_harness_stop_without_start() {
199+
setup_logging();
200+
let mut h = TestHarness::new().await.unwrap();
201+
202+
h.stop().await;
203+
204+
assert!(h.simulator_handle.is_none());
205+
}
206+
207+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
208+
async fn test_harness_emits_multiple_results() {
209+
setup_logging();
210+
let mut h = TestHarness::new().await.unwrap();
211+
212+
let keys = h.rollup.anvil().keys();
213+
let signer = PrivateKeySigner::from_signing_key(keys[0].clone().into());
214+
215+
// First tick uses a transaction added before the simulator starts.
216+
h.add_tx(&signer, 0, U256::from(1_u64), 11_000);
217+
h.start().await;
218+
h.mine_blocks(1).await.unwrap();
219+
220+
let wait = Duration::from_secs(h.config.slot_calculator.slot_duration() + 5);
221+
let first = h.recv_result(wait).await.expect("first sim result");
222+
assert_eq!(first.block.tx_count(), 1);
223+
224+
// Second tick shouldn't need new transactions to emit a block.
225+
h.mine_blocks(1).await.unwrap();
226+
let second = h.recv_result(wait).await.expect("second sim result");
227+
assert_eq!(second.block.tx_count(), 0);
228+
assert_eq!(second.block.block_number(), first.block.block_number() + 1);
229+
230+
h.stop().await;
231+
}
232+
233+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
234+
async fn test_harness_result_matches_headers() {
235+
setup_logging();
236+
let mut h = TestHarness::new().await.unwrap();
237+
238+
let keys = h.rollup.anvil().keys();
239+
let signer = PrivateKeySigner::from_signing_key(keys[0].clone().into());
240+
241+
// Capture the headers the harness should target.
242+
let (prev_rollup, prev_host) = h.get_headers().await.unwrap();
243+
244+
h.add_tx(&signer, 0, U256::from(1_u64), 11_000);
245+
h.start().await;
246+
h.mine_blocks(1).await.unwrap();
247+
248+
let wait = Duration::from_secs(h.config.slot_calculator.slot_duration() + 5);
249+
let got = h.recv_result(wait).await.expect("sim result");
250+
251+
assert_eq!(got.block.tx_count(), 1);
252+
assert_eq!(got.rollup_block_number(), prev_rollup.number + 2);
253+
assert_eq!(got.host_block_number(), prev_host.number + 2);
254+
assert_eq!(got.prev_rollup().number, prev_rollup.number + 1);
255+
assert_eq!(got.prev_host().number, prev_host.number + 1);
256+
158257
h.stop().await;
159258
}

0 commit comments

Comments
 (0)