Skip to content

Commit

Permalink
provide helpful function to generate storage key, add better comments…
Browse files Browse the repository at this point in the history
…, and remove redundant StorageProof construction
  • Loading branch information
vedhavyas committed Oct 19, 2023
1 parent 6af5662 commit 8c59838
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
9 changes: 6 additions & 3 deletions crates/sp-domains/src/fraud_proof.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::verification::StorageProofVerifier;
use crate::{DomainId, ExecutionReceipt, ReceiptHash, SealedBundleHeader};
use hash_db::Hasher;
use parity_scale_codec::{Compact, Decode, Encode};
use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;
use sp_consensus_slots::Slot;
use sp_core::storage::StorageKey;
use sp_core::H256;
use sp_domain_digests::AsPredigest;
use sp_runtime::traits::{
Expand Down Expand Up @@ -157,11 +156,15 @@ impl ExecutionPhase {
mismatch_index,
extrinsic,
} => {
let storage_key =
StorageProofVerifier::<DomainHeader::Hashing>::enumerated_storage_key(
*mismatch_index,
);
if !StorageProofVerifier::<DomainHeader::Hashing>::verify_storage_proof(
proof_of_inclusion.clone(),
&bad_receipt.domain_block_extrinsic_root.into(),
extrinsic.clone(),
StorageKey(Compact(*mismatch_index).encode()),
storage_key,
) {
return Err(VerificationError::InvalidApplyExtrinsicCallData);
}
Expand Down
4 changes: 1 addition & 3 deletions crates/sp-domains/src/valued_trie_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,7 @@ where

let backend = TrieBackendBuilder::new(db, root).build();
let key = Compact(index).encode();
Some(StorageProof::new(
prove_read(backend, &[key]).ok()?.iter_nodes().cloned(),
))
prove_read(backend, &[key]).ok()
}
}

Expand Down
16 changes: 10 additions & 6 deletions crates/sp-domains/src/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{ExecutionReceipt, DOMAIN_EXTRINSICS_SHUFFLING_SEED_SUBJECT};
use domain_runtime_primitives::opaque::AccountId;
use frame_support::PalletError;
use hash_db::Hasher;
use parity_scale_codec::{Decode, Encode};
use parity_scale_codec::{Compact, Decode, Encode};
use rand::seq::SliceRandom;
use rand::SeedableRng;
use rand_chacha::ChaCha8Rng;
Expand Down Expand Up @@ -35,21 +35,19 @@ pub enum VerificationError {
pub struct StorageProofVerifier<H: Hasher>(PhantomData<H>);

impl<H: Hasher> StorageProofVerifier<H> {
/// Extracts the value against a given key and returns a decoded value.
pub fn get_decoded_value<V: Decode>(
state_root: &H::Out,
proof: StorageProof,
key: StorageKey,
) -> Result<V, VerificationError> {
let db = proof.into_memory_db::<H>();
let val = read_trie_value::<LayoutV1<H>, _>(&db, state_root, key.as_ref(), None, None)
.map_err(|_| VerificationError::InvalidProof)?
.ok_or(VerificationError::MissingValue)?;

let val = Self::get_bare_value(state_root, proof, key)?;
let decoded = V::decode(&mut &val[..]).map_err(|_| VerificationError::FailedToDecode)?;

Ok(decoded)
}

/// Returns the value against a given key.
pub fn get_bare_value(
state_root: &H::Out,
proof: StorageProof,
Expand All @@ -63,6 +61,7 @@ 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,
Expand All @@ -78,6 +77,11 @@ impl<H: Hasher> StorageProofVerifier<H> {
false
}
}

/// Constructs the storage key from a given enumerated index.
pub fn enumerated_storage_key(index: u32) -> StorageKey {
StorageKey(Compact(index).encode())
}
}

pub fn verify_invalid_total_rewards_fraud_proof<
Expand Down

0 comments on commit 8c59838

Please sign in to comment.