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
use anyhow::{anyhow, bail, Context};
use eth_types::{
    geth_types::{Account, TxType},
    Address, Bytes, Word, H256, U256, U64,
};
use ethers_core::{
    k256::ecdsa::SigningKey,
    types::{
        transaction::{eip2718::TypedTransaction, eip2930::AccessList},
        Eip1559TransactionRequest, TransactionRequest,
    },
    utils::secret_key_to_address,
};
use std::{
    collections::{BTreeMap, HashMap},
    str::FromStr,
};

/// <https://github.com/ethereum/tests/pull/857> "set default gasPrice to 10"
pub const DEFAULT_BASE_FEE: u32 = 10;

const ETH_CHAIN_ID: u64 = 1;

#[derive(PartialEq, Eq, Debug, Clone)]
pub struct Env {
    pub current_base_fee: U256,
    pub current_coinbase: Address,
    pub current_difficulty: U256,
    pub current_gas_limit: u64,
    pub current_number: u64,
    pub current_timestamp: u64,
    pub previous_hash: H256,
}

#[derive(PartialEq, Eq, Default, Debug, Clone)]
pub struct AccountMatch {
    pub address: Address,
    pub balance: Option<U256>,
    pub code: Option<Bytes>,
    pub nonce: Option<u64>,
    pub storage: HashMap<U256, U256>,
}

impl TryInto<Account> for AccountMatch {
    type Error = anyhow::Error;
    fn try_into(self) -> Result<Account, Self::Error> {
        Ok(Account {
            address: self.address,
            balance: self.balance.context("balance")?,
            code: self.code.context("code")?,
            nonce: self.nonce.context("nonce")?.into(),
            storage: self.storage,
        })
    }
}

pub type StateTestResult = HashMap<Address, AccountMatch>;

#[derive(PartialEq, Clone, Eq, Debug)]
pub struct StateTest {
    pub path: String,
    pub id: String,
    pub env: Env,
    pub secret_key: Bytes,
    pub from: Address,
    pub to: Option<Address>,
    pub gas_limit: u64,
    pub max_priority_fee_per_gas: Option<U256>,
    pub max_fee_per_gas: Option<U256>,
    pub gas_price: U256,
    pub nonce: u64,
    pub value: U256,
    pub data: Bytes,
    pub access_list: Option<AccessList>,
    pub pre: BTreeMap<Address, Account>,
    pub result: StateTestResult,
    pub exception: bool,
}

impl std::fmt::Display for StateTest {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        let max_len = 100;

        let format = |v: &str, k: &str| -> String {
            let mut text = String::new();
            let k = if k.is_empty() {
                k.to_string()
            } else {
                format!("{k} :")
            };
            let max_len = max_len - k.len();
            let v = v.chars().collect::<Vec<_>>();
            for i in 0..=v.len() / max_len {
                if i == 0 && !k.is_empty() {
                    text.push_str(&k);
                } else {
                    let padding: String = " ".repeat(k.len());
                    text.push_str(&padding);
                }
                text.push_str(
                    &v[i * max_len..std::cmp::min((i + 1) * max_len, v.len())]
                        .iter()
                        .collect::<String>(),
                );
                text.push('\n');
            }
            text
        };

        use prettytable::Table;
        let mut table = Table::new();
        if !self.id.is_empty() {
            table.add_row(row!["id", self.id]);
        }
        if !self.path.is_empty() {
            table.add_row(row!["path", self.path]);
        }
        table.add_row(row!["coinbase", format!("{:?}", self.env.current_coinbase)]);

        table.add_row(row![
            "difficulty",
            format!("{}", self.env.current_difficulty)
        ]);
        table.add_row(row!["number", format!("{}", self.env.current_number)]);
        table.add_row(row!["timestamp", format!("{}", self.env.current_timestamp)]);
        table.add_row(row!["prev_hash", format!("{:?}", self.env.previous_hash)]);
        table.add_row(row!["sk", hex::encode(&self.secret_key)]);
        table.add_row(row!["from", format!("{:?}", self.from)]);
        table.add_row(row!["to", format!("{:?}", self.to)]);
        table.add_row(row!["gas_limit", format!("{}", self.gas_limit)]);
        table.add_row(row![
            "max_priority_fee_per_gas",
            format!("{:?}", self.max_priority_fee_per_gas)
        ]);
        table.add_row(row![
            "max_fee_per_gas",
            format!("{:?}", self.max_fee_per_gas)
        ]);
        table.add_row(row!["gas_price", format!("{}", self.gas_price)]);
        table.add_row(row!["nonce", format!("{}", self.nonce)]);
        table.add_row(row!["value", format!("{}", self.value)]);
        table.add_row(row!["data", format(&hex::encode(&self.data), "")]);
        table.add_row(row!["access_list", format!("{:?}", self.access_list)]);
        table.add_row(row!["exception", self.exception]);

        let mut addrs: Vec<_> = self.pre.keys().collect();
        addrs.extend(self.result.keys());
        addrs.sort();
        addrs.dedup();
        for addr in addrs {
            let mut state = HashMap::new();
            if let Some(pre) = self.pre.get(addr) {
                state.insert("balance".to_string(), format!("{}", pre.balance));
                state.insert("nonce".to_string(), format!("{}", pre.nonce));
                state.insert("code".to_string(), hex::encode(&pre.code).to_string());
                for (key, value) in &pre.storage {
                    let (k, v) = (format!("slot {key}"), format!("{value}"));
                    state.insert(k, v);
                }
            }
            if let Some(result) = self.result.get(addr) {
                let none = String::from("∅");
                if let Some(balance) = result.balance {
                    let pre = state.get("balance").unwrap_or(&none);
                    let text = format!("{pre} → {balance}");
                    state.insert("balance".to_string(), text);
                }
                if let Some(code) = &result.code {
                    let pre = state.get("code").unwrap_or(&none);
                    let text = format!("{pre} → {code}");
                    state.insert("code".to_string(), text);
                }
                if let Some(nonce) = &result.nonce {
                    let pre = state.get("nonce").unwrap_or(&none);
                    let text = format!("{pre} → {nonce}");
                    state.insert("nonce".to_string(), text);
                }
                for (key, value) in &result.storage {
                    let (k, v) = (format!("slot {key}"), format!("{value}"));
                    let pre = state.get(&k).unwrap_or(&none);
                    let text = format!("{pre} → {v}");
                    state.insert(k, text);
                }
            }
            let mut text = String::new();
            let mut keys: Vec<_> = state.keys().collect();
            keys.sort();
            for k in keys {
                text.push_str(&format(state.get(k).unwrap(), k));
            }
            table.add_row(row![format!("{addr:?}"), text]);
        }
        write!(f, "{table}")?;

        Ok(())
    }
}

impl StateTest {
    pub fn parse_oneline_spec(tx: &str) -> anyhow::Result<StateTest> {
        // call;calldata;value;gas addr;code;balance;slot1:val1;slot2:val2
        // create;calldata;value;gas addr;code;balance;slot1:val1;slot2:val2

        let parse_u256 = |s: &str| {
            if s.is_empty() {
                Ok(Word::zero())
            } else if let Some(hex) = s.strip_prefix("0x") {
                Word::from_str_radix(hex, 16)
            } else {
                Word::from_str_radix(s, 10)
            }
        };

        let mut param = tx.split(' ');

        // parse tx parameters
        let mut tx = param
            .next()
            .ok_or_else(|| anyhow!("bad params"))?
            .split(';');
        let is_create = {
            match tx
                .next()
                .ok_or_else(|| anyhow!("no call or create specified"))?
            {
                "call" => false,
                "create" => true,
                _ => bail!("no call or create specified"),
            }
        };
        let data = hex::decode(tx.next().unwrap_or(""))?;
        let value = parse_u256(tx.next().unwrap_or("0"))?;
        let gas_limit = u64::from_str(tx.next().unwrap_or("10000000"))?;
        let secret_key = Bytes::from(&[1u8; 32]);
        let from = secret_key_to_address(&SigningKey::from_slice(&secret_key)?);

        let mut pre = BTreeMap::<Address, Account>::new();

        // setup tx.origin (from) account
        pre.insert(
            from,
            Account {
                address: from,
                nonce: 0.into(),
                balance: U256::from(10).pow(18.into()),
                code: Bytes::default(),
                storage: HashMap::new(),
            },
        );

        // parse rest accounts
        let mut to = None;
        for account in param {
            let mut account = account.split(';');

            // parse address, code, balance
            let address = account
                .next()
                .ok_or_else(|| anyhow!("Invalid account"))?
                .replace("0x", "");
            let address = format!("{address:0>40}");
            let address = Address::from_str(&address)?;
            if !is_create && to.is_none() {
                to = Some(address);
            }
            let code = crate::utils::bytecode_of(account.next().unwrap_or(""))?;
            let balance = Word::from_str(account.next().unwrap_or("0"))?;
            let mut storage = HashMap::<U256, U256>::new();

            // parse storage (if any)
            for key_value in account {
                let (key, value) = key_value
                    .split_once(':')
                    .ok_or_else(|| anyhow!("Invalid storage spec"))?;
                storage.insert(parse_u256(key)?, parse_u256(value)?);
            }
            pre.insert(
                address,
                Account {
                    address,
                    nonce: U64::one(),
                    code: Bytes::from(code.code()),
                    balance,
                    storage,
                },
            );
        }

        let state_test = StateTest {
            path: String::default(),
            id: String::default(),
            env: Env {
                current_base_fee: U256::from(DEFAULT_BASE_FEE),
                current_coinbase: Address::default(),
                current_difficulty: U256::default(),
                current_gas_limit: 16000000,
                current_number: 1,
                current_timestamp: 1,
                previous_hash: H256::default(),
            },
            secret_key,
            from,
            to,
            gas_limit,
            max_priority_fee_per_gas: None,
            max_fee_per_gas: None,
            gas_price: U256::one(),
            nonce: 0,
            value,
            data: data.into(),
            access_list: None,
            pre,
            result: HashMap::new(),
            exception: false,
        };

        Ok(state_test)
    }

    /// Parse transaction type.
    pub fn tx_type(&self) -> TxType {
        if self.max_priority_fee_per_gas.is_some() {
            // For EIP-1559, both maxPriorityFeePerGas and maxFeePerGas must
            // exist, and accessList should exist but may be empty.
            assert!(self.max_fee_per_gas.is_some());
            assert!(self.access_list.is_some());

            TxType::Eip1559
        } else if self.access_list.is_some() {
            TxType::Eip2930
        } else {
            // Set transaction type to EIP-155 as default.
            TxType::Eip155
        }
    }

    /// Normalize the signature back to 0/1.
    pub fn normalize_sig_v(&self, v: u64) -> u64 {
        match self.tx_type() {
            TxType::Eip1559 | TxType::Eip2930 => {
                // <https://github.com/gakonst/ethers-rs/blob/8421cfdbb4f26be3989bd11e525f8768d4323bfe/ethers-core/src/types/transaction/mod.rs#L40>
                if v > 1 {
                    v - ETH_CHAIN_ID * 2 - 35
                } else {
                    v
                }
            }
            _ => v,
        }
    }

    /// Build a transaction from this test case.
    pub fn build_tx(&self) -> TypedTransaction {
        match self.tx_type() {
            TxType::Eip1559 => self.build_eip1559_tx(),
            TxType::Eip2930 => self.build_eip2930_tx(),
            _ => self.build_normal_tx_request().into(),
        }
    }

    fn build_eip1559_tx(&self) -> TypedTransaction {
        let mut request = Eip1559TransactionRequest::new()
            .chain_id(ETH_CHAIN_ID)
            .from(self.from)
            .nonce(self.nonce)
            .value(self.value)
            .data(self.data.clone())
            .gas(self.gas_limit)
            .access_list(self.access_list.clone().unwrap())
            .max_priority_fee_per_gas(self.max_priority_fee_per_gas.unwrap())
            .max_fee_per_gas(self.max_fee_per_gas.unwrap());

        if let Some(to) = self.to {
            request = request.to(to);
        }

        request.into()
    }

    fn build_eip2930_tx(&self) -> TypedTransaction {
        let request = self.build_normal_tx_request();
        request
            .with_access_list(self.access_list.clone().unwrap())
            .into()
    }

    fn build_normal_tx_request(&self) -> TransactionRequest {
        let mut request = TransactionRequest::new()
            .chain_id(ETH_CHAIN_ID)
            .from(self.from)
            .nonce(self.nonce)
            .value(self.value)
            .data(self.data.clone())
            .gas(self.gas_limit)
            .gas_price(self.gas_price);

        if let Some(to) = self.to {
            request = request.to(to);
        }

        request
    }
}