-
Notifications
You must be signed in to change notification settings - Fork 11
/
execution.rs
101 lines (93 loc) · 3.96 KB
/
execution.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
//! Module containing the execution test fixture.
use alloy_primitives::{Address, Bloom, B256, U256};
use alloy_rpc_types::trace::geth::AccountState;
use op_alloy_consensus::OpTypedTransaction;
use op_alloy_rpc_types::OpTransactionReceipt;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
/// The execution fixture is the top-level object that contains
/// everything needed to run an execution test.
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(rename_all = "camelCase")]
pub struct ExecutionFixture {
/// The execution environment sets up the current block context.
pub env: ExecutionEnvironment,
/// The initial state of the accounts before running the transactions, also called the
/// "pre-state".
pub alloc: HashMap<Address, AccountState>,
/// The expected state of the accounts after running the transactions, also called the
/// "post-state".
pub out_alloc: HashMap<Address, AccountState>,
/// Transactions to execute.
#[serde(rename = "txs")]
pub transactions: Vec<OpTypedTransaction>,
/// The expected result after executing transactions.
pub result: ExecutionResult,
}
/// The execution environment is the initial state of the execution context.
/// It's used to set the execution environment current block information.
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(rename_all = "camelCase")]
pub struct ExecutionEnvironment {
/// The current block coinbase.
pub current_coinbase: Address,
/// The current block difficulty.
pub current_difficulty: U256,
/// The current block gas limit.
pub current_gas_limit: U256,
/// The previous block hash.
pub previous_hash: B256,
/// The current block number.
pub current_number: U256,
/// The current block timestamp.
pub current_timestamp: U256,
/// The block hashes of the previous blocks.
#[serde(skip_serializing_if = "Option::is_none")]
pub block_hashes: Option<HashMap<U256, B256>>,
}
/// The execution result is the expected result after running the transactions
/// in the execution environment over the pre-state.
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(rename_all = "camelCase")]
pub struct ExecutionResult {
/// The state root.
pub state_root: B256,
/// The transaction root.
pub tx_root: B256,
/// The receipt root.
pub receipt_root: B256,
/// The logs bloom.
pub logs_bloom: Bloom,
/// A list of execution receipts for each executed transaction.
pub receipts: Vec<OpTransactionReceipt>,
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::Value;
#[test]
fn test_serialize_execution_environment() {
let expected_env = include_str!("./testdata/environment.json");
let env = serde_json::from_str::<ExecutionEnvironment>(expected_env)
.expect("failed to parse environment");
let serialized_env = serde_json::to_string(&env).expect("failed to serialize environment");
let serialized_value = serde_json::from_str::<Value>(&serialized_env)
.expect("failed to parse serialized environment");
let expected_value = serde_json::from_str::<Value>(expected_env)
.expect("failed to parse expected environment");
assert_eq!(serialized_value, expected_value);
}
#[test]
fn test_serialize_execution_result() {
let expected_result = include_str!("./testdata/result.json");
let execution_result = serde_json::from_str::<ExecutionResult>(expected_result)
.expect("failed to parse result");
let serialized_result =
serde_json::to_string(&execution_result).expect("failed to serialize result");
let serialized_value = serde_json::from_str::<Value>(&serialized_result)
.expect("failed to parse serialized result");
let expected_value = serde_json::from_str::<Value>(expected_result)
.expect("failed to parse expected result");
assert_eq!(serialized_value, expected_value);
}
}