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

use tempo for commit-reveal #486

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion pallets/admin-utils/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ impl pallet_admin_utils::SubtensorInterface<AccountId, Balance, RuntimeOrigin> f
}

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

fn set_commit_reveal_weights_enabled(netuid: u16, enabled: bool) {
Expand Down
4 changes: 2 additions & 2 deletions pallets/admin-utils/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1116,7 +1116,7 @@ fn test_sudo_set_weight_commit_interval() {
add_network(netuid, 10);

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

assert_ok!(AdminUtils::sudo_set_commit_reveal_weights_interval(
<<Test as Config>::RuntimeOrigin>::root(),
Expand All @@ -1126,7 +1126,7 @@ fn test_sudo_set_weight_commit_interval() {

assert!(init_value != to_be_set);
assert_eq!(
SubtensorModule::get_commit_reveal_weights_interval(netuid),
SubtensorModule::get_commit_reveal_weights_tempos_per_interval(netuid),
to_be_set
);
});
Expand Down
2 changes: 1 addition & 1 deletion pallets/subtensor/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ reveal_weights {
);

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

let commit_hash: H256 = BlakeTwo256::hash_of(&(
hotkey.clone(),
Expand Down
4 changes: 2 additions & 2 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -932,11 +932,11 @@ pub mod pallet {
/// Default value for weight commit reveal interval.
#[pallet::type_value]
pub fn DefaultWeightCommitRevealInterval<T: Config>() -> u64 {
1000
1
}
// --- DMAP ( netuid ) --> interval
#[pallet::storage]
pub type WeightCommitRevealInterval<T> =
pub type WeightCommitRevealTempo<T> =
StorageMap<_, Identity, u16, u64, ValueQuery, DefaultWeightCommitRevealInterval<T>>;

/// Default value for weight commit/reveal enabled.
Expand Down
8 changes: 5 additions & 3 deletions pallets/subtensor/src/subnet_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub struct SubnetHyperparams {
max_validators: Compact<u16>,
adjustment_alpha: Compact<u64>,
difficulty: Compact<u64>,
commit_reveal_weights_interval: Compact<u64>,
commit_reveal_weights_tempos_per_interval: Compact<u64>,
commit_reveal_weights_enabled: bool,
}

Expand Down Expand Up @@ -153,7 +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_tempos_per_interval =
Self::get_commit_reveal_weights_tempos_per_interval(netuid);
let commit_reveal_weights_enabled = Self::get_commit_reveal_weights_enabled(netuid);

Some(SubnetHyperparams {
Expand All @@ -179,7 +180,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_tempos_per_interval: commit_reveal_weights_tempos_per_interval
.into(),
commit_reveal_weights_enabled,
})
}
Expand Down
8 changes: 4 additions & 4 deletions pallets/subtensor/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,11 +477,11 @@ 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 get_commit_reveal_weights_tempos_per_interval(netuid: u16) -> u64 {
WeightCommitRevealTempo::<T>::get(netuid)
}
pub fn set_commit_reveal_weights_interval(netuid: u16, interval: u64) {
WeightCommitRevealInterval::<T>::set(netuid, interval);
pub fn set_commit_reveal_weights_tempos_per_interval(netuid: u16, interval: u64) {
WeightCommitRevealTempo::<T>::set(netuid, interval);
}
pub fn get_commit_reveal_weights_enabled(netuid: u16) -> bool {
CommitRevealWeightsEnabled::<T>::get(netuid)
Expand Down
14 changes: 10 additions & 4 deletions pallets/subtensor/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,12 @@ impl<T: Config> Pallet<T> {

pub fn can_commit(netuid: u16, who: &T::AccountId) -> bool {
if let Some((_hash, commit_block)) = WeightCommits::<T>::get(netuid, who) {
let interval: u64 = Self::get_commit_reveal_weights_interval(netuid);
let epochs: u64 = Self::get_commit_reveal_weights_tempos_per_interval(netuid);
let root_netuid: u16 = Self::get_root_netuid();
let tempo: u16 = Self::get_tempo(root_netuid);
let interval: u64 = epochs * tempo as u64;
if interval == 0 {
return true; //prevent division by 0
return true;
}

let current_block: u64 = Self::get_current_block_as_u64();
Expand All @@ -458,9 +461,12 @@ impl<T: Config> Pallet<T> {
}

pub fn is_reveal_block_range(netuid: u16, commit_block: u64) -> bool {
let interval: u64 = Self::get_commit_reveal_weights_interval(netuid);
let root_netuid: u16 = Self::get_root_netuid();
let tempo: u16 = Self::get_tempo(root_netuid);
let epochs: u64 = Self::get_commit_reveal_weights_tempos_per_interval(netuid);
let interval: u64 = epochs * tempo as u64;
if interval == 0 {
return true; //prevent division by 0
return true;
}

let commit_interval_start: u64 = commit_block - (commit_block % interval); // Find the start of the interval in which the commit occurred
Expand Down
59 changes: 39 additions & 20 deletions pallets/subtensor/tests/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,8 @@ fn test_commit_reveal_weights_ok() {
let weight_values: Vec<u16> = vec![10, 10];
let version_key: u64 = 0;
let hotkey: U256 = U256::from(1);
let tempo: u16 = 360;
let epochs: u64 = 5;

let commit_hash: H256 = BlakeTwo256::hash_of(&(
hotkey,
Expand All @@ -1012,20 +1014,21 @@ fn test_commit_reveal_weights_ok() {
add_network(netuid, 0, 0);
register_ok_neuron(netuid, U256::from(3), U256::from(4), 300000);
register_ok_neuron(netuid, U256::from(1), U256::from(2), 100000);
SubtensorModule::set_weights_set_rate_limit(netuid, 5);
SubtensorModule::set_weights_set_rate_limit(netuid, epochs);
SubtensorModule::set_validator_permit_for_uid(netuid, 0, true);
SubtensorModule::set_validator_permit_for_uid(netuid, 1, true);

SubtensorModule::set_commit_reveal_weights_interval(netuid, 5);
SubtensorModule::set_commit_reveal_weights_tempos_per_interval(netuid, 5);
SubtensorModule::set_commit_reveal_weights_enabled(netuid, true);
SubtensorModule::set_tempo(0, tempo);

assert_ok!(SubtensorModule::commit_weights(
RuntimeOrigin::signed(hotkey),
netuid,
commit_hash
));

step_block(5);
step_block(epochs as u16 * tempo);

assert_ok!(SubtensorModule::reveal_weights(
RuntimeOrigin::signed(hotkey),
Expand All @@ -1045,6 +1048,9 @@ fn test_commit_reveal_interval() {
let weight_values: Vec<u16> = vec![10, 10];
let version_key: u64 = 0;
let hotkey: U256 = U256::from(1);
let tempo: u16 = 360;
let epochs: u64 = 10;
let blocks_to_step: u16 = epochs as u16 * tempo;

let commit_hash: H256 = BlakeTwo256::hash_of(&(
hotkey,
Expand All @@ -1061,8 +1067,9 @@ fn test_commit_reveal_interval() {
SubtensorModule::set_validator_permit_for_uid(netuid, 0, true);
SubtensorModule::set_validator_permit_for_uid(netuid, 1, true);

SubtensorModule::set_commit_reveal_weights_interval(netuid, 100);
SubtensorModule::set_commit_reveal_weights_tempos_per_interval(netuid, epochs);
SubtensorModule::set_commit_reveal_weights_enabled(netuid, true);
SubtensorModule::set_tempo(0, tempo);
System::set_block_number(0);

assert_ok!(SubtensorModule::commit_weights(
Expand All @@ -1084,7 +1091,7 @@ fn test_commit_reveal_interval() {
),
Error::<Test>::InvalidRevealTempo
);
step_block(99);
step_block(blocks_to_step - 1);
assert_err!(
SubtensorModule::commit_weights(RuntimeOrigin::signed(hotkey), netuid, commit_hash),
Error::<Test>::CommitNotAllowed
Expand Down Expand Up @@ -1122,7 +1129,7 @@ fn test_commit_reveal_interval() {
),
Error::<Test>::InvalidRevealTempo
);
step_block(100);
step_block(blocks_to_step);
assert_ok!(SubtensorModule::reveal_weights(
RuntimeOrigin::signed(hotkey),
netuid,
Expand All @@ -1137,7 +1144,7 @@ fn test_commit_reveal_interval() {
netuid,
commit_hash
));
step_block(205);
step_block((blocks_to_step * 2) + 5);
assert_err!(
SubtensorModule::reveal_weights(
RuntimeOrigin::signed(hotkey),
Expand All @@ -1155,7 +1162,7 @@ fn test_commit_reveal_interval() {
netuid,
commit_hash
));
step_block(425);
step_block((blocks_to_step * 4) + 25);
let commit_hash_2: H256 = BlakeTwo256::hash_of(&(
hotkey,
netuid,
Expand All @@ -1168,7 +1175,7 @@ fn test_commit_reveal_interval() {
netuid,
commit_hash_2
));
step_block(100);
step_block(blocks_to_step);
assert_err!(
SubtensorModule::reveal_weights(
RuntimeOrigin::signed(hotkey),
Expand Down Expand Up @@ -1197,6 +1204,9 @@ fn test_commit_reveal_hash() {
let weight_values: Vec<u16> = vec![10, 10];
let version_key: u64 = 0;
let hotkey: U256 = U256::from(1);
let tempo: u16 = 360;
let epochs: u64 = 10;
let blocks_to_step: u16 = epochs as u16 * tempo;

add_network(netuid, 0, 0);
register_ok_neuron(netuid, U256::from(3), U256::from(4), 300000);
Expand All @@ -1205,7 +1215,8 @@ fn test_commit_reveal_hash() {
SubtensorModule::set_validator_permit_for_uid(netuid, 0, true);
SubtensorModule::set_validator_permit_for_uid(netuid, 1, true);

SubtensorModule::set_commit_reveal_weights_interval(netuid, 5);
SubtensorModule::set_commit_reveal_weights_tempos_per_interval(netuid, epochs);
SubtensorModule::set_tempo(0, tempo);
SubtensorModule::set_commit_reveal_weights_enabled(netuid, true);

let commit_hash: H256 = BlakeTwo256::hash_of(&(
Expand All @@ -1222,7 +1233,7 @@ fn test_commit_reveal_hash() {
commit_hash
));

step_block(5);
step_block(blocks_to_step);

assert_err!(
SubtensorModule::reveal_weights(
Expand Down Expand Up @@ -1283,6 +1294,9 @@ fn test_commit_reveal_disabled_or_enabled() {
let weight_values: Vec<u16> = vec![10, 10];
let version_key: u64 = 0;
let hotkey: U256 = U256::from(1);
let tempo: u16 = 360;
let epochs: u64 = 10;
let blocks_to_step: u16 = epochs as u16 * tempo;

let commit_hash: H256 = BlakeTwo256::hash_of(&(
hotkey,
Expand All @@ -1299,15 +1313,16 @@ fn test_commit_reveal_disabled_or_enabled() {
SubtensorModule::set_validator_permit_for_uid(netuid, 0, true);
SubtensorModule::set_validator_permit_for_uid(netuid, 1, true);

SubtensorModule::set_commit_reveal_weights_interval(netuid, 5);
SubtensorModule::set_commit_reveal_weights_tempos_per_interval(netuid, epochs);
SubtensorModule::set_tempo(0, tempo);
SubtensorModule::set_commit_reveal_weights_enabled(netuid, false);

assert_err!(
SubtensorModule::commit_weights(RuntimeOrigin::signed(hotkey), netuid, commit_hash),
Error::<Test>::CommitRevealDisabled
);

step_block(5);
step_block(blocks_to_step);

assert_err!(
SubtensorModule::reveal_weights(
Expand All @@ -1328,7 +1343,7 @@ fn test_commit_reveal_disabled_or_enabled() {
Error::<Test>::CommitRevealDisabled
);

step_block(5);
step_block(blocks_to_step);

assert_err!(
SubtensorModule::reveal_weights(
Expand All @@ -1350,7 +1365,7 @@ fn test_commit_reveal_disabled_or_enabled() {
commit_hash
));

step_block(5);
step_block(blocks_to_step);

assert_ok!(SubtensorModule::reveal_weights(
RuntimeOrigin::signed(hotkey),
Expand All @@ -1370,6 +1385,9 @@ fn test_toggle_commit_reveal_weights_and_set_weights() {
let weight_values: Vec<u16> = vec![10, 10];
let version_key: u64 = 0;
let hotkey: U256 = U256::from(1);
let tempo: u16 = 360;
let epochs: u64 = 10;
let blocks_to_step: u16 = epochs as u16 * tempo;

let commit_hash: H256 = BlakeTwo256::hash_of(&(
hotkey,
Expand All @@ -1386,9 +1404,10 @@ fn test_toggle_commit_reveal_weights_and_set_weights() {
SubtensorModule::set_validator_permit_for_uid(netuid, 1, true);

SubtensorModule::set_weights_set_rate_limit(netuid, 5);
SubtensorModule::set_commit_reveal_weights_interval(netuid, 5);
SubtensorModule::set_commit_reveal_weights_tempos_per_interval(netuid, epochs);
SubtensorModule::set_tempo(0, tempo);

step_block(5);
step_block(blocks_to_step);

// Set weights OK
let result = SubtensorModule::set_weights(
Expand All @@ -1410,7 +1429,7 @@ fn test_toggle_commit_reveal_weights_and_set_weights() {
commit_hash
));

step_block(5); //Step to the next commit/reveal tempo
step_block(blocks_to_step); //Step to the next commit/reveal tempo

// Reveal OK
assert_ok!(SubtensorModule::reveal_weights(
Expand All @@ -1425,7 +1444,7 @@ fn test_toggle_commit_reveal_weights_and_set_weights() {
SubtensorModule::set_commit_reveal_weights_enabled(netuid, false);

// Cannot set weights the same block due to WeightsRateLimit
step_block(5); //step to avoid settingweightstofast
step_block(blocks_to_step); //step to avoid settingweightstofast

let result = SubtensorModule::set_weights(
RuntimeOrigin::signed(hotkey),
Expand All @@ -1445,7 +1464,7 @@ fn commit_reveal_set_weights(
weights: Vec<u16>,
version_key: u64,
) -> DispatchResult {
SubtensorModule::set_commit_reveal_weights_interval(netuid, 5);
SubtensorModule::set_commit_reveal_weights_tempos_per_interval(netuid, 5);
SubtensorModule::set_weights_set_rate_limit(netuid, 5);
SubtensorModule::set_commit_reveal_weights_enabled(netuid, true);

Expand Down
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,7 @@ impl
}

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

fn set_commit_reveal_weights_enabled(netuid: u16, enabled: bool) {
Expand Down