Skip to content

Commit 6cd22b4

Browse files
committed
Merge remote-tracking branch 'origin/devnet-ready' into sam-fix-try-runtime
2 parents 35a87a6 + dbbfb61 commit 6cd22b4

File tree

20 files changed

+962
-60
lines changed

20 files changed

+962
-60
lines changed

.github/workflows/require-clean-merges.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ jobs:
4646
git checkout $PR_BRANCH
4747
git reset --hard origin/$PR_BRANCH
4848
49+
# Configure a temporary Git identity to allow merging
50+
git config --local user.email "[email protected]"
51+
git config --local user.name "GitHub Actions"
52+
4953
for branch in $MERGE_BRANCHES; do
5054
echo "Checking merge from $branch into $PR_BRANCH..."
5155

pallets/admin-utils/src/lib.rs

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ mod tests;
2323
#[frame_support::pallet]
2424
pub mod pallet {
2525
use super::*;
26-
use frame_support::dispatch::DispatchResult;
2726
use frame_support::pallet_prelude::*;
2827
use frame_support::traits::tokens::Balance;
28+
use frame_support::{dispatch::DispatchResult, pallet_prelude::StorageMap};
2929
use frame_system::pallet_prelude::*;
3030
use pallet_evm_chain_id::{self, ChainId};
3131
use sp_runtime::BoundedVec;
@@ -69,7 +69,16 @@ pub mod pallet {
6969
}
7070

7171
#[pallet::event]
72-
pub enum Event<T: Config> {}
72+
#[pallet::generate_deposit(pub(super) fn deposit_event)]
73+
pub enum Event<T: Config> {
74+
/// Event emitted when a precompile operation is updated.
75+
PrecompileUpdated {
76+
/// The type of precompile operation being updated.
77+
precompile_id: PrecompileEnum,
78+
/// Indicates if the precompile operation is enabled or not.
79+
enabled: bool,
80+
},
81+
}
7382

7483
// Errors inform users that something went wrong.
7584
#[pallet::error]
@@ -81,6 +90,37 @@ pub mod pallet {
8190
/// The maximum number of subnet validators must be more than the current number of UIDs already in the subnet.
8291
MaxAllowedUIdsLessThanCurrentUIds,
8392
}
93+
/// Enum for specifying the type of precompile operation.
94+
#[derive(Encode, Decode, TypeInfo, Clone, PartialEq, Eq, Debug, Copy)]
95+
pub enum PrecompileEnum {
96+
/// Enum for balance transfer precompile
97+
BalanceTransfer,
98+
/// Enum for staking precompile
99+
Staking,
100+
/// Enum for subnet precompile
101+
Subnet,
102+
/// Enum for metagraph precompile
103+
Metagraph,
104+
/// Enum for neuron precompile
105+
Neuron,
106+
}
107+
108+
#[pallet::type_value]
109+
/// Default value for precompile enable
110+
pub fn DefaultPrecompileEnabled<T: Config>() -> bool {
111+
true
112+
}
113+
114+
#[pallet::storage]
115+
/// Map PrecompileEnum --> enabled
116+
pub type PrecompileEnable<T: Config> = StorageMap<
117+
_,
118+
Blake2_128Concat,
119+
PrecompileEnum,
120+
bool,
121+
ValueQuery,
122+
DefaultPrecompileEnabled<T>,
123+
>;
84124

85125
/// Dispatchable functions allows users to interact with the pallet and invoke state changes.
86126
#[pallet::call]
@@ -613,7 +653,7 @@ pub mod pallet {
613653
netuid: u16,
614654
difficulty: u64,
615655
) -> DispatchResult {
616-
pallet_subtensor::Pallet::<T>::ensure_subnet_owner_or_root(origin, netuid)?;
656+
ensure_root(origin)?;
617657
ensure!(
618658
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
619659
Error::<T>::SubnetDoesNotExist
@@ -1307,6 +1347,36 @@ pub mod pallet {
13071347
pallet_subtensor::Pallet::<T>::ensure_subnet_owner_or_root(origin, netuid)?;
13081348
pallet_subtensor::Pallet::<T>::toggle_transfer(netuid, toggle)
13091349
}
1350+
1351+
/// Toggles the enablement of an EVM precompile.
1352+
///
1353+
/// # Arguments
1354+
/// * `origin` - The origin of the call, which must be the root account.
1355+
/// * `precompile_id` - The identifier of the EVM precompile to toggle.
1356+
/// * `enabled` - The new enablement state of the precompile.
1357+
///
1358+
/// # Errors
1359+
/// * `BadOrigin` - If the caller is not the root account.
1360+
///
1361+
/// # Weight
1362+
/// Weight is handled by the `#[pallet::weight]` attribute.
1363+
#[pallet::call_index(62)]
1364+
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
1365+
pub fn sudo_toggle_evm_precompile(
1366+
origin: OriginFor<T>,
1367+
precompile_id: PrecompileEnum,
1368+
enabled: bool,
1369+
) -> DispatchResult {
1370+
ensure_root(origin)?;
1371+
if PrecompileEnable::<T>::get(precompile_id) != enabled {
1372+
PrecompileEnable::<T>::insert(precompile_id, enabled);
1373+
Self::deposit_event(Event::PrecompileUpdated {
1374+
precompile_id,
1375+
enabled,
1376+
});
1377+
}
1378+
Ok(())
1379+
}
13101380
}
13111381
}
13121382

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ pub(crate) fn run_to_block(n: u64) {
392392
while System::block_number() < n {
393393
SubtensorModule::on_finalize(System::block_number());
394394
System::on_finalize(System::block_number());
395+
System::reset_events();
395396
System::set_block_number(System::block_number() + 1);
396397
System::on_initialize(System::block_number());
397398
SubtensorModule::on_initialize(System::block_number());

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

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use pallet_subtensor::Event;
1111
use sp_consensus_grandpa::AuthorityId as GrandpaId;
1212
use sp_core::{ed25519, Pair, U256};
1313

14+
use crate::pallet::PrecompileEnable;
1415
use crate::Error;
1516
use mock::*;
1617

@@ -642,6 +643,18 @@ fn test_sudo_set_difficulty() {
642643
to_be_set
643644
));
644645
assert_eq!(SubtensorModule::get_difficulty_as_u64(netuid), to_be_set);
646+
647+
// Test that SN owner can't set difficulty
648+
pallet_subtensor::SubnetOwner::<Test>::insert(netuid, U256::from(1));
649+
assert_eq!(
650+
AdminUtils::sudo_set_difficulty(
651+
<<Test as Config>::RuntimeOrigin>::signed(U256::from(1)),
652+
netuid,
653+
init_value
654+
),
655+
Err(DispatchError::BadOrigin)
656+
);
657+
assert_eq!(SubtensorModule::get_difficulty_as_u64(netuid), to_be_set); // no change
645658
});
646659
}
647660

@@ -1365,3 +1378,74 @@ fn test_schedule_grandpa_change() {
13651378
assert_eq!(Grandpa::grandpa_authorities(), vec![(bob, 1)]);
13661379
});
13671380
}
1381+
1382+
#[test]
1383+
fn test_sudo_toggle_evm_precompile() {
1384+
new_test_ext().execute_with(|| {
1385+
let precompile_id = crate::PrecompileEnum::BalanceTransfer;
1386+
let initial_enabled = PrecompileEnable::<Test>::get(precompile_id);
1387+
assert!(initial_enabled); // Assuming the default is true
1388+
1389+
run_to_block(1);
1390+
1391+
assert_eq!(
1392+
AdminUtils::sudo_toggle_evm_precompile(
1393+
<<Test as Config>::RuntimeOrigin>::signed(U256::from(0)),
1394+
precompile_id,
1395+
false
1396+
),
1397+
Err(DispatchError::BadOrigin)
1398+
);
1399+
1400+
assert_ok!(AdminUtils::sudo_toggle_evm_precompile(
1401+
RuntimeOrigin::root(),
1402+
precompile_id,
1403+
false
1404+
));
1405+
1406+
assert_eq!(
1407+
System::events()
1408+
.iter()
1409+
.filter(|r| r.event
1410+
== RuntimeEvent::AdminUtils(crate::Event::PrecompileUpdated {
1411+
precompile_id,
1412+
enabled: false
1413+
}))
1414+
.count(),
1415+
1
1416+
);
1417+
1418+
let updated_enabled = PrecompileEnable::<Test>::get(precompile_id);
1419+
assert!(!updated_enabled);
1420+
1421+
run_to_block(2);
1422+
1423+
assert_ok!(AdminUtils::sudo_toggle_evm_precompile(
1424+
RuntimeOrigin::root(),
1425+
precompile_id,
1426+
false
1427+
));
1428+
1429+
// no event without status change
1430+
assert_eq!(
1431+
System::events()
1432+
.iter()
1433+
.filter(|r| r.event
1434+
== RuntimeEvent::AdminUtils(crate::Event::PrecompileUpdated {
1435+
precompile_id,
1436+
enabled: false
1437+
}))
1438+
.count(),
1439+
0
1440+
);
1441+
1442+
assert_ok!(AdminUtils::sudo_toggle_evm_precompile(
1443+
RuntimeOrigin::root(),
1444+
precompile_id,
1445+
true
1446+
));
1447+
1448+
let final_enabled = PrecompileEnable::<Test>::get(precompile_id);
1449+
assert!(final_enabled);
1450+
});
1451+
}

pallets/subtensor/src/lib.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,6 +1674,10 @@ pub enum CustomTransactionError {
16741674
InsufficientLiquidity,
16751675
SlippageTooHigh,
16761676
TransferDisallowed,
1677+
HotKeyNotRegisteredInNetwork,
1678+
InvalidIpAddress,
1679+
ServingRateLimitExceeded,
1680+
InvalidPort,
16771681
BadRequest,
16781682
}
16791683

@@ -1690,6 +1694,10 @@ impl From<CustomTransactionError> for u8 {
16901694
CustomTransactionError::InsufficientLiquidity => 7,
16911695
CustomTransactionError::SlippageTooHigh => 8,
16921696
CustomTransactionError::TransferDisallowed => 9,
1697+
CustomTransactionError::HotKeyNotRegisteredInNetwork => 10,
1698+
CustomTransactionError::InvalidIpAddress => 11,
1699+
CustomTransactionError::ServingRateLimitExceeded => 12,
1700+
CustomTransactionError::InvalidPort => 13,
16931701
CustomTransactionError::BadRequest => 255,
16941702
}
16951703
}
@@ -1773,6 +1781,22 @@ where
17731781
CustomTransactionError::TransferDisallowed.into(),
17741782
)
17751783
.into()),
1784+
Error::<T>::HotKeyNotRegisteredInNetwork => Err(InvalidTransaction::Custom(
1785+
CustomTransactionError::HotKeyNotRegisteredInNetwork.into(),
1786+
)
1787+
.into()),
1788+
Error::<T>::InvalidIpAddress => Err(InvalidTransaction::Custom(
1789+
CustomTransactionError::InvalidIpAddress.into(),
1790+
)
1791+
.into()),
1792+
Error::<T>::ServingRateLimitExceeded => Err(InvalidTransaction::Custom(
1793+
CustomTransactionError::ServingRateLimitExceeded.into(),
1794+
)
1795+
.into()),
1796+
Error::<T>::InvalidPort => Err(InvalidTransaction::Custom(
1797+
CustomTransactionError::InvalidPort.into(),
1798+
)
1799+
.into()),
17761800
_ => Err(
17771801
InvalidTransaction::Custom(CustomTransactionError::BadRequest.into()).into(),
17781802
),
@@ -2175,6 +2199,32 @@ where
21752199
})
21762200
}
21772201
}
2202+
Some(Call::serve_axon {
2203+
netuid,
2204+
version,
2205+
ip,
2206+
port,
2207+
ip_type,
2208+
protocol,
2209+
placeholder1,
2210+
placeholder2,
2211+
}) => {
2212+
// Fully validate the user input
2213+
Self::result_to_validity(
2214+
Pallet::<T>::validate_serve_axon(
2215+
who,
2216+
*netuid,
2217+
*version,
2218+
*ip,
2219+
*port,
2220+
*ip_type,
2221+
*protocol,
2222+
*placeholder1,
2223+
*placeholder2,
2224+
),
2225+
Self::get_priority_vanilla(),
2226+
)
2227+
}
21782228
_ => {
21792229
if let Some(
21802230
BalancesCall::transfer_keep_alive { .. }

pallets/subtensor/src/macros/hooks.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ mod hooks {
8181
// Upgrade identities to V2
8282
.saturating_add(migrations::migrate_identities_v2::migrate_identities_to_v2::<T>())
8383
// Set the min burn across all subnets to a new minimum
84-
.saturating_add(migrations::migrate_set_min_burn::migrate_set_min_burn::<T>());
84+
.saturating_add(migrations::migrate_set_min_burn::migrate_set_min_burn::<T>())
85+
// Set the min difficulty across all subnets to a new minimum
86+
.saturating_add(migrations::migrate_set_min_difficulty::migrate_set_min_difficulty::<T>());
8587
weight
8688
}
8789

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use alloc::string::String;
2+
3+
use frame_support::IterableStorageMap;
4+
use frame_support::{traits::Get, weights::Weight};
5+
6+
use super::*;
7+
8+
pub fn migrate_set_min_difficulty<T: Config>() -> Weight {
9+
let migration_name = b"migrate_set_min_difficulty".to_vec();
10+
11+
// Initialize the weight with one read operation.
12+
let mut weight = T::DbWeight::get().reads(1);
13+
14+
// Check if the migration has already run
15+
if HasMigrationRun::<T>::get(&migration_name) {
16+
log::info!(
17+
"Migration '{:?}' has already run. Skipping.",
18+
migration_name
19+
);
20+
return weight;
21+
}
22+
log::info!(
23+
"Running migration '{}'",
24+
String::from_utf8_lossy(&migration_name)
25+
);
26+
27+
let netuids: Vec<u16> = <NetworksAdded<T> as IterableStorageMap<u16, bool>>::iter()
28+
.map(|(netuid, _)| netuid)
29+
.collect();
30+
weight = weight.saturating_add(T::DbWeight::get().reads(netuids.len() as u64));
31+
32+
for netuid in netuids.iter().clone() {
33+
if *netuid == 0 {
34+
continue;
35+
}
36+
// Set min difficulty to 10 million for all subnets
37+
Pallet::<T>::set_min_difficulty(*netuid, 10_000_000);
38+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
39+
}
40+
41+
// Mark the migration as completed
42+
HasMigrationRun::<T>::insert(&migration_name, true);
43+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
44+
45+
log::info!(
46+
"Migration '{:?}' completed.",
47+
String::from_utf8_lossy(&migration_name)
48+
);
49+
50+
// Return the migration weight.
51+
weight
52+
}

pallets/subtensor/src/migrations/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub mod migrate_populate_owned_hotkeys;
1212
pub mod migrate_populate_staking_hotkeys;
1313
pub mod migrate_rao;
1414
pub mod migrate_set_min_burn;
15+
pub mod migrate_set_min_difficulty;
1516
pub mod migrate_stake_threshold;
1617
pub mod migrate_subnet_volume;
1718
pub mod migrate_to_v1_separate_emission;

0 commit comments

Comments
 (0)