Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions pallets/admin-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ pub mod pallet {
/// It is only callable by the root account or subnet owner.
/// The extrinsic will call the Subtensor pallet to set the difficulty.
#[pallet::call_index(24)]
#[pallet::weight(Weight::from_parts(17_040_000, 0)
#[pallet::weight(Weight::from_parts(15_540_000, 0)
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(1_u64))
.saturating_add(<T as frame_system::Config>::DbWeight::get().writes(1_u64)))]
pub fn sudo_set_difficulty(
Expand All @@ -727,7 +727,7 @@ pub mod pallet {
/// It is only callable by the root account.
/// The extrinsic will call the Subtensor pallet to set the maximum allowed validators.
#[pallet::call_index(25)]
#[pallet::weight(Weight::from_parts(19_710_000, 0)
#[pallet::weight(Weight::from_parts(23_860_000, 0)
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(2_u64))
.saturating_add(<T as frame_system::Config>::DbWeight::get().writes(1_u64)))]
pub fn sudo_set_max_allowed_validators(
Expand Down Expand Up @@ -1658,7 +1658,13 @@ pub mod pallet {

/// Sets the commit-reveal weights version for all subnets
#[pallet::call_index(71)]
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
#[pallet::weight((
Weight::from_parts(6_171_000, 0)
.saturating_add(<T as frame_system::Config>::DbWeight::get().writes(1))
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(0_u64)),
DispatchClass::Operational,
Pays::No
))]
pub fn sudo_set_commit_reveal_version(
origin: OriginFor<T>,
version: u16,
Expand Down
9 changes: 4 additions & 5 deletions pallets/drand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,11 @@ pub mod pallet {
}
}
fn on_runtime_upgrade() -> frame_support::weights::Weight {
/* let weight = */
frame_support::weights::Weight::from_parts(0, 0) /*;*/
let mut weight = frame_support::weights::Weight::from_parts(0, 0);

//weight = weight.saturating_add(migrations::migrate_prune_old_pulses::<T>());
weight = weight.saturating_add(migrations::migrate_set_oldest_round::<T>());

//weight
weight
}
}

Expand Down Expand Up @@ -676,7 +675,7 @@ impl<T: Config> Pallet<T> {
}

let mut removed: u64 = 0;
while last_stored_round.saturating_sub(oldest) + 1 > MAX_KEPT_PULSES
while last_stored_round.saturating_sub(oldest).saturating_add(1) > MAX_KEPT_PULSES
&& removed < MAX_REMOVED_PULSES
{
Pulses::<T>::remove(oldest);
Expand Down
2 changes: 1 addition & 1 deletion pallets/drand/src/migrations/migrate_prune_old_pulses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn migrate_prune_old_pulses<T: Config>() -> Weight {

let mut new_oldest = rounds[0];
if num_pulses > MAX_KEPT_PULSES {
let num_to_delete = num_pulses - MAX_KEPT_PULSES;
let num_to_delete = num_pulses.saturating_sub(MAX_KEPT_PULSES);
new_oldest = rounds[num_to_delete as usize];

for &round in &rounds[0..num_to_delete as usize] {
Expand Down
57 changes: 57 additions & 0 deletions pallets/drand/src/migrations/migrate_set_oldest_round.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use crate::*;
use frame_support::weights::Weight;
use log;

/// Migration to set `OldestStoredRound` to the oldest round in storage.
pub fn migrate_set_oldest_round<T: Config>() -> Weight {
use frame_support::traits::Get;

let migration_name = BoundedVec::truncate_from(b"migrate_set_oldest_round".to_vec());

// Start with one read for HasMigrationRun
let mut weight = T::DbWeight::get().reads(1);

// Skip if already run.
if HasMigrationRun::<T>::get(&migration_name) {
log::info!(
"Migration '{}' has already run. Skipping.",
String::from_utf8_lossy(&migration_name)
);
return weight;
}
log::info!(
"Running migration '{}'",
String::from_utf8_lossy(&migration_name)
);

// Single-pass over keys: track min and how many keys we read.
let mut reads: u64 = 0;
let mut min_round: Option<RoundNumber> = None;

for r in Pulses::<T>::iter_keys() {
reads = reads.saturating_add(1);
if min_round.is_none_or(|m| r < m) {
min_round = Some(r);
}
}

// Account for all key reads
weight = weight.saturating_add(T::DbWeight::get().reads(reads));

let oldest = min_round.unwrap_or(0u64);
OldestStoredRound::<T>::put(oldest);
weight = weight.saturating_add(T::DbWeight::get().writes(1));

// Mark as completed.
HasMigrationRun::<T>::insert(&migration_name, true);
weight = weight.saturating_add(T::DbWeight::get().writes(1));

log::info!(
"Migration '{}' completed. OldestStoredRound set to {} (scanned {} rounds).",
String::from_utf8_lossy(&migration_name),
oldest,
reads
);

weight
}
2 changes: 2 additions & 0 deletions pallets/drand/src/migrations/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
pub mod migrate_prune_old_pulses;
pub use migrate_prune_old_pulses::*;
pub mod migrate_set_oldest_round;
pub use migrate_set_oldest_round::*;
40 changes: 39 additions & 1 deletion pallets/drand/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
use crate::{
BeaconConfig, BeaconConfigurationPayload, BeaconInfoResponse, Call, DrandResponseBody,
ENDPOINTS, Error, HasMigrationRun, LastStoredRound, MAX_KEPT_PULSES, OldestStoredRound, Pulse,
Pulses, PulsesPayload, QUICKNET_CHAIN_HASH, migrations::migrate_prune_old_pulses, mock::*,
Pulses, PulsesPayload, QUICKNET_CHAIN_HASH, migrations::migrate_prune_old_pulses,
migrations::migrate_set_oldest_round, mock::*,
};
use codec::Encode;
use frame_support::{
Expand Down Expand Up @@ -705,3 +706,40 @@ fn test_prune_maximum_of_100_pulses_per_call() {
);
});
}

#[test]
fn test_migrate_set_oldest_round() {
new_test_ext().execute_with(|| {
let migration_name = BoundedVec::truncate_from(b"migrate_set_oldest_round".to_vec());
let db_weight: RuntimeDbWeight = <Test as frame_system::Config>::DbWeight::get();
let pulse = Pulse::default();

assert_eq!(Pulses::<Test>::iter().count(), 0);
assert!(!HasMigrationRun::<Test>::get(&migration_name));
assert_eq!(OldestStoredRound::<Test>::get(), 0);
assert_eq!(LastStoredRound::<Test>::get(), 0);

// Insert out-of-order rounds: oldest should be 5
for r in [10u64, 7, 5].into_iter() {
Pulses::<Test>::insert(r, pulse.clone());
}
let num_rounds = 3u64;

// Run migration
let weight = migrate_set_oldest_round::<Test>();

assert_eq!(OldestStoredRound::<Test>::get(), 5);
// Migration does NOT touch LastStoredRound
assert_eq!(LastStoredRound::<Test>::get(), 0);
// Pulses untouched
assert!(Pulses::<Test>::contains_key(5));
assert!(Pulses::<Test>::contains_key(7));
assert!(Pulses::<Test>::contains_key(10));
// Flag set
assert!(HasMigrationRun::<Test>::get(&migration_name));

// Weight: reads(1 + num_rounds) + writes(2) [Oldest + HasMigrationRun]
let expected = db_weight.reads(1 + num_rounds) + db_weight.writes(2);
assert_eq!(weight, expected);
});
}
30 changes: 18 additions & 12 deletions pallets/subtensor/src/coinbase/reveal_commits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl<T: Config> Pallet<T> {

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

Expand All @@ -69,7 +69,7 @@ impl<T: Config> Pallet<T> {
Some(p) => p,
None => {
// Round number used was not found on the chain. Skip this commit.
log::warn!(
log::trace!(
"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."
);
unrevealed.push_back((
Expand All @@ -86,7 +86,7 @@ impl<T: Config> Pallet<T> {
let commit = match TLECiphertext::<TinyBLS381>::deserialize_compressed(reader) {
Ok(c) => c,
Err(e) => {
log::warn!(
log::trace!(
"Failed to reveal commit for subnet {netuid} submitted by {who:?} due to error deserializing the commit: {e:?}"
);
continue;
Expand All @@ -104,7 +104,7 @@ impl<T: Config> Pallet<T> {
) {
Ok(s) => s,
Err(e) => {
log::warn!(
log::trace!(
"Failed to reveal commit for subnet {netuid} submitted by {who:?} due to error deserializing signature from drand pallet: {e:?}"
);
continue;
Expand All @@ -116,7 +116,7 @@ impl<T: Config> Pallet<T> {
) {
Ok(d) => d,
Err(e) => {
log::warn!(
log::trace!(
"Failed to reveal commit for subnet {netuid} submitted by {who:?} due to error decrypting the commit: {e:?}"
);
continue;
Expand All @@ -136,16 +136,22 @@ impl<T: Config> Pallet<T> {
(payload.uids, payload.values, payload.version_key)
}
Ok(_) => {
log::warn!(
log::trace!(
"Failed to reveal commit for subnet {netuid} submitted by {who:?} due to hotkey mismatch in payload"
);
continue;
}
Err(e) => {
log::warn!(
"Failed to reveal commit for subnet {netuid} submitted by {who:?} due to error deserializing hotkey: {e:?}"
);
continue;
let mut reader_legacy = &decrypted_bytes[..];
match LegacyWeightsTlockPayload::decode(&mut reader_legacy) {
Ok(legacy) => (legacy.uids, legacy.values, legacy.version_key),
Err(_) => {
log::trace!(
"Failed to reveal commit for subnet {netuid} submitted by {who:?} due to error deserializing hotkey: {e:?}"
);
continue;
}
}
}
}
} else {
Expand All @@ -154,7 +160,7 @@ impl<T: Config> Pallet<T> {
match LegacyWeightsTlockPayload::decode(&mut reader_legacy) {
Ok(legacy) => (legacy.uids, legacy.values, legacy.version_key),
Err(e) => {
log::warn!(
log::trace!(
"Failed to reveal commit for subnet {netuid} submitted by {who:?} due to error deserializing both payload formats: {e:?}"
);
continue;
Expand All @@ -173,7 +179,7 @@ impl<T: Config> Pallet<T> {
values,
version_key,
) {
log::warn!(
log::trace!(
"Failed to `do_set_weights` for subnet {netuid} submitted by {who:?}: {e:?}"
);
continue;
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// `spec_version`, and `authoring_version` are the same between Wasm and native.
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
// the compatible custom types.
spec_version: 299,
spec_version: 300,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down
Loading