Skip to content

Commit

Permalink
Feat: on-chain storage fee for operate_multisig
Browse files Browse the repository at this point in the history
  • Loading branch information
Anny0nn committed Jun 7, 2024
1 parent e19df48 commit bcbef93
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 6 deletions.
7 changes: 6 additions & 1 deletion INV4/pallet-inv4/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ pub mod pallet {
fungibles::{Balanced, Inspect},
Currency, Get, GetCallMetadata, ReservableCurrency,
},
transactional, Parameter,
transactional,
weights::WeightToFee,
Parameter,
};
use frame_system::{pallet_prelude::*, RawOrigin};
use primitives::CoreInfo;
Expand Down Expand Up @@ -172,6 +174,9 @@ pub mod pallet {

/// Weight info for dispatchable calls
type WeightInfo: WeightInfo;

/// Weight to fee conversion provider, from pallet_transaction_payment
type LengthToFee: WeightToFee<Balance = BalanceOf<Self>>;
}

/// The current storage version.
Expand Down
20 changes: 18 additions & 2 deletions INV4/pallet-inv4/src/multisig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use super::pallet::{self, *};
use crate::{
account_derivation::CoreAccountDerivation,
fee_handling::FeeAsset,
fee_handling::{FeeAsset, FeeAssetNegativeImbalance, MultisigFeeHandler},
origin::{ensure_multisig, INV4Origin},
voting::{Tally, Vote},
};
Expand All @@ -27,8 +27,9 @@ use frame_support::{
traits::{
fungibles::{Inspect, Mutate},
tokens::{Fortitude, Precision},
Currency, VoteTally,
Currency, ExistenceRequirement, VoteTally, WithdrawReasons,
},
weights::WeightToFee,
BoundedBTreeMap,
};
use frame_system::{ensure_signed, pallet_prelude::*};
Expand Down Expand Up @@ -169,6 +170,21 @@ where
.try_into()
.map_err(|_| Error::<T>::MaxCallLengthExceeded)?;

let total_lenght = (bounded_call.len() as u64)
.saturating_add(metadata.clone().unwrap_or_default().len() as u64);

let storage_cost: BalanceOf<T> =
T::LengthToFee::weight_to_fee(&Weight::from_parts(total_lenght as u64, 0));

T::FeeCharger::handle_creation_fee(FeeAssetNegativeImbalance::Native(
<T as Config>::Currency::withdraw(
&owner,
storage_cost,
WithdrawReasons::TRANSACTION_PAYMENT,
ExistenceRequirement::KeepAlive,
)?,
));

// Insert proposal in storage, it's now in the voting stage
Multisig::<T>::insert(
core_id,
Expand Down
17 changes: 17 additions & 0 deletions INV4/pallet-inv4/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use frame_support::{
fungibles::Credit, ConstU128, ConstU32, ConstU64, Contains, Currency, EnsureOrigin,
EnsureOriginWithArg,
},
weights::ConstantMultiplier,
};
use frame_system::EnsureRoot;
use orml_asset_registry::AssetMetadata;
Expand Down Expand Up @@ -87,6 +88,7 @@ impl pallet_balances::Config for Test {
}

const UNIT: u128 = 1000000000000;
const MICROUNIT: Balance = 1_000_000;

pub struct CoreDustRemovalWhitelist;
impl Contains<AccountId> for CoreDustRemovalWhitelist {
Expand Down Expand Up @@ -208,6 +210,8 @@ parameter_types! {
pub const MaxReserves: u32 = 1;
pub const MaxCallSize: u32 = 50 * 1024;
pub const StringLimit: u32 = 2125;
pub const TransactionByteFee: Balance = 10 * MICROUNIT;

}

pub struct AssetAuthority;
Expand All @@ -220,6 +224,18 @@ impl EnsureOriginWithArg<RuntimeOrigin, Option<u32>> for AssetAuthority {
) -> Result<Self::Success, RuntimeOrigin> {
<EnsureRoot<_> as EnsureOrigin<RuntimeOrigin>>::try_origin(origin)
}

fn ensure_origin(
o: RuntimeOrigin,
a: &Option<u32>,
) -> Result<Self::Success, sp_runtime::traits::BadOrigin> {
Self::try_origin(o, a).map_err(|_| sp_runtime::traits::BadOrigin)
}

#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin(_o: &Option<u32>) -> Result<RuntimeOrigin, ()> {
Err(())
}
}

impl orml_asset_registry::Config for Test {
Expand Down Expand Up @@ -340,6 +356,7 @@ impl pallet::Config for Test {
type MaxCallSize = MaxCallSize;

type ParaId = ConstU32<2125>;
type LengthToFee = ConstantMultiplier<Balance, TransactionByteFee>;
}

pub struct ExtBuilder;
Expand Down
4 changes: 2 additions & 2 deletions OCIF/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ pub mod pallet {
let _total_stakers = _core_stake_info.number_of_stakers;

let message = primitives::UnregisterMessage::<T> {
core_id: core_id.clone(),
core_id,
era: current_era,
stakers_to_unstake: _total_stakers,
}
Expand Down Expand Up @@ -1329,7 +1329,7 @@ pub mod pallet {
let total_remaning_stakers = stakers.saturating_sub(chunk_size as u32);
if total_remaning_stakers != 0 {
let _call: Vec<u8> = primitives::UnregisterMessage::<T> {
core_id: core_id.clone(),
core_id,
stakers_to_unstake: total_remaning_stakers,
era: start_era,
}
Expand Down
5 changes: 4 additions & 1 deletion OCIF/staking/src/testing/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use frame_support::{
},
weights::{
constants::{BlockExecutionWeight, ExtrinsicBaseWeight, WEIGHT_REF_TIME_PER_SECOND},
Weight,
ConstantMultiplier, Weight,
},
PalletId,
};
Expand Down Expand Up @@ -39,6 +39,7 @@ pub(crate) const UNBONDING_PERIOD: EraIndex = 3;
pub(crate) const MAX_ERA_STAKE_VALUES: u32 = 8;
pub(crate) const BLOCKS_PER_ERA: BlockNumber = 3;
pub(crate) const REGISTER_DEPOSIT: Balance = 10;
const MICROUNIT: Balance = 1_000_000;

construct_runtime!(
pub struct Test {
Expand Down Expand Up @@ -245,6 +246,7 @@ impl pallet_inv4::Config for Test {
type MaxCallSize = ConstU32<51200>;

type ParaId = ConstU32<2125>;
type LengthToFee = ConstantMultiplier<Balance, TransactionByteFee>;
}

impl pallet_ocif_staking::Config for Test {
Expand Down Expand Up @@ -304,6 +306,7 @@ parameter_types! {
pub MessageQueueServiceWeight: Weight = Perbill::from_percent(35) * RuntimeBlockWeights::get().max_block;
pub const MessageQueueMaxStale: u32 = 8;
pub const MessageQueueHeapSize: u32 = 128 * 1048;
pub const TransactionByteFee: Balance = 10 * MICROUNIT;
}

impl pallet_message_queue::Config for Test {
Expand Down
5 changes: 5 additions & 0 deletions pallet-rings/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use frame_support::{
fungibles::Credit, ConstU128, ConstU32, ConstU64, Contains, Currency, EnsureOrigin,
EnsureOriginWithArg, Everything, Nothing,
},
weights::ConstantMultiplier,
};
use frame_system::EnsureRoot;
use orml_asset_registry::AssetMetadata;
Expand All @@ -34,6 +35,8 @@ type Balance = u128;

type AccountId = AccountId32;

const MICROUNIT: Balance = 1_000_000;

pub const EXISTENTIAL_DEPOSIT: Balance = 1_000_000_000;

pub const ALICE: AccountId = AccountId32::new([
Expand Down Expand Up @@ -324,6 +327,7 @@ parameter_types! {
pub const ExistentialDeposit: u128 = 100000000000;
pub const MaxLocks: u32 = 1;
pub const MaxReserves: u32 = 1;
pub const TransactionByteFee: Balance = 10 * MICROUNIT;
}

pub struct AssetAuthority;
Expand Down Expand Up @@ -455,6 +459,7 @@ impl pallet_inv4::Config for Test {
type MaxCallSize = ConstU32<51200>;

type ParaId = ConstU32<2125>;
type LengthToFee = ConstantMultiplier<Balance, TransactionByteFee>;
}

parameter_types! {
Expand Down

0 comments on commit bcbef93

Please sign in to comment.