Skip to content

Commit

Permalink
Merge allow list storage proof, and cleanup old struct
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Dec 23, 2024
1 parent f657e5c commit a42d135
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 129 deletions.
49 changes: 35 additions & 14 deletions crates/pallet-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ use sp_consensus_subspace::WrappedPotOutput;
use sp_core::H256;
use sp_domains::bundle_producer_election::BundleProducerElectionParams;
use sp_domains::{
DomainBundleLimit, DomainId, DomainInstanceData, ExecutionReceipt, OpaqueBundle, OperatorId,
OperatorPublicKey, OperatorRewardSource, OperatorSignature, ProofOfElection, RuntimeId,
SealedSingletonReceipt, DOMAIN_EXTRINSICS_SHUFFLING_SEED_SUBJECT, EMPTY_EXTRINSIC_ROOT,
DomainAllowlistUpdates, DomainBundleLimit, DomainId, DomainInstanceData, ExecutionReceipt,
OpaqueBundle, OperatorId, OperatorPublicKey, OperatorRewardSource, OperatorSignature,
ProofOfElection, RuntimeId, SealedSingletonReceipt, DOMAIN_EXTRINSICS_SHUFFLING_SEED_SUBJECT,
EMPTY_EXTRINSIC_ROOT,
};
use sp_domains_fraud_proof::fraud_proof::{
DomainRuntimeCodeAt, FraudProof, FraudProofVariant, InvalidBlockFeesProof,
Expand Down Expand Up @@ -208,9 +209,10 @@ mod pallet {
#[cfg(not(feature = "runtime-benchmarks"))]
use crate::MAX_NOMINATORS_TO_SLASH;
use crate::{
BalanceOf, BlockSlot, BlockTreeNodeFor, DomainBlockNumberFor, ElectionVerificationParams,
ExecutionReceiptOf, FraudProofFor, HoldIdentifier, NominatorId, OpaqueBundleOf,
ReceiptHashFor, SingletonReceiptOf, StateRootOf, MAX_BUNDLE_PER_BLOCK, STORAGE_VERSION,
BalanceOf, BlockSlot, BlockTreeNodeFor, DomainAllowlistUpdatesProvider,
DomainBlockNumberFor, ElectionVerificationParams, ExecutionReceiptOf, FraudProofFor,
HoldIdentifier, NominatorId, OpaqueBundleOf, ReceiptHashFor, SingletonReceiptOf,
StateRootOf, MAX_BUNDLE_PER_BLOCK, STORAGE_VERSION,
};
#[cfg(not(feature = "std"))]
use alloc::string::String;
Expand Down Expand Up @@ -415,6 +417,9 @@ mod pallet {
/// A hook to call after a domain is instantiated
type OnDomainInstantiated: OnDomainInstantiated;

/// Domain allow list update source
type DomainAllowlistUpdates: DomainAllowlistUpdatesProvider;

/// Hash type of MMR
type MmrHash: Parameter + Member + Default + Clone;

Expand Down Expand Up @@ -1856,7 +1861,8 @@ mod pallet {

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

#[pallet::hooks]
// TODO: proper benchmark
Expand Down Expand Up @@ -1902,7 +1908,7 @@ mod pallet {
}
}

BlockInvalidInherentExtrinsicData::<T>::kill();
let _ = BlockInvalidInherentExtrinsicData::<T>::clear(u32::MAX, None);

Weight::zero()
}
Expand Down Expand Up @@ -1931,13 +1937,23 @@ mod pallet {
let consensus_transaction_byte_fee =
sp_domains::DOMAIN_STORAGE_FEE_MULTIPLIER * transaction_byte_fee;

let invalid_inherent_extrinsic_data = InvalidInherentExtrinsicData {
extrinsics_shuffling_seed,
timestamp,
consensus_transaction_byte_fee,
};
for domain_id in SuccessfulBundles::<T>::iter_keys() {
let domain_chain_allowlist =
T::DomainAllowlistUpdates::domain_allowlist_updates(domain_id)
.unwrap_or_default();

let invalid_inherent_extrinsic_data = InvalidInherentExtrinsicData {
extrinsics_shuffling_seed,
timestamp,
consensus_transaction_byte_fee,
domain_chain_allowlist,
};

BlockInvalidInherentExtrinsicData::<T>::set(Some(invalid_inherent_extrinsic_data));
BlockInvalidInherentExtrinsicData::<T>::insert(
domain_id,
invalid_inherent_extrinsic_data,
);
}
}

let _ = LastEpochStakingDistribution::<T>::clear(u32::MAX, None);
Expand Down Expand Up @@ -3120,3 +3136,8 @@ pub fn calculate_tx_range(
};
new_tx_range.clamp(lower_bound, upper_bound)
}

/// An abstraction that gets domain allow list updates from pallet-messenger.
pub trait DomainAllowlistUpdatesProvider {
fn domain_allowlist_updates(domain_id: DomainId) -> Option<DomainAllowlistUpdates>;
}
16 changes: 12 additions & 4 deletions crates/pallet-domains/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use crate::runtime_registry::ScheduledRuntimeUpgrade;
use crate::staking::Operator;
use crate::{
self as pallet_domains, BalanceOf, BlockSlot, BlockTree, BlockTreeNodes, BundleError, Config,
ConsensusBlockHash, DomainBlockNumberFor, DomainHashingFor, DomainRegistry,
DomainRuntimeUpgradeRecords, DomainRuntimeUpgrades, ExecutionInbox, ExecutionReceiptOf,
FraudProofError, FungibleHoldId, HeadDomainNumber, HeadReceiptNumber, NextDomainId, Operators,
RuntimeRegistry, ScheduledRuntimeUpgrades,
ConsensusBlockHash, DomainAllowlistUpdates, DomainAllowlistUpdatesProvider,
DomainBlockNumberFor, DomainHashingFor, DomainRegistry, DomainRuntimeUpgradeRecords,
DomainRuntimeUpgrades, ExecutionInbox, ExecutionReceiptOf, FraudProofError, FungibleHoldId,
HeadDomainNumber, HeadReceiptNumber, NextDomainId, Operators, RuntimeRegistry,
ScheduledRuntimeUpgrades,
};
use codec::{Decode, Encode, MaxEncodedLen};
use core::mem;
Expand Down Expand Up @@ -237,6 +238,12 @@ impl sp_domains::DomainsTransfersTracker<Balance> for MockDomainsTransfersTracke
}
}

impl DomainAllowlistUpdatesProvider for () {
fn domain_allowlist_updates(_domain_id: DomainId) -> Option<DomainAllowlistUpdates> {
None
}
}

impl pallet_domains::Config for Test {
type RuntimeEvent = RuntimeEvent;
type DomainHash = sp_core::H256;
Expand Down Expand Up @@ -272,6 +279,7 @@ impl pallet_domains::Config for Test {
type ConsensusSlotProbability = SlotProbability;
type DomainBundleSubmitted = ();
type OnDomainInstantiated = ();
type DomainAllowlistUpdates = ();
type Balance = Balance;
type MmrHash = H256;
type MmrProofVerifier = ();
Expand Down
5 changes: 0 additions & 5 deletions crates/pallet-transaction-fees/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@

pub mod weights;

#[cfg(not(feature = "std"))]
extern crate alloc;

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use codec::{Codec, Decode, Encode};
use frame_support::sp_runtime::traits::Zero;
use frame_support::sp_runtime::SaturatedConversion;
Expand Down
4 changes: 0 additions & 4 deletions crates/sp-domains-fraud-proof/src/fraud_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,10 +497,6 @@ pub struct InvalidExtrinsicsRootProof {
/// 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 domain runtime code upgrade storage proof
pub maybe_domain_runtime_upgrade_proof: MaybeDomainRuntimeUpgradedProof,

Expand Down
86 changes: 9 additions & 77 deletions crates/sp-domains-fraud-proof/src/storage_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ pub enum VerificationError {
UnexpectedDomainRuntimeUpgrade,
InvalidInherentExtrinsicStorageProof(StorageProofVerificationError),
SuccessfulBundlesStorageProof(StorageProofVerificationError),
DomainAllowlistUpdatesStorageProof(StorageProofVerificationError),
DomainRuntimeUpgradesStorageProof(StorageProofVerificationError),
RuntimeRegistryStorageProof(StorageProofVerificationError),
DigestStorageProof(StorageProofVerificationError),
Expand All @@ -50,9 +49,8 @@ pub enum VerificationError {

#[derive(Clone, Debug, Decode, Encode, Eq, PartialEq, TypeInfo)]
pub enum FraudProofStorageKeyRequest<Number> {
InvalidInherentExtrinsicData,
InvalidInherentExtrinsicData(DomainId),
SuccessfulBundles(DomainId),
DomainAllowlistUpdates(DomainId),
DomainRuntimeUpgrades,
RuntimeRegistry(RuntimeId),
DomainSudoCall(DomainId),
Expand All @@ -62,13 +60,10 @@ pub enum FraudProofStorageKeyRequest<Number> {
impl<Number> FraudProofStorageKeyRequest<Number> {
fn into_error(self, err: StorageProofVerificationError) -> VerificationError {
match self {
Self::InvalidInherentExtrinsicData => {
Self::InvalidInherentExtrinsicData(_) => {
VerificationError::InvalidInherentExtrinsicStorageProof(err)
}
Self::SuccessfulBundles(_) => VerificationError::SuccessfulBundlesStorageProof(err),
Self::DomainAllowlistUpdates(_) => {
VerificationError::DomainAllowlistUpdatesStorageProof(err)
}
Self::DomainRuntimeUpgrades => {
VerificationError::DomainRuntimeUpgradesStorageProof(err)
}
Expand Down Expand Up @@ -167,18 +162,6 @@ impl<Block: BlockT> BasicStorageProof<Block> for SuccessfulBundlesProof {
}
}

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

impl_storage_proof!(DomainChainsAllowlistUpdateStorageProof);
impl<Block: BlockT> BasicStorageProof<Block> for DomainChainsAllowlistUpdateStorageProof {
type StorageValue = DomainAllowlistUpdates;
type Key = DomainId;
fn storage_key_request(key: Self::Key) -> FraudProofStorageKeyRequest<NumberFor<Block>> {
FraudProofStorageKeyRequest::DomainAllowlistUpdates(key)
}
}

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

Expand Down Expand Up @@ -366,7 +349,7 @@ impl MaybeDomainRuntimeUpgradedProof {
}
}

#[derive(Clone, Debug, Decode, Encode, Eq, PartialEq, TypeInfo)]
#[derive(Clone, Debug, Default, Decode, Encode, Eq, PartialEq, TypeInfo)]
pub struct InvalidInherentExtrinsicData {
/// Extrinsics shuffling seed, derived from block randomness
pub extrinsics_shuffling_seed: Randomness,
Expand All @@ -376,6 +359,9 @@ pub struct InvalidInherentExtrinsicData {

/// Transaction byte fee, derived from dynamic cost of storage and the consensus chain byte fee
pub consensus_transaction_byte_fee: Balance,

/// Changes in the chains that are allowed to open a channel with each domain
pub domain_chain_allowlist: DomainAllowlistUpdates,
}

impl PassBy for InvalidInherentExtrinsicData {
Expand All @@ -388,63 +374,9 @@ 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 {
/// Change in the allowed chains storage proof
pub domain_chain_allowlist_proof: DomainChainsAllowlistUpdateStorageProof,
}

/// The verified data from an `InvalidInherentExtrinsicProof`
#[derive(Clone, Debug, Decode, Encode, Eq, PartialEq, TypeInfo)]
pub struct InvalidInherentExtrinsicVerified {
pub domain_chain_allowlist: DomainAllowlistUpdates,
}

impl InvalidInherentExtrinsicProof {
#[cfg(feature = "std")]
#[allow(clippy::let_and_return)]
pub fn generate<
Block: BlockT,
PP: ProofProvider<Block>,
SKP: FraudProofStorageKeyProviderInstance<NumberFor<Block>>,
>(
storage_key_provider: &SKP,
proof_provider: &PP,
domain_id: DomainId,
block_hash: Block::Hash,
) -> Result<Self, GenerationError> {
let domain_chain_allowlist_proof = DomainChainsAllowlistUpdateStorageProof::generate(
proof_provider,
block_hash,
domain_id,
storage_key_provider,
)?;

Ok(Self {
domain_chain_allowlist_proof,
})
}

pub fn verify<Block: BlockT, SKP: FraudProofStorageKeyProvider<NumberFor<Block>>>(
&self,
domain_id: DomainId,
state_root: &Block::Hash,
) -> Result<InvalidInherentExtrinsicVerified, VerificationError> {
let domain_chain_allowlist =
<DomainChainsAllowlistUpdateStorageProof as BasicStorageProof<Block>>::verify::<SKP>(
self.domain_chain_allowlist_proof.clone(),
domain_id,
state_root,
)?;

Ok(InvalidInherentExtrinsicVerified {
domain_chain_allowlist,
})
type Key = DomainId;
fn storage_key_request(key: Self::Key) -> FraudProofStorageKeyRequest<NumberFor<Block>> {
FraudProofStorageKeyRequest::InvalidInherentExtrinsicData(key)
}
}

Expand Down
8 changes: 2 additions & 6 deletions crates/sp-domains-fraud-proof/src/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,17 @@ where
let InvalidExtrinsicsRootProof {
valid_bundle_digests,
invalid_inherent_extrinsic_proofs,
invalid_inherent_extrinsic_proof,
maybe_domain_runtime_upgrade_proof,
domain_sudo_call_proof,
} = fraud_proof;

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

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

let maybe_domain_runtime_upgrade =
maybe_domain_runtime_upgrade_proof.verify::<CBlock, SKP>(runtime_id, &state_root)?;

Expand All @@ -97,7 +93,7 @@ where
maybe_domain_runtime_upgrade,
consensus_transaction_byte_fee: invalid_inherent_extrinsic_data
.consensus_transaction_byte_fee,
domain_chain_allowlist: inherent_extrinsic_verified.domain_chain_allowlist,
domain_chain_allowlist: invalid_inherent_extrinsic_data.domain_chain_allowlist,
maybe_sudo_runtime_call: domain_sudo_call.maybe_call,
};

Expand Down
18 changes: 13 additions & 5 deletions crates/subspace-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,14 @@ impl pallet_domains::BlockSlot<Runtime> for BlockSlot {
}
}

pub struct DomainAllowlistUpdatesSource;

impl pallet_domains::DomainAllowlistUpdatesProvider for DomainAllowlistUpdatesSource {
fn domain_allowlist_updates(domain_id: DomainId) -> Option<DomainAllowlistUpdates> {
Messenger::domain_chains_allowlist_update(domain_id)
}
}

pub struct OnChainRewards;

impl sp_domains::OnChainRewards<Balance> for OnChainRewards {
Expand Down Expand Up @@ -844,6 +852,7 @@ impl pallet_domains::Config for Runtime {
type MinInitialDomainAccountBalance = MinInitialDomainAccountBalance;
type DomainBundleSubmitted = Messenger;
type OnDomainInstantiated = Messenger;
type DomainAllowlistUpdates = DomainAllowlistUpdatesSource;
type Balance = Balance;
type MmrHash = mmr::Hash;
type MmrProofVerifier = MmrProofVerifier;
Expand Down Expand Up @@ -1039,15 +1048,14 @@ pub struct StorageKeyProvider;
impl FraudProofStorageKeyProvider<NumberFor<Block>> for StorageKeyProvider {
fn storage_key(req: FraudProofStorageKeyRequest<NumberFor<Block>>) -> Vec<u8> {
match req {
FraudProofStorageKeyRequest::InvalidInherentExtrinsicData => {
pallet_domains::BlockInvalidInherentExtrinsicData::<Runtime>::hashed_key().to_vec()
FraudProofStorageKeyRequest::InvalidInherentExtrinsicData(domain_id) => {
pallet_domains::BlockInvalidInherentExtrinsicData::<Runtime>::hashed_key_for(
domain_id,
)
}
FraudProofStorageKeyRequest::SuccessfulBundles(domain_id) => {
pallet_domains::SuccessfulBundles::<Runtime>::hashed_key_for(domain_id)
}
FraudProofStorageKeyRequest::DomainAllowlistUpdates(domain_id) => {
Messenger::domain_allow_list_update_storage_key(domain_id)
}
FraudProofStorageKeyRequest::DomainRuntimeUpgrades => {
pallet_domains::DomainRuntimeUpgrades::<Runtime>::hashed_key().to_vec()
}
Expand Down
10 changes: 1 addition & 9 deletions domains/client/domain-operator/src/fraud_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,15 +385,8 @@ where
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(),
domain_id,
consensus_block_hash,
&self.storage_key_provider,
)?;

let maybe_domain_runtime_upgrade_proof = MaybeDomainRuntimeUpgradedProof::generate(
Expand All @@ -418,7 +411,6 @@ where
proof: FraudProofVariant::InvalidExtrinsicsRoot(InvalidExtrinsicsRootProof {
valid_bundle_digests,
invalid_inherent_extrinsic_proofs,
invalid_inherent_extrinsic_proof,
maybe_domain_runtime_upgrade_proof,
domain_sudo_call_proof,
}),
Expand Down
Loading

0 comments on commit a42d135

Please sign in to comment.