Skip to content

Commit 5742411

Browse files
authored
Merge pull request #1376 from opentensor/chore/precompile-try-execute
Add try_execute to PrecompileExt
2 parents 4b2a266 + 40e0b87 commit 5742411

File tree

2 files changed

+40
-51
lines changed

2 files changed

+40
-51
lines changed

precompiles/src/extensions.rs

Lines changed: 28 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,8 +128,29 @@ 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

151+
// allowing unreachable for the whole module fixes clippy reports about precompile macro
152+
// implementation for `TestPrecompile`, that couldn't be fixed granularly
153+
#[allow(unreachable_code)]
132154
#[cfg(test)]
133155
mod test {
134156
use super::*;
@@ -152,4 +174,7 @@ mod test {
152174
impl PrecompileExt<AccountId32> for TestPrecompile {
153175
const INDEX: u64 = 2051;
154176
}
177+
178+
#[precompile_utils::precompile]
179+
impl TestPrecompile {}
155180
}

precompiles/src/lib.rs

Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use core::marker::PhantomData;
66

77
use frame_support::dispatch::{GetDispatchInfo, PostDispatchInfo};
88
use pallet_evm::{
9-
AddressMapping, ExitError, IsPrecompileResult, Precompile, PrecompileFailure, PrecompileHandle,
10-
PrecompileResult, PrecompileSet,
9+
AddressMapping, IsPrecompileResult, Precompile, PrecompileHandle, PrecompileResult,
10+
PrecompileSet,
1111
};
1212
use pallet_evm_precompile_modexp::Modexp;
1313
use pallet_evm_precompile_sha3fips::Sha3FIPS256;
@@ -17,7 +17,7 @@ use sp_runtime::traits::Dispatchable;
1717
use sp_runtime::traits::StaticLookup;
1818
use subtensor_runtime_common::ProxyType;
1919

20-
use pallet_admin_utils::{PrecompileEnable, PrecompileEnum};
20+
use pallet_admin_utils::PrecompileEnum;
2121

2222
use crate::balance_transfer::*;
2323
use crate::ed25519::*;
@@ -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+
StakingPrecompileV2::<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)