Skip to content

Commit

Permalink
Merge pull request #432 from opentensor/sudo-calls-commit-reveal
Browse files Browse the repository at this point in the history
add sudo calls & tests
  • Loading branch information
distributedstatemachine committed May 29, 2024
2 parents 8b91ed4 + cac9b44 commit 86b7f6d
Show file tree
Hide file tree
Showing 15 changed files with 717 additions and 59 deletions.
Empty file added CITATION.cft
Empty file.
16 changes: 16 additions & 0 deletions pallets/admin-utils/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,5 +223,21 @@ mod benchmarks {
_(RawOrigin::Root, 1u16/*netuid*/, 1u16/*tempo*/)/*sudo_set_tempo*/;
}

#[benchmark]
fn sudo_set_commit_reveal_weights_interval() {
T::Subtensor::init_new_network(1u16 /*netuid*/, 1u16 /*sudo_tempo*/);

#[extrinsic_call]
_(RawOrigin::Root, 1u16/*netuid*/, 3u64/*interval*/)/*set_commit_reveal_weights_interval()*/;
}

#[benchmark]
fn sudo_set_commit_reveal_weights_enabled() {
T::Subtensor::init_new_network(1u16 /*netuid*/, 1u16 /*sudo_tempo*/);

#[extrinsic_call]
_(RawOrigin::Root, 1u16/*netuid*/, true/*enabled*/)/*set_commit_reveal_weights_enabled*/;
}

//impl_benchmark_test_suite!(AdminUtils, crate::mock::new_test_ext(), crate::mock::Test);
}
50 changes: 50 additions & 0 deletions pallets/admin-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,54 @@ pub mod pallet {
);
Ok(())
}

/// The extrinsic sets the commit/reveal interval for a subnet.
/// It is only callable by the root account or subnet owner.
/// The extrinsic will call the Subtensor pallet to set the interval.
#[pallet::call_index(48)]
#[pallet::weight(T::WeightInfo::sudo_set_commit_reveal_weights_interval())]
pub fn sudo_set_commit_reveal_weights_interval(
origin: OriginFor<T>,
netuid: u16,
interval: u64,
) -> DispatchResult {
T::Subtensor::ensure_subnet_owner_or_root(origin, netuid)?;

ensure!(
T::Subtensor::if_subnet_exist(netuid),
Error::<T>::SubnetDoesNotExist
);

T::Subtensor::set_commit_reveal_weights_interval(netuid, interval);
log::info!(
"SetWeightCommitInterval( netuid: {:?}, interval: {:?} ) ",
netuid,
interval
);
Ok(())
}

/// The extrinsic enabled/disables commit/reaveal for a given subnet.
/// It is only callable by the root account or subnet owner.
/// The extrinsic will call the Subtensor pallet to set the value.
#[pallet::call_index(49)]
#[pallet::weight(T::WeightInfo::sudo_set_commit_reveal_weights_enabled())]
pub fn sudo_set_commit_reveal_weights_enabled(
origin: OriginFor<T>,
netuid: u16,
enabled: bool,
) -> DispatchResult {
T::Subtensor::ensure_subnet_owner_or_root(origin, netuid)?;

ensure!(
T::Subtensor::if_subnet_exist(netuid),
Error::<T>::SubnetDoesNotExist
);

T::Subtensor::set_commit_reveal_weights_enabled(netuid, enabled);
log::info!("ToggleSetWeightsCommitReveal( netuid: {:?} ) ", netuid);
Ok(())
}
}
}

Expand Down Expand Up @@ -1042,4 +1090,6 @@ pub trait SubtensorInterface<AccountId, Balance, RuntimeOrigin> {
fn set_nominator_min_required_stake(min_stake: u64);
fn clear_small_nominations();
fn set_target_stakes_per_interval(target_stakes_per_interval: u64);
fn set_commit_reveal_weights_interval(netuid: u16, interval: u64);
fn set_commit_reveal_weights_enabled(netuid: u16, enabled: bool);
}
45 changes: 45 additions & 0 deletions pallets/admin-utils/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ pub trait WeightInfo {
fn sudo_set_min_burn() -> Weight;
fn sudo_set_network_registration_allowed() -> Weight;
fn sudo_set_tempo() -> Weight;
fn sudo_set_commit_reveal_weights_interval() -> Weight;
fn sudo_set_commit_reveal_weights_enabled() -> Weight;

}

/// Weights for `pallet_admin_utils` using the Substrate node and recommended hardware.
Expand Down Expand Up @@ -411,6 +414,24 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
.saturating_add(T::DbWeight::get().reads(1_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
fn sudo_set_commit_reveal_weights_interval() -> Weight {
// Proof Size summary in bytes:
// Measured: `1111`
// Estimated: `4697`
// Minimum execution time: 46_450_000 picoseconds.
Weight::from_parts(47_279_000, 4697)
.saturating_add(T::DbWeight::get().reads(1_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
fn sudo_set_commit_reveal_weights_enabled() -> Weight {
// Proof Size summary in bytes:
// Measured: `1111`
// Estimated: `4697`
// Minimum execution time: 46_450_000 picoseconds.
Weight::from_parts(47_279_000, 4697)
.saturating_add(T::DbWeight::get().reads(1_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
}

// For backwards compatibility and tests.
Expand Down Expand Up @@ -761,4 +782,28 @@ impl WeightInfo for () {
.saturating_add(RocksDbWeight::get().reads(1_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
fn sudo_set_commit_reveal_weights_interval() -> Weight {
// -- Extrinsic Time --
// Model:
// Time ~= 20.42
// µs
// Reads = 1
// Writes = 1
// Recorded proof Size = 456
Weight::from_parts(20_420_000, 456)
.saturating_add(RocksDbWeight::get().reads(1_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
fn sudo_set_commit_reveal_weights_enabled() -> Weight {
// -- Extrinsic Time --
// Model:
// Time ~= 19.78
// µs
// Reads = 1
// Writes = 1
// Recorded proof Size = 456
Weight::from_parts(19_780_000, 456)
.saturating_add(RocksDbWeight::get().reads(1_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
}
8 changes: 8 additions & 0 deletions pallets/admin-utils/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,14 @@ impl pallet_admin_utils::SubtensorInterface<AccountId, Balance, RuntimeOrigin> f
fn set_target_stakes_per_interval(target_stakes_per_interval: u64) {
SubtensorModule::set_target_stakes_per_interval(target_stakes_per_interval);
}

fn set_commit_reveal_weights_interval(netuid: u16, interval: u64) {
SubtensorModule::set_commit_reveal_weights_interval(netuid, interval);
}

fn set_commit_reveal_weights_enabled(netuid: u16, enabled: bool) {
SubtensorModule::set_commit_reveal_weights_enabled(netuid, enabled);
}
}

impl pallet_admin_utils::Config for Test {
Expand Down
46 changes: 46 additions & 0 deletions pallets/admin-utils/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,52 @@ fn test_sudo_set_min_delegate_take() {
});
}

#[test]
fn test_sudo_set_weight_commit_interval() {
new_test_ext().execute_with(|| {
let netuid: u16 = 1;
add_network(netuid, 10);

let to_be_set = 55;
let init_value = SubtensorModule::get_commit_reveal_weights_interval(netuid);

assert_ok!(AdminUtils::sudo_set_commit_reveal_weights_interval(
<<Test as Config>::RuntimeOrigin>::root(),
netuid,
to_be_set
));

assert!(init_value != to_be_set);
assert_eq!(
SubtensorModule::get_commit_reveal_weights_interval(netuid),
to_be_set
);
});
}

#[test]
fn test_sudo_set_commit_reveal_weights_enabled() {
new_test_ext().execute_with(|| {
let netuid: u16 = 1;
add_network(netuid, 10);

let to_be_set: bool = true;
let init_value: bool = SubtensorModule::get_commit_reveal_weights_enabled(netuid);

assert_ok!(AdminUtils::sudo_set_commit_reveal_weights_enabled(
<<Test as Config>::RuntimeOrigin>::root(),
netuid,
to_be_set
));

assert!(init_value != to_be_set);
assert_eq!(
SubtensorModule::get_commit_reveal_weights_enabled(netuid),
to_be_set
);
});
}

#[test]
fn test_sudo_set_target_stakes_per_interval() {
new_test_ext().execute_with(|| {
Expand Down
6 changes: 4 additions & 2 deletions pallets/subtensor/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ reveal_weights {
let version_key: u64 = 0;
let uids: Vec<u16> = vec![0];
let weight_values: Vec<u16> = vec![10];
let salt: Vec<u16> = vec![8];
let hotkey: T::AccountId = account("hot", 0, 1);
let coldkey: T::AccountId = account("cold", 1, 2);

Expand All @@ -414,16 +415,17 @@ reveal_weights {
);

Subtensor::<T>::set_validator_permit_for_uid(netuid, 0, true);
Subtensor::<T>::set_weight_commit_interval(0);
Subtensor::<T>::set_commit_reveal_weights_interval(netuid, 0);

let commit_hash: H256 = BlakeTwo256::hash_of(&(
hotkey.clone(),
netuid,
uids.clone(),
weight_values.clone(),
salt.clone(),
version_key,
));
let _ = Subtensor::<T>::commit_weights(<T as frame_system::Config>::RuntimeOrigin::from(RawOrigin::Signed(hotkey.clone())), netuid, commit_hash);

}: reveal_weights(RawOrigin::Signed(hotkey.clone()), netuid, uids, weight_values, version_key)
}: reveal_weights(RawOrigin::Signed(hotkey.clone()), netuid, uids, weight_values, salt, version_key)
}
6 changes: 5 additions & 1 deletion pallets/subtensor/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,12 @@ mod errors {
/// No commit found for the provided hotkey+netuid combination when attempting to reveal the weights.
NoWeightsCommitFound,
/// Not the correct block/range to reveal weights.
InvalidRevealCommitHashNotMatchTempo,
InvalidRevealCommitTempo,
/// Committed hash does not equal the hashed reveal data.
InvalidRevealCommitHashNotMatch,
/// Attempting to call set_weights when commit/reveal is enabled
CommitRevealEnabled,
/// Attemtping to commit/reveal weights when disabled.
CommitRevealDisabled,
}
}
26 changes: 22 additions & 4 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -934,10 +934,20 @@ pub mod pallet {
pub fn DefaultWeightCommitRevealInterval<T: Config>() -> u64 {
1000
}

// --- DMAP ( netuid ) --> interval
#[pallet::storage]
pub type WeightCommitRevealInterval<T> =
StorageValue<_, u64, ValueQuery, DefaultWeightCommitRevealInterval<T>>;
StorageMap<_, Identity, u16, u64, ValueQuery, DefaultWeightCommitRevealInterval<T>>;

/// Default value for weight commit/reveal enabled.
#[pallet::type_value]
pub fn DefaultCommitRevealWeightsEnabled<T: Config>() -> bool {
false
}
// --- DMAP ( netuid ) --> interval
#[pallet::storage]
pub type CommitRevealWeightsEnabled<T> =
StorageMap<_, Identity, u16, bool, ValueQuery, DefaultCommitRevealWeightsEnabled<T>>;

/// =======================================
/// ==== Subnetwork Consensus Storage ====
Expand Down Expand Up @@ -1354,7 +1364,11 @@ pub mod pallet {
weights: Vec<u16>,
version_key: u64,
) -> DispatchResult {
Self::do_set_weights(origin, netuid, dests, weights, version_key)
if !Self::get_commit_reveal_weights_enabled(netuid) {
return Self::do_set_weights(origin, netuid, dests, weights, version_key);
}

Err(Error::<T>::CommitRevealEnabled.into())
}

/// ---- Used to commit a hash of your weight values to later be revealed.
Expand Down Expand Up @@ -1400,6 +1414,9 @@ pub mod pallet {
/// * `values` (`Vec<u16>`):
/// - The values of the weights being revealed.
///
/// * `salt` (`Vec<u8>`):
/// - The random salt to protect from brute-force guessing attack in case of small weight changes bit-wise.
///
/// * `version_key` (`u64`):
/// - The network version key.
///
Expand All @@ -1422,9 +1439,10 @@ pub mod pallet {
netuid: u16,
uids: Vec<u16>,
values: Vec<u16>,
salt: Vec<u16>,
version_key: u64,
) -> DispatchResult {
Self::do_reveal_weights(origin, netuid, uids, values, version_key)
Self::do_reveal_weights(origin, netuid, uids, values, salt, version_key)
}

/// # Args:
Expand Down
6 changes: 6 additions & 0 deletions pallets/subtensor/src/subnet_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ pub struct SubnetHyperparams {
max_validators: Compact<u16>,
adjustment_alpha: Compact<u64>,
difficulty: Compact<u64>,
commit_reveal_weights_interval: Compact<u64>,
commit_reveal_weights_enabled: bool,
}

impl<T: Config> Pallet<T> {
Expand Down Expand Up @@ -151,6 +153,8 @@ impl<T: Config> Pallet<T> {
let max_validators = Self::get_max_allowed_validators(netuid);
let adjustment_alpha = Self::get_adjustment_alpha(netuid);
let difficulty = Self::get_difficulty_as_u64(netuid);
let commit_reveal_weights_interval = Self::get_commit_reveal_weights_interval(netuid);
let commit_reveal_weights_enabled = Self::get_commit_reveal_weights_enabled(netuid);

Some(SubnetHyperparams {
rho: rho.into(),
Expand All @@ -175,6 +179,8 @@ impl<T: Config> Pallet<T> {
max_validators: max_validators.into(),
adjustment_alpha: adjustment_alpha.into(),
difficulty: difficulty.into(),
commit_reveal_weights_interval: commit_reveal_weights_interval.into(),
commit_reveal_weights_enabled,
})
}
}
13 changes: 13 additions & 0 deletions pallets/subtensor/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,19 @@ impl<T: Config> Pallet<T> {
Self::deposit_event(Event::KappaSet(netuid, kappa));
}

pub fn get_commit_reveal_weights_interval(netuid: u16) -> u64 {
WeightCommitRevealInterval::<T>::get(netuid)
}
pub fn set_commit_reveal_weights_interval(netuid: u16, interval: u64) {
WeightCommitRevealInterval::<T>::set(netuid, interval);
}
pub fn get_commit_reveal_weights_enabled(netuid: u16) -> bool {
CommitRevealWeightsEnabled::<T>::get(netuid)
}
pub fn set_commit_reveal_weights_enabled(netuid: u16, enabled: bool) {
CommitRevealWeightsEnabled::<T>::set(netuid, enabled);
}

pub fn get_rho(netuid: u16) -> u16 {
Rho::<T>::get(netuid)
}
Expand Down
Loading

0 comments on commit 86b7f6d

Please sign in to comment.