1+ //! Module containing the fault proof test fixture.
2+
3+ use alloy_primitives:: { BlockNumber , Bytes , ChainId , B256 , U256 } ;
4+ use serde:: { Deserialize , Serialize } ;
5+ use hashbrown:: HashMap ;
6+
7+ /// The fault proof fixture is the top-level object that contains
8+ /// everything needed to run a fault proof test.
9+ #[ derive( Serialize , Deserialize , Debug , Default , PartialEq , Eq ) ]
10+ #[ serde( rename_all = "camelCase" ) ]
11+ pub struct FaultProofFixture {
12+ /// The inputs to the fault proof test.
13+ pub inputs : FaultProofInputs ,
14+ /// The expected status of the fault proof test.
15+ pub expected_status : FaultProofStatus ,
16+ /// The witness data for the fault proof test.
17+ pub witness_data : HashMap < U256 , Bytes > ,
18+ }
19+
20+ /// The fault proof inputs are the inputs to the fault proof test.
21+ #[ derive( Serialize , Deserialize , Debug , Default , PartialEq , Eq ) ]
22+ #[ serde( rename_all = "camelCase" ) ]
23+ pub struct FaultProofInputs {
24+ /// The L1 head block hash.
25+ pub l1_head : B256 ,
26+ /// The L2 head block hash.
27+ pub l2_head : B256 ,
28+ /// The claimed L2 output root to validate.
29+ pub l2_claim : B256 ,
30+ /// The agreed L2 output root to start derivation from.
31+ pub l2_output_root : B256 ,
32+ /// The L2 block number that the claim is from.
33+ pub l2_block_number : BlockNumber ,
34+ /// The L2 chain ID that the claim is from.
35+ pub l2_chain_id : ChainId ,
36+ }
37+
38+ /// The fault proof status is the result of executing the fault proof program.
39+ #[ derive( Serialize , Deserialize , Debug , Default , PartialEq , Eq ) ]
40+ pub enum FaultProofStatus {
41+ /// The claim is valid.
42+ #[ default]
43+ Valid ,
44+ /// The claim is invalid.
45+ Invalid ,
46+ /// Executing the program resulted in a panic.
47+ Panic ,
48+ /// The status is unknown.
49+ Unknown
50+ }
51+
52+ #[ cfg( test) ]
53+ mod tests {
54+ use super :: * ;
55+
56+ #[ test]
57+ fn test_serialize_fault_proof_status ( ) {
58+ let statuses = vec ! [
59+ FaultProofStatus :: Valid ,
60+ FaultProofStatus :: Invalid ,
61+ FaultProofStatus :: Panic ,
62+ FaultProofStatus :: Unknown ,
63+ ] ;
64+
65+ for status in statuses {
66+ let serialized_status = serde_json:: to_string ( & status) . expect ( "failed to serialize status" ) ;
67+ let deserialized_status = serde_json:: from_str :: < FaultProofStatus > ( & serialized_status)
68+ . expect ( "failed to deserialize status" ) ;
69+ assert_eq ! ( status, deserialized_status) ;
70+ }
71+ }
72+
73+ #[ test]
74+ fn test_serialize_fault_proof_inputs ( ) {
75+ let inputs = FaultProofInputs {
76+ l1_head : B256 :: from ( [ 1 ; 32 ] ) ,
77+ l2_head : B256 :: from ( [ 2 ; 32 ] ) ,
78+ l2_claim : B256 :: from ( [ 3 ; 32 ] ) ,
79+ l2_output_root : B256 :: from ( [ 4 ; 32 ] ) ,
80+ l2_block_number : 1337 ,
81+ l2_chain_id : 42 ,
82+ } ;
83+
84+ let serialized_inputs = serde_json:: to_string ( & inputs) . expect ( "failed to serialize inputs" ) ;
85+ let deserialized_inputs = serde_json:: from_str :: < FaultProofInputs > ( & serialized_inputs)
86+ . expect ( "failed to deserialize inputs" ) ;
87+ assert_eq ! ( inputs, deserialized_inputs) ;
88+ }
89+
90+ #[ test]
91+ fn test_serialize_fault_proof_fixture ( ) {
92+ let mut witness_data = HashMap :: new ( ) ;
93+ witness_data. insert ( U256 :: from ( 1 ) , Bytes :: from ( [ 1 ; 32 ] ) ) ;
94+ witness_data. insert ( U256 :: from ( 2 ) , Bytes :: from ( [ 2 ; 32 ] ) ) ;
95+
96+ let fixture = FaultProofFixture {
97+ inputs : FaultProofInputs {
98+ l1_head : B256 :: from ( [ 1 ; 32 ] ) ,
99+ l2_head : B256 :: from ( [ 2 ; 32 ] ) ,
100+ l2_claim : B256 :: from ( [ 3 ; 32 ] ) ,
101+ l2_output_root : B256 :: from ( [ 4 ; 32 ] ) ,
102+ l2_block_number : 1337 ,
103+ l2_chain_id : 42 ,
104+ } ,
105+ expected_status : FaultProofStatus :: Valid ,
106+ witness_data : witness_data,
107+ } ;
108+
109+ let serialized_fixture = serde_json:: to_string ( & fixture) . expect ( "failed to serialize fixture" ) ;
110+ let deserialized_fixture = serde_json:: from_str :: < FaultProofFixture > ( & serialized_fixture)
111+ . expect ( "failed to deserialize fixture" ) ;
112+ assert_eq ! ( fixture, deserialized_fixture) ;
113+ }
114+ }
0 commit comments