Skip to content

Commit

Permalink
Move BlockRandomness into a combined storage proof
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Dec 16, 2024
1 parent 0694218 commit 4300b34
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 36 deletions.
5 changes: 5 additions & 0 deletions crates/pallet-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ mod pallet {
#[cfg(not(feature = "runtime-benchmarks"))]
use crate::staking_epoch::do_slash_operator;
use crate::staking_epoch::{do_finalize_domain_current_epoch, Error as StakingEpochError};
use crate::storage_proof::InvalidInherentExtrinsicData;
use crate::weights::WeightInfo;
#[cfg(not(feature = "runtime-benchmarks"))]
use crate::DomainHashingFor;
Expand Down Expand Up @@ -1850,6 +1851,10 @@ mod pallet {
}
}

/// Combined fraud proof data for the InvalidInherentExtrinsic fraud proof
#[pallet::storage]
pub type BlockInvalidInherentExtrinsicData<T> = StorageValue<_, InvalidInherentExtrinsicData>;

#[pallet::hooks]
// TODO: proper benchmark
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
Expand Down
6 changes: 5 additions & 1 deletion crates/sp-domains-fraud-proof/src/fraud_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,11 @@ pub struct InvalidExtrinsicsRootProof {
/// Valid Bundle digests
pub valid_bundle_digests: Vec<ValidBundleDigest>,

/// The storage proof used during verification
/// The combined storage proofs used during verification
pub invalid_inherent_extrinsic_proofs: InvalidInherentExtrinsicDataProof,

/// The individual storage proofs used during verification
// TODO: combine these proofs into `InvalidInherentExtrinsicDataProof`
pub invalid_inherent_extrinsic_proof: InvalidInherentExtrinsicProof,

/// Optional sudo extrinsic call storage proof
Expand Down
7 changes: 6 additions & 1 deletion crates/sp-domains-fraud-proof/src/host_functions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#[cfg(not(feature = "std"))]
extern crate alloc;

use crate::storage_proof::InvalidInherentExtrinsicData;
use crate::{
DomainInherentExtrinsic, DomainInherentExtrinsicData, DomainStorageKeyRequest,
StatelessDomainRuntimeCall,
Expand Down Expand Up @@ -75,6 +76,7 @@ pub trait FraudProofHostFunctions: Send + Sync {
fn construct_domain_inherent_extrinsic(
&self,
domain_runtime_code: Vec<u8>,
invalid_inherent_extrinsic_data: InvalidInherentExtrinsicData,
domain_inherent_extrinsic_data: DomainInherentExtrinsicData,
) -> Option<DomainInherentExtrinsic>;

Expand Down Expand Up @@ -259,11 +261,14 @@ where
fn construct_domain_inherent_extrinsic(
&self,
domain_runtime_code: Vec<u8>,
invalid_inherent_extrinsic_data: InvalidInherentExtrinsicData,
domain_inherent_extrinsic_data: DomainInherentExtrinsicData,
) -> Option<DomainInherentExtrinsic> {
let DomainInherentExtrinsicData {
let InvalidInherentExtrinsicData {
// Used by caller
block_randomness: _,
} = invalid_inherent_extrinsic_data;
let DomainInherentExtrinsicData {
timestamp,
maybe_domain_runtime_upgrade,
consensus_transaction_byte_fee,
Expand Down
3 changes: 1 addition & 2 deletions crates/sp-domains-fraud-proof/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ use sp_runtime::transaction_validity::{InvalidTransaction, TransactionValidity};
use sp_runtime::OpaqueExtrinsic;
use sp_runtime_interface::pass_by;
use sp_runtime_interface::pass_by::PassBy;
use subspace_core_primitives::{Randomness, U256};
use subspace_core_primitives::U256;
use subspace_runtime_primitives::{Balance, Moment};

/// Custom invalid validity code for the extrinsics in pallet-domains.
Expand Down Expand Up @@ -108,7 +108,6 @@ pub enum DomainChainAllowlistUpdateExtrinsic {

#[derive(Debug, Decode, Encode, TypeInfo, PartialEq, Eq, Clone)]
pub struct DomainInherentExtrinsicData {
pub block_randomness: Randomness,
pub timestamp: Moment,
pub maybe_domain_runtime_upgrade: Option<Vec<u8>>,
pub consensus_transaction_byte_fee: Balance,
Expand Down
3 changes: 3 additions & 0 deletions crates/sp-domains-fraud-proof/src/runtime_interface.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#[cfg(not(feature = "std"))]
extern crate alloc;

use crate::storage_proof::InvalidInherentExtrinsicData;
#[cfg(feature = "std")]
use crate::FraudProofExtension;
use crate::{
Expand Down Expand Up @@ -75,12 +76,14 @@ pub trait FraudProofRuntimeInterface {
fn construct_domain_inherent_extrinsic(
&mut self,
domain_runtime_code: Vec<u8>,
invalid_inherent_extrinsic_data: InvalidInherentExtrinsicData,
domain_inherent_extrinsic_data: DomainInherentExtrinsicData,
) -> Option<DomainInherentExtrinsic> {
self.extension::<FraudProofExtension>()
.expect("No `FraudProofExtension` associated for the current context!")
.construct_domain_inherent_extrinsic(
domain_runtime_code,
invalid_inherent_extrinsic_data,
domain_inherent_extrinsic_data,
)
}
Expand Down
55 changes: 28 additions & 27 deletions crates/sp-domains-fraud-proof/src/storage_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use sp_domains::{
};
use sp_runtime::generic::Digest;
use sp_runtime::traits::{Block as BlockT, HashingFor, Header as HeaderT, NumberFor};
use sp_runtime_interface::pass_by;
use sp_runtime_interface::pass_by::PassBy;
use sp_std::marker::PhantomData;
use sp_std::vec::Vec;
use sp_trie::StorageProof;
Expand All @@ -36,7 +38,7 @@ pub enum VerificationError {
InvalidBundleStorageProof,
RuntimeCodeNotFound,
UnexpectedDomainRuntimeUpgrade,
BlockRandomnessStorageProof(StorageProofVerificationError),
InvalidInherentExtrinsicStorageProof(StorageProofVerificationError),
TimestampStorageProof(StorageProofVerificationError),
SuccessfulBundlesStorageProof(StorageProofVerificationError),
TransactionByteFeeStorageProof(StorageProofVerificationError),
Expand All @@ -54,7 +56,7 @@ pub enum VerificationError {

#[derive(Clone, Debug, Decode, Encode, Eq, PartialEq, TypeInfo)]
pub enum FraudProofStorageKeyRequest<Number> {
BlockRandomness,
InvalidInherentExtrinsicData,
Timestamp,
SuccessfulBundles(DomainId),
TransactionByteFee,
Expand All @@ -69,7 +71,9 @@ pub enum FraudProofStorageKeyRequest<Number> {
impl<Number> FraudProofStorageKeyRequest<Number> {
fn into_error(self, err: StorageProofVerificationError) -> VerificationError {
match self {
Self::BlockRandomness => VerificationError::BlockRandomnessStorageProof(err),
Self::InvalidInherentExtrinsicData => {
VerificationError::InvalidInherentExtrinsicStorageProof(err)
}
Self::Timestamp => VerificationError::TimestampStorageProof(err),
Self::SuccessfulBundles(_) => VerificationError::SuccessfulBundlesStorageProof(err),
Self::TransactionByteFee => VerificationError::TransactionByteFeeStorageProof(err),
Expand Down Expand Up @@ -173,17 +177,6 @@ impl<Block: BlockT> BasicStorageProof<Block> for SuccessfulBundlesProof {
}
}

#[derive(Clone, Debug, Decode, Encode, Eq, PartialEq, TypeInfo)]
pub struct BlockRandomnessProof(StorageProof);

impl_storage_proof!(BlockRandomnessProof);
impl<Block: BlockT> BasicStorageProof<Block> for BlockRandomnessProof {
type StorageValue = Randomness;
fn storage_key_request(_key: Self::Key) -> FraudProofStorageKeyRequest<NumberFor<Block>> {
FraudProofStorageKeyRequest::BlockRandomness
}
}

#[derive(Clone, Debug, Decode, Encode, Eq, PartialEq, TypeInfo)]
pub struct DomainChainsAllowlistUpdateStorageProof(StorageProof);

Expand Down Expand Up @@ -414,10 +407,28 @@ impl MaybeDomainRuntimeUpgradedProof {
}

#[derive(Clone, Debug, Decode, Encode, Eq, PartialEq, TypeInfo)]
pub struct InvalidInherentExtrinsicProof {
/// Block randomness storage proof
pub block_randomness_proof: BlockRandomnessProof,
pub struct InvalidInherentExtrinsicData {
/// Block randomness
pub block_randomness: Randomness,
}

impl PassBy for InvalidInherentExtrinsicData {
type PassBy = pass_by::Codec<Self>;
}

#[derive(Clone, Debug, Decode, Encode, Eq, PartialEq, TypeInfo)]
pub struct InvalidInherentExtrinsicDataProof(StorageProof);

impl_storage_proof!(InvalidInherentExtrinsicDataProof);
impl<Block: BlockT> BasicStorageProof<Block> for InvalidInherentExtrinsicDataProof {
type StorageValue = InvalidInherentExtrinsicData;
fn storage_key_request(_key: Self::Key) -> FraudProofStorageKeyRequest<NumberFor<Block>> {
FraudProofStorageKeyRequest::InvalidInherentExtrinsicData
}
}

#[derive(Clone, Debug, Decode, Encode, Eq, PartialEq, TypeInfo)]
pub struct InvalidInherentExtrinsicProof {
/// Block timestamp storage proof
pub timestamp_proof: TimestampStorageProof,

Expand Down Expand Up @@ -448,8 +459,6 @@ impl InvalidInherentExtrinsicProof {
block_hash: Block::Hash,
maybe_runtime_id: Option<RuntimeId>,
) -> Result<Self, GenerationError> {
let block_randomness_proof =
BlockRandomnessProof::generate(proof_provider, block_hash, (), storage_key_provider)?;
let timestamp_proof =
TimestampStorageProof::generate(proof_provider, block_hash, (), storage_key_provider)?;
let maybe_domain_runtime_upgrade_proof = MaybeDomainRuntimeUpgradedProof::generate(
Expand Down Expand Up @@ -478,7 +487,6 @@ impl InvalidInherentExtrinsicProof {
)?;

Ok(Self {
block_randomness_proof,
timestamp_proof,
maybe_domain_runtime_upgrade_proof,
dynamic_cost_of_storage_proof,
Expand All @@ -493,12 +501,6 @@ impl InvalidInherentExtrinsicProof {
runtime_id: RuntimeId,
state_root: &Block::Hash,
) -> Result<DomainInherentExtrinsicData, VerificationError> {
let block_randomness = <BlockRandomnessProof as BasicStorageProof<Block>>::verify::<SKP>(
self.block_randomness_proof.clone(),
(),
state_root,
)?;

let timestamp = <TimestampStorageProof as BasicStorageProof<Block>>::verify::<SKP>(
self.timestamp_proof.clone(),
(),
Expand Down Expand Up @@ -536,7 +538,6 @@ impl InvalidInherentExtrinsicProof {
)?;

Ok(DomainInherentExtrinsicData {
block_randomness,
timestamp,
maybe_domain_runtime_upgrade,
consensus_transaction_byte_fee,
Expand Down
11 changes: 10 additions & 1 deletion crates/sp-domains-fraud-proof/src/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,18 @@ where
{
let InvalidExtrinsicsRootProof {
valid_bundle_digests,
invalid_inherent_extrinsic_proofs,
invalid_inherent_extrinsic_proof,
domain_sudo_call_proof,
} = fraud_proof;

let invalid_inherent_extrinsic_data =
<InvalidInherentExtrinsicDataProof as BasicStorageProof<CBlock>>::verify::<SKP>(
invalid_inherent_extrinsic_proofs.clone(),
(),
&state_root,
)?;

let mut domain_inherent_extrinsic_data = invalid_inherent_extrinsic_proof
.verify::<CBlock, SKP>(domain_id, runtime_id, &state_root)?;

Expand All @@ -80,7 +88,7 @@ where
domain_inherent_extrinsic_data.maybe_sudo_runtime_call = domain_sudo_call.maybe_call;

let shuffling_seed = H256::from_slice(
extrinsics_shuffling_seed::<Hashing>(domain_inherent_extrinsic_data.block_randomness)
extrinsics_shuffling_seed::<Hashing>(invalid_inherent_extrinsic_data.block_randomness)
.as_ref(),
);

Expand All @@ -92,6 +100,7 @@ where
maybe_domain_sudo_call_extrinsic,
} = fraud_proof_runtime_interface::construct_domain_inherent_extrinsic(
domain_runtime_code,
invalid_inherent_extrinsic_data,
domain_inherent_extrinsic_data,
)
.ok_or(VerificationError::FailedToDeriveDomainInherentExtrinsic)?;
Expand Down
4 changes: 2 additions & 2 deletions crates/subspace-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1038,8 +1038,8 @@ pub struct StorageKeyProvider;
impl FraudProofStorageKeyProvider<NumberFor<Block>> for StorageKeyProvider {
fn storage_key(req: FraudProofStorageKeyRequest<NumberFor<Block>>) -> Vec<u8> {
match req {
FraudProofStorageKeyRequest::BlockRandomness => {
pallet_subspace::BlockRandomness::<Runtime>::hashed_key().to_vec()
FraudProofStorageKeyRequest::InvalidInherentExtrinsicData => {
pallet_domains::BlockInvalidInherentExtrinsicData::<Runtime>::hashed_key().to_vec()
}
FraudProofStorageKeyRequest::Timestamp => {
pallet_timestamp::Now::<Runtime>::hashed_key().to_vec()
Expand Down
8 changes: 8 additions & 0 deletions domains/client/domain-operator/src/fraud_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,13 @@ where
let maybe_runtime_id =
self.is_domain_runtime_upgraded_at(domain_id, consensus_block_hash)?;

let invalid_inherent_extrinsic_proofs = InvalidInherentExtrinsicDataProof::generate(
self.consensus_client.as_ref(),
consensus_block_hash,
(),
&self.storage_key_provider,
)?;

let invalid_inherent_extrinsic_proof = InvalidInherentExtrinsicProof::generate(
&self.storage_key_provider,
self.consensus_client.as_ref(),
Expand All @@ -404,6 +411,7 @@ where
maybe_domain_runtime_code_proof,
proof: FraudProofVariant::InvalidExtrinsicsRoot(InvalidExtrinsicsRootProof {
valid_bundle_digests,
invalid_inherent_extrinsic_proofs,
invalid_inherent_extrinsic_proof,
domain_sudo_call_proof,
}),
Expand Down
4 changes: 2 additions & 2 deletions test/subspace-test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1115,8 +1115,8 @@ pub struct StorageKeyProvider;
impl FraudProofStorageKeyProvider<NumberFor<Block>> for StorageKeyProvider {
fn storage_key(req: FraudProofStorageKeyRequest<NumberFor<Block>>) -> Vec<u8> {
match req {
FraudProofStorageKeyRequest::BlockRandomness => {
pallet_subspace::BlockRandomness::<Runtime>::hashed_key().to_vec()
FraudProofStorageKeyRequest::InvalidInherentExtrinsicData => {
pallet_domains::BlockInvalidInherentExtrinsicData::<Runtime>::hashed_key().to_vec()
}
FraudProofStorageKeyRequest::Timestamp => {
pallet_timestamp::Now::<Runtime>::hashed_key().to_vec()
Expand Down

0 comments on commit 4300b34

Please sign in to comment.