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

added offset to VerExtrinsicBaseWeight and FeeLockWeight to xyk extri… #837

Merged
merged 3 commits into from
Nov 8, 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
1 change: 1 addition & 0 deletions pallets/bootstrap/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ impl pallet_xyk::Config for Test {
type DisabledTokens = Nothing;
type VestingProvider = Vesting;
type AssetMetadataMutation = AssetMetadataMutation;
type FeeLockWeight = ();
}

impl pallet_proof_of_stake::Config for Test {
Expand Down
86 changes: 86 additions & 0 deletions pallets/fee-lock/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,90 @@ const MGA_TOKEN_ID: u32 = 0;

benchmarks! {

process_fee_lock{

let caller: T::AccountId = whitelisted_caller();
let period_length: BlockNumberFor<T> = 10u32.into();
let fee_lock_amount: BalanceOf<T> = 1000_u32.into();
let queue_position = 10u128;
let token_id = MGA_TOKEN_ID.into();
<frame_system::Pallet<T>>::set_block_number(1u32.into());
let now= <frame_system::Pallet<T>>::block_number();
let mint_amount: BalanceOf<T> = 1_000_000u32.into();
let swap_value_threshold: BalanceOf<T> = 1000_u32.into();

// This should be a while loop
// But this is fine here since token_id is 0
if <T as Config>::Tokens::get_next_currency_id() > token_id {
assert_ok!(<T as Config>::Tokens::mint(token_id, &caller.clone(), mint_amount));
} else {
assert_eq!(<T as Config>::Tokens::create(&caller.clone(), mint_amount).unwrap(), token_id);
}

assert_ok!(FeeLock::<T>::update_fee_lock_metadata(RawOrigin::Root.into(), Some(period_length), Some(fee_lock_amount), Some(swap_value_threshold), None));

FeeLockMetadataQeueuePosition::<T>::insert(caller.clone(), queue_position);
UnlockQueue::<T>::insert(queue_position, caller.clone());


let initial_user_free_balance = <T as Config>::Tokens::free_balance(token_id, &caller.clone());
let initial_user_reserved_balance = <T as Config>::Tokens::reserved_balance(token_id, &caller.clone());
let initial_user_locked_balance = <T as Config>::Tokens::locked_balance(token_id, &caller.clone());

assert_eq!(FeeLock::<T>::get_account_fee_lock_data(caller.clone()), AccountFeeLockDataInfo{
total_fee_lock_amount: Default::default(),
last_fee_lock_block: Default::default(),
});

} : {FeeLock::<T>::process_fee_lock(&caller)}
verify{

assert_eq!(<T as Config>::Tokens::free_balance(token_id, &caller.clone()),
initial_user_free_balance - fee_lock_amount);
assert_eq!(<T as Config>::Tokens::reserved_balance(token_id, &caller.clone()),
initial_user_reserved_balance + fee_lock_amount);
assert_eq!(<T as Config>::Tokens::locked_balance(token_id, &caller.clone()),
initial_user_locked_balance);

assert_eq!(FeeLock::<T>::get_account_fee_lock_data(caller.clone()), AccountFeeLockDataInfo{
total_fee_lock_amount: fee_lock_amount,
last_fee_lock_block: now,
});
}

get_swap_valuation_for_token {

let caller: T::AccountId = whitelisted_caller();
let mint_amount: BalanceOf<T> = 1_000_000u32.into();
let pool_amount: BalanceOf<T> = 100_000u32.into();
let token_id = MGA_TOKEN_ID.into();

// This should be a while loop
// But this is fine here since token_id is 0
if <T as Config>::Tokens::get_next_currency_id() > token_id {
assert_ok!(<T as Config>::Tokens::mint(token_id, &caller.clone(), mint_amount));
} else {
assert_eq!(<T as Config>::Tokens::create(&caller.clone(), mint_amount).unwrap(), token_id);
}

let valuating_token_amount: BalanceOf<T> = 1000_u32.into();
let valuating_token_id = <T as Config>::Tokens::create(&caller.clone(), mint_amount)?;

// Order of tokens in the create_pool call below is important
#[cfg(not(test))]
assert_ok!(<T as Config>::PoolReservesProvider::create_pool(caller, valuating_token_id, pool_amount, token_id, pool_amount.saturating_mul(2u8.into())));

// We want to avoid having the value being 1000, since that is what get_swap_valuation_for_token returns if the valuating_token_id is the native token id
#[cfg(test)]
let value: BalanceOf<T> = 500_u32.into();
#[cfg(not(test))]
let value: BalanceOf<T> = 2000_u32.into();
let mut valuation: Option<BalanceOf<T>> = None;
}: {valuation = FeeLock::<T>::get_swap_valuation_for_token(valuating_token_id, valuating_token_amount);}
verify{
assert_eq!(valuation, Some(value));
}

update_fee_lock_metadata{
let period_length: BlockNumberFor<T> = 1000u32.into();
let fee_lock_amount: BalanceOf<T> = 1000_u32.into();
Expand All @@ -58,6 +142,8 @@ benchmarks! {
let now= <frame_system::Pallet<T>>::block_number();
let token_id = MGA_TOKEN_ID.into();

// This should be a while loop
// But this is fine here since token_id is 0
if <T as Config>::Tokens::get_next_currency_id() > token_id {
assert_ok!(<T as Config>::Tokens::mint(token_id, &caller.clone(), 1_000_000u32.into()));
} else {
Expand Down
18 changes: 17 additions & 1 deletion pallets/fee-lock/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use frame_support::{
transactional,
};
use frame_system::{ensure_signed, pallet_prelude::*};
use mangata_support::traits::{FeeLockTriggerTrait, Valuate};
use mangata_support::traits::{FeeLockTriggerTrait, Valuate, XykFunctionsTrait};
use orml_tokens::{MultiTokenCurrencyExtended, MultiTokenReservableCurrency};
use sp_arithmetic::per_things::Rounding;
use sp_runtime::helpers_128bit::multiply_by_rational_with_rounding;
Expand Down Expand Up @@ -225,7 +225,11 @@ pub mod pallet {
type MaxCuratedTokens: Get<u32>;
type Tokens: MultiTokenCurrencyExtended<Self::AccountId>
+ MultiTokenReservableCurrency<Self::AccountId>;
#[cfg(not(all(feature = "runtime-benchmarks", not(test))))]
type PoolReservesProvider: Valuate<BalanceOf<Self>, CurrencyIdOf<Self>>;
#[cfg(all(feature = "runtime-benchmarks", not(test)))]
type PoolReservesProvider: Valuate<BalanceOf<Self>, CurrencyIdOf<Self>>
+ XykFunctionsTrait<Self::AccountId, BalanceOf<Self>, CurrencyIdOf<Self>>;
#[pallet::constant]
type NativeTokenId: Get<CurrencyIdOf<Self>>;
type WeightInfo: WeightInfo;
Expand Down Expand Up @@ -553,3 +557,15 @@ impl<T: Config> FeeLockTriggerTrait<T::AccountId, BalanceOf<T>, CurrencyIdOf<T>>
Ok(())
}
}

pub struct FeeLockWeightProvider<T>(PhantomData<T>);

impl<T: Config> Get<Weight> for FeeLockWeightProvider<T> {
fn get() -> Weight {
// We assume that process_fee_lock is heavier than unlock_fee
// The FeeLockMetadata read is not accounted for since it is called no matter the extrinsic and hence would be accounted for in the ExtrinsicBaseWeight
T::WeightInfo::process_fee_lock()
.saturating_add(T::WeightInfo::get_swap_valuation_for_token())
.saturating_add(Weight::from_parts(40_000_000, 0))
}
}
61 changes: 49 additions & 12 deletions pallets/fee-lock/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,32 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Autogenerated weights for pallet_token_timeout
//! Autogenerated weights for pallet_fee_lock
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2022-09-01, STEPS: `2`, REPEAT: 2, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 32.0.0
//! DATE: 2024-11-07, STEPS: `2`, REPEAT: 2, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! EXECUTION: , WASM-EXECUTION: Compiled, CHAIN: Some("rollup-local"), DB CACHE: 1024

// Executed Command:
// /home/striker/work/mangata-ws/mangata-node/scripts/..//target/release/mangata-node
// /home/striker/work/mangata-ws/mangata-node/scripts/..//target/release/rollup-node
// benchmark
// pallet
// --chain
// dev
// rollup-local
// --execution
// wasm
// --wasm-execution
// compiled
// --pallet
// pallet_token_timeout
// pallet_fee_lock
// --extrinsic
// *
// --steps
// 2
// --repeat
// 2
// --output
// ./benchmarks/pallet_token_timeout_weights.rs
// ./benchmarks/pallet_fee_lock_weights.rs
// --template
// ./templates/module-weight-template.hbs

Expand All @@ -55,20 +55,57 @@ use sp_std::marker::PhantomData;

/// Weight functions needed for pallet_fee_lock.
pub trait WeightInfo {
fn process_fee_lock() -> Weight;
fn get_swap_valuation_for_token() -> Weight;
fn update_fee_lock_metadata() -> Weight;
fn unlock_fee() -> Weight;
}

// For backwards compatibility and tests
impl WeightInfo for () {
// Storage: `FeeLock::FeeLockMetadata` (r:1 w:0)
// Proof: `FeeLock::FeeLockMetadata` (`max_values`: Some(1), `max_size`: Some(438), added: 933, mode: `MaxEncodedLen`)
// Storage: `FeeLock::AccountFeeLockData` (r:1 w:1)
// Proof: `FeeLock::AccountFeeLockData` (`max_values`: None, `max_size`: Some(48), added: 2523, mode: `MaxEncodedLen`)
// Storage: `Tokens::Accounts` (r:1 w:1)
// Proof: `Tokens::Accounts` (`max_values`: None, `max_size`: Some(96), added: 2571, mode: `MaxEncodedLen`)
// Storage: `FeeLock::FeeLockMetadataQeueuePosition` (r:1 w:1)
// Proof: `FeeLock::FeeLockMetadataQeueuePosition` (`max_values`: None, `max_size`: Some(44), added: 2519, mode: `MaxEncodedLen`)
// Storage: `FeeLock::UnlockQueue` (r:1 w:2)
// Proof: `FeeLock::UnlockQueue` (`max_values`: None, `max_size`: Some(44), added: 2519, mode: `MaxEncodedLen`)
// Storage: `FeeLock::UnlockQueueEnd` (r:1 w:1)
// Proof: `FeeLock::UnlockQueueEnd` (`max_values`: Some(1), `max_size`: Some(16), added: 511, mode: `MaxEncodedLen`)
fn process_fee_lock() -> Weight {
(Weight::from_parts(39_182_000, 0))
.saturating_add(RocksDbWeight::get().reads(6 as u64))
.saturating_add(RocksDbWeight::get().writes(6 as u64))
}
// Storage: `Xyk::Pools` (r:2 w:0)
// Proof: `Xyk::Pools` (`max_values`: None, `max_size`: Some(56), added: 2531, mode: `MaxEncodedLen`)
fn get_swap_valuation_for_token() -> Weight {
(Weight::from_parts(10_686_000, 0))
.saturating_add(RocksDbWeight::get().reads(2 as u64))
}
// Storage: `FeeLock::FeeLockMetadata` (r:1 w:1)
// Proof: `FeeLock::FeeLockMetadata` (`max_values`: Some(1), `max_size`: Some(438), added: 933, mode: `MaxEncodedLen`)
fn update_fee_lock_metadata() -> Weight {
(Weight::from_parts(44_245_000, 0))
(Weight::from_parts(22_559_000, 0))
.saturating_add(RocksDbWeight::get().reads(1 as u64))
.saturating_add(RocksDbWeight::get().writes(1 as u64))
}
// Storage: `FeeLock::AccountFeeLockData` (r:1 w:1)
// Proof: `FeeLock::AccountFeeLockData` (`max_values`: None, `max_size`: Some(48), added: 2523, mode: `MaxEncodedLen`)
// Storage: `FeeLock::FeeLockMetadata` (r:1 w:0)
// Proof: `FeeLock::FeeLockMetadata` (`max_values`: Some(1), `max_size`: Some(438), added: 933, mode: `MaxEncodedLen`)
// Storage: `Tokens::Accounts` (r:1 w:1)
// Proof: `Tokens::Accounts` (`max_values`: None, `max_size`: Some(96), added: 2571, mode: `MaxEncodedLen`)
// Storage: `FeeLock::FeeLockMetadataQeueuePosition` (r:1 w:1)
// Proof: `FeeLock::FeeLockMetadataQeueuePosition` (`max_values`: None, `max_size`: Some(44), added: 2519, mode: `MaxEncodedLen`)
// Storage: `FeeLock::UnlockQueue` (r:1 w:1)
// Proof: `FeeLock::UnlockQueue` (`max_values`: None, `max_size`: Some(44), added: 2519, mode: `MaxEncodedLen`)
fn unlock_fee() -> Weight {
(Weight::from_parts(50_286_000, 0))
.saturating_add(RocksDbWeight::get().reads(3 as u64))
.saturating_add(RocksDbWeight::get().writes(2 as u64))
(Weight::from_parts(39_182_000, 0))
.saturating_add(RocksDbWeight::get().reads(5 as u64))
.saturating_add(RocksDbWeight::get().writes(4 as u64))
}
}
1 change: 1 addition & 0 deletions pallets/proof-of-stake/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ impl pallet_xyk::Config for Test {
type DisabledTokens = Nothing;
type VestingProvider = Vesting;
type AssetMetadataMutation = AssetMetadataMutation;
type FeeLockWeight = ();
}

#[cfg(not(feature = "runtime-benchmarks"))]
Expand Down
2 changes: 1 addition & 1 deletion pallets/rolldown/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ std = [
"pallet-sequencer-staking/std",
]

runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks"]
runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks", "pallet-sequencer-staking/runtime-benchmarks"]

try-runtime = ["frame-support/try-runtime", "frame-system/try-runtime", "orml-tokens/try-runtime", "sp-runtime/try-runtime"]
23 changes: 12 additions & 11 deletions pallets/xyk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ pub mod pallet {
Moment = BlockNumberFor<Self>,
>;
type AssetMetadataMutation: AssetMetadataMutationTrait<CurrencyIdOf<Self>>;
type FeeLockWeight: Get<Weight>;
type WeightInfo: WeightInfo;
}

Expand Down Expand Up @@ -677,7 +678,7 @@ pub mod pallet {
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight(<<T as Config>::WeightInfo>::create_pool())]
#[pallet::weight(<<T as Config>::WeightInfo>::create_pool().saturating_add(T::FeeLockWeight::get()))]
pub fn create_pool(
origin: OriginFor<T>,
first_asset_id: CurrencyIdOf<T>,
Expand Down Expand Up @@ -724,7 +725,7 @@ pub mod pallet {
/// - `sold_asset_amount`: The amount of the sold token being sold
/// - `min_amount_out` - The minimum amount of bought asset that must be bought in order to not fail on slippage. Slippage failures still charge exchange commission.
#[pallet::call_index(1)]
#[pallet::weight((<<T as Config>::WeightInfo>::sell_asset(), DispatchClass::Operational, Pays::No))]
#[pallet::weight((<<T as Config>::WeightInfo>::sell_asset().saturating_add(T::FeeLockWeight::get()), DispatchClass::Operational, Pays::No))]
#[deprecated(note = "multiswap_sell_asset should be used instead")]
pub fn sell_asset(
origin: OriginFor<T>,
Expand Down Expand Up @@ -769,7 +770,7 @@ pub mod pallet {
/// - `sold_asset_amount`: The amount of the first asset sold
/// - `min_amount_out` - The minimum amount of last asset that must be bought in order to not fail on slippage. Slippage failures still charge exchange commission.
#[pallet::call_index(2)]
#[pallet::weight((<<T as Config>::WeightInfo>::multiswap_sell_asset(swap_token_list.len() as u32), DispatchClass::Operational, Pays::No))]
#[pallet::weight((<<T as Config>::WeightInfo>::multiswap_sell_asset(swap_token_list.len() as u32).saturating_add(T::FeeLockWeight::get()), DispatchClass::Operational, Pays::No))]
pub fn multiswap_sell_asset(
origin: OriginFor<T>,
swap_token_list: Vec<CurrencyIdOf<T>>,
Expand Down Expand Up @@ -826,7 +827,7 @@ pub mod pallet {
/// - `bought_asset_amount`: The amount of the bought token being bought
/// - `max_amount_in` - The maximum amount of sold asset that must be sold in order to not fail on slippage. Slippage failures still charge exchange commission.
#[pallet::call_index(3)]
#[pallet::weight((<<T as Config>::WeightInfo>::buy_asset(), DispatchClass::Operational, Pays::No))]
#[pallet::weight((<<T as Config>::WeightInfo>::buy_asset().saturating_add(T::FeeLockWeight::get()), DispatchClass::Operational, Pays::No))]
#[deprecated(note = "multiswap_buy_asset should be used instead")]
pub fn buy_asset(
origin: OriginFor<T>,
Expand Down Expand Up @@ -874,7 +875,7 @@ pub mod pallet {
/// - `bought_asset_amount`: The amount of the last asset bought
/// - `max_amount_in` - The maximum amount of first asset that can be sold in order to not fail on slippage. Slippage failures still charge exchange commission.
#[pallet::call_index(4)]
#[pallet::weight((<<T as Config>::WeightInfo>::multiswap_buy_asset(swap_token_list.len() as u32), DispatchClass::Operational, Pays::No))]
#[pallet::weight((<<T as Config>::WeightInfo>::multiswap_buy_asset(swap_token_list.len() as u32).saturating_add(T::FeeLockWeight::get()), DispatchClass::Operational, Pays::No))]
pub fn multiswap_buy_asset(
origin: OriginFor<T>,
swap_token_list: Vec<CurrencyIdOf<T>>,
Expand Down Expand Up @@ -917,7 +918,7 @@ pub mod pallet {
}

#[pallet::call_index(5)]
#[pallet::weight(<<T as Config>::WeightInfo>::mint_liquidity_using_vesting_native_tokens())]
#[pallet::weight(<<T as Config>::WeightInfo>::mint_liquidity_using_vesting_native_tokens().saturating_add(T::FeeLockWeight::get()))]
#[transactional]
pub fn mint_liquidity_using_vesting_native_tokens_by_vesting_index(
origin: OriginFor<T>,
Expand Down Expand Up @@ -977,7 +978,7 @@ pub mod pallet {
}

#[pallet::call_index(6)]
#[pallet::weight(<<T as Config>::WeightInfo>::mint_liquidity_using_vesting_native_tokens())]
#[pallet::weight(<<T as Config>::WeightInfo>::mint_liquidity_using_vesting_native_tokens().saturating_add(T::FeeLockWeight::get()))]
#[transactional]
pub fn mint_liquidity_using_vesting_native_tokens(
origin: OriginFor<T>,
Expand Down Expand Up @@ -1034,7 +1035,7 @@ pub mod pallet {
}

#[pallet::call_index(7)]
#[pallet::weight(<<T as Config>::WeightInfo>::mint_liquidity())]
#[pallet::weight(<<T as Config>::WeightInfo>::mint_liquidity().saturating_add(T::FeeLockWeight::get()))]
pub fn mint_liquidity(
origin: OriginFor<T>,
first_asset_id: CurrencyIdOf<T>,
Expand Down Expand Up @@ -1063,7 +1064,7 @@ pub mod pallet {
}

#[pallet::call_index(8)]
#[pallet::weight(<<T as Config>::WeightInfo>::compound_rewards())]
#[pallet::weight(<<T as Config>::WeightInfo>::compound_rewards().saturating_add(T::FeeLockWeight::get()))]
#[transactional]
pub fn compound_rewards(
origin: OriginFor<T>,
Expand All @@ -1082,7 +1083,7 @@ pub mod pallet {
}

#[pallet::call_index(9)]
#[pallet::weight(<<T as Config>::WeightInfo>::provide_liquidity_with_conversion())]
#[pallet::weight(<<T as Config>::WeightInfo>::provide_liquidity_with_conversion().saturating_add(T::FeeLockWeight::get()))]
#[transactional]
pub fn provide_liquidity_with_conversion(
origin: OriginFor<T>,
Expand Down Expand Up @@ -1114,7 +1115,7 @@ pub mod pallet {
}

#[pallet::call_index(10)]
#[pallet::weight(<<T as Config>::WeightInfo>::burn_liquidity())]
#[pallet::weight(<<T as Config>::WeightInfo>::burn_liquidity().saturating_add(T::FeeLockWeight::get()))]
pub fn burn_liquidity(
origin: OriginFor<T>,
first_asset_id: CurrencyIdOf<T>,
Expand Down
2 changes: 2 additions & 0 deletions pallets/xyk/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ impl Config for Test {
type DisallowedPools = DummyBlacklistedPool;
type DisabledTokens = Nothing;
type AssetMetadataMutation = MockAssetRegister;
type FeeLockWeight = ();
}

#[cfg(feature = "runtime-benchmarks")]
Expand All @@ -294,6 +295,7 @@ impl Config for Test {
type DisallowedPools = Nothing;
type DisabledTokens = Nothing;
type AssetMetadataMutation = MockAssetRegister;
type FeeLockWeight = ();
}

#[cfg(not(feature = "runtime-benchmarks"))]
Expand Down
5 changes: 4 additions & 1 deletion rollup/runtime/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#![cfg_attr(not(feature = "std"), no_std)]
pub mod fee {
use crate::{runtime_config::consts::UNIT, weights::VerExtrinsicBaseWeight, Balance};
use crate::{
runtime_config::{config::frame_system::VerExtrinsicBaseWeight, consts::UNIT},
Balance,
};
use frame_support::weights::{
constants::WEIGHT_REF_TIME_PER_SECOND, WeightToFeeCoefficient, WeightToFeeCoefficients,
WeightToFeePolynomial,
Expand Down
Loading
Loading