diff --git a/crates/sp-domains-fraud-proof/src/fraud_proof.rs b/crates/sp-domains-fraud-proof/src/fraud_proof.rs index 9242d52e1b..1a71d6ec59 100644 --- a/crates/sp-domains-fraud-proof/src/fraud_proof.rs +++ b/crates/sp-domains-fraud-proof/src/fraud_proof.rs @@ -23,9 +23,8 @@ pub enum ExecutionPhase { InitializeBlock, /// Executes some extrinsic. ApplyExtrinsic { - proof_of_inclusion: StorageProof, + extrinsic_proof: StorageProof, mismatch_index: u32, - extrinsic: Vec, }, /// Executes the `finalize_block` hook. FinalizeBlock, @@ -143,9 +142,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` @@ -154,15 +152,13 @@ impl ExecutionPhase { StorageProofVerifier::::enumerated_storage_key( extrinsic_index, ); - if !StorageProofVerifier::::verify_storage_proof( - proof_of_inclusion.clone(), + + StorageProofVerifier::::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(), }) diff --git a/crates/sp-domains/src/proof_provider_and_verifier.rs b/crates/sp-domains/src/proof_provider_and_verifier.rs index 1986fb9db0..c48c9630c4 100644 --- a/crates/sp-domains/src/proof_provider_and_verifier.rs +++ b/crates/sp-domains/src/proof_provider_and_verifier.rs @@ -53,23 +53,6 @@ impl StorageProofVerifier { 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, - storage_key: StorageKey, - ) -> bool - where - H: Hasher, - { - if let Ok(got_data) = StorageProofVerifier::::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()) diff --git a/crates/sp-domains/src/valued_trie.rs b/crates/sp-domains/src/valued_trie.rs index fb0af3f6df..9096c322d3 100644 --- a/crates/sp-domains/src/valued_trie.rs +++ b/crates/sp-domains/src/valued_trie.rs @@ -303,35 +303,36 @@ mod test { ) .unwrap(); - assert!(StorageProofVerifier::::verify_storage_proof( - storage_proof.clone(), - &root, - ext.clone(), - storage_key.clone(), - )); + assert_eq!( + StorageProofVerifier::::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::::verify_storage_proof( - storage_proof.clone(), + // Verifying the proof with a wrong root/key will fail + assert!(StorageProofVerifier::::get_bare_value( &H256::random(), - ext.clone(), - storage_key.clone(), - )); - - assert!(!StorageProofVerifier::::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::::verify_storage_proof( - storage_proof, + let result = StorageProofVerifier::::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 diff --git a/domains/client/domain-operator/src/fraud_proof.rs b/domains/client/domain-operator/src/fraud_proof.rs index 38434b014e..ea75c0ef5f 100644 --- a/domains/client/domain-operator/src/fraud_proof.rs +++ b/domains/client/domain-operator/src/fraud_proof.rs @@ -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(), } };