Skip to content

Commit b6e8d80

Browse files
authored
Merge pull request #1925 from opentensor/fix-weight-structure
CR Scale Patch
2 parents 2d613cf + 9f59bfa commit b6e8d80

File tree

8 files changed

+131
-23
lines changed

8 files changed

+131
-23
lines changed

pallets/admin-utils/src/lib.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ pub mod pallet {
705705
/// It is only callable by the root account or subnet owner.
706706
/// The extrinsic will call the Subtensor pallet to set the difficulty.
707707
#[pallet::call_index(24)]
708-
#[pallet::weight(Weight::from_parts(17_040_000, 0)
708+
#[pallet::weight(Weight::from_parts(15_540_000, 0)
709709
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(1_u64))
710710
.saturating_add(<T as frame_system::Config>::DbWeight::get().writes(1_u64)))]
711711
pub fn sudo_set_difficulty(
@@ -727,7 +727,7 @@ pub mod pallet {
727727
/// It is only callable by the root account.
728728
/// The extrinsic will call the Subtensor pallet to set the maximum allowed validators.
729729
#[pallet::call_index(25)]
730-
#[pallet::weight(Weight::from_parts(19_710_000, 0)
730+
#[pallet::weight(Weight::from_parts(23_860_000, 0)
731731
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(2_u64))
732732
.saturating_add(<T as frame_system::Config>::DbWeight::get().writes(1_u64)))]
733733
pub fn sudo_set_max_allowed_validators(
@@ -1658,7 +1658,13 @@ pub mod pallet {
16581658

16591659
/// Sets the commit-reveal weights version for all subnets
16601660
#[pallet::call_index(71)]
1661-
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
1661+
#[pallet::weight((
1662+
Weight::from_parts(6_171_000, 0)
1663+
.saturating_add(<T as frame_system::Config>::DbWeight::get().writes(1))
1664+
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(0_u64)),
1665+
DispatchClass::Operational,
1666+
Pays::No
1667+
))]
16621668
pub fn sudo_set_commit_reveal_version(
16631669
origin: OriginFor<T>,
16641670
version: u16,

pallets/drand/src/lib.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,11 @@ pub mod pallet {
277277
}
278278
}
279279
fn on_runtime_upgrade() -> frame_support::weights::Weight {
280-
/* let weight = */
281-
frame_support::weights::Weight::from_parts(0, 0) /*;*/
280+
let mut weight = frame_support::weights::Weight::from_parts(0, 0);
282281

283-
//weight = weight.saturating_add(migrations::migrate_prune_old_pulses::<T>());
282+
weight = weight.saturating_add(migrations::migrate_set_oldest_round::<T>());
284283

285-
//weight
284+
weight
286285
}
287286
}
288287

@@ -676,7 +675,7 @@ impl<T: Config> Pallet<T> {
676675
}
677676

678677
let mut removed: u64 = 0;
679-
while last_stored_round.saturating_sub(oldest) + 1 > MAX_KEPT_PULSES
678+
while last_stored_round.saturating_sub(oldest).saturating_add(1) > MAX_KEPT_PULSES
680679
&& removed < MAX_REMOVED_PULSES
681680
{
682681
Pulses::<T>::remove(oldest);

pallets/drand/src/migrations/migrate_prune_old_pulses.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub fn migrate_prune_old_pulses<T: Config>() -> Weight {
3535

3636
let mut new_oldest = rounds[0];
3737
if num_pulses > MAX_KEPT_PULSES {
38-
let num_to_delete = num_pulses - MAX_KEPT_PULSES;
38+
let num_to_delete = num_pulses.saturating_sub(MAX_KEPT_PULSES);
3939
new_oldest = rounds[num_to_delete as usize];
4040

4141
for &round in &rounds[0..num_to_delete as usize] {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use crate::*;
2+
use frame_support::weights::Weight;
3+
use log;
4+
5+
/// Migration to set `OldestStoredRound` to the oldest round in storage.
6+
pub fn migrate_set_oldest_round<T: Config>() -> Weight {
7+
use frame_support::traits::Get;
8+
9+
let migration_name = BoundedVec::truncate_from(b"migrate_set_oldest_round".to_vec());
10+
11+
// Start with one read for HasMigrationRun
12+
let mut weight = T::DbWeight::get().reads(1);
13+
14+
// Skip if already run.
15+
if HasMigrationRun::<T>::get(&migration_name) {
16+
log::info!(
17+
"Migration '{}' has already run. Skipping.",
18+
String::from_utf8_lossy(&migration_name)
19+
);
20+
return weight;
21+
}
22+
log::info!(
23+
"Running migration '{}'",
24+
String::from_utf8_lossy(&migration_name)
25+
);
26+
27+
// Single-pass over keys: track min and how many keys we read.
28+
let mut reads: u64 = 0;
29+
let mut min_round: Option<RoundNumber> = None;
30+
31+
for r in Pulses::<T>::iter_keys() {
32+
reads = reads.saturating_add(1);
33+
if min_round.is_none_or(|m| r < m) {
34+
min_round = Some(r);
35+
}
36+
}
37+
38+
// Account for all key reads
39+
weight = weight.saturating_add(T::DbWeight::get().reads(reads));
40+
41+
let oldest = min_round.unwrap_or(0u64);
42+
OldestStoredRound::<T>::put(oldest);
43+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
44+
45+
// Mark as completed.
46+
HasMigrationRun::<T>::insert(&migration_name, true);
47+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
48+
49+
log::info!(
50+
"Migration '{}' completed. OldestStoredRound set to {} (scanned {} rounds).",
51+
String::from_utf8_lossy(&migration_name),
52+
oldest,
53+
reads
54+
);
55+
56+
weight
57+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
pub mod migrate_prune_old_pulses;
22
pub use migrate_prune_old_pulses::*;
3+
pub mod migrate_set_oldest_round;
4+
pub use migrate_set_oldest_round::*;

pallets/drand/src/tests.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
use crate::{
1818
BeaconConfig, BeaconConfigurationPayload, BeaconInfoResponse, Call, DrandResponseBody,
1919
ENDPOINTS, Error, HasMigrationRun, LastStoredRound, MAX_KEPT_PULSES, OldestStoredRound, Pulse,
20-
Pulses, PulsesPayload, QUICKNET_CHAIN_HASH, migrations::migrate_prune_old_pulses, mock::*,
20+
Pulses, PulsesPayload, QUICKNET_CHAIN_HASH, migrations::migrate_prune_old_pulses,
21+
migrations::migrate_set_oldest_round, mock::*,
2122
};
2223
use codec::Encode;
2324
use frame_support::{
@@ -705,3 +706,40 @@ fn test_prune_maximum_of_100_pulses_per_call() {
705706
);
706707
});
707708
}
709+
710+
#[test]
711+
fn test_migrate_set_oldest_round() {
712+
new_test_ext().execute_with(|| {
713+
let migration_name = BoundedVec::truncate_from(b"migrate_set_oldest_round".to_vec());
714+
let db_weight: RuntimeDbWeight = <Test as frame_system::Config>::DbWeight::get();
715+
let pulse = Pulse::default();
716+
717+
assert_eq!(Pulses::<Test>::iter().count(), 0);
718+
assert!(!HasMigrationRun::<Test>::get(&migration_name));
719+
assert_eq!(OldestStoredRound::<Test>::get(), 0);
720+
assert_eq!(LastStoredRound::<Test>::get(), 0);
721+
722+
// Insert out-of-order rounds: oldest should be 5
723+
for r in [10u64, 7, 5].into_iter() {
724+
Pulses::<Test>::insert(r, pulse.clone());
725+
}
726+
let num_rounds = 3u64;
727+
728+
// Run migration
729+
let weight = migrate_set_oldest_round::<Test>();
730+
731+
assert_eq!(OldestStoredRound::<Test>::get(), 5);
732+
// Migration does NOT touch LastStoredRound
733+
assert_eq!(LastStoredRound::<Test>::get(), 0);
734+
// Pulses untouched
735+
assert!(Pulses::<Test>::contains_key(5));
736+
assert!(Pulses::<Test>::contains_key(7));
737+
assert!(Pulses::<Test>::contains_key(10));
738+
// Flag set
739+
assert!(HasMigrationRun::<Test>::get(&migration_name));
740+
741+
// Weight: reads(1 + num_rounds) + writes(2) [Oldest + HasMigrationRun]
742+
let expected = db_weight.reads(1 + num_rounds) + db_weight.writes(2);
743+
assert_eq!(weight, expected);
744+
});
745+
}

pallets/subtensor/src/coinbase/reveal_commits.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<T: Config> Pallet<T> {
5353

5454
// No commits to reveal until at least epoch reveal_period.
5555
if cur_epoch < reveal_period {
56-
log::warn!("Failed to reveal commit for subnet {netuid} Too early");
56+
log::trace!("Failed to reveal commit for subnet {netuid} Too early");
5757
return Ok(());
5858
}
5959

@@ -69,7 +69,7 @@ impl<T: Config> Pallet<T> {
6969
Some(p) => p,
7070
None => {
7171
// Round number used was not found on the chain. Skip this commit.
72-
log::warn!(
72+
log::trace!(
7373
"Failed to reveal commit for subnet {netuid} submitted by {who:?} on block {commit_block} due to missing round number {round_number}; will retry every block in reveal epoch."
7474
);
7575
unrevealed.push_back((
@@ -86,7 +86,7 @@ impl<T: Config> Pallet<T> {
8686
let commit = match TLECiphertext::<TinyBLS381>::deserialize_compressed(reader) {
8787
Ok(c) => c,
8888
Err(e) => {
89-
log::warn!(
89+
log::trace!(
9090
"Failed to reveal commit for subnet {netuid} submitted by {who:?} due to error deserializing the commit: {e:?}"
9191
);
9292
continue;
@@ -104,7 +104,7 @@ impl<T: Config> Pallet<T> {
104104
) {
105105
Ok(s) => s,
106106
Err(e) => {
107-
log::warn!(
107+
log::trace!(
108108
"Failed to reveal commit for subnet {netuid} submitted by {who:?} due to error deserializing signature from drand pallet: {e:?}"
109109
);
110110
continue;
@@ -116,7 +116,7 @@ impl<T: Config> Pallet<T> {
116116
) {
117117
Ok(d) => d,
118118
Err(e) => {
119-
log::warn!(
119+
log::trace!(
120120
"Failed to reveal commit for subnet {netuid} submitted by {who:?} due to error decrypting the commit: {e:?}"
121121
);
122122
continue;
@@ -136,16 +136,22 @@ impl<T: Config> Pallet<T> {
136136
(payload.uids, payload.values, payload.version_key)
137137
}
138138
Ok(_) => {
139-
log::warn!(
139+
log::trace!(
140140
"Failed to reveal commit for subnet {netuid} submitted by {who:?} due to hotkey mismatch in payload"
141141
);
142142
continue;
143143
}
144144
Err(e) => {
145-
log::warn!(
146-
"Failed to reveal commit for subnet {netuid} submitted by {who:?} due to error deserializing hotkey: {e:?}"
147-
);
148-
continue;
145+
let mut reader_legacy = &decrypted_bytes[..];
146+
match LegacyWeightsTlockPayload::decode(&mut reader_legacy) {
147+
Ok(legacy) => (legacy.uids, legacy.values, legacy.version_key),
148+
Err(_) => {
149+
log::trace!(
150+
"Failed to reveal commit for subnet {netuid} submitted by {who:?} due to error deserializing hotkey: {e:?}"
151+
);
152+
continue;
153+
}
154+
}
149155
}
150156
}
151157
} else {
@@ -154,7 +160,7 @@ impl<T: Config> Pallet<T> {
154160
match LegacyWeightsTlockPayload::decode(&mut reader_legacy) {
155161
Ok(legacy) => (legacy.uids, legacy.values, legacy.version_key),
156162
Err(e) => {
157-
log::warn!(
163+
log::trace!(
158164
"Failed to reveal commit for subnet {netuid} submitted by {who:?} due to error deserializing both payload formats: {e:?}"
159165
);
160166
continue;
@@ -173,7 +179,7 @@ impl<T: Config> Pallet<T> {
173179
values,
174180
version_key,
175181
) {
176-
log::warn!(
182+
log::trace!(
177183
"Failed to `do_set_weights` for subnet {netuid} submitted by {who:?}: {e:?}"
178184
);
179185
continue;

runtime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
218218
// `spec_version`, and `authoring_version` are the same between Wasm and native.
219219
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
220220
// the compatible custom types.
221-
spec_version: 299,
221+
spec_version: 300,
222222
impl_version: 1,
223223
apis: RUNTIME_API_VERSIONS,
224224
transaction_version: 1,

0 commit comments

Comments
 (0)