Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add sudo calls & tests #432

Merged
merged 20 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 15 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
Empty file added CITATION.cft
Empty file.
2 changes: 1 addition & 1 deletion node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ fn localnet_genesis(
let mut balances = vec![
(
get_account_id_from_seed::<sr25519::Public>("Alice"),
1000000000000u128,
1000000000000000000u128,
),
(
get_account_id_from_seed::<sr25519::Public>("Bob"),
Expand Down
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 @@ -931,6 +931,54 @@ pub mod pallet {
log::info!("TxMinDelegateTakeSet( tx_min_delegate_take: {:?} ) ", take);
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(47)]
#[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(48)]
#[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 @@ -1023,4 +1071,6 @@ pub trait SubtensorInterface<AccountId, Balance, RuntimeOrigin> {
fn get_nominator_min_required_stake() -> u64;
fn set_nominator_min_required_stake(min_stake: u64);
fn clear_small_nominations();
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 @@ -450,6 +450,14 @@ impl pallet_admin_utils::SubtensorInterface<AccountId, Balance, RuntimeOrigin> f
fn clear_small_nominations() {
SubtensorModule::clear_small_nominations();
}

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 @@ -1108,3 +1108,49 @@ fn test_sudo_set_min_delegate_take() {
assert_eq!(SubtensorModule::get_min_delegate_take(), to_be_set);
});
}

#[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
);
});
}
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)
}
46 changes: 32 additions & 14 deletions pallets/subtensor/src/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,6 @@ impl<T: Config> Pallet<T> {
}

/// Output unnormalized sparse weights, input weights are assumed to be row max-upscaled in u16.
#[allow(clippy::indexing_slicing)]
pub fn get_weights_sparse(netuid: u16) -> Vec<Vec<(u16, I32F32)>> {
let n: usize = Self::get_subnetwork_n(netuid) as usize;
let mut weights: Vec<Vec<(u16, I32F32)>> = vec![vec![]; n];
Expand All @@ -743,52 +742,71 @@ impl<T: Config> Pallet<T> {
.filter(|(uid_i, _)| *uid_i < n as u16)
{
for (uid_j, weight_ij) in weights_i.iter().filter(|(uid_j, _)| *uid_j < n as u16) {
weights[uid_i as usize].push((*uid_j, I32F32::from_num(*weight_ij)));
weights
.get_mut(uid_i as usize)
.expect("uid_i is filtered to be less than n; qed")
.push((*uid_j, I32F32::from_num(*weight_ij)));
}
}
weights
}

/// Output unnormalized weights in [n, n] matrix, input weights are assumed to be row max-upscaled in u16.
#[allow(clippy::indexing_slicing)]
pub fn get_weights(netuid: u16) -> Vec<Vec<I32F32>> {
let n: usize = Self::get_subnetwork_n(netuid) as usize;
let mut weights: Vec<Vec<I32F32>> = vec![vec![I32F32::from_num(0.0); n]; n];
for (uid_i, weights_i) in
for (uid_i, weights_vec) in
<Weights<T> as IterableStorageDoubleMap<u16, u16, Vec<(u16, u16)>>>::iter_prefix(netuid)
.filter(|(uid_i, _)| *uid_i < n as u16)
{
for (uid_j, weight_ij) in weights_i {
weights[uid_i as usize][uid_j as usize] = I32F32::from_num(weight_ij);
for (uid_j, weight_ij) in weights_vec
.into_iter()
.filter(|(uid_j, _)| *uid_j < n as u16)
{
*weights
.get_mut(uid_i as usize)
.expect("uid_i is filtered to be less than n; qed")
.get_mut(uid_j as usize)
.expect("uid_j is filtered to be less than n; qed") =
I32F32::from_num(weight_ij);
}
}
weights
}

/// Output unnormalized sparse bonds, input bonds are assumed to be column max-upscaled in u16.
#[allow(clippy::indexing_slicing)]
pub fn get_bonds_sparse(netuid: u16) -> Vec<Vec<(u16, I32F32)>> {
let n: usize = Self::get_subnetwork_n(netuid) as usize;
let mut bonds: Vec<Vec<(u16, I32F32)>> = vec![vec![]; n];
for (uid_i, bonds_i) in
for (uid_i, bonds_vec) in
<Bonds<T> as IterableStorageDoubleMap<u16, u16, Vec<(u16, u16)>>>::iter_prefix(netuid)
.filter(|(uid_i, _)| *uid_i < n as u16)
{
for (uid_j, bonds_ij) in bonds_i {
bonds[uid_i as usize].push((uid_j, I32F32::from_num(bonds_ij)));
for (uid_j, bonds_ij) in bonds_vec {
bonds
.get_mut(uid_i as usize)
.expect("uid_i is filtered to be less than n; qed")
.push((uid_j, I32F32::from_num(bonds_ij)));
}
}
bonds
}

/// Output unnormalized bonds in [n, n] matrix, input bonds are assumed to be column max-upscaled in u16.
#[allow(clippy::indexing_slicing)]
pub fn get_bonds(netuid: u16) -> Vec<Vec<I32F32>> {
let n: usize = Self::get_subnetwork_n(netuid) as usize;
let mut bonds: Vec<Vec<I32F32>> = vec![vec![I32F32::from_num(0.0); n]; n];
for (uid_i, bonds_i) in
for (uid_i, bonds_vec) in
<Bonds<T> as IterableStorageDoubleMap<u16, u16, Vec<(u16, u16)>>>::iter_prefix(netuid)
.filter(|(uid_i, _)| *uid_i < n as u16)
{
for (uid_j, bonds_ij) in bonds_i {
bonds[uid_i as usize][uid_j as usize] = I32F32::from_num(bonds_ij);
for (uid_j, bonds_ij) in bonds_vec.into_iter().filter(|(uid_j, _)| *uid_j < n as u16) {
*bonds
.get_mut(uid_i as usize)
.expect("uid_i has been filtered to be less than n; qed")
.get_mut(uid_j as usize)
.expect("uid_j has been filtered to be less than n; qed") =
I32F32::from_num(bonds_ij);
}
}
bonds
Expand Down
4 changes: 4 additions & 0 deletions pallets/subtensor/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,9 @@ mod errors {
InvalidRevealTempo,
/// Committed hash does not equal the hashed reveal data
InvalidReveal,
/// Attempting to call set_weights when commit/reveal is enabled
CommitRevealEnabled,
/// Attemtping to commit/reveal weights when disabled.
CommitRevealDisabled,
}
}
Loading