forked from privacy-scaling-explorations/zkevm-circuits
-
Notifications
You must be signed in to change notification settings - Fork 391
/
Copy pathtransaction.rs
519 lines (463 loc) · 17.5 KB
/
transaction.rs
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
//! Mock Transaction definition and builder related methods.
use super::{MOCK_ACCOUNTS, MOCK_CHAIN_ID};
use eth_types::{
geth_types::Transaction as GethTransaction, word, AccessList, Address, Bytes, Hash,
Transaction, Word, U64,
};
use ethers_core::{
rand::{CryptoRng, RngCore},
types::{
Eip1559TransactionRequest, Eip2930TransactionRequest, OtherFields, TransactionRequest,
},
};
use ethers_signers::{LocalWallet, Signer};
use rand::SeedableRng;
use rand_chacha::{rand_core::OsRng, ChaCha20Rng};
use std::sync::LazyLock;
/// Collection of correctly hashed and signed Transactions which can be used to test circuits or
/// opcodes that have to check integrity of the Tx itself. Some of the parameters of the Tx are
/// hardcoded such as `nonce`, `value`, `gas_price` etc...
pub static CORRECT_MOCK_TXS: LazyLock<Vec<MockTransaction>> = LazyLock::new(|| {
let mut rng = ChaCha20Rng::seed_from_u64(2u64);
vec![
MockTransaction::default()
.transaction_idx(1u64)
.from(AddrOrWallet::random(&mut rng))
.to(MOCK_ACCOUNTS[0])
.nonce(word!("0x103"))
.value(word!("0x3e8"))
.gas_price(word!("0x4d2"))
.input(vec![1, 2, 3, 4, 5, 0, 6, 7, 8, 9].into()) // call data gas cost of 0 is 4
.build(),
MockTransaction::default()
.transaction_idx(2u64)
.from(AddrOrWallet::random(&mut rng))
.to(MOCK_ACCOUNTS[1])
.nonce(word!("0x104"))
.value(word!("0x3e8"))
.gas_price(word!("0x4d2"))
.input(Bytes::from(b"hello"))
.build(),
MockTransaction::default()
.transaction_idx(3u64)
.from(AddrOrWallet::random(&mut rng))
.to(MOCK_ACCOUNTS[2])
.nonce(word!("0x105"))
.value(word!("0x3e8"))
.gas_price(word!("0x4d2"))
.input(Bytes::from(b"hello"))
.build(),
MockTransaction::default()
.transaction_idx(4u64)
.from(AddrOrWallet::random(&mut rng))
.to(MOCK_ACCOUNTS[3])
.nonce(word!("0x106"))
.value(word!("0x3e8"))
.gas_price(word!("0x4d2"))
.input(Bytes::from(b""))
.build(),
MockTransaction::default()
.transaction_idx(5u64)
.from(AddrOrWallet::random(&mut rng))
.to(MOCK_ACCOUNTS[4])
.nonce(word!("0x0"))
.value(word!("0x0"))
.gas_price(word!("0x4d2"))
.input(Bytes::from(b"hello"))
.build(),
MockTransaction::default()
.transaction_idx(6u64)
.from(AddrOrWallet::random(&mut rng))
.to(AddrOrWallet::Addr(Address::zero()))
.nonce(word!("0x0"))
.value(word!("0x0"))
.gas_price(word!("0x4d2"))
.input(Bytes::from(b"hello"))
.build(),
]
});
#[derive(Debug, Clone)]
pub enum AddrOrWallet {
Addr(Address),
Wallet(LocalWallet),
}
impl Default for AddrOrWallet {
fn default() -> Self {
AddrOrWallet::Addr(Address::default())
}
}
impl From<Address> for AddrOrWallet {
fn from(addr: Address) -> Self {
AddrOrWallet::Addr(addr)
}
}
impl From<LocalWallet> for AddrOrWallet {
fn from(wallet: LocalWallet) -> Self {
AddrOrWallet::Wallet(wallet)
}
}
impl AddrOrWallet {
/// Generates a random Wallet from a random secpk256 keypair
pub fn random<R: RngCore + CryptoRng>(rng: &mut R) -> Self {
AddrOrWallet::Wallet(LocalWallet::new(rng))
}
}
impl AddrOrWallet {
/// Returns the underlying address associated to the `AddrOrWallet` enum.
pub fn address(&self) -> Address {
match self {
Self::Addr(addr) => *addr,
Self::Wallet(wallet) => wallet.address(),
}
}
/// Returns true if the enum variant of `self` corresponds to a
/// [`LocalWallet`] structure and not simply and [`Address`].
const fn is_wallet(&self) -> bool {
matches!(self, Self::Wallet(_))
}
/// Returns the underlying wallet stored in the enum.
/// # Panics
/// This function will panic if the enum does not contain a [`LocalWallet`]
/// and instead contains the [`Address`] variant.
pub fn as_wallet(&self) -> LocalWallet {
match self {
Self::Wallet(wallet) => wallet.to_owned(),
_ => panic!("Broken AddrOrWallet invariant"),
}
}
}
#[derive(Debug, Clone)]
/// Mock structure which represents a Transaction and can be used for tests.
/// It contains all the builder-pattern methods required to be able to specify
/// any of it's details.
pub struct MockTransaction {
pub hash: Option<Hash>,
pub nonce: Word,
pub block_hash: Hash,
pub block_number: U64,
pub transaction_index: U64,
pub from: AddrOrWallet,
pub to: Option<AddrOrWallet>,
pub value: Word,
pub gas_price: Option<Word>,
pub gas: Word,
pub input: Bytes,
pub v: Option<U64>,
pub r: Option<Word>,
pub s: Option<Word>,
pub transaction_type: U64,
pub access_list: AccessList,
pub max_priority_fee_per_gas: Word,
pub max_fee_per_gas: Word,
pub chain_id: u64,
}
impl Default for MockTransaction {
fn default() -> Self {
MockTransaction {
hash: None,
nonce: Word::zero(),
block_hash: Hash::zero(),
block_number: U64::zero(),
transaction_index: U64::zero(),
//from: AddrOrWallet::Addr(MOCK_ACCOUNTS[0]),
from: AddrOrWallet::random(&mut OsRng),
to: None,
value: Word::zero(),
gas_price: None,
gas: Word::from(1_000_000u64),
input: Bytes::default(),
v: None,
r: None,
s: None,
transaction_type: U64::zero(),
access_list: AccessList::default(),
max_priority_fee_per_gas: Word::zero(),
max_fee_per_gas: Word::zero(),
chain_id: MOCK_CHAIN_ID,
}
}
}
impl From<MockTransaction> for Transaction {
fn from(mock: MockTransaction) -> Self {
Transaction {
hash: mock.hash.unwrap_or_default(),
nonce: mock.nonce,
block_hash: Some(mock.block_hash),
block_number: Some(mock.block_number),
transaction_index: Some(mock.transaction_index),
from: mock.from.address(),
to: mock.to.map(|addr| addr.address()),
value: mock.value,
gas_price: mock.gas_price,
gas: mock.gas,
input: mock.input,
v: mock.v.unwrap_or_default(),
r: mock.r.unwrap_or_default(),
s: mock.s.unwrap_or_default(),
transaction_type: Some(mock.transaction_type),
access_list: Some(mock.access_list),
max_priority_fee_per_gas: Some(mock.max_priority_fee_per_gas),
max_fee_per_gas: Some(mock.max_fee_per_gas),
chain_id: Some(mock.chain_id.into()),
other: OtherFields::default(),
}
}
}
impl From<MockTransaction> for GethTransaction {
fn from(mock: MockTransaction) -> Self {
GethTransaction::from(&Transaction::from(mock))
}
}
impl MockTransaction {
/// Tx Hash computed based on the fields of the Tx by
/// default unless `Some(hash)` is specified on build process.
pub fn hash(&mut self, hash: Hash) -> &mut Self {
self.hash = Some(hash);
self
}
/// Set nonce field for the MockTransaction.
pub fn nonce(&mut self, nonce: Word) -> &mut Self {
self.nonce = nonce;
self
}
/// Set block_hash field for the MockTransaction.
pub fn block_hash(&mut self, block_hash: Hash) -> &mut Self {
self.block_hash = block_hash;
self
}
/// Set block_number field for the MockTransaction.
pub fn block_number(&mut self, block_number: u64) -> &mut Self {
self.block_number = U64::from(block_number);
self
}
/// Set transaction_idx field for the MockTransaction.
pub fn transaction_idx(&mut self, transaction_idx: u64) -> &mut Self {
self.transaction_index = U64::from(transaction_idx);
self
}
/// Set from field for the MockTransaction.
pub fn from<T: Into<AddrOrWallet>>(&mut self, from: T) -> &mut Self {
self.from = from.into();
self
}
/// Set to field for the MockTransaction.
pub fn to<T: Into<AddrOrWallet>>(&mut self, to: T) -> &mut Self {
self.to = Some(to.into());
self
}
/// Set value field for the MockTransaction.
pub fn value(&mut self, value: Word) -> &mut Self {
self.value = value;
self
}
/// Set gas_price field for the MockTransaction.
pub fn gas_price(&mut self, gas_price: Word) -> &mut Self {
self.gas_price = Some(gas_price);
self
}
/// Set gas field for the MockTransaction.
pub fn gas(&mut self, gas: Word) -> &mut Self {
self.gas = gas;
self
}
/// Set input field for the MockTransaction.
pub fn input(&mut self, input: Bytes) -> &mut Self {
self.input = input;
self
}
/// Set sig_data field for the MockTransaction.
pub fn sig_data(&mut self, data: (u64, Word, Word)) -> &mut Self {
self.v = Some(U64::from(data.0));
self.r = Some(data.1);
self.s = Some(data.2);
self
}
/// Set transaction_type field for the MockTransaction.
pub fn transaction_type(&mut self, transaction_type: u64) -> &mut Self {
self.transaction_type = U64::from(transaction_type);
self
}
/// Set access_list field for the MockTransaction.
pub fn access_list(&mut self, access_list: AccessList) -> &mut Self {
self.access_list = access_list;
self
}
/// Set max_priority_fee_per_gas field for the MockTransaction.
pub fn max_priority_fee_per_gas(&mut self, max_priority_fee_per_gas: Word) -> &mut Self {
self.max_priority_fee_per_gas = max_priority_fee_per_gas;
self
}
/// Set max_fee_per_gas field for the MockTransaction.
pub fn max_fee_per_gas(&mut self, max_fee_per_gas: Word) -> &mut Self {
self.max_fee_per_gas = max_fee_per_gas;
self
}
/// Set chain_id field for the MockTransaction.
pub fn chain_id(&mut self, chain_id: u64) -> &mut Self {
self.chain_id = chain_id;
self
}
/// Consumes the mutable ref to the MockTransaction returning the structure
/// by value.
pub fn build(&mut self) -> Self {
if self.transaction_type == U64::from(2) {
return self.build_1559();
} else if self.transaction_type == U64::from(1) {
return self.build_2930();
}
let tx = TransactionRequest::new()
.from(self.from.address())
.nonce(self.nonce)
.value(self.value)
.data(self.input.clone())
.gas(self.gas)
// Note: even pre-eip155 type transaction doesn't have chain_id field, here having chain_id won't
// result in negative effects, because eventually geth_type::Transaction decide the tx type by TxType::get_tx_type(tx)
// then trace.go will treat it as correct pre-eip155 type transaction. the additional chain_id
// is not used finally.
.chain_id(self.chain_id);
let tx = if let Some(gas_price) = self.gas_price {
tx.gas_price(gas_price)
} else {
tx
};
let tx = if let Some(to_addr) = self.to.clone() {
tx.to(to_addr.address())
} else {
tx
};
match (self.v, self.r, self.s) {
(Some(_), Some(_), Some(_)) => {
// already have entire signature data, won't do anything.
}
(None, None, None) => {
// Compute sig params and set them in case we have a wallet as `from` attr.
if self.from.is_wallet() && self.hash.is_none() {
let sig = self
.from
.as_wallet()
.with_chain_id(self.chain_id)
.sign_transaction_sync(&tx.into()) // sign for legacy tx type in ethers-rs.
.expect("sign mock tx");
// Set sig parameters
self.sig_data((sig.v, sig.r, sig.s));
}
}
_ => panic!("Either all or none of the SigData params have to be set"),
}
// Compute tx hash in case is not already set
if self.hash.is_none() {
let tmp_tx = Transaction::from(self.to_owned());
// FIXME: Note that tmp_tx does not have sigs if self.from.is_wallet() = false.
// This means tmp_tx.hash() is not correct.
self.hash(tmp_tx.hash());
}
self.to_owned()
}
/// build eip 1559 type tx
pub fn build_1559(&mut self) -> Self {
let tx = Eip1559TransactionRequest::new()
.from(self.from.address())
.nonce(self.nonce)
.value(self.value)
.data(self.input.clone())
.gas(self.gas)
.chain_id(self.chain_id)
.max_priority_fee_per_gas(self.max_priority_fee_per_gas)
.max_fee_per_gas(self.max_fee_per_gas)
.access_list(self.access_list.clone());
let tx = if let Some(to_addr) = self.to.clone() {
tx.to(to_addr.address())
} else {
tx
};
match (self.v, self.r, self.s) {
(None, None, None) => {
// Compute sig params and set them in case we have a wallet as `from` attr.
if self.from.is_wallet() && self.hash.is_none() {
let mut sig = self
.from
.as_wallet()
.with_chain_id(self.chain_id)
.sign_transaction_sync(&tx.into())
.expect("sign mock 1559 tx");
// helper `sign_transaction_sync` in ethers-rs lib does not handle correctly
// about v for non legacy tx, here correct it for 1559 type.
sig.v = Self::normalize_v(sig.v, self.chain_id); // convert v to [0, 1]
self.sig_data((sig.v, sig.r, sig.s));
} else {
#[cfg(feature = "scroll")]
panic!("1559 type tx must have signature data, otherwise will be treated as L1Msg type in trace.go of l2geth");
}
}
_ => panic!("Either all or none of the SigData params have to be set"),
}
// Compute tx hash in case is not already set
if self.hash.is_none() {
let tmp_tx = Transaction::from(self.to_owned());
// FIXME: Note that tmp_tx does not have sigs if self.from.is_wallet() = false.
// This means tmp_tx.hash() is not correct.
self.hash(tmp_tx.hash());
}
self.to_owned()
}
/// build eip 2930 type tx
pub fn build_2930(&mut self) -> Self {
let legacy_tx = TransactionRequest::new()
.from(self.from.address())
.nonce(self.nonce)
.value(self.value)
.data(self.input.clone())
.gas(self.gas)
.chain_id(self.chain_id);
let legacy_tx = if let Some(gas_price) = self.gas_price {
legacy_tx.gas_price(gas_price)
} else {
legacy_tx
};
let legacy_tx = if let Some(to_addr) = self.to.clone() {
legacy_tx.to(to_addr.address())
} else {
legacy_tx
};
let tx = Eip2930TransactionRequest::new(legacy_tx, self.access_list.clone());
match (self.v, self.r, self.s) {
(None, None, None) => {
// Compute sig params and set them in case we have a wallet as `from` attr.
if self.from.is_wallet() && self.hash.is_none() {
let mut sig = self
.from
.as_wallet()
.with_chain_id(self.chain_id)
.sign_transaction_sync(&tx.into())
.expect("sign mock eip 2930 tx");
// helper `sign_transaction_sync` in ethers-rs lib does not handle correctly
// about v for non legacy tx, here correct it for 2930 type.
sig.v = Self::normalize_v(sig.v, self.chain_id); // convert v to [0, 1]
self.sig_data((sig.v, sig.r, sig.s));
} else {
#[cfg(feature = "scroll")]
panic!("2930 type tx must have signature data, otherwise will be treated as L1Msg type in trace.go of l2geth");
}
}
_ => panic!("Either all or none of the SigData params have to be set"),
}
// Compute tx hash in case is not already set
if self.hash.is_none() {
let tmp_tx = Transaction::from(self.to_owned());
// FIXME: Note that tmp_tx does not have sigs if self.from.is_wallet() = false.
// This means tmp_tx.hash() is not correct.
self.hash(tmp_tx.hash());
}
self.to_owned()
}
// helper `sign_transaction_sync` in ethers-rs lib compute V using legacy tx pattern(V =
// recover_id + 2 * chain_id + 35), this method converts above V value to origin recover_id.
pub(crate) fn normalize_v(v: u64, chain_id: u64) -> u64 {
if v > 1 {
v - chain_id * 2 - 35
} else {
v
}
}
}