Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for stateful precompiles #9507

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

palango
Copy link

@palango palango commented Dec 6, 2024

Motivation

I'm trying to add support for the Celo transfer precompile to anvil. This precompile transfers funds and therefore requires access to the state database.

This also has been requested in #7703.

Solution

Below is what I managed to do so far, along with a couple of questions.

Add the precompile. As far as I can tell I cannot get access to the state from the Env. So I need to use ContextPrecompile instead to access state through InnerEvmContext.

#[derive(Debug, Clone)]
pub struct Transfer;

impl<DB: revm::Database> ContextStatefulPrecompileMut<DB> for Transfer {
    fn call_mut(
        &mut self,
        bytes: &Bytes,
        _gas_price: u64,
        evmctx: &mut revm::InnerEvmContext<DB>,
    ) -> PrecompileResult {
        let amount = U256::from(123);

        evmctx.journaled_state.transfer(&FROM_ADDR, &TO_ADDR, amount, &mut evmctx.db);
        Ok(PrecompileOutput { gas_used: 100, bytes: bytes.clone() })
    }
}

In order to use inject_precompiles(&mut evm, CustomPrecompileFactory.precompiles()); as in the example, a change to the signature of PrecompileFactory is required:

pub trait PrecompileFactory<DB: revm::Database>: Send + Sync + Unpin + Debug {
    /// Returns a set of precompiles to extend the EVM with.
    fn precompiles(&self) -> Vec<(Address, ContextPrecompile<DB>)>;
}

This new generic parameter now leads to errors through the whole codebase which I haven't managed to solve so far.
Is there a simpler way than propagating the generic parameter through the whole codebase? Any ideas or recommendations are appreciated.

@palango palango marked this pull request as ready for review December 10, 2024 12:36
@palango palango changed the title WIP: Support for stateful precompiles Support for stateful precompiles Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

1 participant