Skip to content

Commit 28780d9

Browse files
committed
Merge remote-tracking branch 'origin/devnet-ready' into feat/batch-set-weights
2 parents 8d4adf9 + 51cdd9c commit 28780d9

File tree

24 files changed

+106029
-610
lines changed

24 files changed

+106029
-610
lines changed

Cargo.lock

Lines changed: 141 additions & 135 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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ 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-evm-chain-id = { workspace = true }
3233
pallet-drand = { workspace = true, default-features = false }
3334

34-
3535
[dev-dependencies]
3636
sp-core = { workspace = true }
3737
sp-io = { workspace = true }
@@ -52,6 +52,7 @@ std = [
5252
"pallet-subtensor/std",
5353
"sp-consensus-aura/std",
5454
"pallet-balances/std",
55+
"pallet-evm-chain-id/std",
5556
"pallet-scheduler/std",
5657
"sp-runtime/std",
5758
"sp-tracing/std",
@@ -77,6 +78,7 @@ try-runtime = [
7778
"frame-support/try-runtime",
7879
"frame-system/try-runtime",
7980
"pallet-balances/try-runtime",
81+
"pallet-evm-chain-id/try-runtime",
8082
"pallet-scheduler/try-runtime",
8183
"sp-runtime/try-runtime",
8284
"pallet-subtensor/try-runtime",

pallets/admin-utils/src/lib.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub mod pallet {
2020
use frame_support::pallet_prelude::*;
2121
use frame_support::traits::tokens::Balance;
2222
use frame_system::pallet_prelude::*;
23+
use pallet_evm_chain_id::{self, ChainId};
2324
use sp_runtime::BoundedVec;
2425

2526
/// The main data structure of the module.
@@ -29,7 +30,11 @@ pub mod pallet {
2930

3031
/// Configure the pallet by specifying the parameters and types on which it depends.
3132
#[pallet::config]
32-
pub trait Config: frame_system::Config + pallet_subtensor::pallet::Config {
33+
pub trait Config:
34+
frame_system::Config
35+
+ pallet_subtensor::pallet::Config
36+
+ pallet_evm_chain_id::pallet::Config
37+
{
3338
/// Because this pallet emits events, it depends on the runtime's definition of an event.
3439
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
3540

@@ -885,9 +890,9 @@ pub mod pallet {
885890
/// The extrinsic will call the Subtensor pallet to set the weights min stake.
886891
#[pallet::call_index(42)]
887892
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
888-
pub fn sudo_set_weights_min_stake(origin: OriginFor<T>, min_stake: u64) -> DispatchResult {
893+
pub fn sudo_set_stake_threshold(origin: OriginFor<T>, min_stake: u64) -> DispatchResult {
889894
ensure_root(origin)?;
890-
pallet_subtensor::Pallet::<T>::set_weights_min_stake(min_stake);
895+
pallet_subtensor::Pallet::<T>::set_stake_threshold(min_stake);
891896
Ok(())
892897
}
893898

@@ -1212,6 +1217,27 @@ pub mod pallet {
12121217
);
12131218
Ok(())
12141219
}
1220+
1221+
/// Sets the EVM ChainID.
1222+
///
1223+
/// # Arguments
1224+
/// * `origin` - The origin of the call, which must be the subnet owner or the root account.
1225+
/// * `chainId` - The u64 chain ID
1226+
///
1227+
/// # Errors
1228+
/// * `BadOrigin` - If the caller is neither the subnet owner nor the root account.
1229+
///
1230+
/// # Weight
1231+
/// Weight is handled by the `#[pallet::weight]` attribute.
1232+
#[pallet::call_index(58)]
1233+
#[pallet::weight(<T as Config>::WeightInfo::sudo_set_evm_chain_id())]
1234+
pub fn sudo_set_evm_chain_id(origin: OriginFor<T>, chain_id: u64) -> DispatchResult {
1235+
// Ensure the call is made by the root account
1236+
ensure_root(origin)?;
1237+
1238+
ChainId::<T>::set(chain_id);
1239+
Ok(())
1240+
}
12151241
}
12161242
}
12171243

pallets/admin-utils/src/tests/mock.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ frame_support::construct_runtime!(
2929
SubtensorModule: pallet_subtensor::{Pallet, Call, Storage, Event<T>, Error<T>} = 4,
3030
Scheduler: pallet_scheduler::{Pallet, Call, Storage, Event<T>} = 5,
3131
Drand: pallet_drand::{Pallet, Call, Storage, Event<T>} = 6,
32+
EVMChainId: pallet_evm_chain_id = 7,
3233
}
3334
);
3435

@@ -277,6 +278,7 @@ impl pallet_scheduler::Config for Test {
277278
type Preimages = ();
278279
}
279280

281+
impl pallet_evm_chain_id::Config for Test {}
280282
impl pallet_drand::Config for Test {
281283
type RuntimeEvent = RuntimeEvent;
282284
type WeightInfo = pallet_drand::weights::SubstrateWeight<Test>;

pallets/admin-utils/src/tests/mod.rs

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -682,23 +682,23 @@ fn test_sudo_set_max_allowed_validators() {
682682
}
683683

684684
#[test]
685-
fn test_sudo_set_weights_min_stake() {
685+
fn test_sudo_set_stake_threshold() {
686686
new_test_ext().execute_with(|| {
687687
let to_be_set: u64 = 10;
688-
let init_value: u64 = SubtensorModule::get_weights_min_stake();
688+
let init_value: u64 = SubtensorModule::get_stake_threshold();
689689
assert_eq!(
690-
AdminUtils::sudo_set_weights_min_stake(
690+
AdminUtils::sudo_set_stake_threshold(
691691
<<Test as Config>::RuntimeOrigin>::signed(U256::from(1)),
692692
to_be_set
693693
),
694694
Err(DispatchError::BadOrigin)
695695
);
696-
assert_eq!(SubtensorModule::get_weights_min_stake(), init_value);
697-
assert_ok!(AdminUtils::sudo_set_weights_min_stake(
696+
assert_eq!(SubtensorModule::get_stake_threshold(), init_value);
697+
assert_ok!(AdminUtils::sudo_set_stake_threshold(
698698
<<Test as Config>::RuntimeOrigin>::root(),
699699
to_be_set
700700
));
701-
assert_eq!(SubtensorModule::get_weights_min_stake(), to_be_set);
701+
assert_eq!(SubtensorModule::get_stake_threshold(), to_be_set);
702702
});
703703
}
704704

@@ -1433,3 +1433,36 @@ fn sudo_set_commit_reveal_weights_interval() {
14331433
assert_eq!(SubtensorModule::get_reveal_period(netuid), to_be_set);
14341434
});
14351435
}
1436+
1437+
#[test]
1438+
fn test_sudo_root_sets_evm_chain_id() {
1439+
new_test_ext().execute_with(|| {
1440+
let chain_id: u64 = 945;
1441+
assert_eq!(pallet_evm_chain_id::ChainId::<Test>::get(), 0);
1442+
1443+
assert_ok!(AdminUtils::sudo_set_evm_chain_id(
1444+
<<Test as Config>::RuntimeOrigin>::root(),
1445+
chain_id
1446+
));
1447+
1448+
assert_eq!(pallet_evm_chain_id::ChainId::<Test>::get(), chain_id);
1449+
});
1450+
}
1451+
1452+
#[test]
1453+
fn test_sudo_non_root_cannot_set_evm_chain_id() {
1454+
new_test_ext().execute_with(|| {
1455+
let chain_id: u64 = 945;
1456+
assert_eq!(pallet_evm_chain_id::ChainId::<Test>::get(), 0);
1457+
1458+
assert_eq!(
1459+
AdminUtils::sudo_set_evm_chain_id(
1460+
<<Test as Config>::RuntimeOrigin>::signed(U256::from(0)),
1461+
chain_id
1462+
),
1463+
Err(DispatchError::BadOrigin)
1464+
);
1465+
1466+
assert_eq!(pallet_evm_chain_id::ChainId::<Test>::get(), 0);
1467+
});
1468+
}

pallets/admin-utils/src/weights.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ pub trait WeightInfo {
6262
fn sudo_set_tempo() -> Weight;
6363
fn sudo_set_commit_reveal_weights_interval() -> Weight;
6464
fn sudo_set_commit_reveal_weights_enabled() -> Weight;
65+
fn sudo_set_evm_chain_id() -> Weight;
6566
}
6667

6768
/// Weights for `pallet_admin_utils` using the Substrate node and recommended hardware.
@@ -431,6 +432,10 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
431432
.saturating_add(T::DbWeight::get().reads(1_u64))
432433
.saturating_add(T::DbWeight::get().writes(1_u64))
433434
}
435+
fn sudo_set_evm_chain_id() -> Weight {
436+
Weight::from_parts(20_200_000, 0)
437+
.saturating_add(RocksDbWeight::get().writes(1_u64))
438+
}
434439
}
435440

436441
// For backwards compatibility and tests.
@@ -805,4 +810,8 @@ impl WeightInfo for () {
805810
.saturating_add(RocksDbWeight::get().reads(1_u64))
806811
.saturating_add(RocksDbWeight::get().writes(1_u64))
807812
}
813+
fn sudo_set_evm_chain_id() -> Weight {
814+
Weight::from_parts(20_200_000, 0)
815+
.saturating_add(RocksDbWeight::get().writes(1_u64))
816+
}
808817
}

pallets/subtensor/src/coinbase/root.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ impl<T: Config> Pallet<T> {
784784

785785
// Check to see if the hotkey has enough stake to set weights.
786786
ensure!(
787-
Self::get_total_stake_for_hotkey(&hotkey) >= Self::get_weights_min_stake(),
787+
Self::get_total_stake_for_hotkey(&hotkey) >= Self::get_stake_threshold(),
788788
Error::<T>::NotEnoughStakeToSetWeights
789789
);
790790

pallets/subtensor/src/coinbase/run_coinbase.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ impl<T: Config> Pallet<T> {
166166
);
167167
log::debug!("Accumulated emissions on hotkey {:?} for netuid {:?}: mining {:?}, validator {:?}", hotkey, *netuid, mining_emission, validator_emission);
168168
}
169+
170+
// 4.5 Apply pending childkeys of this subnet for the next epoch
171+
Self::do_set_pending_children(*netuid);
169172
} else {
170173
// No epoch, increase blocks since last step and continue
171174
Self::set_blocks_since_last_step(

pallets/subtensor/src/lib.rs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,11 @@ pub mod pallet {
311311
vec![]
312312
}
313313
#[pallet::type_value]
314+
/// Default pending childkeys
315+
pub fn DefaultPendingChildkeys<T: Config>() -> (Vec<(u64, T::AccountId)>, u64) {
316+
(vec![], 0)
317+
}
318+
#[pallet::type_value]
314319
/// Default account linkage
315320
pub fn DefaultProportion<T: Config>() -> u64 {
316321
0
@@ -576,7 +581,7 @@ pub mod pallet {
576581
}
577582
#[pallet::type_value]
578583
/// Default minimum stake for weights.
579-
pub fn DefaultWeightsMinStake<T: Config>() -> u64 {
584+
pub fn DefaultStakeThreshold<T: Config>() -> u64 {
580585
0
581586
}
582587
#[pallet::type_value]
@@ -677,6 +682,18 @@ pub mod pallet {
677682
T::InitialColdkeySwapScheduleDuration::get()
678683
}
679684

685+
#[pallet::type_value]
686+
/// Default value for applying pending items (e.g. childkeys).
687+
pub fn DefaultPendingCooldown<T: Config>() -> u64 {
688+
7200
689+
}
690+
691+
#[pallet::type_value]
692+
/// Default minimum stake for setting childkeys.
693+
pub fn DefaultChildkeysMinStake<T: Config>() -> u64 {
694+
1_000_000_000_000
695+
}
696+
680697
#[pallet::storage]
681698
pub type ColdkeySwapScheduleDuration<T: Config> =
682699
StorageValue<_, BlockNumberFor<T>, ValueQuery, DefaultColdkeySwapScheduleDuration<T>>;
@@ -824,6 +841,18 @@ pub mod pallet {
824841
DefaultStakeDelta<T>,
825842
>;
826843
#[pallet::storage]
844+
/// DMAP ( netuid, parent ) --> (Vec<(proportion,child)>, cool_down_block)
845+
pub type PendingChildKeys<T: Config> = StorageDoubleMap<
846+
_,
847+
Identity,
848+
u16,
849+
Blake2_128Concat,
850+
T::AccountId,
851+
(Vec<(u64, T::AccountId)>, u64),
852+
ValueQuery,
853+
DefaultPendingChildkeys<T>,
854+
>;
855+
#[pallet::storage]
827856
/// DMAP ( parent, netuid ) --> Vec<(proportion,child)>
828857
pub type ChildKeys<T: Config> = StorageDoubleMap<
829858
_,
@@ -1270,7 +1299,7 @@ pub mod pallet {
12701299
StorageMap<_, Identity, T::AccountId, u64, ValueQuery, DefaultLastTxBlock<T>>;
12711300
#[pallet::storage]
12721301
/// ITEM( weights_min_stake )
1273-
pub type WeightsMinStake<T> = StorageValue<_, u64, ValueQuery, DefaultWeightsMinStake<T>>;
1302+
pub type StakeThreshold<T> = StorageValue<_, u64, ValueQuery, DefaultStakeThreshold<T>>;
12741303
#[pallet::storage]
12751304
/// --- MAP (netuid, who) --> VecDeque<(hash, commit_block, first_reveal_block, last_reveal_block)> | Stores a queue of commits for an account on a given netuid.
12761305
pub type WeightCommits<T: Config> = StorageDoubleMap<
@@ -1342,7 +1371,7 @@ pub mod pallet {
13421371
/// Is the caller allowed to set weights
13431372
pub fn check_weights_min_stake(hotkey: &T::AccountId, netuid: u16) -> bool {
13441373
// Blacklist weights transactions for low stake peers.
1345-
Self::get_stake_for_hotkey_on_subnet(hotkey, netuid) >= Self::get_weights_min_stake()
1374+
Self::get_stake_for_hotkey_on_subnet(hotkey, netuid) >= Self::get_stake_threshold()
13461375
}
13471376

13481377
/// Helper function to check if register is allowed

pallets/subtensor/src/macros/dispatches.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1269,7 +1269,7 @@ mod dispatches {
12691269
netuid: u16,
12701270
children: Vec<(u64, T::AccountId)>,
12711271
) -> DispatchResultWithPostInfo {
1272-
Self::do_set_children(origin, hotkey, netuid, children)?;
1272+
Self::do_schedule_children(origin, hotkey, netuid, children)?;
12731273
Ok(().into())
12741274
}
12751275

0 commit comments

Comments
 (0)