Remove without_storage_info from pallet transaction-storage (#11668)

* Introduce BoundedVec

* Fix typos

* Add comments to the bounds

* Remove migration

* Improve bound value access syntax
This commit is contained in:
Sebastian Kunert
2022-06-15 23:12:26 +02:00
committed by GitHub
parent c013f0d6c1
commit 2248d19163
6 changed files with 59 additions and 48 deletions
+42 -38
View File
@@ -28,7 +28,7 @@ mod mock;
#[cfg(test)]
mod tests;
use codec::{Decode, Encode};
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::{
dispatch::{Dispatchable, GetDispatchInfo},
traits::{Currency, OnUnbalanced, ReservableCurrency},
@@ -57,7 +57,16 @@ pub const DEFAULT_MAX_TRANSACTION_SIZE: u32 = 8 * 1024 * 1024;
pub const DEFAULT_MAX_BLOCK_TRANSACTIONS: u32 = 512;
/// State data for a stored transaction.
#[derive(Encode, Decode, Clone, sp_runtime::RuntimeDebug, PartialEq, Eq, scale_info::TypeInfo)]
#[derive(
Encode,
Decode,
Clone,
sp_runtime::RuntimeDebug,
PartialEq,
Eq,
scale_info::TypeInfo,
MaxEncodedLen,
)]
pub struct TransactionInfo {
/// Chunk trie root.
chunk_root: <BlakeTwo256 as Hash>::Output,
@@ -95,6 +104,10 @@ pub mod pallet {
type FeeDestination: OnUnbalanced<NegativeImbalanceOf<Self>>;
/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
/// Maximum number of indexed transactions in the block.
type MaxBlockTransactions: Get<u32>;
/// Maximum data set in a single transaction in bytes.
type MaxTransactionSize: Get<u32>;
}
#[pallet::error]
@@ -129,7 +142,6 @@ pub mod pallet {
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::without_storage_info]
pub struct Pallet<T>(_);
#[pallet::hooks]
@@ -180,7 +192,7 @@ pub mod pallet {
pub fn store(origin: OriginFor<T>, data: Vec<u8>) -> DispatchResult {
ensure!(data.len() > 0, Error::<T>::EmptyTransaction);
ensure!(
data.len() <= MaxTransactionSize::<T>::get() as usize,
data.len() <= T::MaxTransactionSize::get() as usize,
Error::<T>::TransactionTooLarge
);
let sender = ensure_signed(origin)?;
@@ -198,17 +210,19 @@ pub mod pallet {
let mut index = 0;
<BlockTransactions<T>>::mutate(|transactions| {
if transactions.len() + 1 > MaxBlockTransactions::<T>::get() as usize {
if transactions.len() + 1 > T::MaxBlockTransactions::get() as usize {
return Err(Error::<T>::TooManyTransactions)
}
let total_chunks = transactions.last().map_or(0, |t| t.block_chunks) + chunk_count;
index = transactions.len() as u32;
transactions.push(TransactionInfo {
chunk_root: root,
size: data.len() as u32,
content_hash: content_hash.into(),
block_chunks: total_chunks,
});
transactions
.try_push(TransactionInfo {
chunk_root: root,
size: data.len() as u32,
content_hash: content_hash.into(),
block_chunks: total_chunks,
})
.map_err(|_| Error::<T>::TooManyTransactions)?;
Ok(())
})?;
Self::deposit_event(Event::Stored { index });
@@ -238,19 +252,20 @@ pub mod pallet {
let mut index = 0;
<BlockTransactions<T>>::mutate(|transactions| {
if transactions.len() + 1 > MaxBlockTransactions::<T>::get() as usize {
if transactions.len() + 1 > T::MaxBlockTransactions::get() as usize {
return Err(Error::<T>::TooManyTransactions)
}
let chunks = num_chunks(info.size);
let total_chunks = transactions.last().map_or(0, |t| t.block_chunks) + chunks;
index = transactions.len() as u32;
transactions.push(TransactionInfo {
chunk_root: info.chunk_root,
size: info.size,
content_hash: info.content_hash,
block_chunks: total_chunks,
});
Ok(())
transactions
.try_push(TransactionInfo {
chunk_root: info.chunk_root,
size: info.size,
content_hash: info.content_hash,
block_chunks: total_chunks,
})
.map_err(|_| Error::<T>::TooManyTransactions)
})?;
Self::deposit_event(Event::Renewed { index });
Ok(().into())
@@ -324,8 +339,13 @@ pub mod pallet {
/// Collection of transaction metadata by block number.
#[pallet::storage]
#[pallet::getter(fn transaction_roots)]
pub(super) type Transactions<T: Config> =
StorageMap<_, Blake2_128Concat, T::BlockNumber, Vec<TransactionInfo>, OptionQuery>;
pub(super) type Transactions<T: Config> = StorageMap<
_,
Blake2_128Concat,
T::BlockNumber,
BoundedVec<TransactionInfo, T::MaxBlockTransactions>,
OptionQuery,
>;
/// Count indexed chunks for each block.
#[pallet::storage]
@@ -342,16 +362,6 @@ pub mod pallet {
/// Storage fee per transaction.
pub(super) type EntryFee<T: Config> = StorageValue<_, BalanceOf<T>>;
#[pallet::storage]
#[pallet::getter(fn max_transaction_size)]
/// Maximum data set in a single transaction in bytes.
pub(super) type MaxTransactionSize<T: Config> = StorageValue<_, u32, ValueQuery>;
#[pallet::storage]
#[pallet::getter(fn max_block_transactions)]
/// Maximum number of indexed transactions in the block.
pub(super) type MaxBlockTransactions<T: Config> = StorageValue<_, u32, ValueQuery>;
/// Storage period for data in blocks. Should match `sp_storage_proof::DEFAULT_STORAGE_PERIOD`
/// for block authoring.
#[pallet::storage]
@@ -360,7 +370,7 @@ pub mod pallet {
// Intermediates
#[pallet::storage]
pub(super) type BlockTransactions<T: Config> =
StorageValue<_, Vec<TransactionInfo>, ValueQuery>;
StorageValue<_, BoundedVec<TransactionInfo, T::MaxBlockTransactions>, ValueQuery>;
/// Was the proof checked in this block?
#[pallet::storage]
@@ -371,8 +381,6 @@ pub mod pallet {
pub byte_fee: BalanceOf<T>,
pub entry_fee: BalanceOf<T>,
pub storage_period: T::BlockNumber,
pub max_block_transactions: u32,
pub max_transaction_size: u32,
}
#[cfg(feature = "std")]
@@ -382,8 +390,6 @@ pub mod pallet {
byte_fee: 10u32.into(),
entry_fee: 1000u32.into(),
storage_period: sp_transaction_storage_proof::DEFAULT_STORAGE_PERIOD.into(),
max_block_transactions: DEFAULT_MAX_BLOCK_TRANSACTIONS,
max_transaction_size: DEFAULT_MAX_TRANSACTION_SIZE,
}
}
}
@@ -393,8 +399,6 @@ pub mod pallet {
fn build(&self) {
<ByteFee<T>>::put(&self.byte_fee);
<EntryFee<T>>::put(&self.entry_fee);
<MaxTransactionSize<T>>::put(&self.max_transaction_size);
<MaxBlockTransactions<T>>::put(&self.max_block_transactions);
<StoragePeriod<T>>::put(&self.storage_period);
}
}