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 { ... }
}Expand description
Trait for implementing precompiled contracts.
Required Methods§
fn precompile_id(&self) -> &PrecompileId
fn precompile_id(&self) -> &PrecompileId
Returns precompile ID.
fn call(
&self,
input: PrecompileInput<'_>,
) -> Result<PrecompileOutput, PrecompileError>
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
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
}
}