Skip to content

Commit 3d4edcf

Browse files
committed
extrinsic to enable disable precompile
1 parent 8cb62c5 commit 3d4edcf

File tree

4 files changed

+182
-6
lines changed

4 files changed

+182
-6
lines changed

pallets/admin-utils/src/lib.rs

Lines changed: 64 additions & 2 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,39 @@ 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 subnets precompile
105+
Subnets,
106+
/// Enum for neurons precompile
107+
Neurons,
108+
}
109+
110+
#[pallet::type_value]
111+
/// Default value for precompile enable
112+
pub fn DefaultPrecompileEnabled<T: Config>() -> bool {
113+
true
114+
}
115+
116+
#[pallet::storage]
117+
/// Map PrecompileEnum --> enabled
118+
pub type PrecompileEnable<T: Config> = StorageMap<
119+
_,
120+
Blake2_128Concat,
121+
PrecompileEnum,
122+
bool,
123+
ValueQuery,
124+
DefaultPrecompileEnabled<T>,
125+
>;
84126

85127
/// Dispatchable functions allows users to interact with the pallet and invoke state changes.
86128
#[pallet::call]
@@ -1276,6 +1318,26 @@ pub mod pallet {
12761318
ensure_root(origin)?;
12771319
T::Grandpa::schedule_change(next_authorities, in_blocks, forced)
12781320
}
1321+
1322+
/// A public interface for `pallet_grandpa::Pallet::schedule_grandpa_change`.
1323+
#[pallet::call_index(60)]
1324+
#[pallet::weight(Weight::from_parts(46_000_000, 0))]
1325+
pub fn sudo_toggle_evm_precompile(
1326+
origin: OriginFor<T>,
1327+
precompile_id: PrecompileEnum,
1328+
enabled: bool,
1329+
) -> DispatchResult {
1330+
ensure_root(origin)?;
1331+
if PrecompileEnable::<T>::get(precompile_id) != enabled {
1332+
PrecompileEnable::<T>::insert(precompile_id, enabled);
1333+
Self::deposit_event(Event::PrecompileUpdated {
1334+
precompile_id,
1335+
enabled,
1336+
});
1337+
}
1338+
// T::Grandpa::schedule_change(next_authorities, in_blocks, forced)
1339+
Ok(())
1340+
}
12791341
}
12801342
}
12811343

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ pub(crate) fn run_to_block(n: u64) {
390390
while System::block_number() < n {
391391
SubtensorModule::on_finalize(System::block_number());
392392
System::on_finalize(System::block_number());
393+
System::reset_events();
393394
System::set_block_number(System::block_number() + 1);
394395
System::on_initialize(System::block_number());
395396
SubtensorModule::on_initialize(System::block_number());

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

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

13+
use crate::pallet::PrecompileEnable;
1314
use crate::Error;
1415
use mock::*;
1516

@@ -1490,3 +1491,74 @@ fn test_schedule_grandpa_change() {
14901491
assert_eq!(Grandpa::grandpa_authorities(), vec![(bob, 1)]);
14911492
});
14921493
}
1494+
1495+
#[test]
1496+
fn test_sudo_toggle_evm_precompile() {
1497+
new_test_ext().execute_with(|| {
1498+
let precompile_id = crate::PrecompileEnum::BalanceTransfer;
1499+
let initial_enabled = PrecompileEnable::<Test>::get(precompile_id);
1500+
assert_eq!(initial_enabled, true); // Assuming the default is true
1501+
1502+
run_to_block(1);
1503+
1504+
assert_eq!(
1505+
AdminUtils::sudo_toggle_evm_precompile(
1506+
<<Test as Config>::RuntimeOrigin>::signed(U256::from(0)),
1507+
precompile_id,
1508+
false
1509+
),
1510+
Err(DispatchError::BadOrigin)
1511+
);
1512+
1513+
assert_ok!(AdminUtils::sudo_toggle_evm_precompile(
1514+
RuntimeOrigin::root(),
1515+
precompile_id,
1516+
false
1517+
));
1518+
1519+
assert_eq!(
1520+
System::events()
1521+
.iter()
1522+
.filter(|r| r.event
1523+
== RuntimeEvent::AdminUtils(crate::Event::PrecompileUpdated {
1524+
precompile_id,
1525+
enabled: false
1526+
}))
1527+
.count(),
1528+
1
1529+
);
1530+
1531+
let updated_enabled = PrecompileEnable::<Test>::get(precompile_id);
1532+
assert_eq!(updated_enabled, false);
1533+
1534+
run_to_block(2);
1535+
1536+
assert_ok!(AdminUtils::sudo_toggle_evm_precompile(
1537+
RuntimeOrigin::root(),
1538+
precompile_id,
1539+
false
1540+
));
1541+
1542+
// no event without status change
1543+
assert_eq!(
1544+
System::events()
1545+
.iter()
1546+
.filter(|r| r.event
1547+
== RuntimeEvent::AdminUtils(crate::Event::PrecompileUpdated {
1548+
precompile_id,
1549+
enabled: false
1550+
}))
1551+
.count(),
1552+
0
1553+
);
1554+
1555+
assert_ok!(AdminUtils::sudo_toggle_evm_precompile(
1556+
RuntimeOrigin::root(),
1557+
precompile_id,
1558+
true
1559+
));
1560+
1561+
let final_enabled = PrecompileEnable::<Test>::get(precompile_id);
1562+
assert_eq!(final_enabled, true);
1563+
});
1564+
}

runtime/src/precompiles/mod.rs

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ use pallet_evm_precompile_modexp::Modexp;
1010
use pallet_evm_precompile_sha3fips::Sha3FIPS256;
1111
use pallet_evm_precompile_simple::{ECRecover, ECRecoverPublicKey, Identity, Ripemd160, Sha256};
1212

13+
use crate::Runtime;
14+
use pallet_admin_utils::{PrecompileEnable, PrecompileEnum};
15+
1316
// Include custom precompiles
1417
mod balance_transfer;
1518
mod ed25519;
@@ -70,14 +73,52 @@ where
7073
// Non-Frontier specific nor Ethereum precompiles :
7174
a if a == hash(1024) => Some(Sha3FIPS256::execute(handle)),
7275
a if a == hash(1025) => Some(ECRecoverPublicKey::execute(handle)),
73-
a if a == hash(EDVERIFY_PRECOMPILE_INDEX) => Some(Ed25519Verify::execute(handle)),
76+
77+
a if a == hash(EDVERIFY_PRECOMPILE_INDEX) => {
78+
if PrecompileEnable::<Runtime>::get(PrecompileEnum::BalanceTransfer) {
79+
Some(Ed25519Verify::execute(handle))
80+
} else {
81+
Some(Err(PrecompileFailure::Error {
82+
exit_status: ExitError::Other(
83+
"Precompile Ed25519Verify is disabled".into(),
84+
),
85+
}))
86+
}
87+
}
7488
// Subtensor specific precompiles :
7589
a if a == hash(BALANCE_TRANSFER_INDEX) => {
76-
Some(BalanceTransferPrecompile::execute(handle))
90+
if PrecompileEnable::<Runtime>::get(PrecompileEnum::BalanceTransfer) {
91+
Some(BalanceTransferPrecompile::execute(handle))
92+
} else {
93+
Some(Err(PrecompileFailure::Error {
94+
exit_status: ExitError::Other(
95+
"Precompile Balance Transfer is disabled".into(),
96+
),
97+
}))
98+
}
7799
}
78-
a if a == hash(STAKING_PRECOMPILE_INDEX) => Some(StakingPrecompile::execute(handle)),
100+
a if a == hash(STAKING_PRECOMPILE_INDEX) => {
101+
if PrecompileEnable::<Runtime>::get(PrecompileEnum::Staking) {
102+
Some(StakingPrecompile::execute(handle))
103+
} else {
104+
Some(Err(PrecompileFailure::Error {
105+
exit_status: ExitError::Other(
106+
"Precompile Balance Transfer is disabled".into(),
107+
),
108+
}))
109+
}
110+
}
111+
79112
a if a == hash(METAGRAPH_PRECOMPILE_INDEX) => {
80-
Some(MetagraphPrecompile::execute(handle))
113+
if PrecompileEnable::<Runtime>::get(PrecompileEnum::Metagraph) {
114+
Some(MetagraphPrecompile::execute(handle))
115+
} else {
116+
Some(Err(PrecompileFailure::Error {
117+
exit_status: ExitError::Other(
118+
"Precompile Balance Transfer is disabled".into(),
119+
),
120+
}))
121+
}
81122
}
82123

83124
_ => None,

0 commit comments

Comments
 (0)