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

Commit-Reveal-3 Tests and Fixes #1012

Merged
merged 7 commits into from
Nov 23, 2024
Merged
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
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pallets/admin-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ log = { workspace = true }
pallet-subtensor = { version = "4.0.0-dev", default-features = false, path = "../subtensor" }
sp-weights = { workspace = true }
substrate-fixed = { workspace = true }
pallet-drand = { workspace = true, default-features = false }


[dev-dependencies]
Expand Down
28 changes: 28 additions & 0 deletions pallets/admin-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,34 @@ pub mod pallet {
);
Ok(())
}

/// The extrinsic sets the commit-reveal-3 weights set rate limit for a subnet.
/// It is only callable by the root account or subnet owner.
/// The extrinsic will call the Subtensor pallet to set the weights set rate limit.
#[pallet::call_index(58)]
#[pallet::weight(<T as Config>::WeightInfo::sudo_set_weights_set_rate_limit())]
pub fn sudo_set_commit_reveal_3_weights_set_rate_limit(
origin: OriginFor<T>,
netuid: u16,
weights_set_rate_limit: u64,
) -> DispatchResult {
pallet_subtensor::Pallet::<T>::ensure_subnet_owner_or_root(origin, netuid)?;

ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Error::<T>::SubnetDoesNotExist
);
pallet_subtensor::Pallet::<T>::set_v3_weights_set_rate_limit(
netuid,
weights_set_rate_limit,
);
log::debug!(
"WeightsSetRateLimitSet( netuid: {:?} weights_set_rate_limit: {:?} ) ",
netuid,
weights_set_rate_limit
);
Ok(())
}
}
}

Expand Down
79 changes: 76 additions & 3 deletions pallets/admin-utils/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ use sp_consensus_aura::sr25519::AuthorityId as AuraId;
use sp_core::U256;
use sp_core::{ConstU64, H256};
use sp_runtime::{
testing::TestXt,
traits::{BlakeTwo256, ConstU32, IdentityLookup},
BuildStorage, Perbill,
BuildStorage, KeyTypeId, Perbill,
};
use sp_std::cmp::Ordering;
use sp_weights::Weight;
Expand All @@ -21,13 +22,13 @@ type Block = frame_system::mocking::MockBlock<Test>;

// Configure a mock runtime to test the pallet.
frame_support::construct_runtime!(
pub enum Test
{
pub enum Test {
System: frame_system = 1,
Balances: pallet_balances = 2,
AdminUtils: pallet_admin_utils = 3,
SubtensorModule: pallet_subtensor::{Pallet, Call, Storage, Event<T>, Error<T>} = 4,
Scheduler: pallet_scheduler::{Pallet, Call, Storage, Event<T>} = 5,
Drand: pallet_drand::{Pallet, Call, Storage, Event<T>} = 6,
}
);

Expand Down Expand Up @@ -63,6 +64,10 @@ pub type Balance = u64;
#[allow(dead_code)]
pub type BlockNumber = u64;

pub type TestAuthId = test_crypto::TestAuthId;
pub type Index = u64;
pub type UncheckedExtrinsic = TestXt<RuntimeCall, ()>;

parameter_types! {
pub const InitialMinAllowedWeights: u16 = 0;
pub const InitialEmissionValue: u16 = 0;
Expand Down Expand Up @@ -272,6 +277,74 @@ impl pallet_scheduler::Config for Test {
type Preimages = ();
}

impl pallet_drand::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = pallet_drand::weights::SubstrateWeight<Test>;
type AuthorityId = TestAuthId;
type Verifier = pallet_drand::verifier::QuicknetVerifier;
type UnsignedPriority = ConstU64<{ 1 << 20 }>;
type HttpFetchTimeout = ConstU64<1_000>;
}

impl frame_system::offchain::SigningTypes for Test {
type Public = test_crypto::Public;
type Signature = test_crypto::Signature;
}

pub const KEY_TYPE: KeyTypeId = KeyTypeId(*b"test");

mod test_crypto {
use super::KEY_TYPE;
use sp_core::sr25519::{Public as Sr25519Public, Signature as Sr25519Signature};
use sp_core::U256;
use sp_runtime::{
app_crypto::{app_crypto, sr25519},
traits::IdentifyAccount,
};

app_crypto!(sr25519, KEY_TYPE);

pub struct TestAuthId;

impl frame_system::offchain::AppCrypto<Public, Signature> for TestAuthId {
type RuntimeAppPublic = Public;
type GenericSignature = Sr25519Signature;
type GenericPublic = Sr25519Public;
}

impl IdentifyAccount for Public {
type AccountId = U256;

fn into_account(self) -> U256 {
let mut bytes = [0u8; 32];
bytes.copy_from_slice(self.as_ref());
U256::from_big_endian(&bytes)
}
}
}

impl frame_system::offchain::CreateSignedTransaction<pallet_drand::Call<Test>> for Test {
fn create_transaction<C: frame_system::offchain::AppCrypto<Self::Public, Self::Signature>>(
call: RuntimeCall,
_public: Self::Public,
_account: Self::AccountId,
nonce: Index,
) -> Option<(
RuntimeCall,
<UncheckedExtrinsic as sp_runtime::traits::Extrinsic>::SignaturePayload,
)> {
Some((call, (nonce, ())))
}
}

impl<C> frame_system::offchain::SendTransactionTypes<C> for Test
where
RuntimeCall: From<C>,
{
type Extrinsic = UncheckedExtrinsic;
type OverarchingCall = RuntimeCall;
}

// Build genesis storage according to the mock runtime.
pub fn new_test_ext() -> sp_io::TestExternalities {
sp_tracing::try_init_simple();
Expand Down
2 changes: 2 additions & 0 deletions pallets/subtensor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ tle = { workspace = true, default-features = false }
ark-bls12-381 = { workspace = true, default-features = false }
ark-serialize = { workspace = true, default-features = false }
w3f-bls = { workspace = true, default-features = false }
sha2 = { workspace = true }
rand_chacha = { workspace = true }

[dev-dependencies]
pallet-balances = { workspace = true, features = ["std"] }
Expand Down
5 changes: 2 additions & 3 deletions pallets/subtensor/src/coinbase/run_coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,8 @@ impl<T: Config> Pallet<T> {
return Ok(());
}

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

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

Expand Down
4 changes: 0 additions & 4 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1174,10 +1174,6 @@ pub mod pallet {
StorageMap<_, Identity, u16, Vec<u64>, ValueQuery, EmptyU64Vec<T>>;
#[pallet::storage]
/// --- DMAP ( netuid ) --> last_update
pub type LastCRV3Update<T: Config> =
StorageMap<_, Identity, u16, Vec<u64>, ValueQuery, EmptyU64Vec<T>>;
#[pallet::storage]
/// --- DMAP ( netuid ) --> last_update
pub type LastUpdate<T: Config> =
StorageMap<_, Identity, u16, Vec<u64>, ValueQuery, EmptyU64Vec<T>>;
#[pallet::storage]
Expand Down
4 changes: 2 additions & 2 deletions pallets/subtensor/src/subnets/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ impl<T: Config> Pallet<T> {
));

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

// 10. Return success.
Ok(())
Expand Down Expand Up @@ -727,7 +727,7 @@ impl<T: Config> Pallet<T> {
pub fn check_crv3_rate_limit(netuid: u16, neuron_uid: u16, current_block: u64) -> bool {
if Self::is_uid_exist_on_network(netuid, neuron_uid) {
// --- 1. Ensure that the diff between current and last_set weights is greater than limit.
let last_set_weights: u64 = Self::get_last_crv3_update_for_uid(netuid, neuron_uid);
let last_set_weights: u64 = Self::get_last_update_for_uid(netuid, neuron_uid);
if last_set_weights == 0 {
return true;
} // (Storage default) Never set weights.
Expand Down
20 changes: 4 additions & 16 deletions pallets/subtensor/src/utils/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ impl<T: Config> Pallet<T> {
pub fn get_dividends(netuid: u16) -> Vec<u16> {
Dividends::<T>::get(netuid)
}
pub fn get_last_crv3_update(netuid: u16) -> Vec<u64> {
LastCRV3Update::<T>::get(netuid)
}
pub fn get_last_update(netuid: u16) -> Vec<u64> {
LastUpdate::<T>::get(netuid)
}
Expand All @@ -109,16 +106,8 @@ impl<T: Config> Pallet<T> {
// ==================================
// ==== YumaConsensus UID params ====
// ==================================
pub fn set_last_crv3_update_for_uid(netuid: u16, uid: u16, last_update: u64) {
let mut updated_last_update_vec = Self::get_last_crv3_update(netuid);
let Some(updated_last_update) = updated_last_update_vec.get_mut(uid as usize) else {
return;
};
*updated_last_update = last_update;
LastCRV3Update::<T>::insert(netuid, updated_last_update_vec);
}
pub fn set_last_update_for_uid(netuid: u16, uid: u16, last_update: u64) {
let mut updated_last_update_vec = Self::get_last_crv3_update(netuid);
let mut updated_last_update_vec = Self::get_last_update(netuid);
let Some(updated_last_update) = updated_last_update_vec.get_mut(uid as usize) else {
return;
};
Expand Down Expand Up @@ -208,10 +197,6 @@ impl<T: Config> Pallet<T> {
let vec = Dividends::<T>::get(netuid);
vec.get(uid as usize).copied().unwrap_or(0)
}
pub fn get_last_crv3_update_for_uid(netuid: u16, uid: u16) -> u64 {
let vec = LastCRV3Update::<T>::get(netuid);
vec.get(uid as usize).copied().unwrap_or(0)
}
pub fn get_last_update_for_uid(netuid: u16, uid: u16) -> u64 {
let vec = LastUpdate::<T>::get(netuid);
vec.get(uid as usize).copied().unwrap_or(0)
Expand Down Expand Up @@ -412,6 +397,9 @@ impl<T: Config> Pallet<T> {
Self::deposit_event(Event::WeightsVersionKeySet(netuid, weights_version_key));
}

pub fn set_v3_weights_set_rate_limit(netuid: u16, weights_set_rate_limit: u64) {
V3WeightsSetRateLimit::<T>::insert(netuid, weights_set_rate_limit);
}
pub fn get_v3_weights_set_rate_limit(netuid: u16) -> u64 {
V3WeightsSetRateLimit::<T>::get(netuid)
}
Expand Down
Loading
Loading