Skip to content

Commit 86b7f6d

Browse files
Merge pull request opentensor#432 from opentensor/sudo-calls-commit-reveal
add sudo calls & tests
2 parents 8b91ed4 + cac9b44 commit 86b7f6d

File tree

15 files changed

+717
-59
lines changed

15 files changed

+717
-59
lines changed

CITATION.cft

Whitespace-only changes.

pallets/admin-utils/src/benchmarking.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,5 +223,21 @@ mod benchmarks {
223223
_(RawOrigin::Root, 1u16/*netuid*/, 1u16/*tempo*/)/*sudo_set_tempo*/;
224224
}
225225

226+
#[benchmark]
227+
fn sudo_set_commit_reveal_weights_interval() {
228+
T::Subtensor::init_new_network(1u16 /*netuid*/, 1u16 /*sudo_tempo*/);
229+
230+
#[extrinsic_call]
231+
_(RawOrigin::Root, 1u16/*netuid*/, 3u64/*interval*/)/*set_commit_reveal_weights_interval()*/;
232+
}
233+
234+
#[benchmark]
235+
fn sudo_set_commit_reveal_weights_enabled() {
236+
T::Subtensor::init_new_network(1u16 /*netuid*/, 1u16 /*sudo_tempo*/);
237+
238+
#[extrinsic_call]
239+
_(RawOrigin::Root, 1u16/*netuid*/, true/*enabled*/)/*set_commit_reveal_weights_enabled*/;
240+
}
241+
226242
//impl_benchmark_test_suite!(AdminUtils, crate::mock::new_test_ext(), crate::mock::Test);
227243
}

pallets/admin-utils/src/lib.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,54 @@ pub mod pallet {
949949
);
950950
Ok(())
951951
}
952+
953+
/// The extrinsic sets the commit/reveal interval for a subnet.
954+
/// It is only callable by the root account or subnet owner.
955+
/// The extrinsic will call the Subtensor pallet to set the interval.
956+
#[pallet::call_index(48)]
957+
#[pallet::weight(T::WeightInfo::sudo_set_commit_reveal_weights_interval())]
958+
pub fn sudo_set_commit_reveal_weights_interval(
959+
origin: OriginFor<T>,
960+
netuid: u16,
961+
interval: u64,
962+
) -> DispatchResult {
963+
T::Subtensor::ensure_subnet_owner_or_root(origin, netuid)?;
964+
965+
ensure!(
966+
T::Subtensor::if_subnet_exist(netuid),
967+
Error::<T>::SubnetDoesNotExist
968+
);
969+
970+
T::Subtensor::set_commit_reveal_weights_interval(netuid, interval);
971+
log::info!(
972+
"SetWeightCommitInterval( netuid: {:?}, interval: {:?} ) ",
973+
netuid,
974+
interval
975+
);
976+
Ok(())
977+
}
978+
979+
/// The extrinsic enabled/disables commit/reaveal for a given subnet.
980+
/// It is only callable by the root account or subnet owner.
981+
/// The extrinsic will call the Subtensor pallet to set the value.
982+
#[pallet::call_index(49)]
983+
#[pallet::weight(T::WeightInfo::sudo_set_commit_reveal_weights_enabled())]
984+
pub fn sudo_set_commit_reveal_weights_enabled(
985+
origin: OriginFor<T>,
986+
netuid: u16,
987+
enabled: bool,
988+
) -> DispatchResult {
989+
T::Subtensor::ensure_subnet_owner_or_root(origin, netuid)?;
990+
991+
ensure!(
992+
T::Subtensor::if_subnet_exist(netuid),
993+
Error::<T>::SubnetDoesNotExist
994+
);
995+
996+
T::Subtensor::set_commit_reveal_weights_enabled(netuid, enabled);
997+
log::info!("ToggleSetWeightsCommitReveal( netuid: {:?} ) ", netuid);
998+
Ok(())
999+
}
9521000
}
9531001
}
9541002

@@ -1042,4 +1090,6 @@ pub trait SubtensorInterface<AccountId, Balance, RuntimeOrigin> {
10421090
fn set_nominator_min_required_stake(min_stake: u64);
10431091
fn clear_small_nominations();
10441092
fn set_target_stakes_per_interval(target_stakes_per_interval: u64);
1093+
fn set_commit_reveal_weights_interval(netuid: u16, interval: u64);
1094+
fn set_commit_reveal_weights_enabled(netuid: u16, enabled: bool);
10451095
}

pallets/admin-utils/src/weights.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ pub trait WeightInfo {
6060
fn sudo_set_min_burn() -> Weight;
6161
fn sudo_set_network_registration_allowed() -> Weight;
6262
fn sudo_set_tempo() -> Weight;
63+
fn sudo_set_commit_reveal_weights_interval() -> Weight;
64+
fn sudo_set_commit_reveal_weights_enabled() -> Weight;
65+
6366
}
6467

6568
/// Weights for `pallet_admin_utils` using the Substrate node and recommended hardware.
@@ -411,6 +414,24 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
411414
.saturating_add(T::DbWeight::get().reads(1_u64))
412415
.saturating_add(T::DbWeight::get().writes(1_u64))
413416
}
417+
fn sudo_set_commit_reveal_weights_interval() -> Weight {
418+
// Proof Size summary in bytes:
419+
// Measured: `1111`
420+
// Estimated: `4697`
421+
// Minimum execution time: 46_450_000 picoseconds.
422+
Weight::from_parts(47_279_000, 4697)
423+
.saturating_add(T::DbWeight::get().reads(1_u64))
424+
.saturating_add(T::DbWeight::get().writes(1_u64))
425+
}
426+
fn sudo_set_commit_reveal_weights_enabled() -> Weight {
427+
// Proof Size summary in bytes:
428+
// Measured: `1111`
429+
// Estimated: `4697`
430+
// Minimum execution time: 46_450_000 picoseconds.
431+
Weight::from_parts(47_279_000, 4697)
432+
.saturating_add(T::DbWeight::get().reads(1_u64))
433+
.saturating_add(T::DbWeight::get().writes(1_u64))
434+
}
414435
}
415436

416437
// For backwards compatibility and tests.
@@ -761,4 +782,28 @@ impl WeightInfo for () {
761782
.saturating_add(RocksDbWeight::get().reads(1_u64))
762783
.saturating_add(RocksDbWeight::get().writes(1_u64))
763784
}
785+
fn sudo_set_commit_reveal_weights_interval() -> Weight {
786+
// -- Extrinsic Time --
787+
// Model:
788+
// Time ~= 20.42
789+
// µs
790+
// Reads = 1
791+
// Writes = 1
792+
// Recorded proof Size = 456
793+
Weight::from_parts(20_420_000, 456)
794+
.saturating_add(RocksDbWeight::get().reads(1_u64))
795+
.saturating_add(RocksDbWeight::get().writes(1_u64))
796+
}
797+
fn sudo_set_commit_reveal_weights_enabled() -> Weight {
798+
// -- Extrinsic Time --
799+
// Model:
800+
// Time ~= 19.78
801+
// µs
802+
// Reads = 1
803+
// Writes = 1
804+
// Recorded proof Size = 456
805+
Weight::from_parts(19_780_000, 456)
806+
.saturating_add(RocksDbWeight::get().reads(1_u64))
807+
.saturating_add(RocksDbWeight::get().writes(1_u64))
808+
}
764809
}

pallets/admin-utils/tests/mock.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,14 @@ impl pallet_admin_utils::SubtensorInterface<AccountId, Balance, RuntimeOrigin> f
454454
fn set_target_stakes_per_interval(target_stakes_per_interval: u64) {
455455
SubtensorModule::set_target_stakes_per_interval(target_stakes_per_interval);
456456
}
457+
458+
fn set_commit_reveal_weights_interval(netuid: u16, interval: u64) {
459+
SubtensorModule::set_commit_reveal_weights_interval(netuid, interval);
460+
}
461+
462+
fn set_commit_reveal_weights_enabled(netuid: u16, enabled: bool) {
463+
SubtensorModule::set_commit_reveal_weights_enabled(netuid, enabled);
464+
}
457465
}
458466

459467
impl pallet_admin_utils::Config for Test {

pallets/admin-utils/tests/tests.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,52 @@ fn test_sudo_set_min_delegate_take() {
11091109
});
11101110
}
11111111

1112+
#[test]
1113+
fn test_sudo_set_weight_commit_interval() {
1114+
new_test_ext().execute_with(|| {
1115+
let netuid: u16 = 1;
1116+
add_network(netuid, 10);
1117+
1118+
let to_be_set = 55;
1119+
let init_value = SubtensorModule::get_commit_reveal_weights_interval(netuid);
1120+
1121+
assert_ok!(AdminUtils::sudo_set_commit_reveal_weights_interval(
1122+
<<Test as Config>::RuntimeOrigin>::root(),
1123+
netuid,
1124+
to_be_set
1125+
));
1126+
1127+
assert!(init_value != to_be_set);
1128+
assert_eq!(
1129+
SubtensorModule::get_commit_reveal_weights_interval(netuid),
1130+
to_be_set
1131+
);
1132+
});
1133+
}
1134+
1135+
#[test]
1136+
fn test_sudo_set_commit_reveal_weights_enabled() {
1137+
new_test_ext().execute_with(|| {
1138+
let netuid: u16 = 1;
1139+
add_network(netuid, 10);
1140+
1141+
let to_be_set: bool = true;
1142+
let init_value: bool = SubtensorModule::get_commit_reveal_weights_enabled(netuid);
1143+
1144+
assert_ok!(AdminUtils::sudo_set_commit_reveal_weights_enabled(
1145+
<<Test as Config>::RuntimeOrigin>::root(),
1146+
netuid,
1147+
to_be_set
1148+
));
1149+
1150+
assert!(init_value != to_be_set);
1151+
assert_eq!(
1152+
SubtensorModule::get_commit_reveal_weights_enabled(netuid),
1153+
to_be_set
1154+
);
1155+
});
1156+
}
1157+
11121158
#[test]
11131159
fn test_sudo_set_target_stakes_per_interval() {
11141160
new_test_ext().execute_with(|| {

pallets/subtensor/src/benchmarks.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ reveal_weights {
388388
let version_key: u64 = 0;
389389
let uids: Vec<u16> = vec![0];
390390
let weight_values: Vec<u16> = vec![10];
391+
let salt: Vec<u16> = vec![8];
391392
let hotkey: T::AccountId = account("hot", 0, 1);
392393
let coldkey: T::AccountId = account("cold", 1, 2);
393394

@@ -414,16 +415,17 @@ reveal_weights {
414415
);
415416

416417
Subtensor::<T>::set_validator_permit_for_uid(netuid, 0, true);
417-
Subtensor::<T>::set_weight_commit_interval(0);
418+
Subtensor::<T>::set_commit_reveal_weights_interval(netuid, 0);
418419

419420
let commit_hash: H256 = BlakeTwo256::hash_of(&(
420421
hotkey.clone(),
421422
netuid,
422423
uids.clone(),
423424
weight_values.clone(),
425+
salt.clone(),
424426
version_key,
425427
));
426428
let _ = Subtensor::<T>::commit_weights(<T as frame_system::Config>::RuntimeOrigin::from(RawOrigin::Signed(hotkey.clone())), netuid, commit_hash);
427429

428-
}: reveal_weights(RawOrigin::Signed(hotkey.clone()), netuid, uids, weight_values, version_key)
430+
}: reveal_weights(RawOrigin::Signed(hotkey.clone()), netuid, uids, weight_values, salt, version_key)
429431
}

pallets/subtensor/src/errors.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,12 @@ mod errors {
119119
/// No commit found for the provided hotkey+netuid combination when attempting to reveal the weights.
120120
NoWeightsCommitFound,
121121
/// Not the correct block/range to reveal weights.
122-
InvalidRevealCommitHashNotMatchTempo,
122+
InvalidRevealCommitTempo,
123123
/// Committed hash does not equal the hashed reveal data.
124124
InvalidRevealCommitHashNotMatch,
125+
/// Attempting to call set_weights when commit/reveal is enabled
126+
CommitRevealEnabled,
127+
/// Attemtping to commit/reveal weights when disabled.
128+
CommitRevealDisabled,
125129
}
126130
}

pallets/subtensor/src/lib.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -934,10 +934,20 @@ pub mod pallet {
934934
pub fn DefaultWeightCommitRevealInterval<T: Config>() -> u64 {
935935
1000
936936
}
937-
937+
// --- DMAP ( netuid ) --> interval
938938
#[pallet::storage]
939939
pub type WeightCommitRevealInterval<T> =
940-
StorageValue<_, u64, ValueQuery, DefaultWeightCommitRevealInterval<T>>;
940+
StorageMap<_, Identity, u16, u64, ValueQuery, DefaultWeightCommitRevealInterval<T>>;
941+
942+
/// Default value for weight commit/reveal enabled.
943+
#[pallet::type_value]
944+
pub fn DefaultCommitRevealWeightsEnabled<T: Config>() -> bool {
945+
false
946+
}
947+
// --- DMAP ( netuid ) --> interval
948+
#[pallet::storage]
949+
pub type CommitRevealWeightsEnabled<T> =
950+
StorageMap<_, Identity, u16, bool, ValueQuery, DefaultCommitRevealWeightsEnabled<T>>;
941951

942952
/// =======================================
943953
/// ==== Subnetwork Consensus Storage ====
@@ -1354,7 +1364,11 @@ pub mod pallet {
13541364
weights: Vec<u16>,
13551365
version_key: u64,
13561366
) -> DispatchResult {
1357-
Self::do_set_weights(origin, netuid, dests, weights, version_key)
1367+
if !Self::get_commit_reveal_weights_enabled(netuid) {
1368+
return Self::do_set_weights(origin, netuid, dests, weights, version_key);
1369+
}
1370+
1371+
Err(Error::<T>::CommitRevealEnabled.into())
13581372
}
13591373

13601374
/// ---- Used to commit a hash of your weight values to later be revealed.
@@ -1400,6 +1414,9 @@ pub mod pallet {
14001414
/// * `values` (`Vec<u16>`):
14011415
/// - The values of the weights being revealed.
14021416
///
1417+
/// * `salt` (`Vec<u8>`):
1418+
/// - The random salt to protect from brute-force guessing attack in case of small weight changes bit-wise.
1419+
///
14031420
/// * `version_key` (`u64`):
14041421
/// - The network version key.
14051422
///
@@ -1422,9 +1439,10 @@ pub mod pallet {
14221439
netuid: u16,
14231440
uids: Vec<u16>,
14241441
values: Vec<u16>,
1442+
salt: Vec<u16>,
14251443
version_key: u64,
14261444
) -> DispatchResult {
1427-
Self::do_reveal_weights(origin, netuid, uids, values, version_key)
1445+
Self::do_reveal_weights(origin, netuid, uids, values, salt, version_key)
14281446
}
14291447

14301448
/// # Args:

pallets/subtensor/src/subnet_info.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ pub struct SubnetHyperparams {
5050
max_validators: Compact<u16>,
5151
adjustment_alpha: Compact<u64>,
5252
difficulty: Compact<u64>,
53+
commit_reveal_weights_interval: Compact<u64>,
54+
commit_reveal_weights_enabled: bool,
5355
}
5456

5557
impl<T: Config> Pallet<T> {
@@ -151,6 +153,8 @@ impl<T: Config> Pallet<T> {
151153
let max_validators = Self::get_max_allowed_validators(netuid);
152154
let adjustment_alpha = Self::get_adjustment_alpha(netuid);
153155
let difficulty = Self::get_difficulty_as_u64(netuid);
156+
let commit_reveal_weights_interval = Self::get_commit_reveal_weights_interval(netuid);
157+
let commit_reveal_weights_enabled = Self::get_commit_reveal_weights_enabled(netuid);
154158

155159
Some(SubnetHyperparams {
156160
rho: rho.into(),
@@ -175,6 +179,8 @@ impl<T: Config> Pallet<T> {
175179
max_validators: max_validators.into(),
176180
adjustment_alpha: adjustment_alpha.into(),
177181
difficulty: difficulty.into(),
182+
commit_reveal_weights_interval: commit_reveal_weights_interval.into(),
183+
commit_reveal_weights_enabled,
178184
})
179185
}
180186
}

pallets/subtensor/src/utils.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,19 @@ impl<T: Config> Pallet<T> {
480480
Self::deposit_event(Event::KappaSet(netuid, kappa));
481481
}
482482

483+
pub fn get_commit_reveal_weights_interval(netuid: u16) -> u64 {
484+
WeightCommitRevealInterval::<T>::get(netuid)
485+
}
486+
pub fn set_commit_reveal_weights_interval(netuid: u16, interval: u64) {
487+
WeightCommitRevealInterval::<T>::set(netuid, interval);
488+
}
489+
pub fn get_commit_reveal_weights_enabled(netuid: u16) -> bool {
490+
CommitRevealWeightsEnabled::<T>::get(netuid)
491+
}
492+
pub fn set_commit_reveal_weights_enabled(netuid: u16, enabled: bool) {
493+
CommitRevealWeightsEnabled::<T>::set(netuid, enabled);
494+
}
495+
483496
pub fn get_rho(netuid: u16) -> u16 {
484497
Rho::<T>::get(netuid)
485498
}

0 commit comments

Comments
 (0)