diff --git a/pallets/admin-utils/src/lib.rs b/pallets/admin-utils/src/lib.rs index 3784b7793d..9243a01059 100644 --- a/pallets/admin-utils/src/lib.rs +++ b/pallets/admin-utils/src/lib.rs @@ -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(::DbWeight::get().reads(1_u64)) .saturating_add(::DbWeight::get().writes(1_u64)))] pub fn sudo_set_difficulty( @@ -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(::DbWeight::get().reads(2_u64)) .saturating_add(::DbWeight::get().writes(1_u64)))] pub fn sudo_set_max_allowed_validators( @@ -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(::DbWeight::get().writes(1)) + .saturating_add(::DbWeight::get().reads(0_u64)), + DispatchClass::Operational, + Pays::No + ))] pub fn sudo_set_commit_reveal_version( origin: OriginFor, version: u16, diff --git a/pallets/drand/src/lib.rs b/pallets/drand/src/lib.rs index 0dfb708e44..2e780f8b61 100644 --- a/pallets/drand/src/lib.rs +++ b/pallets/drand/src/lib.rs @@ -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::()); + weight = weight.saturating_add(migrations::migrate_set_oldest_round::()); - //weight + weight } } @@ -676,7 +675,7 @@ impl Pallet { } 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::::remove(oldest); diff --git a/pallets/drand/src/migrations/migrate_prune_old_pulses.rs b/pallets/drand/src/migrations/migrate_prune_old_pulses.rs index 0bdfaeb159..0a6697f6fd 100644 --- a/pallets/drand/src/migrations/migrate_prune_old_pulses.rs +++ b/pallets/drand/src/migrations/migrate_prune_old_pulses.rs @@ -35,7 +35,7 @@ pub fn migrate_prune_old_pulses() -> 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] { diff --git a/pallets/drand/src/migrations/migrate_set_oldest_round.rs b/pallets/drand/src/migrations/migrate_set_oldest_round.rs new file mode 100644 index 0000000000..c1ef6b9e04 --- /dev/null +++ b/pallets/drand/src/migrations/migrate_set_oldest_round.rs @@ -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() -> 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::::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 = None; + + for r in Pulses::::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::::put(oldest); + weight = weight.saturating_add(T::DbWeight::get().writes(1)); + + // Mark as completed. + HasMigrationRun::::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 +} diff --git a/pallets/drand/src/migrations/mod.rs b/pallets/drand/src/migrations/mod.rs index 6853d46b7a..7518996fc9 100644 --- a/pallets/drand/src/migrations/mod.rs +++ b/pallets/drand/src/migrations/mod.rs @@ -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::*; diff --git a/pallets/drand/src/tests.rs b/pallets/drand/src/tests.rs index 405c9b8339..fdc450b5e2 100644 --- a/pallets/drand/src/tests.rs +++ b/pallets/drand/src/tests.rs @@ -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::{ @@ -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 = ::DbWeight::get(); + let pulse = Pulse::default(); + + assert_eq!(Pulses::::iter().count(), 0); + assert!(!HasMigrationRun::::get(&migration_name)); + assert_eq!(OldestStoredRound::::get(), 0); + assert_eq!(LastStoredRound::::get(), 0); + + // Insert out-of-order rounds: oldest should be 5 + for r in [10u64, 7, 5].into_iter() { + Pulses::::insert(r, pulse.clone()); + } + let num_rounds = 3u64; + + // Run migration + let weight = migrate_set_oldest_round::(); + + assert_eq!(OldestStoredRound::::get(), 5); + // Migration does NOT touch LastStoredRound + assert_eq!(LastStoredRound::::get(), 0); + // Pulses untouched + assert!(Pulses::::contains_key(5)); + assert!(Pulses::::contains_key(7)); + assert!(Pulses::::contains_key(10)); + // Flag set + assert!(HasMigrationRun::::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); + }); +} diff --git a/pallets/subtensor/src/coinbase/reveal_commits.rs b/pallets/subtensor/src/coinbase/reveal_commits.rs index e4b2ed3f43..029b77227c 100644 --- a/pallets/subtensor/src/coinbase/reveal_commits.rs +++ b/pallets/subtensor/src/coinbase/reveal_commits.rs @@ -53,7 +53,7 @@ impl Pallet { // 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(()); } @@ -69,7 +69,7 @@ impl Pallet { 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(( @@ -86,7 +86,7 @@ impl Pallet { let commit = match TLECiphertext::::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; @@ -104,7 +104,7 @@ impl Pallet { ) { 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; @@ -116,7 +116,7 @@ impl Pallet { ) { 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; @@ -136,16 +136,22 @@ impl Pallet { (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 { @@ -154,7 +160,7 @@ impl Pallet { 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; @@ -173,7 +179,7 @@ impl Pallet { values, version_key, ) { - log::warn!( + log::trace!( "Failed to `do_set_weights` for subnet {netuid} submitted by {who:?}: {e:?}" ); continue; diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index db35faf7e0..64f48294e4 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -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,