-
Notifications
You must be signed in to change notification settings - Fork 654
/
Copy pathrunner.rs
324 lines (300 loc) · 13 KB
/
runner.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
use std::{
collections::HashMap,
ffi::OsStr,
path::{Path, PathBuf},
str::FromStr,
sync::{atomic::AtomicBool, Arc, Mutex},
time::{Duration, Instant},
};
use indicatif::ProgressBar;
use revm::{db::AccountState, U256,Bytecode, bits::{B160,B256},CreateScheme, Env, ExecutionResult, SpecId, TransactTo};
use std::sync::atomic::Ordering;
use walkdir::{DirEntry, WalkDir};
use super::{
merkle_trie::{log_rlp_hash, state_merkle_trie_root},
models::{SpecName, TestSuit},
trace::CustomPrintTracer,
};
use revm::common::keccak256;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum TestError {
#[error(" Test:{spec_id:?}:{id}, Root missmatched, Expected: {expect:?} got:{got:?}")]
RootMissmatch {
spec_id: SpecId,
id: usize,
got: B256,
expect: B256,
},
#[error("Serde json error")]
SerdeDeserialize(#[from] serde_json::Error),
#[error("Internal system error")]
SystemError,
#[error("Unknown private key: {private_key:?}")]
UnknownPrivateKey { private_key: B256 },
}
pub fn find_all_json_tests(path: &Path) -> Vec<PathBuf> {
WalkDir::new(path)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.file_name().to_string_lossy().ends_with(".json"))
.map(DirEntry::into_path)
.collect::<Vec<PathBuf>>()
}
pub fn execute_test_suit(path: &Path, elapsed: &Arc<Mutex<Duration>>) -> Result<(), TestError> {
// funky test with `bigint 0x00` value in json :) not possible to happen on mainnet and require custom json parser.
// https://github.com/ethereum/tests/issues/971
if path.file_name() == Some(OsStr::new("ValueOverflow.json")) {
return Ok(());
}
// txbyte is of type 02 and we dont parse tx bytes for this test to fail.
if path.file_name() == Some(OsStr::new("typeTwoBerlin.json")) {
return Ok(());
}
// Test checks if nonce overflows. We are handling this correctly but we are not parsing exception in testsuite
// There are more nonce overflow tests that are in internal call/create, and those tests are passing and are enabled.
if path.file_name() == Some(OsStr::new("CreateTransactionHighNonce.json")) {
return Ok(());
}
// Skip test where basefee/accesslist/diffuculty is present but it shouldn't be supported in London/Berlin/TheMerge.
// https://github.com/ethereum/tests/blob/5b7e1ab3ffaf026d99d20b17bb30f533a2c80c8b/GeneralStateTests/stExample/eip1559.json#L130
// It is expected to not execute these tests.
if path.file_name() == Some(OsStr::new("accessListExample.json"))
|| path.file_name() == Some(OsStr::new("basefeeExample.json"))
|| path.file_name() == Some(OsStr::new("eip1559.json"))
|| path.file_name() == Some(OsStr::new("mergeTest.json"))
{
return Ok(());
}
// These tests are passing, but they take a lot of time to execute so we are going to skip them.
if path.file_name() == Some(OsStr::new("loopExp.json"))
|| path.file_name() == Some(OsStr::new("Call50000_sha256.json"))
|| path.file_name() == Some(OsStr::new("static_Call50000_sha256.json"))
|| path.file_name() == Some(OsStr::new("loopMul.json"))
|| path.file_name() == Some(OsStr::new("CALLBlake2f_MaxRounds.json"))
{
return Ok(());
}
let json_reader = std::fs::read(path).unwrap();
let suit: TestSuit = serde_json::from_reader(&*json_reader)?;
let map_caller_keys: HashMap<_, _> = vec![
(
B256::from_str("0x45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8")
.unwrap(),
B160::from_str("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b").unwrap(),
),
(
B256::from_str("0xc85ef7d79691fe79573b1a7064c19c1a9819ebdbd1faaab1a8ec92344438aaf4")
.unwrap(),
B160::from_str("0xcd2a3d9f938e13cd947ec05abc7fe734df8dd826").unwrap(),
),
(
B256::from_str("0x044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d")
.unwrap(),
B160::from_str("0x82a978b3f5962a5b0957d9ee9eef472ee55b42f1").unwrap(),
),
(
B256::from_str("0x6a7eeac5f12b409d42028f66b0b2132535ee158cfda439e3bfdd4558e8f4bf6c")
.unwrap(),
B160::from_str("0xc9c5a15a403e41498b6f69f6f89dd9f5892d21f7").unwrap(),
),
(
B256::from_str("0xa95defe70ebea7804f9c3be42d20d24375e2a92b9d9666b832069c5f3cd423dd")
.unwrap(),
B160::from_str("0x3fb1cd2cd96c6d5c0b5eb3322d807b34482481d4").unwrap(),
),
(
B256::from_str("0xfe13266ff57000135fb9aa854bbfe455d8da85b21f626307bf3263a0c2a8e7fe")
.unwrap(),
B160::from_str("0xdcc5ba93a1ed7e045690d722f2bf460a51c61415").unwrap(),
),
]
.into_iter()
.collect();
for (name, unit) in suit.0.into_iter() {
// Create database and insert cache
let mut database = revm::InMemoryDB::default();
for (address, info) in unit.pre.iter() {
let acc_info = revm::AccountInfo {
balance: info.balance,
code_hash: keccak256(&info.code), // try with dummy hash.
code: Some(Bytecode::new_raw(info.code.clone())),
nonce: info.nonce,
};
database.insert_account_info(*address, acc_info);
// insert storage:
for (&slot, &value) in info.storage.iter() {
let _ = database.insert_account_storage(*address, slot, value);
}
}
let mut env = Env::default();
// cfg env. SpecId is set down the road
env.cfg.chain_id = U256::from(1); // for mainnet
// block env
env.block.number = unit.env.current_number;
env.block.coinbase = unit.env.current_coinbase;
env.block.timestamp = unit.env.current_timestamp;
env.block.gas_limit = unit.env.current_gas_limit;
env.block.basefee = unit.env.current_base_fee.unwrap_or_default();
env.block.difficulty = unit.env.current_difficulty;
//tx env
env.tx.caller =
if let Some(caller) = map_caller_keys.get(&unit.transaction.secret_key.unwrap()) {
*caller
} else {
let private_key = unit.transaction.secret_key.unwrap();
return Err(TestError::UnknownPrivateKey { private_key });
};
env.tx.gas_price = unit
.transaction
.gas_price
.unwrap_or_else(|| unit.transaction.max_fee_per_gas.unwrap_or_default());
env.tx.gas_priority_fee = unit.transaction.max_priority_fee_per_gas;
// post and execution
for (spec_name, tests) in unit.post {
if matches!(
spec_name,
SpecName::ByzantiumToConstantinopleAt5 | SpecName::Constantinople
) {
continue;
}
env.cfg.spec_id = spec_name.to_spec_id();
for (id, test) in tests.into_iter().enumerate() {
let gas_limit = *unit.transaction.gas_limit.get(test.indexes.gas).unwrap();
let gas_limit = u64::try_from(gas_limit).unwrap_or(u64::MAX);
env.tx.gas_limit = gas_limit;
env.tx.data = unit
.transaction
.data
.get(test.indexes.data)
.unwrap()
.clone();
env.tx.value = *unit.transaction.value.get(test.indexes.value).unwrap();
let access_list = match unit.transaction.access_lists {
Some(ref access_list) => access_list
.get(test.indexes.data)
.cloned()
.flatten()
.unwrap_or_default()
.into_iter()
.map(|item| {
(
item.address,
item.storage_keys
.into_iter()
.map(|key| key.into())
.collect::<Vec<_>>(),
)
})
.collect(),
None => Vec::new(),
};
env.tx.access_list = access_list;
let to = match unit.transaction.to {
Some(add) => TransactTo::Call(add),
None => TransactTo::Create(CreateScheme::Create),
};
env.tx.transact_to = to;
let mut database_cloned = database.clone();
let mut evm = revm::new();
evm.database(&mut database_cloned);
evm.env = env.clone();
// do the deed
let timer = Instant::now();
let ExecutionResult {
exit_reason,
gas_used,
gas_refunded,
logs,
..
} = evm.transact_commit();
let timer = timer.elapsed();
*elapsed.lock().unwrap() += timer;
let is_legacy = !SpecId::enabled(evm.env.cfg.spec_id, SpecId::SPURIOUS_DRAGON);
let db = evm.db().unwrap();
let state_root = state_merkle_trie_root(
db.accounts
.iter()
.filter(|(_address, acc)| {
(is_legacy && !matches!(acc.account_state, AccountState::NotExisting))
|| (!is_legacy
&& (!(acc.info.is_empty())
|| matches!(acc.account_state, AccountState::None)))
})
.map(|(k, v)| (*k, v.clone())),
);
let logs_root = log_rlp_hash(logs);
if test.hash != state_root || test.logs != logs_root {
println!(
"ROOTS mismath:\nstate_root:{:?}:{state_root:?}\nlogs_root:{:?}:{logs_root:?}",
test.hash, test.logs
);
let mut database_cloned = database.clone();
evm.database(&mut database_cloned);
evm.inspect_commit(CustomPrintTracer::new());
let db = evm.db().unwrap();
println!("{path:?} UNIT_TEST:{name}\n");
println!(
"fail reson: {:?} {:?} UNIT_TEST:{}\n gas:{:?} ({:?} refunded)",
exit_reason, path, name, gas_used, gas_refunded,
);
println!("\nApplied state:{db:?}\n");
println!("\nStateroot: {state_root:?}\n");
return Err(TestError::RootMissmatch {
spec_id: env.cfg.spec_id,
id,
got: state_root,
expect: test.hash,
});
}
}
}
}
Ok(())
}
pub fn run(test_files: Vec<PathBuf>) -> Result<(), TestError> {
let endjob = Arc::new(AtomicBool::new(false));
let console_bar = Arc::new(ProgressBar::new(test_files.len() as u64));
let mut joins: Vec<std::thread::JoinHandle<Result<(), TestError>>> = Vec::new();
let queue = Arc::new(Mutex::new((0, test_files)));
let elapsed = Arc::new(Mutex::new(std::time::Duration::ZERO));
for _ in 0..10 {
let queue = queue.clone();
let endjob = endjob.clone();
let console_bar = console_bar.clone();
let elapsed = elapsed.clone();
joins.push(
std::thread::Builder::new()
.stack_size(50 * 1024 * 1024)
.spawn(move || loop {
let (index, test_path) = {
let mut queue = queue.lock().unwrap();
if queue.1.len() <= queue.0 {
return Ok(());
}
let test_path = queue.1[queue.0].clone();
queue.0 += 1;
(queue.0 - 1, test_path)
};
if endjob.load(Ordering::SeqCst) {
return Ok(());
}
//println!("Test:{:?}\n",test_path);
if let Err(err) = execute_test_suit(&test_path, &elapsed) {
endjob.store(true, Ordering::SeqCst);
println!("Test[{index}] named:\n{test_path:?} failed: {err}\n");
return Err(err);
}
//println!("TestDone:{:?}\n",test_path);
console_bar.inc(1);
})
.unwrap(),
);
}
for handler in joins {
handler.join().map_err(|_| TestError::SystemError)??;
}
console_bar.finish();
println!("Finished execution. Time:{:?}", elapsed.lock().unwrap());
Ok(())
}