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
mod env;
mod transaction;

pub use env::*;
pub use transaction::*;

use serde::Deserialize;
use std::collections::{BTreeMap, HashMap};

use crate::{AccountInfo, Config, SpecName};
use cfx_rpc_primitives::Bytes;
use cfx_types::{Address, H256};

/// State test indexed state result deserialization.
#[derive(Debug, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct StateTest {
    pub expect_exception: Option<String>,

    /// Indexes
    pub indexes: TxPartIndices,
    /// Post state hash
    pub hash: H256,
    // /// Post state
    // #[serde(default)]
    // pub post_state: HashMap<Address, AccountInfo>,
    /// Logs root
    pub logs: H256,

    /// Output state.
    ///
    /// Note: Not used.
    #[serde(default)]
    pub state: HashMap<Address, AccountInfo>,

    /// Tx bytes
    pub txbytes: Option<Bytes>,
}

/// Single test unit struct
#[derive(Debug, PartialEq, Eq, Deserialize)]
//#[serde(deny_unknown_fields)]
// field config
pub struct StateTestUnit {
    /// Test info is optional.
    #[serde(default, rename = "_info")]
    pub info: Option<serde_json::Value>,

    pub env: Env,
    pub pre: HashMap<Address, AccountInfo>,
    pub post: BTreeMap<SpecName, Vec<StateTest>>,
    pub transaction: TransactionParts,
    #[serde(default)]
    pub out: Option<Bytes>,
    pub config: Config,
}

/// The top level test suite struct
#[derive(Debug, PartialEq, Eq, Deserialize)]
pub struct StateTestSuite(pub BTreeMap<String, StateTestUnit>);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn deserialize_test_unit() {
        let json = r#"
        {
            "env": {
                "currentCoinbase": "0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba",
                "currentGasLimit": "0x016345785d8a0000",
                "currentNumber": "0x01",
                "currentTimestamp": "0x03e8",
                "currentRandom": "0x0000000000000000000000000000000000000000000000000000000000000000",
                "currentDifficulty": "0x00",
                "currentBaseFee": "0x07",
                "currentExcessBlobGas": "0x0e0000"
            },
            "pre": {
                "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b": {
                    "nonce": "0x00",
                    "balance": "0x04b2e7dc",
                    "code": "0x",
                    "storage": {}
                }
            },
            "transaction": {
                "nonce": "0x00",
                "maxPriorityFeePerGas": "0x00",
                "maxFeePerGas": "0x07",
                "gasLimit": [
                    "0x6a44"
                ],
                "to": "0x8a0a19589531694250d570040a0c4b74576919b8",
                "value": [
                    "0x00"
                ],
                "data": [
                    "0x00"
                ],
                "accessLists": [
                    [
                        {
                            "address": "0x0000000000000000000000000000000000000064",
                            "storageKeys": [
                                "0x0000000000000000000000000000000000000000000000000000000000000064",
                                "0x00000000000000000000000000000000000000000000000000000000000000c8"
                            ]
                        }
                    ]
                ],
                "maxFeePerBlobGas": "0x64",
                "blobVersionedHashes": [
                    "0x0100000000000000000000000000000000000000000000000000000000000000",
                    "0x0100000000000000000000000000000000000000000000000000000000000001",
                    "0x0100000000000000000000000000000000000000000000000000000000000002",
                    "0x0100000000000000000000000000000000000000000000000000000000000003",
                    "0x0100000000000000000000000000000000000000000000000000000000000004",
                    "0x0100000000000000000000000000000000000000000000000000000000000005"
                ],
                "sender": "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b",
                "secretKey": "0x45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8"
            },
            "post": {
                "Cancun": [
                    {
                        "hash": "0x70151ef6e11989505a79932f38c191bdbb22a2ff455460ddc53ef3b056d378ff",
                        "logs": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
                        "txbytes": "0x03f9018701808007826a44948a0a19589531694250d570040a0c4b74576919b88000f85bf859940000000000000000000000000000000000000064f842a00000000000000000000000000000000000000000000000000000000000000064a000000000000000000000000000000000000000000000000000000000000000c864f8c6a00100000000000000000000000000000000000000000000000000000000000000a00100000000000000000000000000000000000000000000000000000000000001a00100000000000000000000000000000000000000000000000000000000000002a00100000000000000000000000000000000000000000000000000000000000003a00100000000000000000000000000000000000000000000000000000000000004a0010000000000000000000000000000000000000000000000000000000000000501a01fd714a1b7065e945bffbe064dfe72b1f0a9f482cd3d03ffa78884bd9331b19ea04066aba133e51239e007914fc7ee21bd2591c39d1d201b3b813f03d2f4ca9088",
                        "indexes": {
                            "data": 0,
                            "gas": 0,
                            "value": 0
                        },
                        "state": {
                            "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b": {
                                "nonce": "0x01",
                                "balance": "0x04a40000",
                                "code": "0x",
                                "storage": {}
                            }
                        }
                    }
                ]
            },
            "config": {
                "blobSchedule": {
                    "Cancun": {
                        "target": "0x03",
                        "max": "0x06",
                        "baseFeeUpdateFraction": "0x32f0ed"
                    }
                },
                "chainid": "0x01"
            },
            "_info": {
                "hash": "0xd992bae1a97e5e6134325c6b02b86087700fef37fa53a6f5a72c50829d8fca77",
                "comment": "`execution-spec-tests` generated test",
                "filling-transition-tool": "ethereum-spec-evm-resolver 0.0.5",
                "description": "Test function documentation:\n\n    Check that transaction is accepted when user can exactly afford the blob gas specified (and\n    max_fee_per_gas would be enough for current block).\n\n    - Transactions with max fee equal or higher than current block base fee\n    - Transactions with and without priority fee\n    - Transactions with and without value\n    - Transactions with and without calldata\n    - Transactions with max fee per blob gas lower or higher than the priority fee",
                "url": "https://github.com/ethereum/execution-spec-tests/tree/v4.1.0/tests/cancun/eip4844_blobs/test_blob_txs.py#L633",
                "fixture-format": "state_test",
                "reference-spec": "https://github.com/ethereum/EIPs/blob/master/EIPS/eip-4844.md",
                "reference-spec-version": "de2e4a46ad93fc04e6fe3174dc6e90a3307bdb5f",
                "eels-resolution": {
                    "git-url": "https://github.com/ethereum/execution-specs.git",
                    "branch": "master",
                    "commit": "78fb726158c69d8fa164e28f195fabf6ab59b915"
                }
            }
        }
        "#;
        let test_unit: StateTestUnit = serde_json::from_str(json).unwrap();
        println!("{:?}", test_unit);
    }
}