openethereum/src/tests/state.rs

66 lines
2.2 KiB
Rust
Raw Normal View History

use super::test_common::*;
use state::*;
use pod_state::*;
use state_diff::*;
use ethereum;
fn do_json_test(json_data: &[u8]) -> Vec<String> {
let json = Json::from_str(::std::str::from_utf8(json_data).unwrap()).expect("Json is invalid");
let mut failed = Vec::new();
let engine = ethereum::new_frontier_test().to_engine().unwrap();
for (name, test) in json.as_object().unwrap() {
2016-01-14 17:40:38 +01:00
println!("name: {:?}", name);
let mut fail = false;
let mut fail_unless = |cond: bool| if !cond && !fail { failed.push(name.to_string()); fail = true; true } else {false};
let t = Transaction::from_json(&test["transaction"]);
let env = EnvInfo::from_json(&test["env"]);
2016-01-14 21:58:37 +01:00
let _out = Bytes::from_json(&test["out"]);
let post_state_root = xjson!(&test["postStateRoot"]);
2016-01-14 12:27:35 +01:00
let pre = PodState::from_json(&test["pre"]);
let post = PodState::from_json(&test["post"]);
let logs: Vec<_> = test["logs"].as_array().unwrap().iter().map(&LogEntry::from_json).collect();
2016-01-14 00:27:36 +01:00
//println!("Transaction: {:?}", t);
//println!("Env: {:?}", env);
2016-01-13 15:54:17 +01:00
2016-01-14 02:09:43 +01:00
{
let mut s = State::new_temp();
s.populate_from(post.clone());
s.commit();
assert_eq!(&post_state_root, s.root());
}
let mut s = State::new_temp();
s.populate_from(pre);
2016-01-14 12:27:35 +01:00
let r = s.apply(&env, engine.deref(), &t).unwrap();
if fail_unless(&r.state_root == &post_state_root) {
2016-01-14 12:39:14 +01:00
println!("!!! {}: State mismatch (got: {}, expect: {}):", name, r.state_root, post_state_root);
2016-01-14 12:27:35 +01:00
let our_post = s.to_pod();
2016-01-14 12:39:14 +01:00
println!("Got:\n{}", our_post);
println!("Expect:\n{}", post);
println!("Diff ---expect -> +++got:\n{}", StateDiff::diff_pod(&post, &our_post));
2016-01-14 12:27:35 +01:00
}
2016-01-13 15:54:17 +01:00
2016-01-14 12:27:35 +01:00
if fail_unless(logs == r.logs) {
println!("!!! {}: Logs mismatch:", name);
println!("Got:\n{:?}", r.logs);
2016-01-14 12:39:14 +01:00
println!("Expect:\n{:?}", logs);
}
2016-01-14 12:27:35 +01:00
// TODO: Add extra APIs for output
//if fail_unless(out == r.)
}
2016-01-14 12:39:14 +01:00
println!("!!! {:?} tests from failed.", failed.len());
failed
}
declare_test!{StateTests_stExample, "StateTests/stExample"}
2016-01-14 22:41:39 +01:00
declare_test!{StateTests_stBlockHashTest, "StateTests/stBlockHashTest"}
2016-01-14 14:36:07 +01:00
declare_test!{StateTests_stLogTests, "StateTests/stLogTests"}
2016-01-14 22:41:39 +01:00
declare_test!{StateTests_stCallCodes, "StateTests/stCallCodes"}
declare_test_ignore!{StateTests_stCallCreateCallCodeTest, "StateTests/stCallCreateCallCodeTest"}