Skip to content

Commit d4e6b11

Browse files
Align store Function between Bulletin and SDK
1 parent 5fec423 commit d4e6b11

File tree

5 files changed

+26
-3
lines changed

5 files changed

+26
-3
lines changed

pallets/transaction-storage/src/lib.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use polkadot_sdk_frame::{
4141
deps::{sp_core::sp_std::prelude::*, *},
4242
prelude::*,
4343
traits::{
44-
fungible::{Balanced, Credit, Inspect, Mutate, MutateHold},
44+
fungible::{hold::Balanced, Credit, Inspect, Mutate, MutateHold},
4545
parameter_types,
4646
},
4747
};
@@ -187,6 +187,9 @@ pub mod pallet {
187187
+ Dispatchable<RuntimeOrigin = Self::RuntimeOrigin>
188188
+ GetDispatchInfo
189189
+ From<frame_system::Call<Self>>;
190+
/// Whether storage-related extrinsics charge a storage fee.
191+
#[pallet::constant]
192+
type ChargeStorageFee: Get<bool>;
190193
/// The fungible type for this pallet.
191194
type Currency: Mutate<Self::AccountId>
192195
+ MutateHold<Self::AccountId, Reason = Self::RuntimeHoldReason>
@@ -344,11 +347,15 @@ pub mod pallet {
344347
#[pallet::call_index(0)]
345348
#[pallet::weight(T::WeightInfo::store(data.len() as u32))]
346349
#[pallet::feeless_if(|origin: &OriginFor<T>, data: &Vec<u8>| -> bool { /*TODO: add here correct validation */ true })]
347-
pub fn store(_origin: OriginFor<T>, data: Vec<u8>) -> DispatchResult {
350+
pub fn store(origin: OriginFor<T>, data: Vec<u8>) -> DispatchResult {
348351
// In the case of a regular unsigned transaction, this should have been checked by
349352
// pre_dispatch. In the case of a regular signed transaction, this should have been
350353
// checked by pre_dispatch_signed.
351354
Self::ensure_data_size_ok(data.len())?;
355+
if T::ChargeStorageFee::get() {
356+
let sender = ensure_signed(origin)?;
357+
Self::apply_fee(sender, data.len() as u32)?;
358+
}
352359

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

899+
fn apply_fee(sender: T::AccountId, size: u32) -> DispatchResult {
900+
let byte_fee = ByteFee::<T>::get().ok_or(Error::<T>::NotConfigured)?;
901+
let entry_fee = EntryFee::<T>::get().ok_or(Error::<T>::NotConfigured)?;
902+
let fee = byte_fee.saturating_mul(size.into()).saturating_add(entry_fee);
903+
T::Currency::hold(&HoldReason::StorageFeeHold.into(), &sender, fee)?;
904+
let (credit, _remainder) =
905+
T::Currency::slash(&HoldReason::StorageFeeHold.into(), &sender, fee);
906+
debug_assert!(_remainder.is_zero());
907+
T::FeeDestination::on_unbalanced(credit);
908+
Ok(())
909+
}
910+
892911
/// Returns `true` if a blob of the given size can be stored.
893912
fn data_size_ok(size: usize) -> bool {
894913
(size > 0) && (size <= T::MaxTransactionSize::get() as usize)

pallets/transaction-storage/src/mock.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ parameter_types! {
5353
impl pallet_transaction_storage::Config for Test {
5454
type RuntimeEvent = RuntimeEvent;
5555
type RuntimeCall = RuntimeCall;
56+
type ChargeStorageFee = ConstBool<false>;
5657
type Currency = NoCurrency<Self::AccountId, RuntimeHoldReason>;
5758
type RuntimeHoldReason = RuntimeHoldReason;
5859
type FeeDestination = ();

runtime/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ impl pallet_sudo::Config for Runtime {
346346
impl pallet_transaction_storage::Config for Runtime {
347347
type RuntimeEvent = RuntimeEvent;
348348
type RuntimeCall = RuntimeCall;
349+
type ChargeStorageFee = ConstBool<false>;
349350
type Currency = NoCurrency<Self::AccountId, RuntimeHoldReason>;
350351
type RuntimeHoldReason = RuntimeHoldReason;
351352
type FeeDestination = ();

runtimes/bulletin-polkadot/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ impl pallet_timestamp::Config for Runtime {
345345
impl pallet_transaction_storage::Config for Runtime {
346346
type RuntimeEvent = RuntimeEvent;
347347
type RuntimeCall = RuntimeCall;
348+
type ChargeStorageFee = ConstBool<false>;
348349
type Currency = NoCurrency<Self::AccountId, RuntimeHoldReason>;
349350
type RuntimeHoldReason = RuntimeHoldReason;
350351
type FeeDestination = ();

runtimes/bulletin-westend/src/storage.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use super::{Runtime, RuntimeCall, RuntimeEvent, RuntimeHoldReason};
2020
use frame_support::{
2121
parameter_types,
22-
traits::{EitherOfDiverse, Equals},
22+
traits::{ConstBool, EitherOfDiverse, Equals},
2323
};
2424
use pallet_xcm::EnsureXcm;
2525
use pallets_common::NoCurrency;
@@ -42,6 +42,7 @@ parameter_types! {
4242
impl pallet_transaction_storage::Config for Runtime {
4343
type RuntimeEvent = RuntimeEvent;
4444
type RuntimeCall = RuntimeCall;
45+
type ChargeStorageFee = ConstBool<false>;
4546
type Currency = NoCurrency<Self::AccountId, RuntimeHoldReason>;
4647
type RuntimeHoldReason = RuntimeHoldReason;
4748
type FeeDestination = ();

0 commit comments

Comments
 (0)