zksync_mempool 0.1.0

ZKsync mempool implementation
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
use std::{
    collections::{HashMap, HashSet},
    iter::FromIterator,
};

use zksync_types::{
    fee::Fee,
    helpers::unix_timestamp_ms,
    l1::{OpProcessingType, PriorityQueueType},
    l2::L2Tx,
    Address, Execute, ExecuteTransactionCommon, L1TxCommonData, Nonce, PriorityOpId, Transaction,
    H256, U256,
};

use crate::{mempool_store::MempoolStore, types::L2TxFilter};

#[test]
fn basic_flow() {
    let mut mempool = MempoolStore::new(PriorityOpId(0), 100);
    let account0 = Address::random();
    let account1 = Address::random();
    let transactions = vec![
        gen_l2_tx(account0, Nonce(0)),
        gen_l2_tx(account0, Nonce(1)),
        gen_l2_tx(account0, Nonce(2)),
        gen_l2_tx(account0, Nonce(3)),
        gen_l2_tx(account1, Nonce(1)),
    ];
    assert_eq!(mempool.next_transaction(&L2TxFilter::default()), None);
    mempool.insert(transactions, HashMap::new());
    assert_eq!(
        view(mempool.next_transaction(&L2TxFilter::default())),
        (account0, 0)
    );
    assert_eq!(
        view(mempool.next_transaction(&L2TxFilter::default())),
        (account0, 1)
    );
    assert_eq!(
        view(mempool.next_transaction(&L2TxFilter::default())),
        (account0, 2)
    );
    assert_eq!(
        view(mempool.next_transaction(&L2TxFilter::default())),
        (account0, 3)
    );
    assert_eq!(mempool.next_transaction(&L2TxFilter::default()), None);
    // unclog second account and insert more transactions
    mempool.insert(
        vec![gen_l2_tx(account1, Nonce(0)), gen_l2_tx(account0, Nonce(3))],
        HashMap::new(),
    );
    assert_eq!(
        view(mempool.next_transaction(&L2TxFilter::default())),
        (account1, 0)
    );
    assert_eq!(
        view(mempool.next_transaction(&L2TxFilter::default())),
        (account1, 1)
    );
    assert_eq!(mempool.next_transaction(&L2TxFilter::default()), None);
}

#[test]
fn missing_txns() {
    let mut mempool = MempoolStore::new(PriorityOpId(0), 100);
    let account = Address::random();
    let transactions = vec![
        gen_l2_tx(account, Nonce(6)),
        gen_l2_tx(account, Nonce(7)),
        gen_l2_tx(account, Nonce(9)),
    ];
    let mut nonces = HashMap::new();
    nonces.insert(account, Nonce(5));
    mempool.insert(transactions, nonces);
    assert_eq!(mempool.next_transaction(&L2TxFilter::default()), None);
    // missing transaction unclogs mempool
    mempool.insert(vec![gen_l2_tx(account, Nonce(5))], HashMap::new());
    assert_eq!(
        view(mempool.next_transaction(&L2TxFilter::default())),
        (account, 5)
    );
    assert_eq!(
        view(mempool.next_transaction(&L2TxFilter::default())),
        (account, 6)
    );
    assert_eq!(
        view(mempool.next_transaction(&L2TxFilter::default())),
        (account, 7)
    );

    // filling remaining gap
    mempool.insert(vec![gen_l2_tx(account, Nonce(8))], HashMap::new());
    assert_eq!(
        view(mempool.next_transaction(&L2TxFilter::default())),
        (account, 8)
    );
    assert_eq!(
        view(mempool.next_transaction(&L2TxFilter::default())),
        (account, 9)
    );
}

#[test]
fn prioritize_l1_txns() {
    let mut mempool = MempoolStore::new(PriorityOpId(0), 100);
    let account = Address::random();
    let transactions = vec![
        gen_l2_tx(account, Nonce(0)),
        gen_l2_tx(account, Nonce(1)),
        gen_l1_tx(PriorityOpId(0)),
    ];
    mempool.insert(transactions, HashMap::new());
    assert!(mempool
        .next_transaction(&L2TxFilter::default())
        .unwrap()
        .is_l1())
}

#[test]
fn l1_txns_priority_id() {
    let mut mempool = MempoolStore::new(PriorityOpId(0), 100);
    let transactions = vec![
        gen_l1_tx(PriorityOpId(1)),
        gen_l1_tx(PriorityOpId(2)),
        gen_l1_tx(PriorityOpId(3)),
    ];
    mempool.insert(transactions, HashMap::new());
    assert!(mempool.next_transaction(&L2TxFilter::default()).is_none());
    mempool.insert(vec![gen_l1_tx(PriorityOpId(0))], HashMap::new());
    for idx in 0..4 {
        let data = mempool
            .next_transaction(&L2TxFilter::default())
            .unwrap()
            .common_data;
        match data {
            ExecuteTransactionCommon::L1(data) => {
                assert_eq!(data.serial_id, PriorityOpId(idx as u64));
            }
            _ => unreachable!("expected L1 transaction"),
        }
    }
}

#[test]
fn rejected_tx() {
    let mut mempool = MempoolStore::new(PriorityOpId(0), 100);
    let account = Address::random();
    let transactions = vec![
        gen_l2_tx(account, Nonce(0)),
        gen_l2_tx(account, Nonce(1)),
        gen_l2_tx(account, Nonce(2)),
        gen_l2_tx(account, Nonce(3)),
        gen_l2_tx(account, Nonce(5)),
    ];
    mempool.insert(transactions, HashMap::new());
    assert_eq!(
        view(mempool.next_transaction(&L2TxFilter::default())),
        (account, 0)
    );
    assert_eq!(
        view(mempool.next_transaction(&L2TxFilter::default())),
        (account, 1)
    );

    mempool.rollback(&gen_l2_tx(account, Nonce(1)));
    assert!(mempool.next_transaction(&L2TxFilter::default()).is_none());

    // replace transaction and unblock account
    mempool.insert(vec![gen_l2_tx(account, Nonce(1))], HashMap::new());
    assert_eq!(
        view(mempool.next_transaction(&L2TxFilter::default())),
        (account, 1)
    );
    assert_eq!(
        view(mempool.next_transaction(&L2TxFilter::default())),
        (account, 2)
    );
    assert_eq!(
        view(mempool.next_transaction(&L2TxFilter::default())),
        (account, 3)
    );
}

#[test]
fn replace_tx() {
    let mut mempool = MempoolStore::new(PriorityOpId(0), 100);
    let account = Address::random();
    mempool.insert(vec![gen_l2_tx(account, Nonce(0))], HashMap::new());
    // replace it
    mempool.insert(
        vec![gen_l2_tx_with_timestamp(
            account,
            Nonce(0),
            unix_timestamp_ms() + 10,
        )],
        HashMap::new(),
    );
    assert!(mempool.next_transaction(&L2TxFilter::default()).is_some());
    assert!(mempool.next_transaction(&L2TxFilter::default()).is_none());
}

#[test]
fn two_ready_txs() {
    let mut mempool = MempoolStore::new(PriorityOpId(0), 100);
    let account0 = Address::random();
    let account1 = Address::random();
    let transactions = vec![gen_l2_tx(account0, Nonce(0)), gen_l2_tx(account1, Nonce(0))];
    mempool.insert(transactions, HashMap::new());
    assert_eq!(
        HashSet::<(_, _)>::from_iter(vec![
            view(mempool.next_transaction(&L2TxFilter::default())),
            view(mempool.next_transaction(&L2TxFilter::default()))
        ]),
        HashSet::<(_, _)>::from_iter(vec![(account0, 0), (account1, 0)]),
    );
}

#[test]
fn mempool_size() {
    let mut mempool = MempoolStore::new(PriorityOpId(0), 100);
    let account0 = Address::random();
    let account1 = Address::random();
    let transactions = vec![
        gen_l2_tx(account0, Nonce(0)),
        gen_l2_tx(account0, Nonce(1)),
        gen_l2_tx(account0, Nonce(2)),
        gen_l2_tx(account0, Nonce(3)),
        gen_l2_tx(account1, Nonce(1)),
    ];
    mempool.insert(transactions, HashMap::new());
    assert_eq!(mempool.stats().l2_transaction_count, 5);
    // replacement
    mempool.insert(vec![gen_l2_tx(account0, Nonce(2))], HashMap::new());
    assert_eq!(mempool.stats().l2_transaction_count, 5);
    // load next
    mempool.next_transaction(&L2TxFilter::default());
    mempool.next_transaction(&L2TxFilter::default());
    assert_eq!(mempool.stats().l2_transaction_count, 3);
}

/// Checks whether filtering transactions based on their fee works as expected.
#[test]
fn filtering() {
    // Filter to find transactions with non-zero `gas_per_pubdata` values.
    let filter_non_zero = L2TxFilter {
        fee_input: Default::default(),
        fee_per_gas: 0u64,
        gas_per_pubdata: 1u32,
    };
    // No-op filter that fetches any transaction.
    let filter_zero = L2TxFilter {
        fee_input: Default::default(),
        fee_per_gas: 0u64,
        gas_per_pubdata: 0u32,
    };

    let mut mempool = MempoolStore::new(PriorityOpId(0), 100);
    let account0 = Address::random();
    let account1 = Address::random();

    // First account will have two transactions: one with too low pubdata price and one with the right value.
    // Second account will have just one transaction with the right value.
    mempool.insert(
        gen_transactions_for_filtering(vec![
            (account0, Nonce(0), unix_timestamp_ms(), 0),
            (account0, Nonce(1), unix_timestamp_ms(), 1),
            (account1, Nonce(0), unix_timestamp_ms() - 10, 1),
        ]),
        HashMap::new(),
    );

    // First transaction from first account doesn't match the filter, so we should get the transaction
    // from the second account.
    assert_eq!(
        view(mempool.next_transaction(&filter_non_zero)),
        (account1, 0)
    );
    // No more transactions can be executed with the non-zero filter.
    assert_eq!(mempool.next_transaction(&filter_non_zero), None);

    // Now we apply zero filter and get the transaction as expected.
    assert_eq!(view(mempool.next_transaction(&filter_zero)), (account0, 0));
    assert_eq!(view(mempool.next_transaction(&filter_zero)), (account0, 1));
    assert_eq!(mempool.next_transaction(&filter_zero), None);
}

#[test]
fn stashed_accounts() {
    let filter_non_zero = L2TxFilter {
        fee_input: Default::default(),
        fee_per_gas: 0u64,
        gas_per_pubdata: 1u32,
    };
    // No-op filter that fetches any transaction.
    let filter_zero = L2TxFilter {
        fee_input: Default::default(),
        fee_per_gas: 0u64,
        gas_per_pubdata: 0u32,
    };
    let mut mempool = MempoolStore::new(PriorityOpId(0), 100);
    let account0 = Address::random();
    let account1 = Address::random();

    mempool.insert(
        gen_transactions_for_filtering(vec![
            (account0, Nonce(0), unix_timestamp_ms(), 0),
            (account0, Nonce(1), unix_timestamp_ms(), 1),
            (account1, Nonce(0), unix_timestamp_ms() + 10, 1),
        ]),
        HashMap::new(),
    );
    assert!(mempool.get_mempool_info().stashed_accounts.is_empty());
    assert_eq!(
        view(mempool.next_transaction(&filter_non_zero)),
        (account1, 0)
    );
    assert_eq!(mempool.get_mempool_info().stashed_accounts, vec![account0]);
    assert!(mempool.next_transaction(&filter_zero).is_none());
}

#[test]
fn mempool_capacity() {
    let mut mempool = MempoolStore::new(PriorityOpId(0), 5);
    let account0 = Address::random();
    let account1 = Address::random();
    let account2 = Address::random();
    let transactions = vec![
        gen_l2_tx(account0, Nonce(0)),
        gen_l2_tx(account0, Nonce(1)),
        gen_l2_tx(account0, Nonce(2)),
        gen_l2_tx(account1, Nonce(1)),
        gen_l2_tx(account2, Nonce(1)),
    ];
    mempool.insert(transactions, HashMap::new());
    // the mempool is full. Accounts with non-sequential nonces got stashed
    assert_eq!(
        HashSet::<_>::from_iter(mempool.get_mempool_info().purged_accounts),
        HashSet::<_>::from_iter(vec![account1, account2]),
    );
    // verify that existing good-to-go transactions and new ones got picked
    mempool.insert(
        vec![gen_l2_tx_with_timestamp(
            account1,
            Nonce(0),
            unix_timestamp_ms() + 1,
        )],
        HashMap::new(),
    );
    for _ in 0..3 {
        assert_eq!(
            mempool
                .next_transaction(&L2TxFilter::default())
                .unwrap()
                .initiator_account(),
            account0
        );
    }
    assert_eq!(
        mempool
            .next_transaction(&L2TxFilter::default())
            .unwrap()
            .initiator_account(),
        account1
    );
}

fn gen_l2_tx(address: Address, nonce: Nonce) -> Transaction {
    gen_l2_tx_with_timestamp(address, nonce, unix_timestamp_ms())
}

fn gen_l2_tx_with_timestamp(address: Address, nonce: Nonce, received_at_ms: u64) -> Transaction {
    let mut txn = L2Tx::new(
        Address::default(),
        Vec::new(),
        nonce,
        Fee::default(),
        address,
        U256::zero(),
        vec![],
        Default::default(),
    );
    txn.received_timestamp_ms = received_at_ms;
    txn.into()
}

fn gen_l1_tx(priority_id: PriorityOpId) -> Transaction {
    let execute = Execute {
        contract_address: Address::repeat_byte(0x11),
        calldata: vec![1, 2, 3],
        factory_deps: vec![],
        value: U256::zero(),
    };
    let op_data = L1TxCommonData {
        sender: Address::random(),
        serial_id: priority_id,
        layer_2_tip_fee: U256::zero(),
        full_fee: U256::zero(),
        gas_limit: U256::zero(),
        max_fee_per_gas: U256::zero(),
        gas_per_pubdata_limit: U256::one(),
        op_processing_type: OpProcessingType::Common,
        priority_queue_type: PriorityQueueType::Deque,
        eth_block: 0,
        canonical_tx_hash: H256::zero(),
        to_mint: U256::zero(),
        refund_recipient: Address::random(),
    };

    Transaction {
        common_data: ExecuteTransactionCommon::L1(op_data),
        execute,
        received_timestamp_ms: 0,
        raw_bytes: None,
    }
}

fn view(transaction: Option<Transaction>) -> (Address, u32) {
    let tx = transaction.unwrap();
    (tx.initiator_account(), tx.nonce().unwrap().0)
}

fn gen_transactions_for_filtering(input: Vec<(Address, Nonce, u64, u32)>) -> Vec<Transaction> {
    // Helper function to conveniently set `max_gas_per_pubdata_byte`.
    fn set_max_gas_per_pubdata_byte(tx: &mut Transaction, value: u32) {
        match &mut tx.common_data {
            ExecuteTransactionCommon::L2(data) => {
                data.fee.gas_per_pubdata_limit = U256::from(value)
            }
            _ => unreachable!(),
        };
    }
    input
        .into_iter()
        .map(|(account, nonce, tst, max_gas_per_pubdata)| {
            let mut tx = gen_l2_tx_with_timestamp(account, nonce, tst);
            set_max_gas_per_pubdata_byte(&mut tx, max_gas_per_pubdata);
            tx
        })
        .collect()
}