|
1 | 1 | //! Runner for generating `misbehaviour` fixtures |
2 | 2 |
|
3 | 3 | use crate::{ |
4 | | - cli::command::{fixtures::MisbehaviourCmd, OutputPath}, |
| 4 | + cli::command::{fixtures::MisbehaviourCmd, OutputPath, SP1ELFPaths}, |
5 | 5 | runners::genesis::SP1ICS07TendermintGenesis, |
6 | 6 | }; |
7 | 7 | use alloy::sol_types::SolValue; |
@@ -41,20 +41,19 @@ struct SP1ICS07SubmitMisbehaviourFixture { |
41 | 41 | submit_msg: Vec<u8>, |
42 | 42 | } |
43 | 43 |
|
| 44 | +struct SP1Programs { |
| 45 | + update_client: UpdateClientProgram, |
| 46 | + membership: MembershipProgram, |
| 47 | + misbehaviour: MisbehaviourProgram, |
| 48 | + uc_and_membership: UpdateClientAndMembershipProgram, |
| 49 | +} |
| 50 | + |
44 | 51 | /// Writes the proof data for misbehaviour to the given fixture path. |
45 | 52 | #[allow(clippy::missing_errors_doc, clippy::missing_panics_doc)] |
46 | 53 | pub async fn run(args: MisbehaviourCmd) -> anyhow::Result<()> { |
47 | 54 | let misbehaviour: RawMisbehaviour = |
48 | 55 | serde_json::from_slice(&std::fs::read(args.misbehaviour_json_path)?)?; |
49 | | - |
50 | | - let update_client_elf = std::fs::read(args.elf_paths.update_client_path)?; |
51 | | - let membership_elf = std::fs::read(args.elf_paths.membership_path)?; |
52 | | - let misbehaviour_elf = std::fs::read(args.elf_paths.misbehaviour_path)?; |
53 | | - let uc_and_membership_elf = std::fs::read(args.elf_paths.uc_and_membership_path)?; |
54 | | - let update_client_program = UpdateClientProgram::new(update_client_elf); |
55 | | - let membership_program = MembershipProgram::new(membership_elf); |
56 | | - let misbehaviour_program = MisbehaviourProgram::new(misbehaviour_elf); |
57 | | - let uc_and_membership_program = UpdateClientAndMembershipProgram::new(uc_and_membership_elf); |
| 56 | + let programs = load_sp1_programs(args.elf_paths)?; |
58 | 57 |
|
59 | 58 | let tm_rpc_client = HttpClient::from_env(); |
60 | 59 | let sp1_prover = if args.sp1.private_cluster { |
@@ -93,28 +92,28 @@ pub async fn run(args: MisbehaviourCmd) -> anyhow::Result<()> { |
93 | 92 | args.trust_options.trusting_period, |
94 | 93 | args.trust_options.trust_level, |
95 | 94 | args.sp1.proof_type, |
96 | | - &update_client_program, |
97 | | - &membership_program, |
98 | | - &uc_and_membership_program, |
99 | | - &misbehaviour_program, |
| 95 | + &programs.update_client, |
| 96 | + &programs.membership, |
| 97 | + &programs.uc_and_membership, |
| 98 | + &programs.misbehaviour, |
100 | 99 | ) |
101 | 100 | .await?; |
102 | 101 | let genesis_2 = SP1ICS07TendermintGenesis::from_env( |
103 | 102 | &trusted_light_block_2, |
104 | 103 | args.trust_options.trusting_period, |
105 | 104 | args.trust_options.trust_level, |
106 | 105 | args.sp1.proof_type, |
107 | | - &update_client_program, |
108 | | - &membership_program, |
109 | | - &uc_and_membership_program, |
110 | | - &misbehaviour_program, |
| 106 | + &programs.update_client, |
| 107 | + &programs.membership, |
| 108 | + &programs.uc_and_membership, |
| 109 | + &programs.misbehaviour, |
111 | 110 | ) |
112 | 111 | .await?; |
113 | 112 | let trusted_consensus_state_1 = ConsensusState::abi_decode(&genesis_1.trusted_consensus_state)?; |
114 | 113 | let trusted_consensus_state_2 = ConsensusState::abi_decode(&genesis_2.trusted_consensus_state)?; |
115 | 114 | let trusted_client_state_2 = ClientState::abi_decode(&genesis_2.trusted_client_state)?; |
116 | 115 | let verify_misbehaviour_prover = |
117 | | - SP1ICS07TendermintProver::new(args.sp1.proof_type, &sp1_prover, &misbehaviour_program) |
| 116 | + SP1ICS07TendermintProver::new(args.sp1.proof_type, &sp1_prover, &programs.misbehaviour) |
118 | 117 | .await; |
119 | 118 | let now_since_unix = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH)?; |
120 | 119 | let proof_data = verify_misbehaviour_prover |
@@ -143,12 +142,32 @@ pub async fn run(args: MisbehaviourCmd) -> anyhow::Result<()> { |
143 | 142 | submit_msg: submit_msg.abi_encode(), |
144 | 143 | }; |
145 | 144 |
|
146 | | - match args.output_path { |
| 145 | + write_fixture(args.output_path, &fixture)?; |
| 146 | + |
| 147 | + Ok(()) |
| 148 | +} |
| 149 | + |
| 150 | +fn load_sp1_programs(elf_paths: SP1ELFPaths) -> anyhow::Result<SP1Programs> { |
| 151 | + Ok(SP1Programs { |
| 152 | + update_client: UpdateClientProgram::new(std::fs::read(elf_paths.update_client_path)?), |
| 153 | + membership: MembershipProgram::new(std::fs::read(elf_paths.membership_path)?), |
| 154 | + misbehaviour: MisbehaviourProgram::new(std::fs::read(elf_paths.misbehaviour_path)?), |
| 155 | + uc_and_membership: UpdateClientAndMembershipProgram::new(std::fs::read( |
| 156 | + elf_paths.uc_and_membership_path, |
| 157 | + )?), |
| 158 | + }) |
| 159 | +} |
| 160 | + |
| 161 | +fn write_fixture( |
| 162 | + output_path: OutputPath, |
| 163 | + fixture: &SP1ICS07SubmitMisbehaviourFixture, |
| 164 | +) -> anyhow::Result<()> { |
| 165 | + match output_path { |
147 | 166 | OutputPath::File(path) => { |
148 | | - std::fs::write(PathBuf::from(path), serde_json::to_string_pretty(&fixture)?)?; |
| 167 | + std::fs::write(PathBuf::from(path), serde_json::to_string_pretty(fixture)?)?; |
149 | 168 | } |
150 | 169 | OutputPath::Stdout => { |
151 | | - println!("{}", serde_json::to_string_pretty(&fixture)?); |
| 170 | + println!("{}", serde_json::to_string_pretty(fixture)?); |
152 | 171 | } |
153 | 172 | } |
154 | 173 |
|
|
0 commit comments