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
use evm::{Context, ExitSucceed};
use fp_evm::Precompile;
use serde::Deserialize;
#[allow(non_snake_case)]
#[derive(Deserialize, Debug)]
struct EthConsensusTest {
Input: String,
Expected: String,
Name: String,
Gas: Option<u64>,
}
pub fn test_precompile_test_vectors<P: Precompile>(filepath: &str) -> Result<(), String> {
use std::fs;
let data = fs::read_to_string(&filepath).expect("Failed to read blake2F.json");
let tests: Vec<EthConsensusTest> = serde_json::from_str(&data).expect("expected json array");
for test in tests {
let input: Vec<u8> = hex::decode(test.Input).expect("Could not hex-decode test input data");
let cost: u64 = 10000000;
let context: Context = Context {
address: Default::default(),
caller: Default::default(),
apparent_value: From::from(0),
};
match P::execute(&input, Some(cost), &context, false) {
Ok(result) => {
let as_hex: String = hex::encode(result.output);
assert_eq!(
result.exit_status,
ExitSucceed::Returned,
"test '{}' returned {:?} (expected 'Returned')",
test.Name,
result.exit_status
);
assert_eq!(
as_hex, test.Expected,
"test '{}' failed (different output)",
test.Name
);
if let Some(expected_gas) = test.Gas {
assert_eq!(
result.cost, expected_gas,
"test '{}' failed (different gas cost)",
test.Name
);
}
}
Err(err) => {
return Err(format!("Test '{}' returned error: {:?}", test.Name, err));
}
}
}
Ok(())
}