Skip to content

Commit fdd4280

Browse files
authored
Merge pull request #1012 from opentensor/spiigot/drand-mock-runtimes
Commit-Reveal-3 Tests and Fixes
2 parents 46b512f + 7492611 commit fdd4280

File tree

12 files changed

+251
-113
lines changed

12 files changed

+251
-113
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pallets/admin-utils/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ log = { workspace = true }
2929
pallet-subtensor = { version = "4.0.0-dev", default-features = false, path = "../subtensor" }
3030
sp-weights = { workspace = true }
3131
substrate-fixed = { workspace = true }
32+
pallet-drand = { workspace = true, default-features = false }
3233

3334

3435
[dev-dependencies]

pallets/admin-utils/src/lib.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,34 @@ pub mod pallet {
12091209
);
12101210
Ok(())
12111211
}
1212+
1213+
/// The extrinsic sets the commit-reveal-3 weights set rate limit for a subnet.
1214+
/// It is only callable by the root account or subnet owner.
1215+
/// The extrinsic will call the Subtensor pallet to set the weights set rate limit.
1216+
#[pallet::call_index(58)]
1217+
#[pallet::weight(<T as Config>::WeightInfo::sudo_set_weights_set_rate_limit())]
1218+
pub fn sudo_set_commit_reveal_3_weights_set_rate_limit(
1219+
origin: OriginFor<T>,
1220+
netuid: u16,
1221+
weights_set_rate_limit: u64,
1222+
) -> DispatchResult {
1223+
pallet_subtensor::Pallet::<T>::ensure_subnet_owner_or_root(origin, netuid)?;
1224+
1225+
ensure!(
1226+
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
1227+
Error::<T>::SubnetDoesNotExist
1228+
);
1229+
pallet_subtensor::Pallet::<T>::set_v3_weights_set_rate_limit(
1230+
netuid,
1231+
weights_set_rate_limit,
1232+
);
1233+
log::debug!(
1234+
"WeightsSetRateLimitSet( netuid: {:?} weights_set_rate_limit: {:?} ) ",
1235+
netuid,
1236+
weights_set_rate_limit
1237+
);
1238+
Ok(())
1239+
}
12121240
}
12131241
}
12141242

pallets/admin-utils/tests/mock.rs

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ use sp_consensus_aura::sr25519::AuthorityId as AuraId;
1111
use sp_core::U256;
1212
use sp_core::{ConstU64, H256};
1313
use sp_runtime::{
14+
testing::TestXt,
1415
traits::{BlakeTwo256, ConstU32, IdentityLookup},
15-
BuildStorage, Perbill,
16+
BuildStorage, KeyTypeId, Perbill,
1617
};
1718
use sp_std::cmp::Ordering;
1819
use sp_weights::Weight;
@@ -21,13 +22,13 @@ type Block = frame_system::mocking::MockBlock<Test>;
2122

2223
// Configure a mock runtime to test the pallet.
2324
frame_support::construct_runtime!(
24-
pub enum Test
25-
{
25+
pub enum Test {
2626
System: frame_system = 1,
2727
Balances: pallet_balances = 2,
2828
AdminUtils: pallet_admin_utils = 3,
2929
SubtensorModule: pallet_subtensor::{Pallet, Call, Storage, Event<T>, Error<T>} = 4,
3030
Scheduler: pallet_scheduler::{Pallet, Call, Storage, Event<T>} = 5,
31+
Drand: pallet_drand::{Pallet, Call, Storage, Event<T>} = 6,
3132
}
3233
);
3334

@@ -63,6 +64,10 @@ pub type Balance = u64;
6364
#[allow(dead_code)]
6465
pub type BlockNumber = u64;
6566

67+
pub type TestAuthId = test_crypto::TestAuthId;
68+
pub type Index = u64;
69+
pub type UncheckedExtrinsic = TestXt<RuntimeCall, ()>;
70+
6671
parameter_types! {
6772
pub const InitialMinAllowedWeights: u16 = 0;
6873
pub const InitialEmissionValue: u16 = 0;
@@ -272,6 +277,74 @@ impl pallet_scheduler::Config for Test {
272277
type Preimages = ();
273278
}
274279

280+
impl pallet_drand::Config for Test {
281+
type RuntimeEvent = RuntimeEvent;
282+
type WeightInfo = pallet_drand::weights::SubstrateWeight<Test>;
283+
type AuthorityId = TestAuthId;
284+
type Verifier = pallet_drand::verifier::QuicknetVerifier;
285+
type UnsignedPriority = ConstU64<{ 1 << 20 }>;
286+
type HttpFetchTimeout = ConstU64<1_000>;
287+
}
288+
289+
impl frame_system::offchain::SigningTypes for Test {
290+
type Public = test_crypto::Public;
291+
type Signature = test_crypto::Signature;
292+
}
293+
294+
pub const KEY_TYPE: KeyTypeId = KeyTypeId(*b"test");
295+
296+
mod test_crypto {
297+
use super::KEY_TYPE;
298+
use sp_core::sr25519::{Public as Sr25519Public, Signature as Sr25519Signature};
299+
use sp_core::U256;
300+
use sp_runtime::{
301+
app_crypto::{app_crypto, sr25519},
302+
traits::IdentifyAccount,
303+
};
304+
305+
app_crypto!(sr25519, KEY_TYPE);
306+
307+
pub struct TestAuthId;
308+
309+
impl frame_system::offchain::AppCrypto<Public, Signature> for TestAuthId {
310+
type RuntimeAppPublic = Public;
311+
type GenericSignature = Sr25519Signature;
312+
type GenericPublic = Sr25519Public;
313+
}
314+
315+
impl IdentifyAccount for Public {
316+
type AccountId = U256;
317+
318+
fn into_account(self) -> U256 {
319+
let mut bytes = [0u8; 32];
320+
bytes.copy_from_slice(self.as_ref());
321+
U256::from_big_endian(&bytes)
322+
}
323+
}
324+
}
325+
326+
impl frame_system::offchain::CreateSignedTransaction<pallet_drand::Call<Test>> for Test {
327+
fn create_transaction<C: frame_system::offchain::AppCrypto<Self::Public, Self::Signature>>(
328+
call: RuntimeCall,
329+
_public: Self::Public,
330+
_account: Self::AccountId,
331+
nonce: Index,
332+
) -> Option<(
333+
RuntimeCall,
334+
<UncheckedExtrinsic as sp_runtime::traits::Extrinsic>::SignaturePayload,
335+
)> {
336+
Some((call, (nonce, ())))
337+
}
338+
}
339+
340+
impl<C> frame_system::offchain::SendTransactionTypes<C> for Test
341+
where
342+
RuntimeCall: From<C>,
343+
{
344+
type Extrinsic = UncheckedExtrinsic;
345+
type OverarchingCall = RuntimeCall;
346+
}
347+
275348
// Build genesis storage according to the mock runtime.
276349
pub fn new_test_ext() -> sp_io::TestExternalities {
277350
sp_tracing::try_init_simple();

pallets/subtensor/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ tle = { workspace = true, default-features = false }
5050
ark-bls12-381 = { workspace = true, default-features = false }
5151
ark-serialize = { workspace = true, default-features = false }
5252
w3f-bls = { workspace = true, default-features = false }
53+
sha2 = { workspace = true }
54+
rand_chacha = { workspace = true }
5355

5456
[dev-dependencies]
5557
pallet-balances = { workspace = true, features = ["std"] }

pallets/subtensor/src/coinbase/run_coinbase.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,8 @@ impl<T: Config> Pallet<T> {
220220
return Ok(());
221221
}
222222

223-
// This fn is run at the VERY BEGINNING of epoch `n`, therefore the weights revealed
224-
// must have been committed during epoch `n-2`.
225-
let reveal_epoch = cur_epoch.saturating_sub(2);
223+
// Weights revealed must have been committed during epoch `cur_epoch - reveal_period`.
224+
let reveal_epoch = cur_epoch.saturating_sub(Self::get_reveal_period(netuid));
226225

227226
let mut entries = CRV3WeightCommits::<T>::take(netuid, reveal_epoch);
228227

pallets/subtensor/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,10 +1174,6 @@ pub mod pallet {
11741174
StorageMap<_, Identity, u16, Vec<u64>, ValueQuery, EmptyU64Vec<T>>;
11751175
#[pallet::storage]
11761176
/// --- DMAP ( netuid ) --> last_update
1177-
pub type LastCRV3Update<T: Config> =
1178-
StorageMap<_, Identity, u16, Vec<u64>, ValueQuery, EmptyU64Vec<T>>;
1179-
#[pallet::storage]
1180-
/// --- DMAP ( netuid ) --> last_update
11811177
pub type LastUpdate<T: Config> =
11821178
StorageMap<_, Identity, u16, Vec<u64>, ValueQuery, EmptyU64Vec<T>>;
11831179
#[pallet::storage]

pallets/subtensor/src/subnets/weights.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ impl<T: Config> Pallet<T> {
201201
));
202202

203203
// 9. Update the last commit block for the hotkey's UID.
204-
Self::set_last_crv3_update_for_uid(netuid, neuron_uid, commit_block);
204+
Self::set_last_update_for_uid(netuid, neuron_uid, commit_block);
205205

206206
// 10. Return success.
207207
Ok(())
@@ -727,7 +727,7 @@ impl<T: Config> Pallet<T> {
727727
pub fn check_crv3_rate_limit(netuid: u16, neuron_uid: u16, current_block: u64) -> bool {
728728
if Self::is_uid_exist_on_network(netuid, neuron_uid) {
729729
// --- 1. Ensure that the diff between current and last_set weights is greater than limit.
730-
let last_set_weights: u64 = Self::get_last_crv3_update_for_uid(netuid, neuron_uid);
730+
let last_set_weights: u64 = Self::get_last_update_for_uid(netuid, neuron_uid);
731731
if last_set_weights == 0 {
732732
return true;
733733
} // (Storage default) Never set weights.

pallets/subtensor/src/utils/misc.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,6 @@ impl<T: Config> Pallet<T> {
9090
pub fn get_dividends(netuid: u16) -> Vec<u16> {
9191
Dividends::<T>::get(netuid)
9292
}
93-
pub fn get_last_crv3_update(netuid: u16) -> Vec<u64> {
94-
LastCRV3Update::<T>::get(netuid)
95-
}
9693
pub fn get_last_update(netuid: u16) -> Vec<u64> {
9794
LastUpdate::<T>::get(netuid)
9895
}
@@ -109,16 +106,8 @@ impl<T: Config> Pallet<T> {
109106
// ==================================
110107
// ==== YumaConsensus UID params ====
111108
// ==================================
112-
pub fn set_last_crv3_update_for_uid(netuid: u16, uid: u16, last_update: u64) {
113-
let mut updated_last_update_vec = Self::get_last_crv3_update(netuid);
114-
let Some(updated_last_update) = updated_last_update_vec.get_mut(uid as usize) else {
115-
return;
116-
};
117-
*updated_last_update = last_update;
118-
LastCRV3Update::<T>::insert(netuid, updated_last_update_vec);
119-
}
120109
pub fn set_last_update_for_uid(netuid: u16, uid: u16, last_update: u64) {
121-
let mut updated_last_update_vec = Self::get_last_crv3_update(netuid);
110+
let mut updated_last_update_vec = Self::get_last_update(netuid);
122111
let Some(updated_last_update) = updated_last_update_vec.get_mut(uid as usize) else {
123112
return;
124113
};
@@ -208,10 +197,6 @@ impl<T: Config> Pallet<T> {
208197
let vec = Dividends::<T>::get(netuid);
209198
vec.get(uid as usize).copied().unwrap_or(0)
210199
}
211-
pub fn get_last_crv3_update_for_uid(netuid: u16, uid: u16) -> u64 {
212-
let vec = LastCRV3Update::<T>::get(netuid);
213-
vec.get(uid as usize).copied().unwrap_or(0)
214-
}
215200
pub fn get_last_update_for_uid(netuid: u16, uid: u16) -> u64 {
216201
let vec = LastUpdate::<T>::get(netuid);
217202
vec.get(uid as usize).copied().unwrap_or(0)
@@ -412,6 +397,9 @@ impl<T: Config> Pallet<T> {
412397
Self::deposit_event(Event::WeightsVersionKeySet(netuid, weights_version_key));
413398
}
414399

400+
pub fn set_v3_weights_set_rate_limit(netuid: u16, weights_set_rate_limit: u64) {
401+
V3WeightsSetRateLimit::<T>::insert(netuid, weights_set_rate_limit);
402+
}
415403
pub fn get_v3_weights_set_rate_limit(netuid: u16) -> u64 {
416404
V3WeightsSetRateLimit::<T>::get(netuid)
417405
}

0 commit comments

Comments
 (0)