Skip to content

Commit a677dcb

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

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
@@ -177,19 +177,18 @@ impl ExecutionPhase {
177177
pub fn decode_execution_result<Header: HeaderT>(
178178
&self,
179179
execution_result: Vec<u8>,
180-
) -> Header::Hash {
180+
) -> Result<Header::Hash, VerificationError> {
181181
match self {
182182
ExecutionPhase::InitializeBlock { .. } | ExecutionPhase::ApplyExtrinsic { .. } => {
183183
let encoded_storage_root = Vec::<u8>::decode(&mut execution_result.as_slice())
184-
.expect("The return value of verifying `initialize_block` and `apply_extrinsic` must be an encoded storage root; qed");
184+
.map_err(VerificationError::InitializeBlockOrApplyExtrinsicDecode)?;
185185
Header::Hash::decode(&mut encoded_storage_root.as_slice())
186-
.expect("storage root must use the same Header Hash type; qed")
186+
.map_err(VerificationError::StorageRootDecode)
187187
}
188188
ExecutionPhase::FinalizeBlock => {
189-
let new_header = Header::decode(&mut execution_result.as_slice()).expect(
190-
"The return value of `BlockBuilder_finalize_block` must be a Header; qed",
191-
);
192-
*new_header.state_root()
189+
let new_header = Header::decode(&mut execution_result.as_slice())
190+
.map_err(VerificationError::HeaderDecode)?;
191+
Ok(*new_header.state_root())
193192
}
194193
}
195194
}
@@ -206,6 +205,12 @@ pub enum VerificationError {
206205
BadProof(sp_std::boxed::Box<dyn sp_state_machine::Error>),
207206
/// The `post_state_root` calculated by farmer does not match the one declared in [`FraudProof`].
208207
BadPostStateRoot { expected: H256, got: H256 },
208+
/// Failed to decode the return value of `initialize_block` and `apply_extrinsic`.
209+
InitializeBlockOrApplyExtrinsicDecode(parity_scale_codec::Error),
210+
/// Failed to decode the storage root produced by verifying `initialize_block` or `apply_extrinsic`.
211+
StorageRootDecode(parity_scale_codec::Error),
212+
/// Failed to decode the header produced by `finalize_block`.
213+
HeaderDecode(parity_scale_codec::Error),
209214
}
210215

211216
/// 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)