Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
23 changes: 21 additions & 2 deletions pallets/transaction-storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use polkadot_sdk_frame::{
deps::{sp_core::sp_std::prelude::*, *},
prelude::*,
traits::{
fungible::{Balanced, Credit, Inspect, Mutate, MutateHold},
fungible::{hold::Balanced, Credit, Inspect, Mutate, MutateHold},
parameter_types,
},
};
Expand Down Expand Up @@ -187,6 +187,9 @@ pub mod pallet {
+ Dispatchable<RuntimeOrigin = Self::RuntimeOrigin>
+ GetDispatchInfo
+ From<frame_system::Call<Self>>;
/// Whether storage-related extrinsics charge a storage fee.
#[pallet::constant]
type ChargeStorageFee: Get<bool>;
/// The fungible type for this pallet.
type Currency: Mutate<Self::AccountId>
+ MutateHold<Self::AccountId, Reason = Self::RuntimeHoldReason>
Expand Down Expand Up @@ -344,11 +347,15 @@ pub mod pallet {
#[pallet::call_index(0)]
#[pallet::weight(T::WeightInfo::store(data.len() as u32))]
#[pallet::feeless_if(|origin: &OriginFor<T>, data: &Vec<u8>| -> bool { /*TODO: add here correct validation */ true })]
pub fn store(_origin: OriginFor<T>, data: Vec<u8>) -> DispatchResult {
pub fn store(origin: OriginFor<T>, data: Vec<u8>) -> DispatchResult {
// In the case of a regular unsigned transaction, this should have been checked by
// pre_dispatch. In the case of a regular signed transaction, this should have been
// checked by pre_dispatch_signed.
Self::ensure_data_size_ok(data.len())?;
if T::ChargeStorageFee::get() {
let sender = ensure_signed(origin)?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's wait with this a little bit, we need to fix feeless_if TODO and remove ValidateUnsigned and use AuthorizeCall extension first, because we also support unsigned transaction with ValidateUnsigned,
so if we allow ChargeStorageFee=true those will fail

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RafalMirowski1 maybe just unrelated note, as we discussed "pass authorization from check_signed to the fn store over origin", I have a feeling that we could use it here also, or make this as a part of ValidateSigned, let's see

Self::apply_fee(sender, data.len() as u32)?;
}

// Chunk data and compute storage root
let chunks: Vec<_> = data.chunks(CHUNK_SIZE).map(|c| c.to_vec()).collect();
Expand Down Expand Up @@ -889,6 +896,18 @@ pub mod pallet {
RetentionPeriod::<T>::get()
}

fn apply_fee(sender: T::AccountId, size: u32) -> DispatchResult {
let byte_fee = ByteFee::<T>::get().ok_or(Error::<T>::NotConfigured)?;
let entry_fee = EntryFee::<T>::get().ok_or(Error::<T>::NotConfigured)?;
let fee = byte_fee.saturating_mul(size.into()).saturating_add(entry_fee);
T::Currency::hold(&HoldReason::StorageFeeHold.into(), &sender, fee)?;
let (credit, remainder) =
T::Currency::slash(&HoldReason::StorageFeeHold.into(), &sender, fee);
debug_assert!(remainder.is_zero());
T::FeeDestination::on_unbalanced(credit);
Ok(())
}

/// Returns `true` if a blob of the given size can be stored.
fn data_size_ok(size: usize) -> bool {
(size > 0) && (size <= T::MaxTransactionSize::get() as usize)
Expand Down
1 change: 1 addition & 0 deletions pallets/transaction-storage/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ parameter_types! {
impl pallet_transaction_storage::Config for Test {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type ChargeStorageFee = ConstBool<false>;
type Currency = NoCurrency<Self::AccountId, RuntimeHoldReason>;
type RuntimeHoldReason = RuntimeHoldReason;
type FeeDestination = ();
Expand Down
1 change: 1 addition & 0 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ impl pallet_sudo::Config for Runtime {
impl pallet_transaction_storage::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type ChargeStorageFee = ConstBool<false>;
type Currency = NoCurrency<Self::AccountId, RuntimeHoldReason>;
type RuntimeHoldReason = RuntimeHoldReason;
type FeeDestination = ();
Expand Down
1 change: 1 addition & 0 deletions runtimes/bulletin-polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ impl pallet_timestamp::Config for Runtime {
impl pallet_transaction_storage::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type ChargeStorageFee = ConstBool<false>;
type Currency = NoCurrency<Self::AccountId, RuntimeHoldReason>;
type RuntimeHoldReason = RuntimeHoldReason;
type FeeDestination = ();
Expand Down
3 changes: 2 additions & 1 deletion runtimes/bulletin-westend/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use super::{Runtime, RuntimeCall, RuntimeEvent, RuntimeHoldReason};
use frame_support::{
parameter_types,
traits::{EitherOfDiverse, Equals},
traits::{ConstBool, EitherOfDiverse, Equals},
};
use pallet_xcm::EnsureXcm;
use pallets_common::NoCurrency;
Expand All @@ -42,6 +42,7 @@ parameter_types! {
impl pallet_transaction_storage::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type ChargeStorageFee = ConstBool<false>;
type Currency = NoCurrency<Self::AccountId, RuntimeHoldReason>;
type RuntimeHoldReason = RuntimeHoldReason;
type FeeDestination = ();
Expand Down