Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions pallets/transaction-storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ pub struct TransactionInfo {
block_chunks: u32,
}

impl TransactionInfo {
/// Get the number of total chunks.
///
/// See the `block_chunks` field of [`TransactionInfo`] for details.
pub fn total_chunks(txs: &[TransactionInfo]) -> u32 {
txs.last().map_or(0, |t| t.block_chunks)
}
}

/// Context of a `check_signed`/`check_unsigned` call.
#[derive(Clone, Copy)]
enum CheckContext {
Expand Down Expand Up @@ -228,7 +237,6 @@ pub mod pallet {
if obsolete > Zero::zero() {
weight.saturating_accrue(db_weight.writes(2));
<Transactions<T>>::remove(obsolete);
<ChunkCount<T>>::remove(obsolete);
}

// For `on_finalize`
Expand All @@ -244,7 +252,12 @@ pub mod pallet {
let number = <frame_system::Pallet<T>>::block_number();
let period = T::StoragePeriod::get();
let target_number = number.saturating_sub(period);
target_number.is_zero() || <ChunkCount<T>>::get(target_number) == 0

target_number.is_zero() || {
// An empty block means no transactions were stored, relying on the fact
// below that we store transactions only if they contain chunks.
!Transactions::<T>::contains_key(target_number)
}
},
"Storage proof must be checked once in the block"
);
Expand All @@ -253,7 +266,6 @@ pub mod pallet {
let transactions = <BlockTransactions<T>>::take();
let total_chunks = transactions.last().map_or(0, |t| t.block_chunks);
if total_chunks != 0 {
<ChunkCount<T>>::insert(n, total_chunks);
<Transactions<T>>::insert(n, transactions);
}
}
Expand Down Expand Up @@ -394,20 +406,12 @@ pub mod pallet {
let period = T::StoragePeriod::get();
let target_number = number.saturating_sub(period);
ensure!(!target_number.is_zero(), Error::<T>::UnexpectedProof);
let total_chunks = <ChunkCount<T>>::get(target_number);
ensure!(total_chunks != 0, Error::<T>::UnexpectedProof);
let total_chunks = ChunkCount::<T>::get(target_number);
let transactions =
Transactions::<T>::get(target_number).ok_or(Error::<T>::MissingStateData)?;

// Verify the proof with a "random" chunk (randomness is based on the parent hash).
let parent_hash = frame_system::Pallet::<T>::parent_hash();
Self::verify_chunk_proof(
proof,
parent_hash.as_ref(),
transactions.to_vec(),
total_chunks,
)?;
Self::verify_chunk_proof(proof, parent_hash.as_ref(), transactions.to_vec())?;
ProofChecked::<T>::put(true);
Self::deposit_event(Event::ProofChecked);
Ok(().into())
Expand Down Expand Up @@ -594,11 +598,6 @@ pub mod pallet {
OptionQuery,
>;

/// Count indexed chunks for each block.
#[pallet::storage]
pub(super) type ChunkCount<T: Config> =
StorageMap<_, Blake2_128Concat, BlockNumberFor<T>, u32, ValueQuery>;

// Intermediates
#[pallet::storage]
pub(super) type BlockTransactions<T: Config> =
Expand Down Expand Up @@ -998,12 +997,11 @@ pub mod pallet {
proof: TransactionStorageProof,
random_hash: &[u8],
infos: Vec<TransactionInfo>,
total_chunks: u32,
) -> Result<(), Error<T>> {
// Get the random chunk index - from all transactions in the block = [0..total_chunks).
let total_chunks: u32 = TransactionInfo::total_chunks(&infos);
ensure!(total_chunks != 0, Error::<T>::UnexpectedProof);
ensure!(!infos.is_empty(), Error::<T>::UnexpectedProof);

// Get the random chunk index (from all transactions in the block = 0..total_chunks).
let selected_block_chunk_index = random_chunk(random_hash, total_chunks);

// Let's find the corresponding transaction and its "local" chunk index for "global"
Expand Down
10 changes: 3 additions & 7 deletions pallets/transaction-storage/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use super::{
new_test_ext, run_to_block, RuntimeCall, RuntimeEvent, RuntimeOrigin, System, Test,
TransactionStorage,
},
AuthorizationExtent, AuthorizationScope, Event, AUTHORIZATION_NOT_EXPIRED, BAD_DATA_SIZE,
DEFAULT_MAX_TRANSACTION_SIZE,
AuthorizationExtent, AuthorizationScope, Event, TransactionInfo, AUTHORIZATION_NOT_EXPIRED,
BAD_DATA_SIZE, DEFAULT_MAX_TRANSACTION_SIZE,
};
use polkadot_sdk_frame::{
prelude::{frame_system::RawOrigin, *},
Expand All @@ -36,7 +36,6 @@ type Error = super::Error<Test>;

type Authorizations = super::Authorizations<Test>;
type BlockTransactions = super::BlockTransactions<Test>;
type ChunkCount = super::ChunkCount<Test>;
type Transactions = super::Transactions<Test>;

const MAX_DATA_SIZE: u32 = DEFAULT_MAX_TRANSACTION_SIZE;
Expand All @@ -63,10 +62,8 @@ fn discards_data() {
assert!(Transactions::get(1).is_some());
let transactions = Transactions::get(1).unwrap();
assert_eq!(transactions.len(), 2);
assert_eq!(ChunkCount::get(1), 16);
run_to_block(12, proof_provider);
assert!(Transactions::get(1).is_none());
assert_eq!(ChunkCount::get(1), 0);
});
}

Expand Down Expand Up @@ -188,8 +185,8 @@ fn verify_chunk_proof_works() {
run_to_block(2, || None);

// Read all the block transactions metadata.
let total_chunks = ChunkCount::get(1);
let tx_infos = Transactions::get(1).unwrap();
let total_chunks: u32 = TransactionInfo::total_chunks(&tx_infos);
assert_eq!(expected_total_chunks, total_chunks);
assert_eq!(9, tx_infos.len());

Expand All @@ -208,7 +205,6 @@ fn verify_chunk_proof_works() {
proof,
random_hash.as_ref(),
tx_infos.to_vec(),
total_chunks
));
}
});
Expand Down
42 changes: 19 additions & 23 deletions runtimes/bulletin-westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ pub mod fast_runtime_binary {
}

mod genesis_config_presets;
pub mod storage;
mod weights;
pub mod xcm_config;
pub mod storage;

extern crate alloc;

Expand All @@ -44,9 +44,7 @@ use frame_support::{
dispatch::DispatchClass,
genesis_builder_helper::{build_state, get_preset},
parameter_types,
traits::{
ConstBool, ConstU32, ConstU64, ConstU8, EitherOfDiverse, TransformOrigin,
},
traits::{ConstBool, ConstU32, ConstU64, ConstU8, EitherOfDiverse, TransformOrigin},
weights::{ConstantMultiplier, Weight},
PalletId,
};
Expand All @@ -68,10 +66,11 @@ use sp_core::{crypto::KeyTypeId, OpaqueMetadata};
pub use sp_runtime::BuildStorage;
use sp_runtime::{
generic, impl_opaque_keys,
traits::{AsSystemOriginSigner, Block as BlockT, DispatchInfoOf, Implication, PostDispatchInfoOf},
traits::{
AsSystemOriginSigner, Block as BlockT, DispatchInfoOf, Implication, PostDispatchInfoOf,
},
transaction_validity::{
TransactionSource,
TransactionValidity, TransactionValidityError, ValidTransaction,
TransactionSource, TransactionValidity, TransactionValidityError, ValidTransaction,
},
ApplyExtrinsicResult, MultiAddress, Perbill,
};
Expand Down Expand Up @@ -132,20 +131,20 @@ pub type Migrations = (
Runtime,
pallet_session::migrations::v1::InitOffenceSeverity<Runtime>,
>,
// permanent
pallet_xcm::migration::MigrateToLatestXcmVersion<Runtime>,
// permanent
pallet_xcm::migration::MigrateToLatestXcmVersion<Runtime>,
cumulus_pallet_aura_ext::migration::MigrateV0ToV1<Runtime>,
);

/// Executive: handles dispatch to the various modules.
#[allow(deprecated)]
pub type Executive = frame_executive::Executive<
Runtime,
Block,
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
(),
Runtime,
Block,
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
(),
>;

impl_opaque_keys! {
Expand Down Expand Up @@ -197,7 +196,6 @@ parameter_types! {
pub const SS58Prefix: u8 = 42;
}


// Configure FRAME pallets to include in runtime.
#[derive_impl(frame_system::config_preludes::ParaChainDefaultConfig)]
impl frame_system::Config for Runtime {
Expand Down Expand Up @@ -536,14 +534,12 @@ parameter_types! {
pub const DepositFactor: Balance = deposit(0, 32);
}


impl pallet_sudo::Config for Runtime {
type RuntimeCall = RuntimeCall;
type RuntimeEvent = RuntimeEvent;
type WeightInfo = pallet_sudo::weights::SubstrateWeight<Runtime>;
}


// Create the runtime by composing the FRAME pallets that were previously configured.
construct_runtime!(
pub enum Runtime
Expand Down Expand Up @@ -827,9 +823,9 @@ impl_runtime_apis! {
}
}

// Removed GetCoreSelectorApi implementation as it no longer exists in this SDK revision.
// Removed GetCoreSelectorApi implementation as it no longer exists in this SDK revision.

// TryRuntime omitted to silence deprecation warnings; re-enable if needed.
// TryRuntime omitted to silence deprecation warnings; re-enable if needed.

#[cfg(feature = "runtime-benchmarks")]
impl frame_benchmarking::Benchmark<Block> for Runtime {
Expand Down Expand Up @@ -913,9 +909,9 @@ impl_runtime_apis! {
))
}

fn reserve_transferable_asset_and_dest() -> Option<(Asset, Location)> {
None
}
fn reserve_transferable_asset_and_dest() -> Option<(Asset, Location)> {
None
}

fn set_up_complex_asset_transfer() -> Option<(Assets, u32, Location, alloc::boxed::Box<dyn FnOnce()>)> {
let native_location = Parent.into();
Expand Down
2 changes: 1 addition & 1 deletion runtimes/bulletin-westend/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ fn people_chain_can_authorize_storage_with_transact() {
let authorize_call = RuntimeCall::TransactionStorage(pallet_transaction_storage::Call::<
Runtime,
>::authorize_account {
who: account.clone().to_account_id(),
who: account.to_account_id(),
transactions: 16,
bytes: 1024,
});
Expand Down