signet-test-utils 0.16.2

Common utilities for testing Signet components
Documentation
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
use alloy::primitives::U256;
use signet_test_utils::{
    contracts::{
        counter::{Counter::incrementCall, COUNTER_SLOT, COUNTER_TEST_ADDRESS},
        token::ERC20::approveCall,
    },
    evm::test_sim_env,
    specs::{signed_simple_call, simple_bundle},
    test_constants::*,
    users::{TEST_SIGNERS, TEST_USERS},
};
use signet_types::UnsignedOrder;
use signet_zenith::{HostOrders::fillCall, RollupOrders::initiateCall};
use tokio::time::{Duration, Instant};
use trevm::revm::DatabaseRef;

#[tokio::test]
async fn host_sim() {
    let builder = test_sim_env(Instant::now() + Duration::from_millis(200));

    // Set up an order, fill pair
    let order = UnsignedOrder::default()
        .with_input(TEST_SYS.rollup().tokens().weth(), U256::from(1000))
        .with_output(
            TEST_SYS.host().tokens().weth(),
            U256::from(1000),
            TEST_USERS[5],
            TEST_SYS.host_chain_id() as u32,
        )
        .to_order();

    let fill_outputs = order
        .outputs
        .clone()
        .into_iter()
        .map(|mut output| {
            output.chainId = TEST_SYS.host_chain_id() as u32;
            output
        })
        .collect::<Vec<_>>();

    let order_call =
        initiateCall { deadline: U256::MAX, inputs: order.inputs, outputs: order.outputs };

    let fill_call = fillCall { outputs: fill_outputs };

    // Make RU and HOST transactions
    let ru_tx = signed_simple_call(
        &TEST_SIGNERS[0],
        TEST_SYS.ru_orders(),
        &order_call,
        U256::ZERO,
        0,
        TEST_SYS.ru_chain_id(),
    );

    let host_tx = signed_simple_call(
        &TEST_SIGNERS[1],
        TEST_SYS.host_orders(),
        &fill_call,
        U256::ZERO,
        0,
        TEST_SYS.host_chain_id(),
    );

    // Make a bundle containing the two transactions
    let bundle = simple_bundle(vec![ru_tx], vec![host_tx], 0);

    builder.sim_items().add_bundle(bundle, 0).unwrap();

    let block = builder.build().await;

    assert_eq!(block.transactions().len(), 1);
    assert_eq!(block.host_transactions().len(), 1);
}

#[tokio::test]
async fn host_sim_insufficient_fill() {
    let builder = test_sim_env(Instant::now() + Duration::from_millis(200));

    // Set up an order, fill pair
    let order = UnsignedOrder::default()
        .with_input(TEST_SYS.rollup().tokens().weth(), U256::from(1000))
        .with_output(
            TEST_SYS.host().tokens().weth(),
            U256::from(1000),
            TEST_USERS[5],
            TEST_SYS.host_chain_id() as u32,
        )
        .to_order();

    let fill_outputs = order
        .outputs
        .clone()
        .into_iter()
        .map(|mut output| {
            output.chainId = TEST_SYS.host_chain_id() as u32;
            output.amount -= U256::ONE; // Make the fill insufficient
            output
        })
        .collect::<Vec<_>>();

    let order_call =
        initiateCall { deadline: U256::MAX, inputs: order.inputs, outputs: order.outputs };

    let fill_call = fillCall { outputs: fill_outputs };

    // Make RU and HOST transactions
    let ru_tx = signed_simple_call(
        &TEST_SIGNERS[0],
        TEST_SYS.ru_orders(),
        &order_call,
        U256::ZERO,
        0,
        TEST_SYS.ru_chain_id(),
    );

    let host_tx = signed_simple_call(
        &TEST_SIGNERS[1],
        TEST_SYS.host_orders(),
        &fill_call,
        U256::ZERO,
        0,
        TEST_SYS.host_chain_id(),
    );

    // Make a bundle containing the two transactions
    let bundle = simple_bundle(vec![ru_tx], vec![host_tx], 0);

    builder.sim_items().add_bundle(bundle, 0).unwrap();

    let block = builder.build().await;

    assert!(block.transactions().is_empty());
    assert!(block.host_transactions().is_empty());
}

// Currently 0-score bundles are dropped entirely. This may change in future.
#[tokio::test]
async fn host_sim_insufficient_fill_reverting_ok() {
    let builder = test_sim_env(Instant::now() + Duration::from_millis(200));

    // Set up an order, fill pair
    let order = UnsignedOrder::default()
        .with_input(TEST_SYS.rollup().tokens().weth(), U256::from(1000))
        .with_output(
            TEST_SYS.host().tokens().weth(),
            U256::from(1000),
            TEST_USERS[5],
            TEST_SYS.host_chain_id() as u32,
        )
        .to_order();

    let fill_outputs = order
        .outputs
        .clone()
        .into_iter()
        .map(|mut output| {
            output.chainId = TEST_SYS.host_chain_id() as u32;
            output.amount -= U256::ONE; // Make the fill insufficient
            output
        })
        .collect::<Vec<_>>();

    let order_call =
        initiateCall { deadline: U256::MAX, inputs: order.inputs, outputs: order.outputs };

    let fill_call = fillCall { outputs: fill_outputs };

    // Make RU and HOST transactions
    let ru_tx = signed_simple_call(
        &TEST_SIGNERS[0],
        TEST_SYS.ru_orders(),
        &order_call,
        U256::ZERO,
        0,
        TEST_SYS.ru_chain_id(),
    );

    let host_tx = signed_simple_call(
        &TEST_SIGNERS[1],
        TEST_SYS.host_orders(),
        &fill_call,
        U256::ZERO,
        0,
        TEST_SYS.host_chain_id(),
    );

    // Make a bundle containing the two transactions
    let ru_tx_hash = *ru_tx.hash();
    let mut bundle = simple_bundle(vec![ru_tx], vec![host_tx], 0);
    bundle.bundle.reverting_tx_hashes.push(ru_tx_hash);

    builder.sim_items().add_bundle(bundle, 0).unwrap();

    let block = builder.build().await;

    // Score 0 bundles are dropped
    assert!(block.transactions().is_empty());
    assert!(block.host_transactions().is_empty());
}

#[tokio::test]
async fn too_much_host_gas() {
    let mut builder = test_sim_env(Instant::now() + Duration::from_millis(200));
    builder.set_max_host_gas(1); // Set max host gas very low

    // Set up an order, fill pair
    let order = UnsignedOrder::default()
        .with_input(TEST_SYS.rollup().tokens().weth(), U256::from(1000))
        .with_output(
            TEST_SYS.host().tokens().weth(),
            U256::from(1000),
            TEST_USERS[5],
            TEST_SYS.host_chain_id() as u32,
        )
        .to_order();

    let fill_outputs = order
        .outputs
        .clone()
        .into_iter()
        .map(|mut output| {
            output.chainId = TEST_SYS.host_chain_id() as u32;
            output
        })
        .collect::<Vec<_>>();

    let order_call =
        initiateCall { deadline: U256::MAX, inputs: order.inputs, outputs: order.outputs };

    let fill_call = fillCall { outputs: fill_outputs };

    // Make RU and HOST transactions
    let ru_tx = signed_simple_call(
        &TEST_SIGNERS[0],
        TEST_SYS.ru_orders(),
        &order_call,
        U256::ZERO,
        0,
        TEST_SYS.ru_chain_id(),
    );

    let host_tx = signed_simple_call(
        &TEST_SIGNERS[1],
        TEST_SYS.host_orders(),
        &fill_call,
        U256::ZERO,
        0,
        TEST_SYS.host_chain_id(),
    );

    // Make a bundle containing the two transactions
    let ru_tx_hash = *ru_tx.hash();
    let mut bundle = simple_bundle(vec![ru_tx], vec![host_tx], 0);
    bundle.bundle.reverting_tx_hashes.push(ru_tx_hash);

    builder.sim_items().add_bundle(bundle, 0).unwrap();

    let block = builder.build().await;

    assert!(block.transactions().is_empty());
    assert!(block.host_transactions().is_empty());
}

#[tokio::test]
async fn larger_bundle() {
    let builder = test_sim_env(Instant::now() + Duration::from_millis(200));

    // Set up an order, fill pair
    let order = UnsignedOrder::default()
        .with_input(TEST_SYS.rollup().tokens().weth(), U256::from(1000))
        .with_output(
            TEST_SYS.host().tokens().weth(),
            U256::from(1000),
            TEST_USERS[5],
            TEST_SYS.host_chain_id() as u32,
        )
        .to_order();

    let fill_outputs = order
        .outputs
        .clone()
        .into_iter()
        .map(|mut output| {
            output.chainId = TEST_SYS.host_chain_id() as u32;
            output
        })
        .collect::<Vec<_>>();

    let order_call =
        initiateCall { deadline: U256::MAX, inputs: order.inputs, outputs: order.outputs };

    let fill_call = fillCall { outputs: fill_outputs };

    // Make RU and HOST transactions
    let ru_tx = signed_simple_call(
        &TEST_SIGNERS[0],
        TEST_SYS.ru_orders(),
        &order_call,
        U256::ZERO,
        0,
        TEST_SYS.ru_chain_id(),
    );
    let ru_tx_1 = signed_simple_call(
        &TEST_SIGNERS[0],
        TEST_SYS.ru_orders(),
        &order_call,
        U256::ZERO,
        1,
        TEST_SYS.ru_chain_id(),
    );
    let ru_tx_2 = signed_simple_call(
        &TEST_SIGNERS[0],
        TEST_SYS.ru_orders(),
        &order_call,
        U256::ZERO,
        2,
        TEST_SYS.ru_chain_id(),
    );

    let host_tx = signed_simple_call(
        &TEST_SIGNERS[1],
        TEST_SYS.host_orders(),
        &fill_call,
        U256::ZERO,
        0,
        TEST_SYS.host_chain_id(),
    );
    let host_tx_1 = signed_simple_call(
        &TEST_SIGNERS[1],
        TEST_SYS.host_orders(),
        &fill_call,
        U256::ZERO,
        1,
        TEST_SYS.host_chain_id(),
    );
    let host_tx_2 = signed_simple_call(
        &TEST_SIGNERS[1],
        TEST_SYS.host_orders(),
        &fill_call,
        U256::ZERO,
        2,
        TEST_SYS.host_chain_id(),
    );

    let bundle =
        simple_bundle(vec![ru_tx, ru_tx_1, ru_tx_2], vec![host_tx, host_tx_1, host_tx_2], 0);

    builder.sim_items().add_bundle(bundle, 0).unwrap();

    let block = builder.build().await;

    assert_eq!(block.transactions().len(), 3);
    assert_eq!(block.host_transactions().len(), 3);
}

#[tokio::test]
async fn larger_bundle_revert_ok() {
    let builder = test_sim_env(Instant::now() + Duration::from_millis(200));

    // Set up an order, fill pair
    let order = UnsignedOrder::default()
        .with_input(TEST_SYS.rollup().tokens().weth(), U256::from(1000))
        .with_output(
            TEST_SYS.host().tokens().weth(),
            U256::from(1000),
            TEST_USERS[5],
            TEST_SYS.host_chain_id() as u32,
        )
        .to_order();

    let fill_outputs = order
        .outputs
        .clone()
        .into_iter()
        .map(|mut output| {
            output.chainId = TEST_SYS.host_chain_id() as u32;
            output
        })
        .collect::<Vec<_>>();

    let order_call =
        initiateCall { deadline: U256::MAX, inputs: order.inputs, outputs: order.outputs };

    let fill_call = fillCall { outputs: fill_outputs };

    // Make RU and HOST transactions
    let ru_tx = signed_simple_call(
        &TEST_SIGNERS[0],
        TEST_SYS.ru_orders(),
        &order_call,
        U256::ZERO,
        0,
        TEST_SYS.ru_chain_id(),
    );
    let ru_tx_1 = signed_simple_call(
        &TEST_SIGNERS[0],
        TEST_SYS.ru_orders(),
        &order_call,
        U256::ZERO,
        1,
        TEST_SYS.ru_chain_id(),
    );
    let ru_tx_2 = signed_simple_call(
        &TEST_SIGNERS[0],
        TEST_SYS.ru_orders(),
        &order_call,
        U256::ZERO,
        2,
        TEST_SYS.ru_chain_id(),
    );

    let host_tx = signed_simple_call(
        &TEST_SIGNERS[1],
        TEST_SYS.host_orders(),
        &fill_call,
        U256::ZERO,
        0,
        TEST_SYS.host_chain_id(),
    );
    let host_tx_1 = signed_simple_call(
        &TEST_SIGNERS[1],
        TEST_SYS.host_orders(),
        &fill_call,
        U256::ZERO,
        1,
        TEST_SYS.host_chain_id(),
    );

    let ru_tx_2_hash = *ru_tx_2.hash();

    let mut bundle = simple_bundle(vec![ru_tx, ru_tx_1], vec![host_tx, host_tx_1], 0);

    bundle.bundle.reverting_tx_hashes.push(ru_tx_2_hash);

    builder.sim_items().add_bundle(bundle, 0).unwrap();

    let block = builder.build().await;

    assert_eq!(block.transactions().len(), 2);
    assert_eq!(block.host_transactions().len(), 2);
}

// Test checks that host cache simulation works when consuming all host balance
#[tokio::test]
async fn host_cache_between_bundles() {
    let builder = test_sim_env(Instant::now() + Duration::from_millis(200));

    let increment_call = incrementCall {};

    // Make RU and HOST transactions
    // In this test the ru txns are dummies. We're testing that the host cache
    // updates correctly.
    let ru_tx = signed_simple_call(
        &TEST_SIGNERS[0],
        TEST_USERS[1],
        &increment_call,
        U256::ZERO,
        0,
        TEST_SYS.ru_chain_id(),
    );
    let ru_tx_1 = signed_simple_call(
        &TEST_SIGNERS[1],
        TEST_USERS[1],
        &increment_call,
        U256::ZERO,
        0,
        TEST_SYS.ru_chain_id(),
    );

    let host_tx = signed_simple_call(
        &TEST_SIGNERS[2],
        COUNTER_TEST_ADDRESS,
        &increment_call,
        U256::ZERO,
        0,
        TEST_SYS.host_chain_id(),
    );
    let host_tx_1 = signed_simple_call(
        &TEST_SIGNERS[3],
        COUNTER_TEST_ADDRESS,
        &increment_call,
        U256::ZERO,
        0,
        TEST_SYS.host_chain_id(),
    );

    let bundle = simple_bundle(vec![ru_tx], vec![host_tx], 0);
    let bundle_1 = simple_bundle(vec![ru_tx_1], vec![host_tx_1], 0);

    builder.sim_items().add_bundle(bundle, 0).unwrap();
    builder.sim_items().add_bundle(bundle_1, 0).unwrap();

    let builder = builder.run_build().await;

    assert_eq!(
        builder.host_env().db().storage_ref(COUNTER_TEST_ADDRESS, COUNTER_SLOT).unwrap(),
        U256::from(2)
    );

    let block = builder.into_block();
    assert_eq!(block.transactions().len(), 2);
    assert_eq!(block.host_transactions().len(), 2);
}

/// Test for ConcurrentDB state isolation bug.
///
/// This test validates that bundles with multiple host transactions touching
/// overlapping contract state (same ERC-20) work correctly. The bug causes
/// the second host transaction to read stale state from the cache after
/// `accept_state()` is called on the first transaction.
///
/// Pattern that should work but fails with the bug:
/// - Host TX 1: approve(spender, amount) on WETH
/// - Host TX 2: fill() which calls transferFrom on WETH
/// - Rollup TX: order initiation
///
/// With the bug, the fill transaction reads stale allowance state and fails
/// with TRANSFER_FROM_FAILED because it doesn't see the approval from TX 1.
#[tokio::test]
async fn multi_host_tx_overlapping_state_isolation_bug() {
    let builder = test_sim_env(Instant::now() + Duration::from_millis(200));

    // Set up an order that will be filled
    let order = UnsignedOrder::default()
        .with_input(TEST_SYS.rollup().tokens().weth(), U256::from(1000))
        .with_output(
            TEST_SYS.host().tokens().weth(),
            U256::from(1000),
            TEST_USERS[5],
            TEST_SYS.host_chain_id() as u32,
        )
        .to_order();

    let fill_outputs = order
        .outputs
        .clone()
        .into_iter()
        .map(|mut output| {
            output.chainId = TEST_SYS.host_chain_id() as u32;
            output
        })
        .collect::<Vec<_>>();

    let order_call =
        initiateCall { deadline: U256::MAX, inputs: order.inputs, outputs: order.outputs };

    let fill_call = fillCall { outputs: fill_outputs };

    // Host TX 1: Approve the host orders contract to spend WETH
    // This touches the WETH contract's allowance storage slot for TEST_SIGNERS[1]
    // Even though the user already has max allowance, this tx still writes to
    // the allowance storage slot and exercises the state transition path.
    let host_approve_tx = signed_simple_call(
        &TEST_SIGNERS[1],
        TEST_SYS.host().tokens().weth(), // WETH token address
        &approveCall { spender: TEST_SYS.host_orders(), amount: U256::MAX },
        U256::ZERO,
        0, // nonce 0
        TEST_SYS.host_chain_id(),
    );

    // Host TX 2: Fill the order (calls transferFrom on WETH)
    // This also touches the WETH contract:
    // - Reads allowance (must see state from TX 1 or pre-existing)
    // - Writes to balance storage slots
    // With the cache bug, this transaction may read stale state.
    let host_fill_tx = signed_simple_call(
        &TEST_SIGNERS[1],
        TEST_SYS.host_orders(),
        &fill_call,
        U256::ZERO,
        1, // nonce 1
        TEST_SYS.host_chain_id(),
    );

    // Rollup TX: Initiate the order
    let ru_tx = signed_simple_call(
        &TEST_SIGNERS[0],
        TEST_SYS.ru_orders(),
        &order_call,
        U256::ZERO,
        0,
        TEST_SYS.ru_chain_id(),
    );

    // Create bundle with:
    // - 2 host transactions touching the same ERC-20 (approve + fill)
    // - 1 rollup transaction
    let bundle = simple_bundle(vec![ru_tx], vec![host_approve_tx, host_fill_tx], 0);

    builder.sim_items().add_bundle(bundle, 0).unwrap();

    let block = builder.build().await;

    // With the ConcurrentDB bug, this fails because:
    // 1. host_approve_tx executes and modifies WETH contract state
    // 2. accept_state() is called, but cache is not properly isolated
    // 3. host_fill_tx executes and reads STALE state from cache
    // 4. transferFrom fails because it doesn't see correct allowance/balance
    //
    // After the fix, both host transactions should succeed because
    // the second transaction correctly sees the state changes from the first.
    assert_eq!(block.transactions().len(), 1, "Rollup transaction should be included");
    assert_eq!(
        block.host_transactions().len(),
        2,
        "Both host transactions (approve + fill) should succeed when state is properly isolated"
    );
}