Skip to content

Commit

Permalink
Merge pull request #3315 from autonomys/fraud-proof-data-timestamp
Browse files Browse the repository at this point in the history
Move timestamp into invalid extrinsics fraud proof storage
  • Loading branch information
NingLin-P authored Dec 20, 2024
2 parents 1a77163 + 797ab57 commit 63c0091
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 36 deletions.
13 changes: 12 additions & 1 deletion crates/pallet-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ mod pallet {
use frame_support::pallet_prelude::*;
use frame_support::traits::fungible::{Inspect, InspectHold, Mutate, MutateHold};
use frame_support::traits::tokens::Preservation;
use frame_support::traits::Randomness as RandomnessT;
use frame_support::traits::{Randomness as RandomnessT, Time};
use frame_support::weights::Weight;
use frame_support::{Identity, PalletError};
use frame_system::pallet_prelude::*;
Expand Down Expand Up @@ -390,6 +390,9 @@ mod pallet {
/// Storage fee interface used to deal with bundle storage fee
type StorageFee: StorageFee<BalanceOf<Self>>;

/// The block timestamp
type BlockTimestamp: Time;

/// The block slot
type BlockSlot: BlockSlot<Self>;

Expand Down Expand Up @@ -1913,8 +1916,16 @@ mod pallet {
Into::<H256>::into(Self::extrinsics_shuffling_seed()).to_fixed_bytes(),
);

// There is no actual conversion here, but the trait bounds required to prove that
// (and debug-print the error in expect()) are very verbose.
let timestamp = T::BlockTimestamp::now()
.try_into()
.map_err(|_| ())
.expect("Moment is the same type in both pallets; qed");

let invalid_inherent_extrinsic_data = InvalidInherentExtrinsicData {
extrinsics_shuffling_seed,
timestamp,
};

BlockInvalidInherentExtrinsicData::<T>::set(Some(invalid_inherent_extrinsic_data));
Expand Down
1 change: 1 addition & 0 deletions crates/pallet-domains/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ impl pallet_domains::Config for Test {
type Randomness = MockRandomness;
type PalletId = DomainsPalletId;
type StorageFee = DummyStorageFee;
type BlockTimestamp = pallet_timestamp::Pallet<Test>;
type BlockSlot = DummyBlockSlot;
type DomainsTransfersTracker = MockDomainsTransfersTracker;
type MaxInitialDomainAccounts = MaxInitialDomainAccounts;
Expand Down
31 changes: 3 additions & 28 deletions crates/sp-domains-fraud-proof/src/storage_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ pub enum VerificationError {
RuntimeCodeNotFound,
UnexpectedDomainRuntimeUpgrade,
InvalidInherentExtrinsicStorageProof(StorageProofVerificationError),
TimestampStorageProof(StorageProofVerificationError),
SuccessfulBundlesStorageProof(StorageProofVerificationError),
TransactionByteFeeStorageProof(StorageProofVerificationError),
DomainAllowlistUpdatesStorageProof(StorageProofVerificationError),
Expand All @@ -56,7 +55,6 @@ pub enum VerificationError {
#[derive(Clone, Debug, Decode, Encode, Eq, PartialEq, TypeInfo)]
pub enum FraudProofStorageKeyRequest<Number> {
InvalidInherentExtrinsicData,
Timestamp,
SuccessfulBundles(DomainId),
TransactionByteFee,
DomainAllowlistUpdates(DomainId),
Expand All @@ -73,7 +71,6 @@ impl<Number> FraudProofStorageKeyRequest<Number> {
Self::InvalidInherentExtrinsicData => {
VerificationError::InvalidInherentExtrinsicStorageProof(err)
}
Self::Timestamp => VerificationError::TimestampStorageProof(err),
Self::SuccessfulBundles(_) => VerificationError::SuccessfulBundlesStorageProof(err),
Self::TransactionByteFee => VerificationError::TransactionByteFeeStorageProof(err),
Self::DomainAllowlistUpdates(_) => {
Expand Down Expand Up @@ -188,17 +185,6 @@ impl<Block: BlockT> BasicStorageProof<Block> for DomainChainsAllowlistUpdateStor
}
}

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

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

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

Expand Down Expand Up @@ -409,6 +395,9 @@ impl MaybeDomainRuntimeUpgradedProof {
pub struct InvalidInherentExtrinsicData {
/// Extrinsics shuffling seed, derived from block randomness
pub extrinsics_shuffling_seed: Randomness,

/// Block timestamp
pub timestamp: Moment,
}

impl PassBy for InvalidInherentExtrinsicData {
Expand All @@ -428,9 +417,6 @@ impl<Block: BlockT> BasicStorageProof<Block> for InvalidInherentExtrinsicDataPro

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

/// Optional domain runtime code upgrade storage proof
pub maybe_domain_runtime_upgrade_proof: MaybeDomainRuntimeUpgradedProof,

Expand All @@ -447,7 +433,6 @@ pub struct InvalidInherentExtrinsicProof {
/// The verified data from an `InvalidInherentExtrinsicProof`
#[derive(Clone, Debug, Decode, Encode, Eq, PartialEq, TypeInfo)]
pub struct InvalidInherentExtrinsicVerified {
pub timestamp: Moment,
pub maybe_domain_runtime_upgrade: Option<Vec<u8>>,
pub consensus_transaction_byte_fee: Balance,
pub domain_chain_allowlist: DomainAllowlistUpdates,
Expand All @@ -467,8 +452,6 @@ impl InvalidInherentExtrinsicProof {
block_hash: Block::Hash,
maybe_runtime_id: Option<RuntimeId>,
) -> Result<Self, GenerationError> {
let timestamp_proof =
TimestampStorageProof::generate(proof_provider, block_hash, (), storage_key_provider)?;
let maybe_domain_runtime_upgrade_proof = MaybeDomainRuntimeUpgradedProof::generate(
storage_key_provider,
proof_provider,
Expand All @@ -495,7 +478,6 @@ impl InvalidInherentExtrinsicProof {
)?;

Ok(Self {
timestamp_proof,
maybe_domain_runtime_upgrade_proof,
dynamic_cost_of_storage_proof,
consensus_chain_byte_fee_proof,
Expand All @@ -509,12 +491,6 @@ impl InvalidInherentExtrinsicProof {
runtime_id: RuntimeId,
state_root: &Block::Hash,
) -> Result<InvalidInherentExtrinsicVerified, VerificationError> {
let timestamp = <TimestampStorageProof as BasicStorageProof<Block>>::verify::<SKP>(
self.timestamp_proof.clone(),
(),
state_root,
)?;

let maybe_domain_runtime_upgrade = self
.maybe_domain_runtime_upgrade_proof
.verify::<Block, SKP>(runtime_id, state_root)?;
Expand Down Expand Up @@ -546,7 +522,6 @@ impl InvalidInherentExtrinsicProof {
)?;

Ok(InvalidInherentExtrinsicVerified {
timestamp,
maybe_domain_runtime_upgrade,
consensus_transaction_byte_fee,
domain_chain_allowlist,
Expand Down
2 changes: 1 addition & 1 deletion crates/sp-domains-fraud-proof/src/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ where
let shuffling_seed = invalid_inherent_extrinsic_data.extrinsics_shuffling_seed;

let domain_inherent_extrinsic_data = DomainInherentExtrinsicData {
timestamp: inherent_extrinsic_verified.timestamp,
timestamp: invalid_inherent_extrinsic_data.timestamp,
maybe_domain_runtime_upgrade: inherent_extrinsic_verified.maybe_domain_runtime_upgrade,
consensus_transaction_byte_fee: inherent_extrinsic_verified.consensus_transaction_byte_fee,
domain_chain_allowlist: inherent_extrinsic_verified.domain_chain_allowlist,
Expand Down
4 changes: 1 addition & 3 deletions crates/subspace-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,7 @@ impl pallet_domains::Config for Runtime {
type Randomness = Subspace;
type PalletId = DomainsPalletId;
type StorageFee = TransactionFees;
type BlockTimestamp = pallet_timestamp::Pallet<Runtime>;
type BlockSlot = BlockSlot;
type BundleLongevity = BundleLongevity;
type DomainsTransfersTracker = Transporter;
Expand Down Expand Up @@ -1041,9 +1042,6 @@ impl FraudProofStorageKeyProvider<NumberFor<Block>> for StorageKeyProvider {
FraudProofStorageKeyRequest::InvalidInherentExtrinsicData => {
pallet_domains::BlockInvalidInherentExtrinsicData::<Runtime>::hashed_key().to_vec()
}
FraudProofStorageKeyRequest::Timestamp => {
pallet_timestamp::Now::<Runtime>::hashed_key().to_vec()
}
FraudProofStorageKeyRequest::SuccessfulBundles(domain_id) => {
pallet_domains::SuccessfulBundles::<Runtime>::hashed_key_for(domain_id)
}
Expand Down
4 changes: 1 addition & 3 deletions test/subspace-test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,7 @@ impl pallet_domains::Config for Runtime {
type MinNominatorStake = MinNominatorStake;
type PalletId = DomainsPalletId;
type StorageFee = TransactionFees;
type BlockTimestamp = pallet_timestamp::Pallet<Runtime>;
type BlockSlot = BlockSlot;
type BundleLongevity = BundleLongevity;
type DomainsTransfersTracker = Transporter;
Expand Down Expand Up @@ -1118,9 +1119,6 @@ impl FraudProofStorageKeyProvider<NumberFor<Block>> for StorageKeyProvider {
FraudProofStorageKeyRequest::InvalidInherentExtrinsicData => {
pallet_domains::BlockInvalidInherentExtrinsicData::<Runtime>::hashed_key().to_vec()
}
FraudProofStorageKeyRequest::Timestamp => {
pallet_timestamp::Now::<Runtime>::hashed_key().to_vec()
}
FraudProofStorageKeyRequest::SuccessfulBundles(domain_id) => {
pallet_domains::SuccessfulBundles::<Runtime>::hashed_key_for(domain_id)
}
Expand Down

0 comments on commit 63c0091

Please sign in to comment.