7

Ethereum currently has 9 precompiled contracts available at addresses 0x01-0x09, none of which are state-changing.

But is that a VM requirement? Is it possible to implement a new precompile that does have state?

1 Answer 1

9

The existing precompiled contracts in Ethereum primarily handle specific computational tasks optimized for performance, and they have been stateless/pure mainly for design reasons, not because of inherent EVM limitations.

Here's a breakdown:

  1. Nature of Precompiled Contracts: The existing precompiled contracts are designed to perform certain computational tasks efficiently, such as elliptic curve operations and specific hashing functions. These operations are computationally intensive when executed within the EVM, but they can be optimized when implemented in native code, which is the primary reason for having them as precompiled contracts.

  2. State Management in Ethereum: State in Ethereum is maintained through a key-value store known as the "state trie." Any entity with an address, be it an externally owned account (EOA) or a contract, can have associated state. Theoretically, there's nothing stopping a precompiled contract from having an associated state within this structure, though it's also possible for a precompile to have its state managed externally.

  3. Implementation Considerations:

    • Gas Costs: Interacting with storage in Ethereum is gas-intensive. If a precompiled contract were to have state, the gas costs for accessing and modifying this state would need to be defined.
    • Security: Introducing state to precompiled contracts can potentially open new attack vectors. The implementation would have to be robust and undergo rigorous security evaluations.
    • Protocol Changes: Implementing stateful precompiled contracts will likely necessitate changes to the Ethereum protocol, which would need to go through the EIP process.
  4. Use Cases: For any proposal introducing state to precompiled contracts, there should be a compelling use case. One can argue that standard contracts are already apt for state management, and introducing state to precompiled contracts might complicate the system without clear benefits.

  5. Examples from EVM Tools and EVM Compatible Networks: Foundry is an example of a framework that uses a stateful precompiled contract albeit for a local environment i.e. the Vm CheatCodes Precompile Contract which exposes numerous pure/view/stateful functions via it's solidity interface to efficiently compute something(e.g. addr(uint256 privateKey)), view or manipulate the state of it's local testing environment. There are also live EVM compatible networks that implement stateful precompiled contracts such as Moonbeam's Collective Precompile or Hedera's HTS Precompile

1
  • Whoah. An awesome answer, thanks Commented Sep 16, 2023 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.