Skip to content

Commit

Permalink
fix runtime benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszaaa committed Oct 2, 2023
1 parent 35b1ebe commit c0fafcd
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 45 deletions.
89 changes: 45 additions & 44 deletions pallets/proof-of-stake/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,48 +220,51 @@ benchmarks! {
}
}

let native_asset_amount: u128 = MILION * Into::<u128>::into(schedules_limit);
let REWARDS_AMOUNT: u128 = <T as Config>::Min3rdPartyRewards::get();
let native_asset_amount: u128 = REWARDS_AMOUNT * Into::<u128>::into(schedules_limit + 1);
TokensOf::<T>::mint(native_asset_id.into(), &caller, native_asset_amount.into()).unwrap();

for _ in 0 .. schedules_limit - 1 {
let token_id = TokensOf::<T>::create(&caller, MILION.into()).unwrap().into();
XykOf::<T>::create_pool(caller.clone(), native_asset_id.into(), MILION.into(), token_id.into(), MILION.into()).unwrap();
let token_id = TokensOf::<T>::create(&caller, REWARDS_AMOUNT.into()).unwrap().into();
XykOf::<T>::create_pool(caller.clone(), native_asset_id.into(), REWARDS_AMOUNT.into(), token_id.into(), REWARDS_AMOUNT.into()).unwrap();
let reward_token = token_id + 1;
let balance:u128 = TokensOf::<T>::free_balance(reward_token.into(), &caller).into();
assert_eq!(balance, REWARDS_AMOUNT);

PoS::<T>::reward_pool(
RawOrigin::Signed(caller.clone().into()).into(),
(native_asset_id, token_id),
reward_token.into(),
MILION,
REWARDS_AMOUNT,
10u32.into(),
).unwrap();
}

let token_id = TokensOf::<T>::create(&caller, MILION.into()).unwrap().into();
XykOf::<T>::create_pool(caller.clone(), native_asset_id.into(), MILION.into(), token_id.into(), MILION.into()).unwrap();
let token_id = TokensOf::<T>::create(&caller, REWARDS_AMOUNT.into()).unwrap().into();
XykOf::<T>::create_pool(caller.clone(), native_asset_id.into(), REWARDS_AMOUNT.into(), token_id.into(), REWARDS_AMOUNT.into()).unwrap();
let reward_token = token_id + 1;
PoS::<T>::reward_pool(
RawOrigin::Signed(caller.clone().into()).into(),
(native_asset_id, token_id),
reward_token.into(),
MILION,
REWARDS_AMOUNT,
2u32.into(),
).unwrap();

forward_to_next_session::<T>();
forward_to_next_session::<T>();
forward_to_next_session::<T>();

let token_id = TokensOf::<T>::create(&caller, MILION.into()).unwrap().into();
XykOf::<T>::create_pool(caller.clone(), native_asset_id.into(), MILION.into(), token_id.into(), MILION.into()).unwrap();
let token_id = TokensOf::<T>::create(&caller, REWARDS_AMOUNT.into()).unwrap().into();
XykOf::<T>::create_pool(caller.clone(), native_asset_id.into(), REWARDS_AMOUNT.into(), token_id.into(), REWARDS_AMOUNT.into()).unwrap();
let reward_token = token_id + 1;

assert_eq!(
RewardsSchedules::<T>::get().len() as u32,
schedules_limit
);

}: reward_pool(RawOrigin::Signed(caller.clone().into()), (native_asset_id,token_id), reward_token.into(), MILION, 10u32.into())
}: reward_pool(RawOrigin::Signed(caller.clone().into()), (native_asset_id,token_id), reward_token.into(), REWARDS_AMOUNT, 10u32.into())
verify {

assert_eq!(
Expand All @@ -281,32 +284,32 @@ benchmarks! {
let schedules_limit = <T as Config>::RewardsSchedulesLimit::get();
let caller: <T as frame_system::Config>::AccountId = whitelisted_caller();
let native_asset_id = <T as Config>::NativeCurrencyId::get();
let REWARDS_AMOUNT: u128 = <T as Config>::Min3rdPartyRewards::get();

loop {
let token_id = TokensOf::<T>::create(&caller, MILION.into()).unwrap().into();
let token_id = TokensOf::<T>::create(&caller, REWARDS_AMOUNT.into()).unwrap().into();
if token_id > native_asset_id {
break;
}
}

let native_asset_amount: u128 = MILION * Into::<u128>::into(schedules_limit);
let native_asset_amount: u128 = REWARDS_AMOUNT * Into::<u128>::into(schedules_limit);
TokensOf::<T>::mint(native_asset_id.into(), &caller, native_asset_amount.into()).unwrap();

let first_token_id = TokensOf::<T>::create(&caller, MILION.into()).unwrap().into();
XykOf::<T>::create_pool(caller.clone(), native_asset_id.into(), MILION.into(), first_token_id.into(), MILION.into()).unwrap();
let first_token_id = TokensOf::<T>::create(&caller, REWARDS_AMOUNT.into()).unwrap().into();
XykOf::<T>::create_pool(caller.clone(), native_asset_id.into(), REWARDS_AMOUNT.into(), first_token_id.into(), REWARDS_AMOUNT.into()).unwrap();
let liquidity_asset_id = first_token_id + 1;

let second_token_id = TokensOf::<T>::create(&caller, MILION.into()).unwrap().into();
XykOf::<T>::create_pool(caller.clone(), native_asset_id.into(), MILION.into(), second_token_id.into(), MILION.into()).unwrap();
let second_token_id = TokensOf::<T>::create(&caller, REWARDS_AMOUNT.into()).unwrap().into();
XykOf::<T>::create_pool(caller.clone(), native_asset_id.into(), REWARDS_AMOUNT.into(), second_token_id.into(), REWARDS_AMOUNT.into()).unwrap();
let reward_token_id = second_token_id + 1;

let rewards_amount = 20_000u128;

PoS::<T>::reward_pool(
RawOrigin::Signed(caller.clone().into()).into(),
(native_asset_id, first_token_id),
reward_token_id.into(),
rewards_amount,
REWARDS_AMOUNT,
2u32.into(),
).unwrap();

Expand All @@ -315,7 +318,7 @@ benchmarks! {
forward_to_next_session::<T>();
assert_eq!(
PoS::<T>::calculate_3rdparty_rewards_amount(caller, liquidity_asset_id, reward_token_id).unwrap(),
rewards_amount/2
REWARDS_AMOUNT/2
)
}

Expand All @@ -330,39 +333,38 @@ benchmarks! {
let schedules_limit = <T as Config>::RewardsSchedulesLimit::get();
let caller: <T as frame_system::Config>::AccountId = whitelisted_caller();
let native_asset_id = <T as Config>::NativeCurrencyId::get();
let REWARDS_AMOUNT: u128 = <T as Config>::Min3rdPartyRewards::get();

loop {
let token_id = TokensOf::<T>::create(&caller, MILION.into()).unwrap().into();
let token_id = TokensOf::<T>::create(&caller, REWARDS_AMOUNT.into()).unwrap().into();
if token_id > native_asset_id {
break;
}
}

let native_asset_amount: u128 = MILION * Into::<u128>::into(schedules_limit);
let native_asset_amount: u128 = REWARDS_AMOUNT * Into::<u128>::into(schedules_limit);
TokensOf::<T>::mint(native_asset_id.into(), &caller, native_asset_amount.into()).unwrap();

let first_token_id = TokensOf::<T>::create(&caller, MILION.into()).unwrap().into();
XykOf::<T>::create_pool(caller.clone(), native_asset_id.into(), MILION.into(), first_token_id.into(), MILION.into()).unwrap();
let first_token_id = TokensOf::<T>::create(&caller, REWARDS_AMOUNT.into()).unwrap().into();
XykOf::<T>::create_pool(caller.clone(), native_asset_id.into(), REWARDS_AMOUNT.into(), first_token_id.into(), REWARDS_AMOUNT.into()).unwrap();
let liquidity_asset_id = first_token_id + 1;

let second_token_id = TokensOf::<T>::create(&caller, MILION.into()).unwrap().into();
XykOf::<T>::create_pool(caller.clone(), native_asset_id.into(), MILION.into(), second_token_id.into(), MILION.into()).unwrap();
let second_token_id = TokensOf::<T>::create(&caller, REWARDS_AMOUNT.into()).unwrap().into();
XykOf::<T>::create_pool(caller.clone(), native_asset_id.into(), REWARDS_AMOUNT.into(), second_token_id.into(), REWARDS_AMOUNT.into()).unwrap();
let reward_token_id = second_token_id + 1;

let rewards_amount = 20_000u128;

PoS::<T>::reward_pool(
RawOrigin::Signed(caller.clone().into()).into(),
(native_asset_id, first_token_id),
reward_token_id.into(),
rewards_amount,
REWARDS_AMOUNT,
2u32.into(),
).unwrap();

assert!(TokensOf::<T>::ensure_can_withdraw(
liquidity_asset_id.into(),
&caller,
MILION.into(),
REWARDS_AMOUNT.into(),
WithdrawReasons::all(),
Default::default(),
).is_ok());
Expand All @@ -379,7 +381,7 @@ benchmarks! {
TokensOf::<T>::ensure_can_withdraw(
liquidity_asset_id.into(),
&caller,
MILION.into(),
REWARDS_AMOUNT.into(),
WithdrawReasons::all(),
Default::default(),
).is_err()
Expand All @@ -392,7 +394,7 @@ benchmarks! {
assert!(TokensOf::<T>::ensure_can_withdraw(
liquidity_asset_id.into(),
&caller,
MILION.into(),
REWARDS_AMOUNT.into(),
WithdrawReasons::all(),
Default::default(),
).is_ok());
Expand All @@ -411,32 +413,31 @@ benchmarks! {
let schedules_limit = <T as Config>::RewardsSchedulesLimit::get();
let caller: <T as frame_system::Config>::AccountId = whitelisted_caller();
let native_asset_id = <T as Config>::NativeCurrencyId::get();
let REWARDS_AMOUNT: u128 = <T as Config>::Min3rdPartyRewards::get();

loop {
let token_id = TokensOf::<T>::create(&caller, MILION.into()).unwrap().into();
let token_id = TokensOf::<T>::create(&caller, REWARDS_AMOUNT.into()).unwrap().into();
if token_id > native_asset_id {
break;
}
}

let native_asset_amount: u128 = MILION * Into::<u128>::into(schedules_limit);
let native_asset_amount: u128 = REWARDS_AMOUNT * Into::<u128>::into(schedules_limit);
TokensOf::<T>::mint(native_asset_id.into(), &caller, native_asset_amount.into()).unwrap();

let first_token_id = TokensOf::<T>::create(&caller, MILION.into()).unwrap().into();
XykOf::<T>::create_pool(caller.clone(), native_asset_id.into(), MILION.into(), first_token_id.into(), MILION.into()).unwrap();
let first_token_id = TokensOf::<T>::create(&caller, REWARDS_AMOUNT.into()).unwrap().into();
XykOf::<T>::create_pool(caller.clone(), native_asset_id.into(), REWARDS_AMOUNT.into(), first_token_id.into(), REWARDS_AMOUNT.into()).unwrap();
let liquidity_asset_id = first_token_id + 1;

let second_token_id = TokensOf::<T>::create(&caller, MILION.into()).unwrap().into();
XykOf::<T>::create_pool(caller.clone(), native_asset_id.into(), MILION.into(), second_token_id.into(), MILION.into()).unwrap();
let second_token_id = TokensOf::<T>::create(&caller, REWARDS_AMOUNT.into()).unwrap().into();
XykOf::<T>::create_pool(caller.clone(), native_asset_id.into(), REWARDS_AMOUNT.into(), second_token_id.into(), REWARDS_AMOUNT.into()).unwrap();
let reward_token_id = second_token_id + 1;

let rewards_amount = 20_000u128;

PoS::<T>::reward_pool(
RawOrigin::Signed(caller.clone().into()).into(),
(native_asset_id, first_token_id),
reward_token_id.into(),
rewards_amount,
REWARDS_AMOUNT,
2u32.into(),
).unwrap();

Expand All @@ -451,17 +452,17 @@ benchmarks! {
forward_to_next_session::<T>();
assert_eq!(
PoS::<T>::calculate_3rdparty_rewards_amount(caller.clone(), liquidity_asset_id, reward_token_id).unwrap(),
10_000u128
REWARDS_AMOUNT / 2
);
forward_to_next_session::<T>();
assert_eq!(
PoS::<T>::calculate_3rdparty_rewards_amount(caller.clone(), liquidity_asset_id, reward_token_id).unwrap(),
20_000u128
REWARDS_AMOUNT
);
forward_to_next_session::<T>();
assert_eq!(
PoS::<T>::calculate_3rdparty_rewards_amount(caller.clone(), liquidity_asset_id, reward_token_id).unwrap(),
20_000u128
REWARDS_AMOUNT
);

let balance_before:u128 = TokensOf::<T>::free_balance(reward_token_id.into(), &caller).into();
Expand All @@ -475,7 +476,7 @@ benchmarks! {
0u128
);

assert_eq!(balance_after - balance_before, 20_000u128);
assert_eq!(balance_after - balance_before, REWARDS_AMOUNT);
}

impl_benchmark_test_suite!(PoS, crate::mock::new_test_ext(), crate::mock::Test)
Expand Down
3 changes: 3 additions & 0 deletions pallets/proof-of-stake/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,9 @@ pub mod pallet {
Error::<T>::RewardTokenNotPairdWithNativeToken

Check warning on line 509 in pallets/proof-of-stake/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

pallets/proof-of-stake/src/lib.rs#L509

Added line #L509 was not covered by tests
);

let valutation =
<T as Config>::ValuationApi::valuate_liquidity_token(liquidity_token_id, amount);

Check warning on line 513 in pallets/proof-of-stake/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

pallets/proof-of-stake/src/lib.rs#L513

Added line #L513 was not covered by tests

ensure!(
<T as Config>::ValuationApi::valuate_liquidity_token(liquidity_token_id, amount) >=
T::Min3rdPartyRewards::get() ||
Expand Down
2 changes: 1 addition & 1 deletion pallets/proof-of-stake/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ impl pos::Config for Test {
type LiquidityMiningIssuanceVault = FakeLiquidityMiningIssuanceVault;
type RewardsDistributionPeriod = ConstU32<10>;
type RewardsSchedulesLimit = ConstU32<10>;
type Min3rdPartyRewards = ConstU128<10>;
type Min3rdPartyRewards = ConstU128<100_000>;
type WeightInfo = ();
type ValuationApi = Xyk;
}
Expand Down

0 comments on commit c0fafcd

Please sign in to comment.