Skip to content

Commit 17b8674

Browse files
committed
Merge branch 'devnet-ready' into hybrid-node
2 parents c5124b2 + b6e8d80 commit 17b8674

File tree

21 files changed

+1619
-502
lines changed

21 files changed

+1619
-502
lines changed

pallets/admin-utils/src/benchmarking.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,5 +335,16 @@ mod benchmarks {
335335
_(RawOrigin::Root, 1u16.into()/*netuid*/, true/*enabled*/)/*set_commit_reveal_weights_enabled*/;
336336
}
337337

338+
#[benchmark]
339+
fn sudo_set_commit_reveal_version() {
340+
pallet_subtensor::Pallet::<T>::init_new_network(
341+
1u16.into(), /*netuid*/
342+
1u16, /*sudo_tempo*/
343+
);
344+
345+
#[extrinsic_call]
346+
_(RawOrigin::Root, 5u16/*version*/)/*sudo_set_commit_reveal_version()*/;
347+
}
348+
338349
//impl_benchmark_test_suite!(AdminUtils, crate::mock::new_test_ext(), crate::mock::Test);
339350
}

pallets/admin-utils/src/lib.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ pub mod pallet {
405405
/// It is only callable by the root account or subnet owner.
406406
/// The extrinsic will call the Subtensor pallet to set the adjustment beta.
407407
#[pallet::call_index(12)]
408-
#[pallet::weight(Weight::from_parts(19_240_000, 0)
408+
#[pallet::weight(Weight::from_parts(14_850_000, 0)
409409
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(1_u64))
410410
.saturating_add(<T as frame_system::Config>::DbWeight::get().writes(1_u64)))]
411411
pub fn sudo_set_max_weight_limit(
@@ -480,7 +480,7 @@ pub mod pallet {
480480
/// It is only callable by the root account.
481481
/// The extrinsic will call the Subtensor pallet to set the maximum allowed UIDs for a subnet.
482482
#[pallet::call_index(15)]
483-
#[pallet::weight(Weight::from_parts(23_820_000, 0)
483+
#[pallet::weight(Weight::from_parts(18_770_000, 0)
484484
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(2_u64))
485485
.saturating_add(<T as frame_system::Config>::DbWeight::get().writes(1_u64)))]
486486
pub fn sudo_set_max_allowed_uids(
@@ -578,7 +578,7 @@ pub mod pallet {
578578
/// The extrinsic will call the Subtensor pallet to set the network registration allowed.
579579
#[pallet::call_index(19)]
580580
#[pallet::weight((
581-
Weight::from_parts(8_696_000, 0)
581+
Weight::from_parts(6_722_000, 0)
582582
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(0))
583583
.saturating_add(<T as frame_system::Config>::DbWeight::get().writes(1)),
584584
DispatchClass::Operational,
@@ -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(
@@ -1102,7 +1102,7 @@ pub mod pallet {
11021102
/// It is only callable by the root account or subnet owner.
11031103
/// The extrinsic will call the Subtensor pallet to set the value.
11041104
#[pallet::call_index(49)]
1105-
#[pallet::weight(Weight::from_parts(19_480_000, 0)
1105+
#[pallet::weight(Weight::from_parts(14_780_000, 0)
11061106
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(1_u64))
11071107
.saturating_add(<T as frame_system::Config>::DbWeight::get().writes(1_u64)))]
11081108
pub fn sudo_set_commit_reveal_weights_enabled(
@@ -1655,6 +1655,24 @@ pub mod pallet {
16551655
);
16561656
Ok(())
16571657
}
1658+
1659+
/// Sets the commit-reveal weights version for all subnets
1660+
#[pallet::call_index(71)]
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+
))]
1668+
pub fn sudo_set_commit_reveal_version(
1669+
origin: OriginFor<T>,
1670+
version: u16,
1671+
) -> DispatchResult {
1672+
ensure_root(origin)?;
1673+
pallet_subtensor::Pallet::<T>::set_commit_reveal_weights_version(version);
1674+
Ok(())
1675+
}
16581676
}
16591677
}
16601678

pallets/admin-utils/src/tests/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1930,3 +1930,24 @@ fn test_sudo_set_yuma3_enabled() {
19301930
assert_eq!(SubtensorModule::get_yuma3_enabled(netuid), !to_be_set);
19311931
});
19321932
}
1933+
1934+
#[test]
1935+
fn test_sudo_set_commit_reveal_version() {
1936+
new_test_ext().execute_with(|| {
1937+
add_network(NetUid::from(1), 10);
1938+
1939+
let to_be_set: u16 = 5;
1940+
let init_value: u16 = SubtensorModule::get_commit_reveal_weights_version();
1941+
1942+
assert_ok!(AdminUtils::sudo_set_commit_reveal_version(
1943+
<<Test as Config>::RuntimeOrigin>::root(),
1944+
to_be_set
1945+
));
1946+
1947+
assert!(init_value != to_be_set);
1948+
assert_eq!(
1949+
SubtensorModule::get_commit_reveal_weights_version(),
1950+
to_be_set
1951+
);
1952+
});
1953+
}

pallets/drand/src/lib.rs

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ use sp_runtime::{
5858
};
5959

6060
pub mod bls12_381;
61+
pub mod migrations;
6162
pub mod types;
6263
pub mod utils;
6364
pub mod verifier;
@@ -91,6 +92,8 @@ pub const QUICKNET_CHAIN_HASH: &str =
9192
const CHAIN_HASH: &str = QUICKNET_CHAIN_HASH;
9293

9394
pub const MAX_PULSES_TO_FETCH: u64 = 50;
95+
pub const MAX_KEPT_PULSES: u64 = 216_000; // 1 week
96+
pub const MAX_REMOVED_PULSES: u64 = 100;
9497

9598
/// Defines application identifier for crypto keys of this module.
9699
///
@@ -212,12 +215,24 @@ pub mod pallet {
212215
}
213216
}
214217

218+
/// Define a maximum length for the migration key
219+
type MigrationKeyMaxLen = ConstU32<128>;
220+
221+
/// Storage for migration run status
222+
#[pallet::storage]
223+
pub type HasMigrationRun<T: Config> =
224+
StorageMap<_, Identity, BoundedVec<u8, MigrationKeyMaxLen>, bool, ValueQuery>;
225+
215226
/// map round number to pulse
216227
#[pallet::storage]
217228
pub type Pulses<T: Config> = StorageMap<_, Blake2_128Concat, RoundNumber, Pulse, OptionQuery>;
218229

219230
#[pallet::storage]
220-
pub(super) type LastStoredRound<T: Config> = StorageValue<_, RoundNumber, ValueQuery>;
231+
pub type LastStoredRound<T: Config> = StorageValue<_, RoundNumber, ValueQuery>;
232+
233+
/// oldest stored round
234+
#[pallet::storage]
235+
pub type OldestStoredRound<T: Config> = StorageValue<_, RoundNumber, ValueQuery>;
221236

222237
/// Defines the block when next unsigned transaction will be accepted.
223238
///
@@ -261,6 +276,13 @@ pub mod pallet {
261276
log::debug!("Drand: Failed to fetch pulse from drand. {e:?}");
262277
}
263278
}
279+
fn on_runtime_upgrade() -> frame_support::weights::Weight {
280+
let mut weight = frame_support::weights::Weight::from_parts(0, 0);
281+
282+
weight = weight.saturating_add(migrations::migrate_set_oldest_round::<T>());
283+
284+
weight
285+
}
264286
}
265287

266288
#[pallet::validate_unsigned]
@@ -308,8 +330,8 @@ pub mod pallet {
308330
/// Verify and write a pulse from the beacon into the runtime
309331
#[pallet::call_index(0)]
310332
#[pallet::weight(Weight::from_parts(5_708_000_000, 0)
311-
.saturating_add(T::DbWeight::get().reads(2_u64))
312-
.saturating_add(T::DbWeight::get().writes(3_u64)))]
333+
.saturating_add(T::DbWeight::get().reads(3_u64))
334+
.saturating_add(T::DbWeight::get().writes(4_u64)))]
313335
pub fn write_pulse(
314336
origin: OriginFor<T>,
315337
pulses_payload: PulsesPayload<T::Public, BlockNumberFor<T>>,
@@ -321,6 +343,10 @@ pub mod pallet {
321343
let mut last_stored_round = LastStoredRound::<T>::get();
322344
let mut new_rounds = Vec::new();
323345

346+
let oldest_stored_round = OldestStoredRound::<T>::get();
347+
let is_first_storage = last_stored_round == 0 && oldest_stored_round == 0;
348+
let mut first_new_round: Option<RoundNumber> = None;
349+
324350
for pulse in &pulses_payload.pulses {
325351
let is_verified = T::Verifier::verify(config.clone(), pulse.clone())
326352
.map_err(|_| Error::<T>::PulseVerificationError)?;
@@ -339,12 +365,25 @@ pub mod pallet {
339365

340366
// Collect the new round
341367
new_rounds.push(pulse.round);
368+
369+
// Set the first new round if this is the initial storage
370+
if is_first_storage && first_new_round.is_none() {
371+
first_new_round = Some(pulse.round);
372+
}
342373
}
343374
}
344375

345376
// Update LastStoredRound storage
346377
LastStoredRound::<T>::put(last_stored_round);
347378

379+
// Set OldestStoredRound if this was the first storage
380+
if let Some(first_round) = first_new_round {
381+
OldestStoredRound::<T>::put(first_round);
382+
}
383+
384+
// Prune old pulses
385+
Self::prune_old_pulses(last_stored_round);
386+
348387
// Update the next unsigned block number
349388
let current_block = frame_system::Pallet::<T>::block_number();
350389
<NextUnsignedAt<T>>::put(current_block);
@@ -628,6 +667,24 @@ impl<T: Config> Pallet<T> {
628667
.propagate(true)
629668
.build()
630669
}
670+
671+
fn prune_old_pulses(last_stored_round: RoundNumber) {
672+
let mut oldest = OldestStoredRound::<T>::get();
673+
if oldest == 0 {
674+
return;
675+
}
676+
677+
let mut removed: u64 = 0;
678+
while last_stored_round.saturating_sub(oldest).saturating_add(1) > MAX_KEPT_PULSES
679+
&& removed < MAX_REMOVED_PULSES
680+
{
681+
Pulses::<T>::remove(oldest);
682+
oldest = oldest.saturating_add(1);
683+
removed = removed.saturating_add(1);
684+
}
685+
686+
OldestStoredRound::<T>::put(oldest);
687+
}
631688
}
632689

633690
/// construct a message (e.g. signed by drand)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use crate::*;
2+
use frame_support::{traits::Get, weights::Weight};
3+
use log;
4+
5+
pub fn migrate_prune_old_pulses<T: Config>() -> Weight {
6+
let migration_name = BoundedVec::truncate_from(b"migrate_prune_old_pulses".to_vec());
7+
8+
// Initialize the weight with one read operation.
9+
let mut weight = T::DbWeight::get().reads(1);
10+
11+
// Check if the migration has already run
12+
if HasMigrationRun::<T>::get(&migration_name) {
13+
log::info!(
14+
"Migration '{:?}' has already run. Skipping.",
15+
String::from_utf8_lossy(&migration_name)
16+
);
17+
return weight;
18+
}
19+
log::info!(
20+
"Running migration '{}'",
21+
String::from_utf8_lossy(&migration_name)
22+
);
23+
24+
// Collect all round numbers
25+
let mut rounds: Vec<RoundNumber> = Pulses::<T>::iter_keys().collect();
26+
weight = weight.saturating_add(T::DbWeight::get().reads(rounds.len() as u64));
27+
28+
if rounds.is_empty() {
29+
OldestStoredRound::<T>::put(0u64);
30+
LastStoredRound::<T>::put(0u64);
31+
weight = weight.saturating_add(T::DbWeight::get().writes(2));
32+
} else {
33+
rounds.sort();
34+
let num_pulses = rounds.len() as u64;
35+
36+
let mut new_oldest = rounds[0];
37+
if num_pulses > MAX_KEPT_PULSES {
38+
let num_to_delete = num_pulses.saturating_sub(MAX_KEPT_PULSES);
39+
new_oldest = rounds[num_to_delete as usize];
40+
41+
for &round in &rounds[0..num_to_delete as usize] {
42+
Pulses::<T>::remove(round);
43+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
44+
}
45+
}
46+
47+
OldestStoredRound::<T>::put(new_oldest);
48+
LastStoredRound::<T>::put(*rounds.last().unwrap());
49+
weight = weight.saturating_add(T::DbWeight::get().writes(2));
50+
}
51+
52+
// Mark the migration as completed
53+
HasMigrationRun::<T>::insert(&migration_name, true);
54+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
55+
56+
log::info!(
57+
"Migration '{:?}' completed.",
58+
String::from_utf8_lossy(&migration_name)
59+
);
60+
61+
// Return the migration weight.
62+
weight
63+
}
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pub mod migrate_prune_old_pulses;
2+
pub use migrate_prune_old_pulses::*;
3+
pub mod migrate_set_oldest_round;
4+
pub use migrate_set_oldest_round::*;

0 commit comments

Comments
 (0)