ovr_evm_test_vector_support/
lib.rs1use evm::{Context, ExitSucceed};
19use fp_evm::Precompile;
20
21use serde::Deserialize;
22
23#[allow(non_snake_case)]
24#[derive(Deserialize, Debug)]
25struct EthConsensusTest {
26 Input: String,
27 Expected: String,
28 Name: String,
29 Gas: Option<u64>,
30}
31
32pub fn test_precompile_test_vectors<P: Precompile>(filepath: &str) -> Result<(), String> {
36 use std::fs;
37
38 let data = fs::read_to_string(&filepath).expect("Failed to read blake2F.json");
39
40 let tests: Vec<EthConsensusTest> = serde_json::from_str(&data).expect("expected json array");
41
42 for test in tests {
43 let input: Vec<u8> = hex::decode(test.Input).expect("Could not hex-decode test input data");
44
45 let cost: u64 = 10000000;
46
47 let context: Context = Context {
48 address: Default::default(),
49 caller: Default::default(),
50 apparent_value: From::from(0),
51 };
52
53 match P::execute(&input, Some(cost), &context, false) {
54 Ok(result) => {
55 let as_hex: String = hex::encode(result.output);
56 assert_eq!(
57 result.exit_status,
58 ExitSucceed::Returned,
59 "test '{}' returned {:?} (expected 'Returned')",
60 test.Name,
61 result.exit_status
62 );
63 assert_eq!(
64 as_hex, test.Expected,
65 "test '{}' failed (different output)",
66 test.Name
67 );
68 if let Some(expected_gas) = test.Gas {
69 assert_eq!(
70 result.cost, expected_gas,
71 "test '{}' failed (different gas cost)",
72 test.Name
73 );
74 }
75 }
76 Err(err) => {
77 return Err(format!("Test '{}' returned error: {:?}", test.Name, err));
78 }
79 }
80 }
81
82 Ok(())
83}