Skip to content
This repository was archived by the owner on Apr 25, 2026. It is now read-only.

Commit 7b6c194

Browse files
authored
fix: weights for stake tiers (#1015)
* chore: update pallet weights for credits * chore: format
1 parent c718235 commit 7b6c194

10 files changed

Lines changed: 63 additions & 41 deletions

File tree

pallets/credits/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ This pallet relies on:
6060
- `RuntimeEvent`: The overarching event type.
6161
- `Currency`: The currency trait for TNT.
6262
- `AssetId`: The Asset ID type.
63-
- `TntAssetId`: The specific Asset ID for TNT.
6463
- `MultiAssetDelegationInfo`: Provides staking information (`get_user_deposit_with_locks`).
6564
- `BurnConversionRate`: Rate for converting burned TNT to potential credits.
6665
- `ClaimWindowBlocks`: The maximum accrual window duration in blocks.

pallets/credits/src/benchmarking.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#![cfg(feature = "runtime-benchmarks")]
2020

2121
use super::*;
22-
use crate::{BalanceOf, Config, LastRewardUpdateBlock, Pallet as Credits};
22+
use crate::{types::StakeTier, BalanceOf, Config, LastRewardUpdateBlock, Pallet as Credits};
2323
use frame_benchmarking::{v2::*, BenchmarkError};
2424
use frame_support::{
2525
traits::{Currency, Get},
@@ -56,6 +56,19 @@ fn setup_delegation<T: Config>(
5656
Ok(())
5757
}
5858

59+
/// Create stake tiers for benchmarking
60+
fn create_stake_tiers<T: Config>(tiers_count: u32) -> Vec<StakeTier<BalanceOf<T>>> {
61+
let mut tiers = Vec::new();
62+
for i in 0..tiers_count {
63+
// Create increasing thresholds and rates
64+
let threshold: BalanceOf<T> = ((i + 1) * 1000u32).into();
65+
let rate: BalanceOf<T> = ((i + 1) * 10u32).into();
66+
67+
tiers.push(StakeTier { threshold, rate_per_block: rate });
68+
}
69+
tiers
70+
}
71+
5972
#[benchmarks]
6073
mod benchmarks {
6174
use super::*;
@@ -108,5 +121,19 @@ mod benchmarks {
108121
Ok(())
109122
}
110123

124+
#[benchmark]
125+
fn set_stake_tiers() -> Result<(), BenchmarkError> {
126+
// Use the maximum allowed number of tiers to benchmark worst-case scenario
127+
let max_tiers = T::MaxStakeTiers::get() as u32;
128+
129+
// Create a set of stake tiers with increasing thresholds and rates
130+
let new_tiers = create_stake_tiers::<T>(max_tiers);
131+
132+
#[extrinsic_call]
133+
set_stake_tiers(RawOrigin::Root, new_tiers);
134+
135+
Ok(())
136+
}
137+
111138
impl_benchmark_test_suite!(Credits, crate::mock::new_test_ext(vec![]), crate::mock::Runtime);
112139
}

pallets/credits/src/lib.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,6 @@ pub mod pallet {
112112
/// The Asset ID type used by the Currency trait and MultiAssetDelegationInfo.
113113
type AssetId: Parameter + Member + MaybeDisplay + Ord + MaxEncodedLen + Copy + Debug;
114114

115-
/// The specific Asset ID for the TNT token.
116-
#[pallet::constant]
117-
type TntAssetId: Get<Self::AssetId>;
118-
119115
/// The provider for checking the active TNT stake.
120116
/// Ensure BalanceOf<Self> here resolves correctly to T::Currency::Balance.
121117
type MultiAssetDelegationInfo: MultiAssetDelegationInfo<
@@ -245,6 +241,8 @@ pub mod pallet {
245241
EmptyStakeTiers,
246242
/// Amount overflowed.
247243
Overflow,
244+
/// The stake tiers are too large to fit into the storage.
245+
StakeTiersOverflow,
248246
}
249247

250248
#[pallet::call]
@@ -324,7 +322,7 @@ pub mod pallet {
324322
///
325323
/// Weight: O(n) where n is the number of tiers
326324
#[pallet::call_index(2)]
327-
#[pallet::weight(T::WeightInfo::burn())]
325+
#[pallet::weight(T::WeightInfo::set_stake_tiers())]
328326
pub fn set_stake_tiers(
329327
origin: OriginFor<T>,
330328
new_tiers: Vec<StakeTier<BalanceOf<T>>>,
@@ -346,7 +344,7 @@ pub mod pallet {
346344
// Try to create a bounded vector
347345
let bounded_tiers =
348346
BoundedVec::<StakeTier<BalanceOf<T>>, T::MaxStakeTiers>::try_from(new_tiers)
349-
.map_err(|_| Error::<T>::EmptyStakeTiers)?; // Reusing error since we don't have a specific one for exceeding max tiers
347+
.map_err(|_| Error::<T>::StakeTiersOverflow)?;
350348

351349
// Update storage
352350
StoredStakeTiers::<T>::set(bounded_tiers);

pallets/credits/src/mock.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -532,11 +532,6 @@ impl pallet_multi_asset_delegation::Config for Runtime {
532532
}
533533

534534
parameter_types! {
535-
#[derive(Default, Copy, Clone, Eq, PartialEq, RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)]
536-
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
537-
pub const TntAssetId: AssetId = 1000;
538-
539-
540535
#[derive(Default, Copy, Clone, Eq, PartialEq, RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)]
541536
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
542537
pub const MaxStakeTiers: u32 = 10;
@@ -550,7 +545,6 @@ impl pallet_credits::Config for Runtime {
550545
type RuntimeEvent = RuntimeEvent;
551546
type Currency = Balances;
552547
type AssetId = AssetId;
553-
type TntAssetId = TntAssetId;
554548
type MultiAssetDelegationInfo = MultiAssetDelegation;
555549
type BurnConversionRate = ConstU128<1000>;
556550
type ClaimWindowBlocks = ConstU64<1000>;

pallets/credits/src/tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ fn get_max_claimable(who: AccountId) -> Balance {
5454
return 0;
5555
}
5656

57-
let tnt_asset_id = <Runtime as crate::Config>::TntAssetId::get();
57+
let tnt_asset_id = 0;
5858
let tnt_asset = tangle_primitives::services::Asset::Custom(tnt_asset_id);
5959

6060
let maybe_deposit_info =
@@ -106,7 +106,7 @@ fn run_to_block(n: BlockNumber) {
106106
}
107107

108108
fn setup_delegation(delegator: AccountId, operator: AccountId, amount: Balance) {
109-
let tnt_asset_id = <Runtime as crate::Config>::TntAssetId::get();
109+
let tnt_asset_id = 0;
110110
let tnt_asset = tangle_primitives::services::Asset::Custom(tnt_asset_id);
111111

112112
let min_bond = <Runtime as pallet_multi_asset_delegation::Config>::MinOperatorBondAmount::get();
@@ -492,7 +492,7 @@ fn accrual_with_stake_change_works() {
492492
let user = DAVE;
493493
let operator = EVE;
494494
let dave_id_str = b"dave_stake_change";
495-
let tnt_asset_id = <Runtime as crate::Config>::TntAssetId::get();
495+
let tnt_asset_id = 0;
496496
let tnt_asset = tangle_primitives::services::Asset::Custom(tnt_asset_id);
497497

498498
let stake_tier3 = 15000;

pallets/credits/src/weights.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
//! Autogenerated weights for credits pallet
1818
//!
1919
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0
20-
//! DATE: 2025-05-07, STEPS: `10`, REPEAT: `2`, LOW RANGE: `[]`, HIGH RANGE: `[]`
20+
//! DATE: 2024-05-21, STEPS: `10`, REPEAT: `2`, LOW RANGE: `[]`, HIGH RANGE: `[]`
2121
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128
2222
2323
// Executed Command:
@@ -46,6 +46,8 @@ pub trait WeightInfo {
4646
fn burn() -> Weight;
4747
/// Weight for the `claim_credits` extrinsic
4848
fn claim_credits() -> Weight;
49+
/// Weight for the `set_stake_tiers` extrinsic
50+
fn set_stake_tiers() -> Weight;
4951
}
5052

5153
/// Weights for credits pallet using the Substrate node and recommended hardware.
@@ -61,8 +63,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
6163
// Proof Size summary in bytes:
6264
// Measured: `512`
6365
// Estimated: `1024`
64-
// Minimum execution time: 25_000 nanoseconds.
65-
Weight::from_parts(26_000_000, 1024)
66+
// Minimum execution time: 24_300 nanoseconds.
67+
Weight::from_parts(24_850_000, 1056)
6668
.saturating_add(T::DbWeight::get().reads(3_u64))
6769
.saturating_add(T::DbWeight::get().writes(2_u64))
6870
}
@@ -79,24 +81,40 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
7981
// Proof Size summary in bytes:
8082
// Measured: `640`
8183
// Estimated: `1280`
82-
// Minimum execution time: 35_000 nanoseconds.
83-
Weight::from_parts(36_000_000, 1280)
84+
// Minimum execution time: 34_100 nanoseconds.
85+
Weight::from_parts(34_750_000, 1312)
8486
.saturating_add(T::DbWeight::get().reads(4_u64))
8587
.saturating_add(T::DbWeight::get().writes(2_u64))
8688
}
89+
90+
/// Storage: `Credits::StakeTiers` (r:0 w:1)
91+
/// Proof: `Credits::StakeTiers` (`max_values`: None, `max_size`: Some(256), mode: `Measured`)
92+
fn set_stake_tiers() -> Weight {
93+
// Proof Size summary in bytes:
94+
// Measured: `0`
95+
// Estimated: `0`
96+
// Minimum execution time: 16_200 nanoseconds.
97+
Weight::from_parts(17_350_000, 32)
98+
.saturating_add(T::DbWeight::get().writes(1_u64))
99+
}
87100
}
88101

89102
// For backwards compatibility and tests
90103
impl WeightInfo for () {
91104
fn burn() -> Weight {
92-
Weight::from_parts(26_000_000, 0)
105+
Weight::from_parts(24_850_000, 0)
93106
.saturating_add(RocksDbWeight::get().reads(3_u64))
94107
.saturating_add(RocksDbWeight::get().writes(2_u64))
95108
}
96109

97110
fn claim_credits() -> Weight {
98-
Weight::from_parts(36_000_000, 0)
111+
Weight::from_parts(34_750_000, 0)
99112
.saturating_add(RocksDbWeight::get().reads(4_u64))
100113
.saturating_add(RocksDbWeight::get().writes(2_u64))
101114
}
115+
116+
fn set_stake_tiers() -> Weight {
117+
Weight::from_parts(17_350_000, 0)
118+
.saturating_add(RocksDbWeight::get().writes(1_u64))
119+
}
102120
}

precompiles/credits/src/mock.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -408,11 +408,6 @@ impl
408408
}
409409

410410
parameter_types! {
411-
#[derive(Default, Copy, Clone, Eq, PartialEq, RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)]
412-
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
413-
pub const TntAssetId: AssetId = 1000;
414-
415-
416411
#[derive(Default, Copy, Clone, Eq, PartialEq, RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)]
417412
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
418413
pub const MaxStakeTiers: u32 = 10;
@@ -426,7 +421,6 @@ impl pallet_credits::Config for Runtime {
426421
type RuntimeEvent = RuntimeEvent;
427422
type Currency = Balances;
428423
type AssetId = AssetId;
429-
type TntAssetId = TntAssetId;
430424
type MultiAssetDelegationInfo = MockDelegationManager;
431425
type BurnConversionRate = ConstU128<1000>;
432426
type ClaimWindowBlocks = ConstU64<1000>;

runtime/mainnet/src/lib.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,10 +1373,6 @@ impl pallet_rewards::Config for Runtime {
13731373
}
13741374

13751375
parameter_types! {
1376-
#[derive(Default, Copy, Clone, Eq, PartialEq, RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)]
1377-
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
1378-
pub const TntAssetId: AssetId = 0;
1379-
13801376
#[derive(Default, Copy, Clone, Eq, PartialEq, RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)]
13811377
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
13821378
pub const MaxStakeTiers: u32 = 10;
@@ -1390,7 +1386,6 @@ impl pallet_credits::Config for Runtime {
13901386
type RuntimeEvent = RuntimeEvent;
13911387
type Currency = Balances;
13921388
type AssetId = AssetId;
1393-
type TntAssetId = TntAssetId;
13941389
type MultiAssetDelegationInfo = MultiAssetDelegation;
13951390
type BurnConversionRate = ConstU128<1000>;
13961391
type ClaimWindowBlocks = ConstU64<1000>;

runtime/testnet/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ runtime-benchmarks = [
185185
"pallet-airdrop-claims/runtime-benchmarks",
186186
"pallet-multi-asset-delegation/runtime-benchmarks",
187187
"pallet-tangle-lst-benchmarking/runtime-benchmarks",
188-
"pallet-rewards/runtime-benchmarks"
188+
"pallet-rewards/runtime-benchmarks",
189+
"pallet-credits/runtime-benchmarks",
189190
]
190191
default = ["std", "with-rocksdb-weights", "evm-tracing"]
191192
local-testing = []

runtime/testnet/src/lib.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,10 +1258,6 @@ impl pallet_rewards::Config for Runtime {
12581258
}
12591259

12601260
parameter_types! {
1261-
#[derive(Default, Copy, Clone, Eq, PartialEq, RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)]
1262-
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
1263-
pub const TntAssetId: AssetId = 0;
1264-
12651261
#[derive(Default, Copy, Clone, Eq, PartialEq, RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)]
12661262
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
12671263
pub const MaxStakeTiers: u32 = 10;
@@ -1275,7 +1271,6 @@ impl pallet_credits::Config for Runtime {
12751271
type RuntimeEvent = RuntimeEvent;
12761272
type Currency = Balances;
12771273
type AssetId = AssetId;
1278-
type TntAssetId = TntAssetId;
12791274
type MultiAssetDelegationInfo = MultiAssetDelegation;
12801275
type BurnConversionRate = ConstU128<1000>;
12811276
type ClaimWindowBlocks = ConstU64<1000>;
@@ -1627,6 +1622,7 @@ mod benches {
16271622
[pallet_tangle_lst_benchmarking, crate::benches::LstBench::<Runtime>]
16281623
[pallet_multi_asset_delegation, MultiAssetDelegation]
16291624
[pallet_rewards, Rewards]
1625+
[pallet_credits, Credits]
16301626
);
16311627
}
16321628

0 commit comments

Comments
 (0)