Skip to content

Commit 24caad5

Browse files
committed
Add try_execute method to PrecompileExt
1 parent 4b2a266 commit 24caad5

File tree

2 files changed

+35
-48
lines changed

2 files changed

+35
-48
lines changed

precompiles/src/extensions.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ use alloc::format;
44

55
use frame_support::dispatch::{GetDispatchInfo, Pays, PostDispatchInfo};
66
use frame_system::RawOrigin;
7+
use pallet_admin_utils::{PrecompileEnable, PrecompileEnum};
78
use pallet_evm::{
8-
AddressMapping, BalanceConverter, ExitError, GasWeightMapping, PrecompileFailure,
9-
PrecompileHandle,
9+
AddressMapping, BalanceConverter, ExitError, GasWeightMapping, Precompile, PrecompileFailure,
10+
PrecompileHandle, PrecompileResult,
1011
};
1112
use precompile_utils::EvmResult;
1213
use sp_core::{H160, U256, blake2_256};
@@ -109,7 +110,7 @@ pub(crate) trait PrecompileHandleExt: PrecompileHandle {
109110

110111
impl<T> PrecompileHandleExt for T where T: PrecompileHandle {}
111112

112-
pub(crate) trait PrecompileExt<AccountId: From<[u8; 32]>> {
113+
pub(crate) trait PrecompileExt<AccountId: From<[u8; 32]>>: Precompile {
113114
const INDEX: u64;
114115

115116
// ss58 public key i.e., the contract sends funds it received to the destination address from
@@ -127,6 +128,24 @@ pub(crate) trait PrecompileExt<AccountId: From<[u8; 32]>> {
127128

128129
hash.into()
129130
}
131+
132+
fn try_execute<R>(
133+
handle: &mut impl PrecompileHandle,
134+
precompile_enum: PrecompileEnum,
135+
) -> Option<PrecompileResult>
136+
where
137+
R: frame_system::Config + pallet_admin_utils::Config,
138+
{
139+
if PrecompileEnable::<R>::get(&precompile_enum) {
140+
Some(Self::execute(handle))
141+
} else {
142+
Some(Err(PrecompileFailure::Error {
143+
exit_status: ExitError::Other(
144+
format!("Precompile {:?} is disabled", precompile_enum).into(),
145+
),
146+
}))
147+
}
148+
}
130149
}
131150

132151
#[cfg(test)]
@@ -152,4 +171,8 @@ mod test {
152171
impl PrecompileExt<AccountId32> for TestPrecompile {
153172
const INDEX: u64 = 2051;
154173
}
174+
175+
#[allow(unreachable_code)]
176+
#[precompile_utils::precompile]
177+
impl TestPrecompile {}
155178
}

precompiles/src/lib.rs

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -138,61 +138,25 @@ where
138138
}
139139
// Subtensor specific precompiles :
140140
a if a == hash(BalanceTransferPrecompile::<R>::INDEX) => {
141-
if PrecompileEnable::<R>::get(PrecompileEnum::BalanceTransfer) {
142-
Some(BalanceTransferPrecompile::<R>::execute(handle))
143-
} else {
144-
Some(Err(PrecompileFailure::Error {
145-
exit_status: ExitError::Other(
146-
"Precompile Balance Transfer is disabled".into(),
147-
),
148-
}))
149-
}
141+
BalanceTransferPrecompile::<R>::try_execute::<R>(
142+
handle,
143+
PrecompileEnum::BalanceTransfer,
144+
)
150145
}
151146
a if a == hash(StakingPrecompile::<R>::INDEX) => {
152-
if PrecompileEnable::<R>::get(PrecompileEnum::Staking) {
153-
Some(StakingPrecompile::<R>::execute(handle))
154-
} else {
155-
Some(Err(PrecompileFailure::Error {
156-
exit_status: ExitError::Other("Precompile Staking is disabled".into()),
157-
}))
158-
}
147+
StakingPrecompile::<R>::try_execute::<R>(handle, PrecompileEnum::Staking)
159148
}
160149
a if a == hash(StakingPrecompileV2::<R>::INDEX) => {
161-
if PrecompileEnable::<R>::get(PrecompileEnum::Staking) {
162-
Some(StakingPrecompileV2::<R>::execute(handle))
163-
} else {
164-
Some(Err(PrecompileFailure::Error {
165-
exit_status: ExitError::Other("Precompile Staking is disabled".into()),
166-
}))
167-
}
150+
StakingPrecompile::<R>::try_execute::<R>(handle, PrecompileEnum::Staking)
168151
}
169-
170152
a if a == hash(SubnetPrecompile::<R>::INDEX) => {
171-
if PrecompileEnable::<R>::get(PrecompileEnum::Subnet) {
172-
Some(SubnetPrecompile::<R>::execute(handle))
173-
} else {
174-
Some(Err(PrecompileFailure::Error {
175-
exit_status: ExitError::Other("Precompile Subnet is disabled".into()),
176-
}))
177-
}
153+
SubnetPrecompile::<R>::try_execute::<R>(handle, PrecompileEnum::Subnet)
178154
}
179155
a if a == hash(MetagraphPrecompile::<R>::INDEX) => {
180-
if PrecompileEnable::<R>::get(PrecompileEnum::Metagraph) {
181-
Some(MetagraphPrecompile::<R>::execute(handle))
182-
} else {
183-
Some(Err(PrecompileFailure::Error {
184-
exit_status: ExitError::Other("Precompile Metagrah is disabled".into()),
185-
}))
186-
}
156+
MetagraphPrecompile::<R>::try_execute::<R>(handle, PrecompileEnum::Metagraph)
187157
}
188158
a if a == hash(NeuronPrecompile::<R>::INDEX) => {
189-
if PrecompileEnable::<R>::get(PrecompileEnum::Neuron) {
190-
Some(NeuronPrecompile::<R>::execute(handle))
191-
} else {
192-
Some(Err(PrecompileFailure::Error {
193-
exit_status: ExitError::Other("Precompile Neuron is disabled".into()),
194-
}))
195-
}
159+
NeuronPrecompile::<R>::try_execute::<R>(handle, PrecompileEnum::Neuron)
196160
}
197161
_ => None,
198162
}

0 commit comments

Comments
 (0)