Skip to content

Commit

Permalink
update weights
Browse files Browse the repository at this point in the history
  • Loading branch information
Roznovjak committed Nov 9, 2023
1 parent c97cc0e commit a634941
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 67 deletions.
15 changes: 14 additions & 1 deletion integration-tests/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,26 @@ use xcm_emulator::TestExt;

use pallet_stableswap::types::AssetAmount;
use pallet_stableswap::MAX_ASSETS_IN_POOL;
use sp_runtime::{traits::ConstU32, DispatchError, FixedU128, Permill};
use sp_runtime::{
traits::{ConstU32, Zero},
DispatchError, FixedU128, Permill,
};

use orml_traits::MultiCurrency;

pub const LBP_SALE_START: BlockNumber = 10;
pub const LBP_SALE_END: BlockNumber = 40;

#[test]
fn router_weights_should_be_non_zero() {
assert!(!RouterWeightInfo::sell_and_calculate_sell_trade_amounts_overhead_weight(0, 1).is_zero());
assert!(!RouterWeightInfo::sell_and_calculate_sell_trade_amounts_overhead_weight(1, 1).is_zero());

assert!(!RouterWeightInfo::buy_and_calculate_buy_trade_amounts_overhead_weight(0, 1).is_zero());
assert!(!RouterWeightInfo::buy_and_calculate_buy_trade_amounts_overhead_weight(1, 0).is_zero());
assert!(!RouterWeightInfo::buy_and_calculate_buy_trade_amounts_overhead_weight(2, 1).is_zero());
}

mod router_different_pools_tests {
use super::*;

Expand Down
3 changes: 2 additions & 1 deletion pallets/lbp/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ benchmarks! {
}
}

// We need to handle this case separately, because for some unknown reason, router_execution_buy provides incorrect weight when just one calculate_buy is executed
calculate_buy {
let caller = funded_account::<T>("caller", 0);
let fee_collector = funded_account::<T>("fee_collector", 0);
Expand All @@ -248,7 +249,7 @@ benchmarks! {
let start = T::BlockNumber::from(1u32);
let end = T::BlockNumber::from(11u32);

LBP::<T>::update_pool_data(RawOrigin::Signed(caller.clone()).into(), pool_id, None, Some(start), Some(end), None, None, None, None, None)?;
LBP::<T>::update_pool_data(RawOrigin::Signed(caller).into(), pool_id, None, Some(start), Some(end), None, None, None, None, None)?;

frame_system::Pallet::<T>::set_block_number(T::BlockNumber::from(2u32));

Expand Down
6 changes: 4 additions & 2 deletions pallets/lbp/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ impl<T: frame_system::Config> WeightInfo for HydraWeight<T> {
.saturating_add(T::DbWeight::get().writes((7 as u64).saturating_mul(e as u64)))
}
fn calculate_buy() -> Weight {
Weight::zero()
// Minimum execution time: 66_824 nanoseconds.
Weight::from_ref_time(67_440_000 as u64).saturating_add(T::DbWeight::get().reads(3 as u64))
}
}

Expand Down Expand Up @@ -351,6 +352,7 @@ impl WeightInfo for () {
.saturating_add(RocksDbWeight::get().writes((7 as u64).saturating_mul(e as u64)))
}
fn calculate_buy() -> Weight {
Weight::zero()
// Minimum execution time: 66_824 nanoseconds.
Weight::from_ref_time(67_440_000 as u64).saturating_add(RocksDbWeight::get().reads(3 as u64))
}
}
80 changes: 40 additions & 40 deletions pallets/route-executor/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ impl<T: frame_system::Config> WeightInfo for HydraWeight<T> {
// Proof: Tokens Locks (max_values: None, max_size: Some(1261), added: 3736, mode: MaxEncodedLen)
/// The range of component `c` is `[0, 1]`.
/// The range of component `s` is `[0, 1]`.
fn calculate_and_execute_sell_in_lbp(c: u32, s: u32, ) -> Weight {
// Minimum execution time: 74_867 nanoseconds.
Weight::from_ref_time(25_402_600 as u64) // Standard Error: 93_955
.saturating_add(Weight::from_ref_time(50_537_097 as u64).saturating_mul(c as u64))
// Standard Error: 93_955
.saturating_add(Weight::from_ref_time(284_755_068 as u64).saturating_mul(s as u64))
.saturating_add(T::DbWeight::get().reads(3 as u64))
.saturating_add(T::DbWeight::get().reads((9 as u64).saturating_mul(s as u64)))
.saturating_add(T::DbWeight::get().writes((7 as u64).saturating_mul(s as u64)))
}
fn calculate_and_execute_sell_in_lbp(c: u32, s: u32) -> Weight {
// Minimum execution time: 74_867 nanoseconds.
Weight::from_ref_time(25_402_600 as u64) // Standard Error: 93_955
.saturating_add(Weight::from_ref_time(50_537_097 as u64).saturating_mul(c as u64))
// Standard Error: 93_955
.saturating_add(Weight::from_ref_time(284_755_068 as u64).saturating_mul(s as u64))
.saturating_add(T::DbWeight::get().reads(3 as u64))
.saturating_add(T::DbWeight::get().reads((9 as u64).saturating_mul(s as u64)))
.saturating_add(T::DbWeight::get().writes((7 as u64).saturating_mul(s as u64)))
}
// Storage: LBP PoolData (r:1 w:0)
// Proof: LBP PoolData (max_values: None, max_size: Some(163), added: 2638, mode: MaxEncodedLen)
// Storage: Tokens Accounts (r:5 w:5)
Expand All @@ -90,16 +90,16 @@ impl<T: frame_system::Config> WeightInfo for HydraWeight<T> {
// Proof: Tokens Locks (max_values: None, max_size: Some(1261), added: 3736, mode: MaxEncodedLen)
/// The range of component `c` is `[1, 2]`.
/// The range of component `b` is `[0, 1]`.
fn calculate_and_execute_buy_in_lbp(c: u32, b: u32, ) -> Weight {
// Minimum execution time: 75_404 nanoseconds.
Weight::from_ref_time(76_035_000 as u64) // Standard Error: 590_075
.saturating_add(Weight::from_ref_time(2_400_398 as u64).saturating_mul(c as u64))
// Standard Error: 1_295_384
.saturating_add(Weight::from_ref_time(237_220_349 as u64).saturating_mul(b as u64))
.saturating_add(T::DbWeight::get().reads(3 as u64))
.saturating_add(T::DbWeight::get().reads((9 as u64).saturating_mul(b as u64)))
.saturating_add(T::DbWeight::get().writes((7 as u64).saturating_mul(b as u64)))
}
fn calculate_and_execute_buy_in_lbp(c: u32, b: u32) -> Weight {
// Minimum execution time: 75_404 nanoseconds.
Weight::from_ref_time(76_035_000 as u64) // Standard Error: 590_075
.saturating_add(Weight::from_ref_time(2_400_398 as u64).saturating_mul(c as u64))
// Standard Error: 1_295_384
.saturating_add(Weight::from_ref_time(237_220_349 as u64).saturating_mul(b as u64))
.saturating_add(T::DbWeight::get().reads(3 as u64))
.saturating_add(T::DbWeight::get().reads((9 as u64).saturating_mul(b as u64)))
.saturating_add(T::DbWeight::get().writes((7 as u64).saturating_mul(b as u64)))
}
}

// For backwards compatibility and tests
Expand All @@ -116,16 +116,16 @@ impl WeightInfo for () {
// Proof: Tokens Locks (max_values: None, max_size: Some(1261), added: 3736, mode: MaxEncodedLen)
/// The range of component `c` is `[0, 1]`.
/// The range of component `s` is `[0, 1]`.
fn calculate_and_execute_sell_in_lbp(c: u32, s: u32, ) -> Weight {
// Minimum execution time: 74_867 nanoseconds.
Weight::from_ref_time(25_402_600 as u64) // Standard Error: 93_955
.saturating_add(Weight::from_ref_time(50_537_097 as u64).saturating_mul(c as u64))
// Standard Error: 93_955
.saturating_add(Weight::from_ref_time(284_755_068 as u64).saturating_mul(s as u64))
.saturating_add(RocksDbWeight::get().reads(3 as u64))
.saturating_add(RocksDbWeight::get().reads((9 as u64).saturating_mul(s as u64)))
.saturating_add(RocksDbWeight::get().writes((7 as u64).saturating_mul(s as u64)))
}
fn calculate_and_execute_sell_in_lbp(c: u32, s: u32) -> Weight {
// Minimum execution time: 74_867 nanoseconds.
Weight::from_ref_time(25_402_600 as u64) // Standard Error: 93_955
.saturating_add(Weight::from_ref_time(50_537_097 as u64).saturating_mul(c as u64))
// Standard Error: 93_955
.saturating_add(Weight::from_ref_time(284_755_068 as u64).saturating_mul(s as u64))
.saturating_add(RocksDbWeight::get().reads(3 as u64))
.saturating_add(RocksDbWeight::get().reads((9 as u64).saturating_mul(s as u64)))
.saturating_add(RocksDbWeight::get().writes((7 as u64).saturating_mul(s as u64)))
}
// Storage: LBP PoolData (r:1 w:0)
// Proof: LBP PoolData (max_values: None, max_size: Some(163), added: 2638, mode: MaxEncodedLen)
// Storage: Tokens Accounts (r:5 w:5)
Expand All @@ -138,14 +138,14 @@ impl WeightInfo for () {
// Proof: Tokens Locks (max_values: None, max_size: Some(1261), added: 3736, mode: MaxEncodedLen)
/// The range of component `c` is `[1, 2]`.
/// The range of component `b` is `[0, 1]`.
fn calculate_and_execute_buy_in_lbp(c: u32, b: u32, ) -> Weight {
// Minimum execution time: 75_404 nanoseconds.
Weight::from_ref_time(76_035_000 as u64) // Standard Error: 590_075
.saturating_add(Weight::from_ref_time(2_400_398 as u64).saturating_mul(c as u64))
// Standard Error: 1_295_384
.saturating_add(Weight::from_ref_time(237_220_349 as u64).saturating_mul(b as u64))
.saturating_add(RocksDbWeight::get().reads(3 as u64))
.saturating_add(RocksDbWeight::get().reads((9 as u64).saturating_mul(b as u64)))
.saturating_add(RocksDbWeight::get().writes((7 as u64).saturating_mul(b as u64)))
}
fn calculate_and_execute_buy_in_lbp(c: u32, b: u32) -> Weight {
// Minimum execution time: 75_404 nanoseconds.
Weight::from_ref_time(76_035_000 as u64) // Standard Error: 590_075
.saturating_add(Weight::from_ref_time(2_400_398 as u64).saturating_mul(c as u64))
// Standard Error: 1_295_384
.saturating_add(Weight::from_ref_time(237_220_349 as u64).saturating_mul(b as u64))
.saturating_add(RocksDbWeight::get().reads(3 as u64))
.saturating_add(RocksDbWeight::get().reads((9 as u64).saturating_mul(b as u64)))
.saturating_add(RocksDbWeight::get().writes((7 as u64).saturating_mul(b as u64)))
}
}
1 change: 1 addition & 0 deletions runtime/hydradx/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ impl RouterWeightInfo {
num_of_calc_buy,
num_of_execute_buy,
);
// Handle this case separately. router_execution_buy provides incorrect weight for the case when only calculate_buy is executed.
let lbp_weight = if (num_of_calc_buy, num_of_execute_buy) == (1, 0) {
weights::lbp::HydraWeight::<Runtime>::calculate_buy()
} else {
Expand Down
3 changes: 2 additions & 1 deletion runtime/hydradx/src/weights/lbp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ impl<T: frame_system::Config> WeightInfo for HydraWeight<T> {
.saturating_add(T::DbWeight::get().writes((7 as u64).saturating_mul(e as u64)))
}
fn calculate_buy() -> Weight {
Weight::zero()
// Minimum execution time: 66_824 nanoseconds.
Weight::from_ref_time(67_440_000 as u64).saturating_add(T::DbWeight::get().reads(3 as u64))
}
}
44 changes: 22 additions & 22 deletions runtime/hydradx/src/weights/route_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
#![allow(clippy::unnecessary_cast)]

use frame_support::{
traits::Get,
weights::{constants::RocksDbWeight, Weight},
traits::Get,
weights::{constants::RocksDbWeight, Weight},
};
use sp_std::marker::PhantomData;

Expand All @@ -63,16 +63,16 @@ impl<T: frame_system::Config> WeightInfo for HydraWeight<T> {
// Proof: Tokens Locks (max_values: None, max_size: Some(1261), added: 3736, mode: MaxEncodedLen)
/// The range of component `c` is `[0, 1]`.
/// The range of component `s` is `[0, 1]`.
fn calculate_and_execute_sell_in_lbp(c: u32, s: u32, ) -> Weight {
// Minimum execution time: 74_867 nanoseconds.
Weight::from_ref_time(25_402_600 as u64) // Standard Error: 93_955
.saturating_add(Weight::from_ref_time(50_537_097 as u64).saturating_mul(c as u64))
// Standard Error: 93_955
.saturating_add(Weight::from_ref_time(284_755_068 as u64).saturating_mul(s as u64))
.saturating_add(T::DbWeight::get().reads(3 as u64))
.saturating_add(T::DbWeight::get().reads((9 as u64).saturating_mul(s as u64)))
.saturating_add(T::DbWeight::get().writes((7 as u64).saturating_mul(s as u64)))
}
fn calculate_and_execute_sell_in_lbp(c: u32, s: u32) -> Weight {
// Minimum execution time: 74_867 nanoseconds.
Weight::from_ref_time(25_402_600 as u64) // Standard Error: 93_955
.saturating_add(Weight::from_ref_time(50_537_097 as u64).saturating_mul(c as u64))
// Standard Error: 93_955
.saturating_add(Weight::from_ref_time(284_755_068 as u64).saturating_mul(s as u64))
.saturating_add(T::DbWeight::get().reads(3 as u64))
.saturating_add(T::DbWeight::get().reads((9 as u64).saturating_mul(s as u64)))
.saturating_add(T::DbWeight::get().writes((7 as u64).saturating_mul(s as u64)))
}
// Storage: LBP PoolData (r:1 w:0)
// Proof: LBP PoolData (max_values: None, max_size: Some(163), added: 2638, mode: MaxEncodedLen)
// Storage: Tokens Accounts (r:5 w:5)
Expand All @@ -85,14 +85,14 @@ impl<T: frame_system::Config> WeightInfo for HydraWeight<T> {
// Proof: Tokens Locks (max_values: None, max_size: Some(1261), added: 3736, mode: MaxEncodedLen)
/// The range of component `c` is `[1, 2]`.
/// The range of component `b` is `[0, 1]`.
fn calculate_and_execute_buy_in_lbp(c: u32, b: u32, ) -> Weight {
// Minimum execution time: 75_404 nanoseconds.
Weight::from_ref_time(76_035_000 as u64) // Standard Error: 590_075
.saturating_add(Weight::from_ref_time(2_400_398 as u64).saturating_mul(c as u64))
// Standard Error: 1_295_384
.saturating_add(Weight::from_ref_time(237_220_349 as u64).saturating_mul(b as u64))
.saturating_add(T::DbWeight::get().reads(3 as u64))
.saturating_add(T::DbWeight::get().reads((9 as u64).saturating_mul(b as u64)))
.saturating_add(T::DbWeight::get().writes((7 as u64).saturating_mul(b as u64)))
}
fn calculate_and_execute_buy_in_lbp(c: u32, b: u32) -> Weight {
// Minimum execution time: 75_404 nanoseconds.
Weight::from_ref_time(76_035_000 as u64) // Standard Error: 590_075
.saturating_add(Weight::from_ref_time(2_400_398 as u64).saturating_mul(c as u64))
// Standard Error: 1_295_384
.saturating_add(Weight::from_ref_time(237_220_349 as u64).saturating_mul(b as u64))
.saturating_add(T::DbWeight::get().reads(3 as u64))
.saturating_add(T::DbWeight::get().reads((9 as u64).saturating_mul(b as u64)))
.saturating_add(T::DbWeight::get().writes((7 as u64).saturating_mul(b as u64)))
}
}

0 comments on commit a634941

Please sign in to comment.