Skip to content

Commit dd93e0c

Browse files
Merge pull request #396 from opentensor/commit-reveal-weights
Commit-Reveal Weights
2 parents c823cd8 + 22a9a66 commit dd93e0c

File tree

5 files changed

+788
-109
lines changed

5 files changed

+788
-109
lines changed

pallets/subtensor/src/benchmarks.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use frame_benchmarking::{account, benchmarks, whitelisted_caller};
88
use frame_support::assert_ok;
99
use frame_system::RawOrigin;
1010
pub use pallet::*;
11+
use sp_core::H256;
12+
use sp_runtime::traits::{BlakeTwo256, Hash};
1113
use sp_std::vec;
1214

1315
benchmarks! {
@@ -333,4 +335,89 @@ benchmarks! {
333335
assert_ok!(Subtensor::<T>::add_stake(RawOrigin::Signed(coldkey).into(), old_hotkey.clone(), 1_000_000_000));
334336
}
335337
}: _(RawOrigin::Signed(coldkey), old_hotkey, new_hotkey)
338+
339+
commit_weights {
340+
let tempo: u16 = 1;
341+
let netuid: u16 = 1;
342+
let version_key: u64 = 0;
343+
let uids: Vec<u16> = vec![0];
344+
let weight_values: Vec<u16> = vec![10];
345+
let hotkey: T::AccountId = account("hot", 0, 1);
346+
let coldkey: T::AccountId = account("cold", 0, 2);
347+
let start_nonce = 300000;
348+
349+
let commit_hash: H256 = BlakeTwo256::hash_of(&(
350+
hotkey.clone(),
351+
netuid,
352+
uids.clone(),
353+
weight_values.clone(),
354+
version_key,
355+
));
356+
357+
Subtensor::<T>::init_new_network(netuid, tempo);
358+
359+
let block_number: u64 = Subtensor::<T>::get_current_block_as_u64();
360+
let (nonce, work): (u64, Vec<u8>) = Subtensor::<T>::create_work_for_block_number(
361+
netuid,
362+
block_number,
363+
start_nonce,
364+
&hotkey,
365+
);
366+
let result = Subtensor::<T>::register(
367+
<T as frame_system::Config>::RuntimeOrigin::from(RawOrigin::Signed(hotkey.clone())),
368+
netuid,
369+
block_number,
370+
nonce,
371+
work,
372+
hotkey.clone(),
373+
coldkey,
374+
);
375+
Subtensor::<T>::set_validator_permit_for_uid(netuid, 0, true);
376+
377+
}: commit_weights(RawOrigin::Signed(hotkey.clone()), netuid, commit_hash)
378+
379+
reveal_weights {
380+
let tempo: u16 = 0;
381+
let netuid: u16 = 1;
382+
let version_key: u64 = 0;
383+
let uids: Vec<u16> = vec![0];
384+
let weight_values: Vec<u16> = vec![10];
385+
let hotkey: T::AccountId = account("hot", 0, 1);
386+
let coldkey: T::AccountId = account("cold", 1, 2);
387+
388+
Subtensor::<T>::init_new_network(netuid, tempo);
389+
Subtensor::<T>::set_network_registration_allowed(netuid, true);
390+
Subtensor::<T>::set_network_pow_registration_allowed(netuid, true);
391+
392+
let block_number: u64 = Subtensor::<T>::get_current_block_as_u64();
393+
let (nonce, work): (u64, Vec<u8>) = Subtensor::<T>::create_work_for_block_number(
394+
netuid,
395+
block_number,
396+
3,
397+
&hotkey,
398+
);
399+
400+
let _ = Subtensor::<T>::register(
401+
<T as frame_system::Config>::RuntimeOrigin::from(RawOrigin::Signed(hotkey.clone())),
402+
netuid,
403+
block_number,
404+
nonce,
405+
work.clone(),
406+
hotkey.clone(),
407+
coldkey.clone(),
408+
);
409+
410+
Subtensor::<T>::set_validator_permit_for_uid(netuid, 0, true);
411+
Subtensor::<T>::set_weight_commit_interval(0);
412+
413+
let commit_hash: H256 = BlakeTwo256::hash_of(&(
414+
hotkey.clone(),
415+
netuid,
416+
uids.clone(),
417+
weight_values.clone(),
418+
version_key,
419+
));
420+
let _ = Subtensor::<T>::commit_weights(<T as frame_system::Config>::RuntimeOrigin::from(RawOrigin::Signed(hotkey.clone())), netuid, commit_hash);
421+
422+
}: reveal_weights(RawOrigin::Signed(hotkey.clone()), netuid, uids, weight_values, version_key)
336423
}

pallets/subtensor/src/errors.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,5 +132,13 @@ mod errors {
132132
NomStakeBelowMinimumThreshold,
133133
/// delegate take is being set out of bounds
134134
InvalidTake,
135+
/// Not allowed to commit weights
136+
CommitNotAllowed,
137+
/// No commit found for provided hotkey+netuid when attempting to reveal weights
138+
NoCommitFound,
139+
/// Not the correct block/range to reveal weights
140+
InvalidRevealTempo,
141+
/// Committed hash does not equal the hashed reveal data
142+
InvalidReveal,
135143
}
136144
}

pallets/subtensor/src/lib.rs

Lines changed: 127 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ pub mod pallet {
6868
traits::{tokens::fungible, UnfilteredDispatchable},
6969
};
7070
use frame_system::pallet_prelude::*;
71+
use sp_core::H256;
7172
use sp_runtime::traits::TrailingZeroInput;
7273
use sp_std::vec;
7374
use sp_std::vec::Vec;
@@ -786,6 +787,27 @@ pub mod pallet {
786787
pub type AdjustmentAlpha<T: Config> =
787788
StorageMap<_, Identity, u16, u64, ValueQuery, DefaultAdjustmentAlpha<T>>;
788789

790+
// --- MAP (netuid, who) --> (hash, weight) | Returns the hash and weight committed by an account for a given netuid.
791+
#[pallet::storage]
792+
pub type WeightCommits<T: Config> = StorageDoubleMap<
793+
_,
794+
Twox64Concat,
795+
u16,
796+
Twox64Concat,
797+
T::AccountId,
798+
(H256, u64),
799+
OptionQuery,
800+
>;
801+
802+
#[pallet::type_value]
803+
pub fn DefaultWeightCommitRevealInterval<T: Config>() -> u64 {
804+
1000
805+
}
806+
807+
#[pallet::storage]
808+
pub type WeightCommitRevealInterval<T> =
809+
StorageValue<_, u64, ValueQuery, DefaultWeightCommitRevealInterval<T>>;
810+
789811
// =======================================
790812
// ==== Subnetwork Consensus Storage ====
791813
// =======================================
@@ -1184,8 +1206,8 @@ pub mod pallet {
11841206
// - Attempting to set weights with max value exceeding limit.
11851207
#[pallet::call_index(0)]
11861208
#[pallet::weight((Weight::from_parts(10_151_000_000, 0)
1187-
.saturating_add(T::DbWeight::get().reads(4104))
1188-
.saturating_add(T::DbWeight::get().writes(2)), DispatchClass::Normal, Pays::No))]
1209+
.saturating_add(T::DbWeight::get().reads(4104))
1210+
.saturating_add(T::DbWeight::get().writes(2)), DispatchClass::Normal, Pays::No))]
11891211
pub fn set_weights(
11901212
origin: OriginFor<T>,
11911213
netuid: u16,
@@ -1196,6 +1218,76 @@ pub mod pallet {
11961218
Self::do_set_weights(origin, netuid, dests, weights, version_key)
11971219
}
11981220

1221+
/// ---- Used to commit a hash of your weight values to later be revealed.
1222+
///
1223+
/// # Args:
1224+
/// * `origin`: (`<T as frame_system::Config>::RuntimeOrigin`):
1225+
/// - The signature of the committing hotkey.
1226+
///
1227+
/// * `netuid` (`u16`):
1228+
/// - The u16 network identifier.
1229+
///
1230+
/// * `commit_hash` (`H256`):
1231+
/// - The hash representing the committed weights.
1232+
///
1233+
/// # Raises:
1234+
/// * `CommitNotAllowed`:
1235+
/// - Attempting to commit when it is not allowed.
1236+
///
1237+
#[pallet::call_index(96)]
1238+
#[pallet::weight((Weight::from_parts(46_000_000, 0)
1239+
.saturating_add(T::DbWeight::get().reads(1))
1240+
.saturating_add(T::DbWeight::get().writes(1)), DispatchClass::Normal, Pays::No))]
1241+
pub fn commit_weights(
1242+
origin: T::RuntimeOrigin,
1243+
netuid: u16,
1244+
commit_hash: H256,
1245+
) -> DispatchResult {
1246+
Self::do_commit_weights(origin, netuid, commit_hash)
1247+
}
1248+
1249+
/// ---- Used to reveal the weights for a previously committed hash.
1250+
///
1251+
/// # Args:
1252+
/// * `origin`: (`<T as frame_system::Config>::RuntimeOrigin`):
1253+
/// - The signature of the revealing hotkey.
1254+
///
1255+
/// * `netuid` (`u16`):
1256+
/// - The u16 network identifier.
1257+
///
1258+
/// * `uids` (`Vec<u16>`):
1259+
/// - The uids for the weights being revealed.
1260+
///
1261+
/// * `values` (`Vec<u16>`):
1262+
/// - The values of the weights being revealed.
1263+
///
1264+
/// * `version_key` (`u64`):
1265+
/// - The network version key.
1266+
///
1267+
/// # Raises:
1268+
/// * `NoCommitFound`:
1269+
/// - Attempting to reveal weights without an existing commit.
1270+
///
1271+
/// * `InvalidRevealTempo`:
1272+
/// - Attempting to reveal weights outside the valid tempo.
1273+
///
1274+
/// * `InvalidReveal`:
1275+
/// - The revealed hash does not match the committed hash.
1276+
///
1277+
#[pallet::call_index(97)]
1278+
#[pallet::weight((Weight::from_parts(103_000_000, 0)
1279+
.saturating_add(T::DbWeight::get().reads(11))
1280+
.saturating_add(T::DbWeight::get().writes(3)), DispatchClass::Normal, Pays::No))]
1281+
pub fn reveal_weights(
1282+
origin: T::RuntimeOrigin,
1283+
netuid: u16,
1284+
uids: Vec<u16>,
1285+
values: Vec<u16>,
1286+
version_key: u64,
1287+
) -> DispatchResult {
1288+
Self::do_reveal_weights(origin, netuid, uids, values, version_key)
1289+
}
1290+
11991291
// # Args:
12001292
// * `origin`: (<T as frame_system::Config>Origin):
12011293
// - The caller, a hotkey who wishes to set their weights.
@@ -1756,7 +1848,7 @@ pub mod pallet {
17561848

17571849
#[pallet::call_index(60)]
17581850
#[pallet::weight((Weight::from_parts(91_000_000, 0)
1759-
.saturating_add(T::DbWeight::get().reads(27))
1851+
.saturating_add(T::DbWeight::get().reads(27))
17601852
.saturating_add(T::DbWeight::get().writes(22)), DispatchClass::Normal, Pays::No))]
17611853
pub fn faucet(
17621854
origin: OriginFor<T>,
@@ -1908,6 +2000,30 @@ where
19082000
_len: usize,
19092001
) -> TransactionValidity {
19102002
match call.is_sub_type() {
2003+
Some(Call::commit_weights { netuid, .. }) => {
2004+
if Self::check_weights_min_stake(who) {
2005+
let priority: u64 = Self::get_priority_set_weights(who, *netuid);
2006+
Ok(ValidTransaction {
2007+
priority,
2008+
longevity: 1,
2009+
..Default::default()
2010+
})
2011+
} else {
2012+
Err(InvalidTransaction::Call.into())
2013+
}
2014+
}
2015+
Some(Call::reveal_weights { netuid, .. }) => {
2016+
if Self::check_weights_min_stake(who) {
2017+
let priority: u64 = Self::get_priority_set_weights(who, *netuid);
2018+
Ok(ValidTransaction {
2019+
priority,
2020+
longevity: 1,
2021+
..Default::default()
2022+
})
2023+
} else {
2024+
Err(InvalidTransaction::Call.into())
2025+
}
2026+
}
19112027
Some(Call::set_weights { netuid, .. }) => {
19122028
if Self::check_weights_min_stake(who) {
19132029
let priority: u64 = Self::get_priority_set_weights(who, *netuid);
@@ -1986,6 +2102,14 @@ where
19862102
let transaction_fee = 0;
19872103
Ok((CallType::SetWeights, transaction_fee, who.clone()))
19882104
}
2105+
Some(Call::commit_weights { .. }) => {
2106+
let transaction_fee = 0;
2107+
Ok((CallType::SetWeights, transaction_fee, who.clone()))
2108+
}
2109+
Some(Call::reveal_weights { .. }) => {
2110+
let transaction_fee = 0;
2111+
Ok((CallType::SetWeights, transaction_fee, who.clone()))
2112+
}
19892113
Some(Call::register { .. }) => {
19902114
let transaction_fee = 0;
19912115
Ok((CallType::Register, transaction_fee, who.clone()))

0 commit comments

Comments
 (0)