Skip to content

Commit

Permalink
add RemovedPrecompilesAt for filtering a list of removed precompiles
Browse files Browse the repository at this point in the history
  • Loading branch information
RomarQ committed May 22, 2024
1 parent 8979a49 commit 88dd6be
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions precompiles/src/precompile_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ impl<T> From<DiscriminantResult<T>> for IsPrecompileResult {
pub enum PrecompileKind {
Single(H160),
Prefixed(Vec<u8>),
Multiple(Vec<H160>),
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -836,6 +837,68 @@ impl<A> IsActivePrecompile for RevertPrecompile<A> {
}
}

/// Precompiles that were removed from a precompile set.
/// Still considered precompiles but are inactive and always revert.
pub struct RemovedPrecompilesAt<A>(PhantomData<A>);
impl<A> PrecompileSetFragment for RemovedPrecompilesAt<A>
where
A: Get<Vec<H160>>,
{
#[inline(always)]
fn new() -> Self {
Self(PhantomData)
}

#[inline(always)]
fn execute<R: pallet_evm::Config>(
&self,
handle: &mut impl PrecompileHandle,
) -> Option<PrecompileResult> {
if A::get().contains(&handle.code_address()) {
Some(Err(revert("Removed precompile")))
} else {
None
}
}

#[inline(always)]
fn is_precompile(&self, address: H160, _gas: u64) -> IsPrecompileResult {
IsPrecompileResult::Answer {
is_precompile: A::get().contains(&address),
extra_cost: 0,
}
}

#[inline(always)]
fn used_addresses(&self) -> Vec<H160> {
A::get()
}

fn summarize_checks(&self) -> Vec<PrecompileCheckSummary> {
vec![PrecompileCheckSummary {
name: None,
precompile_kind: PrecompileKind::Multiple(A::get()),
recursion_limit: Some(0),
accept_delegate_call: true,
callable_by_smart_contract: "Reverts in all cases".into(),
callable_by_precompile: "Reverts in all cases".into(),
}]
}
}

impl<A> IsActivePrecompile for RemovedPrecompilesAt<A>
where
Self: PrecompileSetFragment,
{
#[inline(always)]
fn is_active_precompile(&self, _address: H160, _gas: u64) -> IsPrecompileResult {
IsPrecompileResult::Answer {
is_precompile: false,
extra_cost: 0,
}
}
}

/// A precompile that was removed from a precompile set.
/// Still considered a precompile but is inactive and always revert.
pub struct RemovedPrecompileAt<A>(PhantomData<A>);
Expand Down

0 comments on commit 88dd6be

Please sign in to comment.