Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sharded core unregistration + various fixes to weights and benchmarks #231

Merged
merged 7 commits into from
Aug 20, 2024
Merged
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ frame-benchmarking = { git = "https://github.com/paritytech/polkadot-sdk.git", d
frame-support = { git = "https://github.com/paritytech/polkadot-sdk.git", default-features = false, branch = "release-polkadot-v1.6.0" }
frame-system = { git = "https://github.com/paritytech/polkadot-sdk.git", default-features = false, branch = "release-polkadot-v1.6.0" }
pallet-balances = { git = "https://github.com/paritytech/polkadot-sdk.git", default-features = false, branch = "release-polkadot-v1.6.0" }
pallet-message-queue = { git = "https://github.com/paritytech/polkadot-sdk.git", default-features = false, branch = "release-polkadot-v1.6.0" }
pallet-session = { git = "https://github.com/paritytech/polkadot-sdk.git", default-features = false, branch = "release-polkadot-v1.6.0" }
pallet-timestamp = { git = "https://github.com/paritytech/polkadot-sdk.git", default-features = false, branch = "release-polkadot-v1.6.0" }
pallet-xcm = { git = "https://github.com/paritytech/polkadot-sdk.git", default-features = false, branch = "release-polkadot-v1.6.0" }
Expand All @@ -43,6 +44,8 @@ xcm-executor = { package = "staging-xcm-executor", git = "https://github.com/par

# dev dependencies

cumulus-primitives-core = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "release-polkadot-v1.6.0", default-features = false }

orml-asset-registry = { git = "https://github.com/InvArch/open-runtime-module-library.git", default-features = false, branch = "polkadot-v1.6.0" }
orml-tokens2 = { package = "orml-tokens", git = "https://github.com/arrudagates/open-runtime-module-library.git", default-features = false, rev = "bc6b41e8a9539971a2da5d62cf8f550cde985f00" }
orml-traits2 = { package = "orml-traits", git = "https://github.com/arrudagates/open-runtime-module-library.git", default-features = false, rev = "bc6b41e8a9539971a2da5d62cf8f550cde985f00" }
Expand Down
5 changes: 3 additions & 2 deletions INV4/pallet-inv4/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ version = '0.1.0-dev'

[dependencies]
serde = { workspace = true, optional = true }
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = [
"derive", "max-encoded-len"
codec = { package = "parity-scale-codec", version = "3.6.5", default-features = false, features = [
"derive",
"max-encoded-len",
] }
sp-runtime = { workspace = true, default-features = false }
sp-arithmetic = { workspace = true, default-features = false }
Expand Down
69 changes: 32 additions & 37 deletions INV4/pallet-inv4/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::{
fee_handling::FeeAsset,
multisig::MAX_SIZE,
origin::{INV4Origin, MultisigInternalOrigin},
util::derive_core_account,
voting::{Tally, Vote},
BalanceOf,
};
Expand Down Expand Up @@ -38,18 +37,21 @@ fn perbill_one() -> Perbill {
Perbill::one()
}

fn derive_account<T: Config>(core_id: T::CoreId) -> T::AccountId {
derive_core_account::<T, T::CoreId, T::AccountId>(core_id)
fn derive_account<T>(core_id: T::CoreId) -> T::AccountId
where
T: Config,
T::AccountId: From<[u8; 32]>,
{
INV4::<T>::derive_core_account(core_id)
}

fn mock_core<T: Config>() -> DispatchResultWithPostInfo
where
Result<
INV4Origin<T, <T as pallet::Config>::CoreId, <T as frame_system::Config>::AccountId>,
<T as frame_system::Config>::RuntimeOrigin,
>: From<<T as frame_system::Config>::RuntimeOrigin>,
Result<INV4Origin<T>, <T as frame_system::Config>::RuntimeOrigin>:
From<<T as frame_system::Config>::RuntimeOrigin>,
<<T as pallet::Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance:
Sum,
T::AccountId: From<[u8; 32]>,
{
T::Currency::make_free_balance_be(
&whitelisted_caller(),
Expand All @@ -61,20 +63,18 @@ where
vec![].try_into().unwrap(),
perbill_one(),
perbill_one(),
FeeAsset::TNKR,
FeeAsset::Native,
)
}

fn mock_mint<T: Config>() -> Result<(), DispatchError>
where
Result<
INV4Origin<T, <T as pallet::Config>::CoreId, <T as frame_system::Config>::AccountId>,
<T as frame_system::Config>::RuntimeOrigin,
>: From<<T as frame_system::Config>::RuntimeOrigin>,
Result<INV4Origin<T>, <T as frame_system::Config>::RuntimeOrigin>:
From<<T as frame_system::Config>::RuntimeOrigin>,
<<T as pallet::Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance:
Sum,
<T as frame_system::Config>::RuntimeOrigin:
From<INV4Origin<T, <T as pallet::Config>::CoreId, <T as frame_system::Config>::AccountId>>,
<T as frame_system::Config>::RuntimeOrigin: From<INV4Origin<T>>,
T::AccountId: From<[u8; 32]>,
{
INV4::<T>::token_mint(
INV4Origin::Multisig(MultisigInternalOrigin::new(0u32.into())).into(),
Expand All @@ -85,14 +85,12 @@ where

fn mock_mint_2<T: Config>() -> Result<(), DispatchError>
where
Result<
INV4Origin<T, <T as pallet::Config>::CoreId, <T as frame_system::Config>::AccountId>,
<T as frame_system::Config>::RuntimeOrigin,
>: From<<T as frame_system::Config>::RuntimeOrigin>,
Result<INV4Origin<T>, <T as frame_system::Config>::RuntimeOrigin>:
From<<T as frame_system::Config>::RuntimeOrigin>,
<<T as pallet::Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance:
Sum,
<T as frame_system::Config>::RuntimeOrigin:
From<INV4Origin<T, <T as pallet::Config>::CoreId, <T as frame_system::Config>::AccountId>>,
<T as frame_system::Config>::RuntimeOrigin: From<INV4Origin<T>>,
T::AccountId: From<[u8; 32]>,
{
INV4::<T>::token_mint(
INV4Origin::Multisig(MultisigInternalOrigin::new(0u32.into())).into(),
Expand All @@ -103,34 +101,30 @@ where

fn mock_call<T: Config>() -> Result<PostDispatchInfo, DispatchErrorWithPostInfo<PostDispatchInfo>>
where
Result<
INV4Origin<T, <T as pallet::Config>::CoreId, <T as frame_system::Config>::AccountId>,
<T as frame_system::Config>::RuntimeOrigin,
>: From<<T as frame_system::Config>::RuntimeOrigin>,
Result<INV4Origin<T>, <T as frame_system::Config>::RuntimeOrigin>:
From<<T as frame_system::Config>::RuntimeOrigin>,
<<T as pallet::Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance:
Sum,
<T as frame_system::Config>::RuntimeOrigin:
From<INV4Origin<T, <T as pallet::Config>::CoreId, <T as frame_system::Config>::AccountId>>,
<T as frame_system::Config>::RuntimeOrigin: From<INV4Origin<T>>,
T::AccountId: From<[u8; 32]>,
{
INV4::<T>::operate_multisig(
SystemOrigin::Signed(whitelisted_caller()).into(),
0u32.into(),
None,
FeeAsset::TNKR,
FeeAsset::Native,
Box::new(frame_system::Call::<T>::remark { remark: vec![0] }.into()),
)
}

fn mock_vote<T: Config>() -> Result<PostDispatchInfo, DispatchErrorWithPostInfo<PostDispatchInfo>>
where
Result<
INV4Origin<T, <T as pallet::Config>::CoreId, <T as frame_system::Config>::AccountId>,
<T as frame_system::Config>::RuntimeOrigin,
>: From<<T as frame_system::Config>::RuntimeOrigin>,
Result<INV4Origin<T>, <T as frame_system::Config>::RuntimeOrigin>:
From<<T as frame_system::Config>::RuntimeOrigin>,
<<T as pallet::Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance:
Sum,
<T as frame_system::Config>::RuntimeOrigin:
From<INV4Origin<T, <T as pallet::Config>::CoreId, <T as frame_system::Config>::AccountId>>,
<T as frame_system::Config>::RuntimeOrigin: From<INV4Origin<T>>,
T::AccountId: From<[u8; 32]>,
{
let call: <T as Config>::RuntimeCall =
frame_system::Call::<T>::remark { remark: vec![0] }.into();
Expand All @@ -149,11 +143,12 @@ benchmarks! {
where_clause {
where
Result<
INV4Origin<T, <T as pallet::Config>::CoreId, <T as frame_system::Config>::AccountId>,
INV4Origin<T>,
<T as frame_system::Config>::RuntimeOrigin,
>: From<<T as frame_system::Config>::RuntimeOrigin>,
<<T as pallet::Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance: Sum,
<T as frame_system::Config>::RuntimeOrigin: From<INV4Origin<T, <T as pallet::Config>::CoreId, <T as frame_system::Config>::AccountId>>,
<T as frame_system::Config>::RuntimeOrigin: From<INV4Origin<T>>,
T::AccountId: From<[u8; 32]>,
}

create_core {
Expand All @@ -163,7 +158,7 @@ benchmarks! {
let caller = whitelisted_caller();
let minimum_support = perbill_one();
let required_approval = perbill_one();
let creation_fee_asset = FeeAsset::TNKR;
let creation_fee_asset = FeeAsset::Native;

T::Currency::make_free_balance_be(&caller, T::CoreCreationFee::get() + T::CoreCreationFee::get());
}: _(SystemOrigin::Signed(caller.clone()), metadata.clone(), minimum_support, required_approval, creation_fee_asset)
Expand Down Expand Up @@ -244,7 +239,7 @@ benchmarks! {
let caller: T::AccountId = whitelisted_caller();
let core_id: T::CoreId = 0u32.into();
let call_hash = <<T as frame_system::Config>::Hashing as Hash>::hash_of(&call.clone());
let fee_asset = FeeAsset::TNKR;
let fee_asset = FeeAsset::Native;

}: _(SystemOrigin::Signed(caller.clone()), core_id, Some(metadata), fee_asset, Box::new(call.clone()))
verify {
Expand Down
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;

/// Byte 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
Loading
Loading