Skip to content

Commit cae4366

Browse files
authored
Merge pull request #864 from opentensor/commit-reveal-2
Commit-Reveal 2.0
2 parents 6c6136c + 6e21d6a commit cae4366

File tree

18 files changed

+3043
-437
lines changed

18 files changed

+3043
-437
lines changed

pallets/admin-utils/src/benchmarking.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,14 @@ mod benchmarks {
228228
}
229229

230230
#[benchmark]
231-
fn sudo_set_commit_reveal_weights_interval() {
231+
fn sudo_set_commit_reveal_weights_periods() {
232232
pallet_subtensor::Pallet::<T>::init_new_network(
233233
1u16, /*netuid*/
234234
1u16, /*sudo_tempo*/
235235
);
236236

237237
#[extrinsic_call]
238-
_(RawOrigin::Root, 1u16/*netuid*/, 3u64/*interval*/)/*set_commit_reveal_weights_interval()*/;
238+
_(RawOrigin::Root, 1u16/*netuid*/, 3u64/*interval*/)/*set_commit_reveal_weights_periods()*/;
239239
}
240240

241241
#[benchmark]

pallets/admin-utils/src/lib.rs

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -960,32 +960,6 @@ pub mod pallet {
960960
Ok(())
961961
}
962962

963-
/// The extrinsic sets the commit/reveal interval for a subnet.
964-
/// It is only callable by the root account or subnet owner.
965-
/// The extrinsic will call the Subtensor pallet to set the interval.
966-
#[pallet::call_index(48)]
967-
#[pallet::weight(T::WeightInfo::sudo_set_commit_reveal_weights_interval())]
968-
pub fn sudo_set_commit_reveal_weights_interval(
969-
origin: OriginFor<T>,
970-
netuid: u16,
971-
interval: u64,
972-
) -> DispatchResult {
973-
pallet_subtensor::Pallet::<T>::ensure_subnet_owner_or_root(origin, netuid)?;
974-
975-
ensure!(
976-
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
977-
Error::<T>::SubnetDoesNotExist
978-
);
979-
980-
pallet_subtensor::Pallet::<T>::set_commit_reveal_weights_interval(netuid, interval);
981-
log::debug!(
982-
"SetWeightCommitInterval( netuid: {:?}, interval: {:?} ) ",
983-
netuid,
984-
interval
985-
);
986-
Ok(())
987-
}
988-
989963
/// The extrinsic enabled/disables commit/reaveal for a given subnet.
990964
/// It is only callable by the root account or subnet owner.
991965
/// The extrinsic will call the Subtensor pallet to set the value.
@@ -1196,6 +1170,45 @@ pub mod pallet {
11961170

11971171
Ok(())
11981172
}
1173+
1174+
/// Sets the commit-reveal weights periods for a specific subnet.
1175+
///
1176+
/// This extrinsic allows the subnet owner or root account to set the duration (in epochs) during which committed weights must be revealed.
1177+
/// The commit-reveal mechanism ensures that users commit weights in advance and reveal them only within a specified period.
1178+
///
1179+
/// # Arguments
1180+
/// * `origin` - The origin of the call, which must be the subnet owner or the root account.
1181+
/// * `netuid` - The unique identifier of the subnet for which the periods are being set.
1182+
/// * `periods` - The number of epochs that define the commit-reveal period.
1183+
///
1184+
/// # Errors
1185+
/// * `BadOrigin` - If the caller is neither the subnet owner nor the root account.
1186+
/// * `SubnetDoesNotExist` - If the specified subnet does not exist.
1187+
///
1188+
/// # Weight
1189+
/// Weight is handled by the `#[pallet::weight]` attribute.
1190+
#[pallet::call_index(56)]
1191+
#[pallet::weight(T::WeightInfo::sudo_set_commit_reveal_weights_periods())]
1192+
pub fn sudo_set_commit_reveal_weights_periods(
1193+
origin: OriginFor<T>,
1194+
netuid: u16,
1195+
periods: u64,
1196+
) -> DispatchResult {
1197+
pallet_subtensor::Pallet::<T>::ensure_subnet_owner_or_root(origin, netuid)?;
1198+
1199+
ensure!(
1200+
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
1201+
Error::<T>::SubnetDoesNotExist
1202+
);
1203+
1204+
pallet_subtensor::Pallet::<T>::set_reveal_period(netuid, periods);
1205+
log::debug!(
1206+
"SetWeightCommitPeriods( netuid: {:?}, periods: {:?} ) ",
1207+
netuid,
1208+
periods
1209+
);
1210+
Ok(())
1211+
}
11991212
}
12001213
}
12011214

pallets/admin-utils/src/weights.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ 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;
63+
fn sudo_set_commit_reveal_weights_periods() -> Weight;
6464
fn sudo_set_commit_reveal_weights_enabled() -> Weight;
6565
}
6666

@@ -413,12 +413,12 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
413413
.saturating_add(T::DbWeight::get().reads(1_u64))
414414
.saturating_add(T::DbWeight::get().writes(1_u64))
415415
}
416-
fn sudo_set_commit_reveal_weights_interval() -> Weight {
416+
fn sudo_set_commit_reveal_weights_periods() -> Weight {
417417
// Proof Size summary in bytes:
418-
// Measured: `1111`
419-
// Estimated: `4697`
420-
// Minimum execution time: 46_450_000 picoseconds.
421-
Weight::from_parts(47_279_000, 4697)
418+
// Measured: `456`
419+
// Estimated: `3921`
420+
// Minimum execution time: 19_070_000 picoseconds.
421+
Weight::from_parts(19_380_000, 456)
422422
.saturating_add(T::DbWeight::get().reads(1_u64))
423423
.saturating_add(T::DbWeight::get().writes(1_u64))
424424
}
@@ -781,17 +781,17 @@ impl WeightInfo for () {
781781
.saturating_add(RocksDbWeight::get().reads(1_u64))
782782
.saturating_add(RocksDbWeight::get().writes(1_u64))
783783
}
784-
fn sudo_set_commit_reveal_weights_interval() -> Weight {
784+
fn sudo_set_commit_reveal_weights_periods() -> Weight {
785785
// -- Extrinsic Time --
786786
// Model:
787-
// Time ~= 20.42
788-
// µs
787+
// Time ~= 19.38
788+
// µs
789789
// Reads = 1
790790
// Writes = 1
791791
// Recorded proof Size = 456
792-
Weight::from_parts(20_420_000, 456)
793-
.saturating_add(RocksDbWeight::get().reads(1_u64))
794-
.saturating_add(RocksDbWeight::get().writes(1_u64))
792+
Weight::from_parts(19_380_000, 456)
793+
.saturating_add(RocksDbWeight::get().reads(1))
794+
.saturating_add(RocksDbWeight::get().writes(1))
795795
}
796796
fn sudo_set_commit_reveal_weights_enabled() -> Weight {
797797
// -- Extrinsic Time --

pallets/admin-utils/tests/tests.rs

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,29 +1113,6 @@ fn test_sudo_set_min_delegate_take() {
11131113
});
11141114
}
11151115

1116-
#[test]
1117-
fn test_sudo_set_weight_commit_interval() {
1118-
new_test_ext().execute_with(|| {
1119-
let netuid: u16 = 1;
1120-
add_network(netuid, 10);
1121-
1122-
let to_be_set = 55;
1123-
let init_value = SubtensorModule::get_commit_reveal_weights_interval(netuid);
1124-
1125-
assert_ok!(AdminUtils::sudo_set_commit_reveal_weights_interval(
1126-
<<Test as Config>::RuntimeOrigin>::root(),
1127-
netuid,
1128-
to_be_set
1129-
));
1130-
1131-
assert!(init_value != to_be_set);
1132-
assert_eq!(
1133-
SubtensorModule::get_commit_reveal_weights_interval(netuid),
1134-
to_be_set
1135-
);
1136-
});
1137-
}
1138-
11391116
#[test]
11401117
fn test_sudo_set_commit_reveal_weights_enabled() {
11411118
new_test_ext().execute_with(|| {
@@ -1435,3 +1412,23 @@ fn test_sudo_set_dissolve_network_schedule_duration() {
14351412
System::assert_last_event(Event::DissolveNetworkScheduleDurationSet(new_duration).into());
14361413
});
14371414
}
1415+
1416+
#[test]
1417+
fn sudo_set_commit_reveal_weights_periods() {
1418+
new_test_ext().execute_with(|| {
1419+
let netuid: u16 = 1;
1420+
add_network(netuid, 10);
1421+
1422+
let to_be_set = 55;
1423+
let init_value = SubtensorModule::get_reveal_period(netuid);
1424+
1425+
assert_ok!(AdminUtils::sudo_set_commit_reveal_weights_periods(
1426+
<<Test as Config>::RuntimeOrigin>::root(),
1427+
netuid,
1428+
to_be_set
1429+
));
1430+
1431+
assert!(init_value != to_be_set);
1432+
assert_eq!(SubtensorModule::get_reveal_period(netuid), to_be_set);
1433+
});
1434+
}

pallets/subtensor/src/benchmarks.rs

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,6 @@ reveal_weights {
416416
);
417417

418418
Subtensor::<T>::set_validator_permit_for_uid(netuid, 0, true);
419-
Subtensor::<T>::set_commit_reveal_weights_interval(netuid, 0);
420419

421420
let commit_hash: H256 = BlakeTwo256::hash_of(&(
422421
hotkey.clone(),
@@ -521,4 +520,82 @@ reveal_weights {
521520
// Benchmark setup complete, now execute the extrinsic
522521
}: swap_coldkey(RawOrigin::Root, old_coldkey.clone(), new_coldkey.clone())
523522

523+
batch_reveal_weights {
524+
let tempo: u16 = 0;
525+
let netuid: u16 = 1;
526+
let num_commits: usize = 10;
527+
528+
let hotkey: T::AccountId = account("hot", 0, 1);
529+
let coldkey: T::AccountId = account("cold", 0, 2);
530+
531+
Subtensor::<T>::init_new_network(netuid, tempo);
532+
Subtensor::<T>::set_network_registration_allowed(netuid, true);
533+
Subtensor::<T>::set_network_pow_registration_allowed(netuid, true);
534+
Subtensor::<T>::set_commit_reveal_weights_enabled(netuid, true);
535+
Subtensor::<T>::set_weights_set_rate_limit(netuid, 0); // Disable rate limiting for benchmarking
536+
537+
let block_number: u64 = Subtensor::<T>::get_current_block_as_u64();
538+
let (nonce, work): (u64, Vec<u8>) = Subtensor::<T>::create_work_for_block_number(
539+
netuid,
540+
block_number,
541+
3,
542+
&hotkey,
543+
);
544+
545+
let origin = T::RuntimeOrigin::from(RawOrigin::Signed(hotkey.clone()));
546+
assert_ok!(Subtensor::<T>::register(
547+
origin.clone(),
548+
netuid,
549+
block_number,
550+
nonce,
551+
work.clone(),
552+
hotkey.clone(),
553+
coldkey.clone(),
554+
));
555+
556+
let uid: u16 = 0;
557+
558+
Subtensor::<T>::set_validator_permit_for_uid(netuid, uid, true);
559+
560+
let mut uids_list = Vec::new();
561+
let mut values_list = Vec::new();
562+
let mut salts_list = Vec::new();
563+
let mut version_keys = Vec::new();
564+
565+
for i in 0..num_commits {
566+
let uids: Vec<u16> = vec![uid];
567+
let values: Vec<u16> = vec![i as u16];
568+
let salt: Vec<u16> = vec![i as u16];
569+
let version_key_i: u64 = i as u64;
570+
571+
let commit_hash: H256 = BlakeTwo256::hash_of(&(
572+
hotkey.clone(),
573+
netuid,
574+
uids.clone(),
575+
values.clone(),
576+
salt.clone(),
577+
version_key_i,
578+
));
579+
580+
assert_ok!(Subtensor::<T>::commit_weights(
581+
T::RuntimeOrigin::from(RawOrigin::Signed(hotkey.clone())),
582+
netuid,
583+
commit_hash,
584+
));
585+
586+
uids_list.push(uids);
587+
values_list.push(values);
588+
salts_list.push(salt);
589+
version_keys.push(version_key_i);
590+
}
591+
}: batch_reveal_weights(
592+
RawOrigin::Signed(hotkey.clone()),
593+
netuid,
594+
uids_list,
595+
values_list,
596+
salts_list,
597+
version_keys
598+
)
599+
600+
524601
}

pallets/subtensor/src/lib.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ pub mod pallet {
7272
use frame_system::pallet_prelude::*;
7373
use sp_core::H256;
7474
use sp_runtime::traits::{Dispatchable, TrailingZeroInput};
75+
use sp_std::collections::vec_deque::VecDeque;
7576
use sp_std::vec;
7677
use sp_std::vec::Vec;
7778
use subtensor_macros::freeze_struct;
@@ -568,6 +569,11 @@ pub mod pallet {
568569
0
569570
}
570571
#[pallet::type_value]
572+
/// Default minimum stake for weights.
573+
pub fn DefaultRevealPeriodEpochs<T: Config>() -> u64 {
574+
1
575+
}
576+
#[pallet::type_value]
571577
/// Value definition for vector of u16.
572578
pub fn EmptyU16Vec<T: Config>() -> Vec<u16> {
573579
vec![]
@@ -629,11 +635,6 @@ pub mod pallet {
629635
T::InitialServingRateLimit::get()
630636
}
631637
#[pallet::type_value]
632-
/// Default value for weight commit reveal interval.
633-
pub fn DefaultWeightCommitRevealInterval<T: Config>() -> u64 {
634-
1000
635-
}
636-
#[pallet::type_value]
637638
/// Default value for weight commit/reveal enabled.
638639
pub fn DefaultCommitRevealWeightsEnabled<T: Config>() -> bool {
639640
false
@@ -1032,10 +1033,6 @@ pub mod pallet {
10321033
StorageMap<_, Identity, u16, u64, ValueQuery, DefaultAdjustmentAlpha<T>>;
10331034
#[pallet::storage]
10341035
/// --- MAP ( netuid ) --> interval
1035-
pub type WeightCommitRevealInterval<T> =
1036-
StorageMap<_, Identity, u16, u64, ValueQuery, DefaultWeightCommitRevealInterval<T>>;
1037-
#[pallet::storage]
1038-
/// --- MAP ( netuid ) --> interval
10391036
pub type CommitRevealWeightsEnabled<T> =
10401037
StorageMap<_, Identity, u16, bool, ValueQuery, DefaultCommitRevealWeightsEnabled<T>>;
10411038
#[pallet::storage]
@@ -1254,16 +1251,20 @@ pub mod pallet {
12541251
/// ITEM( weights_min_stake )
12551252
pub type WeightsMinStake<T> = StorageValue<_, u64, ValueQuery, DefaultWeightsMinStake<T>>;
12561253
#[pallet::storage]
1257-
/// --- MAP (netuid, who) --> (hash, weight) | Returns the hash and weight committed by an account for a given netuid.
1254+
/// --- MAP (netuid, who) --> VecDeque<(hash, commit_block)> | Stores a queue of commits for an account on a given netuid.
12581255
pub type WeightCommits<T: Config> = StorageDoubleMap<
12591256
_,
12601257
Twox64Concat,
12611258
u16,
12621259
Twox64Concat,
12631260
T::AccountId,
1264-
(H256, u64),
1261+
VecDeque<(H256, u64)>,
12651262
OptionQuery,
12661263
>;
1264+
#[pallet::storage]
1265+
/// --- Map (netuid) --> Number of epochs allowed for commit reveal periods
1266+
pub type RevealPeriodEpochs<T: Config> =
1267+
StorageMap<_, Twox64Concat, u16, u64, ValueQuery, DefaultRevealPeriodEpochs<T>>;
12671268

12681269
/// ==================
12691270
/// ==== Genesis =====
@@ -1460,6 +1461,18 @@ where
14601461
Err(InvalidTransaction::Custom(2).into())
14611462
}
14621463
}
1464+
Some(Call::batch_reveal_weights { netuid, .. }) => {
1465+
if Self::check_weights_min_stake(who) {
1466+
let priority: u64 = Self::get_priority_set_weights(who, *netuid);
1467+
Ok(ValidTransaction {
1468+
priority,
1469+
longevity: 1,
1470+
..Default::default()
1471+
})
1472+
} else {
1473+
Err(InvalidTransaction::Custom(6).into())
1474+
}
1475+
}
14631476
Some(Call::set_weights { netuid, .. }) => {
14641477
if Self::check_weights_min_stake(who) {
14651478
let priority: u64 = Self::get_priority_set_weights(who, *netuid);

0 commit comments

Comments
 (0)