Skip to content

Commit

Permalink
staking: added weights
Browse files Browse the repository at this point in the history
  • Loading branch information
martinfridrich committed Jul 18, 2023
1 parent 17225f8 commit ec764df
Show file tree
Hide file tree
Showing 6 changed files with 467 additions and 27 deletions.
48 changes: 31 additions & 17 deletions math/src/staking/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,25 +191,39 @@ fn sigmoid_should_work() {

#[test]
fn calculate_percentage_amount_should_work() {
assert_eq!(calculate_percentage_amount(3_000_000_u128, FixedU128::from_float(0.5)), 1_500_000_u128);

assert_eq!(calculate_percentage_amount(3_000_000_u128, FixedU128::from(0)), 0_u128);

assert_eq!(calculate_percentage_amount(3_000_000_u128, FixedU128::from(1)), 3_000_000_u128);

assert_eq!(calculate_percentage_amount(3_000_000_u128, FixedU128::from_float(0.13264959)), 397_948_u128);
assert_eq!(
calculate_percentage_amount(3_000_000_u128, FixedU128::from_float(0.5)),
1_500_000_u128
);

assert_eq!(calculate_percentage_amount(3_000_000_u128, FixedU128::from(0)), 0_u128);

assert_eq!(
calculate_percentage_amount(3_000_000_u128, FixedU128::from(1)),
3_000_000_u128
);

assert_eq!(
calculate_percentage_amount(3_000_000_u128, FixedU128::from_float(0.13264959)),
397_948_u128
);
}

#[test]
fn calculate_rewards_should_work() {
let accumulated_rps = FixedU128::from_inner(23_423_523_230_000_000_000);
let rps = FixedU128::from_inner(23_423_000_000_000_000_000);
let amount = 1_000 * ONE;
assert_eq!(calculate_rewards(accumulated_rps, rps, amount).unwrap(), 523_230_000_000_u128);


let accumulated_rps = FixedU128::from_inner(23_423_523_230_000_000_000);
let rps = FixedU128::from_inner(19_423_000_000_000_000_000);
let amount = 1_000 * ONE;
assert_eq!(calculate_rewards(accumulated_rps, rps, amount).unwrap(), 4_000_523_230_000_000_u128);
let accumulated_rps = FixedU128::from_inner(23_423_523_230_000_000_000);
let rps = FixedU128::from_inner(23_423_000_000_000_000_000);
let amount = 1_000 * ONE;
assert_eq!(
calculate_rewards(accumulated_rps, rps, amount).unwrap(),
523_230_000_000_u128
);

let accumulated_rps = FixedU128::from_inner(23_423_523_230_000_000_000);
let rps = FixedU128::from_inner(19_423_000_000_000_000_000);
let amount = 1_000 * ONE;
assert_eq!(
calculate_rewards(accumulated_rps, rps, amount).unwrap(),
4_000_523_230_000_000_u128
);
}
10 changes: 5 additions & 5 deletions pallets/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ pub mod pallet {
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight(1_000)]
#[pallet::weight(<T as Config>::WeightInfo::initialize_staking())]
pub fn initialize_staking(origin: OriginFor<T>) -> DispatchResult {
T::AuthorityOrigin::ensure_origin(origin)?;

Expand All @@ -339,7 +339,7 @@ pub mod pallet {
}

#[pallet::call_index(1)]
#[pallet::weight(1_000)]
#[pallet::weight(<T as Config>::WeightInfo::stake())]
pub fn stake(origin: OriginFor<T>, amount: Balance) -> DispatchResult {
let who = ensure_signed(origin)?;

Expand Down Expand Up @@ -374,7 +374,7 @@ pub mod pallet {
}

#[pallet::call_index(2)]
#[pallet::weight(1_000)]
#[pallet::weight(<T as Config>::WeightInfo::increase_stake())]
pub fn increase_stake(origin: OriginFor<T>, position_id: T::PositionItemId, amount: Balance) -> DispatchResult {
let who = ensure_signed(origin)?;

Expand Down Expand Up @@ -474,7 +474,7 @@ pub mod pallet {
}

#[pallet::call_index(3)]
#[pallet::weight(1_000)]
#[pallet::weight(<T as Config>::WeightInfo::claim())]
pub fn claim(origin: OriginFor<T>, position_id: T::PositionItemId) -> DispatchResult {
let who = ensure_signed(origin)?;

Expand Down Expand Up @@ -574,7 +574,7 @@ pub mod pallet {
}

#[pallet::call_index(4)]
#[pallet::weight(1_000)]
#[pallet::weight(<T as Config>::WeightInfo::unstake())]
pub fn unstake(origin: OriginFor<T>, position_id: T::PositionItemId) -> DispatchResult {
let who = ensure_signed(origin)?;

Expand Down
274 changes: 270 additions & 4 deletions pallets/staking/src/weights.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,274 @@
// This file is part of HydraDX.

// Copyright (C) 2020-2023 Intergalactic, Limited (GIB).
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Autogenerated weights for pallet_staking
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2023-07-18, STEPS: 10, REPEAT: 30, LOW RANGE: [], HIGH RANGE: []
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024

// Executed Command:
// target/release/hydradx
// benchmark
// pallet
// --chain=dev
// --steps=10
// --repeat=30
// --execution=wasm
// --wasm-execution=compiled
// --heap-pages=4096
// --template=.maintain/pallet-weight-template-no-back.hbs
// --pallet=pallet_staking
// --output=staking.rs
// --extrinsic=*

#![allow(unused_parens)]
#![allow(unused_imports)]
#![allow(clippy::unnecessary_cast)]

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

pub trait WeightInfo {}
/// Weight functions needed for pallet_staking.
pub trait WeightInfo {
fn initialize_staking() -> Weight;
fn stake() -> Weight;
fn increase_stake() -> Weight;
fn claim() -> Weight;
fn unstake() -> Weight;
}

/// Weights for pallet_staking using the hydraDX node and recommended hardware.
pub struct HydraWeight<T>(PhantomData<T>);

pub struct SubstrateWeight<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {}
impl<T: frame_system::Config> WeightInfo for HydraWeight<T> {
// Storage: Staking Staking (r:1 w:1)
// Proof: Staking Staking (max_values: Some(1), max_size: Some(48), added: 543, mode: MaxEncodedLen)
// Storage: System Account (r:1 w:0)
// Proof: System Account (max_values: None, max_size: Some(128), added: 2603, mode: MaxEncodedLen)
// Storage: Uniques Class (r:1 w:1)
// Proof: Uniques Class (max_values: None, max_size: Some(190), added: 2665, mode: MaxEncodedLen)
// Storage: Uniques ClassAccount (r:0 w:1)
// Proof: Uniques ClassAccount (max_values: None, max_size: Some(80), added: 2555, mode: MaxEncodedLen)
fn initialize_staking() -> Weight {
// Minimum execution time: 45_493 nanoseconds.
Weight::from_ref_time(46_285_000 as u64)
.saturating_add(T::DbWeight::get().reads(3 as u64))
.saturating_add(T::DbWeight::get().writes(3 as u64))
}
// Storage: Staking Staking (r:1 w:1)
// Proof: Staking Staking (max_values: Some(1), max_size: Some(48), added: 543, mode: MaxEncodedLen)
// Storage: Uniques Account (r:1 w:1)
// Proof: Uniques Account (max_values: None, max_size: Some(112), added: 2587, mode: MaxEncodedLen)
// Storage: System Account (r:2 w:1)
// Proof: System Account (max_values: None, max_size: Some(128), added: 2603, mode: MaxEncodedLen)
// Storage: Balances Locks (r:1 w:1)
// Proof: Balances Locks (max_values: None, max_size: Some(1299), added: 3774, mode: MaxEncodedLen)
// Storage: Staking NextPositionId (r:1 w:1)
// Proof: Staking NextPositionId (max_values: Some(1), max_size: Some(16), added: 511, mode: MaxEncodedLen)
// Storage: Uniques Asset (r:1 w:1)
// Proof: Uniques Asset (max_values: None, max_size: Some(146), added: 2621, mode: MaxEncodedLen)
// Storage: Uniques Class (r:1 w:1)
// Proof: Uniques Class (max_values: None, max_size: Some(190), added: 2665, mode: MaxEncodedLen)
// Storage: Uniques CollectionMaxSupply (r:1 w:0)
// Proof: Uniques CollectionMaxSupply (max_values: None, max_size: Some(36), added: 2511, mode: MaxEncodedLen)
// Storage: Staking Positions (r:0 w:1)
// Proof: Staking Positions (max_values: None, max_size: Some(132), added: 2607, mode: MaxEncodedLen)
fn stake() -> Weight {
// Minimum execution time: 93_365 nanoseconds.
Weight::from_ref_time(94_660_000 as u64)
.saturating_add(T::DbWeight::get().reads(9 as u64))
.saturating_add(T::DbWeight::get().writes(8 as u64))
}
// Storage: Staking Staking (r:1 w:1)
// Proof: Staking Staking (max_values: Some(1), max_size: Some(48), added: 543, mode: MaxEncodedLen)
// Storage: Uniques Asset (r:1 w:0)
// Proof: Uniques Asset (max_values: None, max_size: Some(146), added: 2621, mode: MaxEncodedLen)
// Storage: System Account (r:2 w:1)
// Proof: System Account (max_values: None, max_size: Some(128), added: 2603, mode: MaxEncodedLen)
// Storage: Staking Positions (r:1 w:1)
// Proof: Staking Positions (max_values: None, max_size: Some(132), added: 2607, mode: MaxEncodedLen)
// Storage: Balances Locks (r:1 w:1)
// Proof: Balances Locks (max_values: None, max_size: Some(1299), added: 3774, mode: MaxEncodedLen)
// Storage: Staking PositionVotes (r:1 w:0)
// Proof: Staking PositionVotes (max_values: None, max_size: Some(2134), added: 4609, mode: MaxEncodedLen)
// Storage: Democracy ReferendumInfoOf (r:100 w:0)
// Proof: Democracy ReferendumInfoOf (max_values: None, max_size: Some(201), added: 2676, mode: MaxEncodedLen)
fn increase_stake() -> Weight {
// Minimum execution time: 206_722 nanoseconds.
Weight::from_ref_time(209_665_000 as u64)
.saturating_add(T::DbWeight::get().reads(107 as u64))
.saturating_add(T::DbWeight::get().writes(4 as u64))
}
// Storage: Staking Staking (r:1 w:1)
// Proof: Staking Staking (max_values: Some(1), max_size: Some(48), added: 543, mode: MaxEncodedLen)
// Storage: Uniques Asset (r:1 w:0)
// Proof: Uniques Asset (max_values: None, max_size: Some(146), added: 2621, mode: MaxEncodedLen)
// Storage: System Account (r:2 w:2)
// Proof: System Account (max_values: None, max_size: Some(128), added: 2603, mode: MaxEncodedLen)
// Storage: Staking Positions (r:1 w:1)
// Proof: Staking Positions (max_values: None, max_size: Some(132), added: 2607, mode: MaxEncodedLen)
// Storage: Staking PositionVotes (r:1 w:0)
// Proof: Staking PositionVotes (max_values: None, max_size: Some(2134), added: 4609, mode: MaxEncodedLen)
// Storage: Democracy ReferendumInfoOf (r:100 w:0)
// Proof: Democracy ReferendumInfoOf (max_values: None, max_size: Some(201), added: 2676, mode: MaxEncodedLen)
// Storage: Balances Locks (r:1 w:1)
// Proof: Balances Locks (max_values: None, max_size: Some(1299), added: 3774, mode: MaxEncodedLen)
fn claim() -> Weight {
// Minimum execution time: 225_912 nanoseconds.
Weight::from_ref_time(227_912_000 as u64)
.saturating_add(T::DbWeight::get().reads(107 as u64))
.saturating_add(T::DbWeight::get().writes(5 as u64))
}
// Storage: Staking Staking (r:1 w:1)
// Proof: Staking Staking (max_values: Some(1), max_size: Some(48), added: 543, mode: MaxEncodedLen)
// Storage: Uniques Asset (r:1 w:1)
// Proof: Uniques Asset (max_values: None, max_size: Some(146), added: 2621, mode: MaxEncodedLen)
// Storage: System Account (r:2 w:2)
// Proof: System Account (max_values: None, max_size: Some(128), added: 2603, mode: MaxEncodedLen)
// Storage: Staking Positions (r:1 w:1)
// Proof: Staking Positions (max_values: None, max_size: Some(132), added: 2607, mode: MaxEncodedLen)
// Storage: Staking PositionVotes (r:1 w:0)
// Proof: Staking PositionVotes (max_values: None, max_size: Some(2134), added: 4609, mode: MaxEncodedLen)
// Storage: Democracy ReferendumInfoOf (r:100 w:0)
// Proof: Democracy ReferendumInfoOf (max_values: None, max_size: Some(201), added: 2676, mode: MaxEncodedLen)
// Storage: Uniques Class (r:1 w:1)
// Proof: Uniques Class (max_values: None, max_size: Some(190), added: 2665, mode: MaxEncodedLen)
// Storage: Balances Locks (r:1 w:1)
// Proof: Balances Locks (max_values: None, max_size: Some(1299), added: 3774, mode: MaxEncodedLen)
// Storage: Uniques Account (r:0 w:1)
// Proof: Uniques Account (max_values: None, max_size: Some(112), added: 2587, mode: MaxEncodedLen)
// Storage: Uniques ItemPriceOf (r:0 w:1)
// Proof: Uniques ItemPriceOf (max_values: None, max_size: Some(113), added: 2588, mode: MaxEncodedLen)
fn unstake() -> Weight {
// Minimum execution time: 246_989 nanoseconds.
Weight::from_ref_time(249_570_000 as u64)
.saturating_add(T::DbWeight::get().reads(108 as u64))
.saturating_add(T::DbWeight::get().writes(9 as u64))
}
}

impl WeightInfo for () {}
// For backwards compatibility and tests
impl WeightInfo for () {
// Storage: Staking Staking (r:1 w:1)
// Proof: Staking Staking (max_values: Some(1), max_size: Some(48), added: 543, mode: MaxEncodedLen)
// Storage: System Account (r:1 w:0)
// Proof: System Account (max_values: None, max_size: Some(128), added: 2603, mode: MaxEncodedLen)
// Storage: Uniques Class (r:1 w:1)
// Proof: Uniques Class (max_values: None, max_size: Some(190), added: 2665, mode: MaxEncodedLen)
// Storage: Uniques ClassAccount (r:0 w:1)
// Proof: Uniques ClassAccount (max_values: None, max_size: Some(80), added: 2555, mode: MaxEncodedLen)
fn initialize_staking() -> Weight {
// Minimum execution time: 45_493 nanoseconds.
Weight::from_ref_time(46_285_000 as u64)
.saturating_add(RocksDbWeight::get().reads(3 as u64))
.saturating_add(RocksDbWeight::get().writes(3 as u64))
}
// Storage: Staking Staking (r:1 w:1)
// Proof: Staking Staking (max_values: Some(1), max_size: Some(48), added: 543, mode: MaxEncodedLen)
// Storage: Uniques Account (r:1 w:1)
// Proof: Uniques Account (max_values: None, max_size: Some(112), added: 2587, mode: MaxEncodedLen)
// Storage: System Account (r:2 w:1)
// Proof: System Account (max_values: None, max_size: Some(128), added: 2603, mode: MaxEncodedLen)
// Storage: Balances Locks (r:1 w:1)
// Proof: Balances Locks (max_values: None, max_size: Some(1299), added: 3774, mode: MaxEncodedLen)
// Storage: Staking NextPositionId (r:1 w:1)
// Proof: Staking NextPositionId (max_values: Some(1), max_size: Some(16), added: 511, mode: MaxEncodedLen)
// Storage: Uniques Asset (r:1 w:1)
// Proof: Uniques Asset (max_values: None, max_size: Some(146), added: 2621, mode: MaxEncodedLen)
// Storage: Uniques Class (r:1 w:1)
// Proof: Uniques Class (max_values: None, max_size: Some(190), added: 2665, mode: MaxEncodedLen)
// Storage: Uniques CollectionMaxSupply (r:1 w:0)
// Proof: Uniques CollectionMaxSupply (max_values: None, max_size: Some(36), added: 2511, mode: MaxEncodedLen)
// Storage: Staking Positions (r:0 w:1)
// Proof: Staking Positions (max_values: None, max_size: Some(132), added: 2607, mode: MaxEncodedLen)
fn stake() -> Weight {
// Minimum execution time: 93_365 nanoseconds.
Weight::from_ref_time(94_660_000 as u64)
.saturating_add(RocksDbWeight::get().reads(9 as u64))
.saturating_add(RocksDbWeight::get().writes(8 as u64))
}
// Storage: Staking Staking (r:1 w:1)
// Proof: Staking Staking (max_values: Some(1), max_size: Some(48), added: 543, mode: MaxEncodedLen)
// Storage: Uniques Asset (r:1 w:0)
// Proof: Uniques Asset (max_values: None, max_size: Some(146), added: 2621, mode: MaxEncodedLen)
// Storage: System Account (r:2 w:1)
// Proof: System Account (max_values: None, max_size: Some(128), added: 2603, mode: MaxEncodedLen)
// Storage: Staking Positions (r:1 w:1)
// Proof: Staking Positions (max_values: None, max_size: Some(132), added: 2607, mode: MaxEncodedLen)
// Storage: Balances Locks (r:1 w:1)
// Proof: Balances Locks (max_values: None, max_size: Some(1299), added: 3774, mode: MaxEncodedLen)
// Storage: Staking PositionVotes (r:1 w:0)
// Proof: Staking PositionVotes (max_values: None, max_size: Some(2134), added: 4609, mode: MaxEncodedLen)
// Storage: Democracy ReferendumInfoOf (r:100 w:0)
// Proof: Democracy ReferendumInfoOf (max_values: None, max_size: Some(201), added: 2676, mode: MaxEncodedLen)
fn increase_stake() -> Weight {
// Minimum execution time: 206_722 nanoseconds.
Weight::from_ref_time(209_665_000 as u64)
.saturating_add(RocksDbWeight::get().reads(107 as u64))
.saturating_add(RocksDbWeight::get().writes(4 as u64))
}
// Storage: Staking Staking (r:1 w:1)
// Proof: Staking Staking (max_values: Some(1), max_size: Some(48), added: 543, mode: MaxEncodedLen)
// Storage: Uniques Asset (r:1 w:0)
// Proof: Uniques Asset (max_values: None, max_size: Some(146), added: 2621, mode: MaxEncodedLen)
// Storage: System Account (r:2 w:2)
// Proof: System Account (max_values: None, max_size: Some(128), added: 2603, mode: MaxEncodedLen)
// Storage: Staking Positions (r:1 w:1)
// Proof: Staking Positions (max_values: None, max_size: Some(132), added: 2607, mode: MaxEncodedLen)
// Storage: Staking PositionVotes (r:1 w:0)
// Proof: Staking PositionVotes (max_values: None, max_size: Some(2134), added: 4609, mode: MaxEncodedLen)
// Storage: Democracy ReferendumInfoOf (r:100 w:0)
// Proof: Democracy ReferendumInfoOf (max_values: None, max_size: Some(201), added: 2676, mode: MaxEncodedLen)
// Storage: Balances Locks (r:1 w:1)
// Proof: Balances Locks (max_values: None, max_size: Some(1299), added: 3774, mode: MaxEncodedLen)
fn claim() -> Weight {
// Minimum execution time: 225_912 nanoseconds.
Weight::from_ref_time(227_912_000 as u64)
.saturating_add(RocksDbWeight::get().reads(107 as u64))
.saturating_add(RocksDbWeight::get().writes(5 as u64))
}
// Storage: Staking Staking (r:1 w:1)
// Proof: Staking Staking (max_values: Some(1), max_size: Some(48), added: 543, mode: MaxEncodedLen)
// Storage: Uniques Asset (r:1 w:1)
// Proof: Uniques Asset (max_values: None, max_size: Some(146), added: 2621, mode: MaxEncodedLen)
// Storage: System Account (r:2 w:2)
// Proof: System Account (max_values: None, max_size: Some(128), added: 2603, mode: MaxEncodedLen)
// Storage: Staking Positions (r:1 w:1)
// Proof: Staking Positions (max_values: None, max_size: Some(132), added: 2607, mode: MaxEncodedLen)
// Storage: Staking PositionVotes (r:1 w:0)
// Proof: Staking PositionVotes (max_values: None, max_size: Some(2134), added: 4609, mode: MaxEncodedLen)
// Storage: Democracy ReferendumInfoOf (r:100 w:0)
// Proof: Democracy ReferendumInfoOf (max_values: None, max_size: Some(201), added: 2676, mode: MaxEncodedLen)
// Storage: Uniques Class (r:1 w:1)
// Proof: Uniques Class (max_values: None, max_size: Some(190), added: 2665, mode: MaxEncodedLen)
// Storage: Balances Locks (r:1 w:1)
// Proof: Balances Locks (max_values: None, max_size: Some(1299), added: 3774, mode: MaxEncodedLen)
// Storage: Uniques Account (r:0 w:1)
// Proof: Uniques Account (max_values: None, max_size: Some(112), added: 2587, mode: MaxEncodedLen)
// Storage: Uniques ItemPriceOf (r:0 w:1)
// Proof: Uniques ItemPriceOf (max_values: None, max_size: Some(113), added: 2588, mode: MaxEncodedLen)
fn unstake() -> Weight {
// Minimum execution time: 246_989 nanoseconds.
Weight::from_ref_time(249_570_000 as u64)
.saturating_add(RocksDbWeight::get().reads(108 as u64))
.saturating_add(RocksDbWeight::get().writes(9 as u64))
}
}
2 changes: 1 addition & 1 deletion runtime/hydradx/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,5 +534,5 @@ impl pallet_staking::Config for Runtime {
type ActionMultiplier = ActionMultiplier;
type Vesting = VestingInfo<Runtime>;
type RewardedVoteUnit = OneHDX;
type WeightInfo = ();
type WeightInfo = weights::staking::HydraWeight<Runtime>;
}
1 change: 1 addition & 0 deletions runtime/hydradx/src/weights/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub mod proxy;
pub mod registry;
pub mod route_executor;
pub mod scheduler;
pub mod staking;
pub mod system;
pub mod technical_comittee;
pub mod timestamp;
Expand Down
Loading

0 comments on commit ec764df

Please sign in to comment.