Skip to content

Commit

Permalink
remove duplicated extrinsic data from ApplyExtrinsic execution phase
Browse files Browse the repository at this point in the history
  • Loading branch information
vedhavyas committed Nov 8, 2023
1 parent 547e883 commit 6730d64
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 52 deletions.
18 changes: 7 additions & 11 deletions crates/sp-domains-fraud-proof/src/fraud_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ pub enum ExecutionPhase {
InitializeBlock,
/// Executes some extrinsic.
ApplyExtrinsic {
proof_of_inclusion: StorageProof,
extrinsic_proof: StorageProof,
mismatch_index: u32,
extrinsic: Vec<u8>,
},
/// Executes the `finalize_block` hook.
FinalizeBlock,
Expand Down Expand Up @@ -142,9 +141,8 @@ impl ExecutionPhase {
new_header.encode()
}
ExecutionPhase::ApplyExtrinsic {
proof_of_inclusion,
extrinsic_proof: proof_of_inclusion,
mismatch_index,
extrinsic,
} => {
// There is a trace root of the `initialize_block` in the head of the trace so we
// need to minus one to get the correct `extrinsic_index`
Expand All @@ -153,15 +151,13 @@ impl ExecutionPhase {
StorageProofVerifier::<DomainHeader::Hashing>::enumerated_storage_key(
extrinsic_index,
);
if !StorageProofVerifier::<DomainHeader::Hashing>::verify_storage_proof(
proof_of_inclusion.clone(),

StorageProofVerifier::<DomainHeader::Hashing>::get_bare_value(
&bad_receipt.domain_block_extrinsic_root,
extrinsic.clone(),
proof_of_inclusion.clone(),
storage_key,
) {
return Err(VerificationError::InvalidApplyExtrinsicCallData);
}
extrinsic.clone()
)
.map_err(|_| VerificationError::InvalidApplyExtrinsicCallData)?
}
ExecutionPhase::FinalizeBlock => Vec::new(),
})
Expand Down
17 changes: 0 additions & 17 deletions crates/sp-domains/src/proof_provider_and_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,6 @@ impl<H: Hasher> StorageProofVerifier<H> {
Ok(val)
}

/// Verifies the given storage proof and checks the expected_value matches the extracted value from the proof.
pub fn verify_storage_proof(
proof: StorageProof,
root: &H::Out,
expected_value: Vec<u8>,
storage_key: StorageKey,
) -> bool
where
H: Hasher,
{
if let Ok(got_data) = StorageProofVerifier::<H>::get_bare_value(root, proof, storage_key) {
expected_value == got_data
} else {
false
}
}

/// Constructs the storage key from a given enumerated index.
pub fn enumerated_storage_key(index: u32) -> StorageKey {
StorageKey(Compact(index).encode())
Expand Down
45 changes: 23 additions & 22 deletions crates/sp-domains/src/valued_trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,35 +303,36 @@ mod test {
)
.unwrap();

assert!(StorageProofVerifier::<BlakeTwo256>::verify_storage_proof(
storage_proof.clone(),
&root,
ext.clone(),
storage_key.clone(),
));
assert_eq!(
StorageProofVerifier::<BlakeTwo256>::get_bare_value(
&root,
storage_proof.clone(),
storage_key.clone(),
)
.unwrap(),
ext.clone()
);

// Verifying the proof with a wrong root/ext/index will fail
assert!(!StorageProofVerifier::<BlakeTwo256>::verify_storage_proof(
storage_proof.clone(),
// Verifying the proof with a wrong root/key will fail
assert!(StorageProofVerifier::<BlakeTwo256>::get_bare_value(
&H256::random(),
ext.clone(),
storage_key.clone(),
));

assert!(!StorageProofVerifier::<BlakeTwo256>::verify_storage_proof(
storage_proof.clone(),
&root,
vec![i as u8; ext.len()],
storage_key,
));
storage_key.clone(),
)
.is_err());

let storage_key = StorageKey(Compact(i as u32 + 1).encode());
assert!(!StorageProofVerifier::<BlakeTwo256>::verify_storage_proof(
storage_proof,
let result = StorageProofVerifier::<BlakeTwo256>::get_bare_value(
&root,
ext,
storage_proof,
storage_key,
));
);

// there is a possibility that wrong key ends up being a different leaf in the merkle tree
// but the data that key holds is neither valid extrinsic nor the the one we expect.
if let Ok(data) = result {
assert_ne!(data, ext.clone())
}
}

// fails to generate storage key for unknown index
Expand Down
3 changes: 1 addition & 2 deletions domains/client/domain-operator/src/fraud_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,8 @@ where
)
.ok_or(FraudProofError::FailToGenerateProofOfInclusion)?;
ExecutionPhase::ApplyExtrinsic {
proof_of_inclusion,
extrinsic_proof: proof_of_inclusion,
mismatch_index: trace_mismatch_index,
extrinsic: target_extrinsic.clone(),
}
};

Expand Down

0 comments on commit 6730d64

Please sign in to comment.