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
//! Mock Transaction definition and builder related methods.

use super::{MOCK_ACCOUNTS, MOCK_CHAIN_ID, MOCK_GASPRICE};
use eth_types::{
    geth_types::Transaction as GethTransaction, word, AccessList, Address, Bytes, Hash,
    Transaction, Word, U64,
};
use ethers_core::{
    rand::{CryptoRng, RngCore},
    types::{OtherFields, TransactionRequest},
};
use ethers_signers::{LocalWallet, Signer};
use lazy_static::lazy_static;
use rand::SeedableRng;
use rand_chacha::ChaCha20Rng;

lazy_static! {
    /// 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 ref CORRECT_MOCK_TXS: Vec<MockTransaction> = {
        let mut rng = ChaCha20Rng::seed_from_u64(2u64);

        vec![MockTransaction::default()
            .from(AddrOrWallet::random(&mut rng))
            .to(MOCK_ACCOUNTS[0])
            .nonce(0x103u64)
            .value(word!("0x3e8"))
            .gas_price(word!("0x4d2"))
            .input(Bytes::from(b"hello"))
            .build(),
            MockTransaction::default()
            .from(AddrOrWallet::random(&mut rng))
            .to(MOCK_ACCOUNTS[1])
            .nonce(0x104u64)
            .value(word!("0x3e8"))
            .gas_price(word!("0x4d2"))
            .input(Bytes::from(b"hello"))
            .build(),
            MockTransaction::default()
            .from(AddrOrWallet::random(&mut rng))
            .to(MOCK_ACCOUNTS[2])
            .nonce(0x105u64)
            .value(word!("0x3e8"))
            .gas_price(word!("0x4d2"))
            .input(Bytes::from(b"hello"))
            .build(),
            MockTransaction::default()
            .from(AddrOrWallet::random(&mut rng))
            .to(MOCK_ACCOUNTS[0])
            .nonce(0x106u64)
            .value(word!("0x3e8"))
            .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: Option<u64>,
    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: 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: Option<Word>,
    pub max_fee_per_gas: Option<Word>,
    pub chain_id: Word,
    pub invalid: bool,
}

impl Default for MockTransaction {
    fn default() -> Self {
        MockTransaction {
            hash: None,
            nonce: None,
            block_hash: Hash::zero(),
            block_number: U64::zero(),
            transaction_index: U64::zero(),
            from: AddrOrWallet::Addr(MOCK_ACCOUNTS[0]),
            to: None,
            value: Word::zero(),
            gas_price: *MOCK_GASPRICE,
            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: None,
            max_fee_per_gas: None,
            chain_id: *MOCK_CHAIN_ID,
            invalid: false,
        }
    }
}

impl From<MockTransaction> for Transaction {
    fn from(mock: MockTransaction) -> Self {
        Transaction {
            hash: mock.hash.unwrap_or_default(),
            nonce: mock.nonce.unwrap_or_default().into(),
            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: Some(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: mock.max_priority_fee_per_gas,
            max_fee_per_gas: mock.max_fee_per_gas,
            chain_id: Some(mock.chain_id),
            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. Overridden in TestContext.
    pub(crate) fn nonce(&mut self, nonce: u64) -> &mut Self {
        self.nonce = Some(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. Overridden in TestContext.
    pub(crate) 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 = 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 = Some(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 = Some(max_fee_per_gas);
        self
    }

    /// Set chain_id field for the MockTransaction.
    pub(crate) fn chain_id(&mut self, chain_id: Word) -> &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 {
        let tx = TransactionRequest::new()
            .from(self.from.address())
            .to(self.to.clone().unwrap_or_default().address())
            .nonce(self.nonce.unwrap_or_default())
            .value(self.value)
            .data(self.input.clone())
            .gas(self.gas)
            .gas_price(self.gas_price)
            .chain_id(self.chain_id.low_u64());

        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 sig = self
                        .from
                        .as_wallet()
                        .with_chain_id(self.chain_id.low_u64())
                        .sign_transaction_sync(&tx.into())
                        .unwrap();
                    // Set sig parameters
                    self.sig_data((sig.v, sig.r, sig.s));
                }
            }
            (Some(_), Some(_), Some(_)) => (),
            _ => 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());
            self.hash(tmp_tx.hash());
        }

        self.to_owned()
    }

    /// Mark the transaction as invalid.
    pub fn invalid(&mut self) -> &mut Self {
        self.invalid = true;
        self
    }

    /// Force the nonce field for the MockTransaction.
    /// Can only be used for invalid transactions.
    pub fn set_nonce(&mut self, nonce: u64) -> &mut Self {
        assert!(self.invalid);
        self.nonce = Some(nonce);
        self
    }
}