Skip to main content

Precompile

Trait Precompile 

pub trait Precompile {
    // Required methods
    fn precompile_id(&self) -> &PrecompileId;
    fn call(
        &self,
        input: PrecompileInput<'_>,
    ) -> Result<PrecompileOutput, PrecompileError>;

    // Provided method
    fn supports_caching(&self) -> bool { ... }
}
Available on crate feature evm only.
Expand description

Trait for implementing precompiled contracts.

Required Methods§

fn precompile_id(&self) -> &PrecompileId

Returns precompile ID.

fn call( &self, input: PrecompileInput<'_>, ) -> Result<PrecompileOutput, PrecompileError>

Execute the precompile with the given input data, gas limit, and caller address.

Provided Methods§

fn supports_caching(&self) -> bool

Returns whether this precompile’s results should be cached.

A cacheable precompile has deterministic output based solely on its input, and the cost of execution is high enough that caching the result is beneficial.

§Default

Returns true by default, indicating the precompile supports caching, as this is what most precompiles benefit from.

§When to return false
  • Non-deterministic precompiles: If the output depends on state or external factors
  • Cheap precompiles: If the cost of computing a cache key (hashing the input) is comparable to just re-executing the precompile (e.g., the identity precompile which simply copies input to output)
§Examples
impl Precompile for MyPrecompile {
    fn call(&self, input: PrecompileInput<'_>) -> PrecompileResult {
        // ...
    }

    fn supports_caching(&self) -> bool {
        false
    }
}

Implementations on Foreign Types§

§

impl<'a, T> Precompile for &'a T
where T: 'a + Precompile + ?Sized,

§

impl<F> Precompile for (&PrecompileId, F)

§

impl<F> Precompile for (PrecompileId, F)

Source§

impl<S> Precompile for CachedPrecompile<S>
where S: Eq + Hash + Debug + Send + Sync + Clone + 'static,

§

impl<T> Precompile for Arc<T>
where T: Precompile + ?Sized,

Implementors§

§

impl Precompile for Precompile

§

impl Precompile for DynPrecompile

§

impl<A, B> Precompile for Either<A, B>
where A: Precompile, B: Precompile,