Skip to content

Commit 1691837

Browse files
committed
Make decode_execution_result fallible
Also added a TODO about fetching runtime code while verifying.
1 parent 4573b0e commit 1691837

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

crates/sp-executor/src/lib.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,19 +176,18 @@ impl ExecutionPhase {
176176
pub fn decode_execution_result<Header: HeaderT>(
177177
&self,
178178
execution_result: Vec<u8>,
179-
) -> Header::Hash {
179+
) -> Result<Header::Hash, VerificationError> {
180180
match self {
181181
ExecutionPhase::InitializeBlock { .. } | ExecutionPhase::ApplyExtrinsic { .. } => {
182182
let encoded_storage_root = Vec::<u8>::decode(&mut execution_result.as_slice())
183-
.expect("The return value of verifying `initialize_block` and `apply_extrinsic` must be an encoded storage root; qed");
183+
.map_err(VerificationError::InitializeBlockOrApplyExtrinsicDecode)?;
184184
Header::Hash::decode(&mut encoded_storage_root.as_slice())
185-
.expect("storage root must use the same Header Hash type; qed")
185+
.map_err(VerificationError::StorageRootDecode)
186186
}
187187
ExecutionPhase::FinalizeBlock => {
188-
let new_header = Header::decode(&mut execution_result.as_slice()).expect(
189-
"The return value of `BlockBuilder_finalize_block` must be a Header; qed",
190-
);
191-
*new_header.state_root()
188+
let new_header = Header::decode(&mut execution_result.as_slice())
189+
.map_err(VerificationError::HeaderDecode)?;
190+
Ok(*new_header.state_root())
192191
}
193192
}
194193
}
@@ -205,6 +204,12 @@ pub enum VerificationError {
205204
BadProof(sp_std::boxed::Box<dyn sp_state_machine::Error>),
206205
/// The `post_state_root` calculated by farmer does not match the one declared in [`FraudProof`].
207206
BadPostStateRoot { expected: H256, got: H256 },
207+
/// Failed to decode the return value of `initialize_block` and `apply_extrinsic`.
208+
InitializeBlockOrApplyExtrinsicDecode(parity_scale_codec::Error),
209+
/// Failed to decode the storage root produced by verifying `initialize_block` or `apply_extrinsic`.
210+
StorageRootDecode(parity_scale_codec::Error),
211+
/// Failed to decode the header produced by `finalize_block`.
212+
HeaderDecode(parity_scale_codec::Error),
208213
}
209214

210215
/// Fraud proof for the state computation.

crates/subspace-fraud-proof/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ impl<
217217
.expect("Block Hash must be H256; qed"),
218218
);
219219

220+
// TODO: ensure the fetched runtime code works as expected.
220221
let state = self
221222
.backend
222223
.state_at(at)
@@ -244,7 +245,7 @@ impl<
244245
.map_err(VerificationError::BadProof)?;
245246

246247
let new_post_state_root =
247-
execution_phase.decode_execution_result::<Block::Header>(execution_result);
248+
execution_phase.decode_execution_result::<Block::Header>(execution_result)?;
248249
let new_post_state_root = H256::decode(&mut new_post_state_root.encode().as_slice())
249250
.expect("Block Hash must be H256; qed");
250251

cumulus/client/cirrus-executor/src/tests.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ async fn execution_proof_creation_and_verification_should_work() {
196196
)
197197
.expect("Check `initialize_block` proof");
198198

199-
let post_execution_root = execution_phase.decode_execution_result::<Header>(execution_result);
199+
let post_execution_root =
200+
execution_phase.decode_execution_result::<Header>(execution_result).unwrap();
200201
assert_eq!(post_execution_root, intermediate_roots[0].into());
201202

202203
// Test extrinsic execution.
@@ -237,7 +238,7 @@ async fn execution_proof_creation_and_verification_should_work() {
237238
.expect("Check extrinsic execution proof");
238239

239240
let post_execution_root =
240-
execution_phase.decode_execution_result::<Header>(execution_result);
241+
execution_phase.decode_execution_result::<Header>(execution_result).unwrap();
241242
assert_eq!(post_execution_root, intermediate_roots[target_extrinsic_index + 1].into());
242243
}
243244

@@ -274,7 +275,8 @@ async fn execution_proof_creation_and_verification_should_work() {
274275
)
275276
.expect("Check `finalize_block` proof");
276277

277-
let post_execution_root = execution_phase.decode_execution_result::<Header>(execution_result);
278+
let post_execution_root =
279+
execution_phase.decode_execution_result::<Header>(execution_result).unwrap();
278280
assert_eq!(post_execution_root, *header.state_root());
279281
}
280282

0 commit comments

Comments
 (0)