Convert Figma logo to code with AI

ethereum logopy-evm

A Python implementation of the Ethereum Virtual Machine

2,249
644
2,249
104

Top Related Projects

Substrate: The platform for blockchain innovators

1,474

An enterprise-grade Java-based, Apache 2.0 licensed Ethereum client https://wiki.hyperledger.org/display/besu

(deprecated) The fast, light, and robust client for the Ethereum mainnet.

Go implementation of the Ethereum protocol

DEPRECATED! Java implementation of the Ethereum yellowpaper. For JSON-RPC and other client features check Ethereum Harmony

4,654

A permissioned implementation of Ethereum supporting data privacy

Quick Overview

Py-EVM is a Python implementation of the Ethereum Virtual Machine (EVM). It aims to be a complete and flexible implementation of the EVM for use in a variety of contexts, including Ethereum client development, testing, and research. Py-EVM is designed to be modular and extensible, allowing developers to customize and experiment with different aspects of the EVM.

Pros

  • Highly modular and extensible architecture
  • Written in Python, making it accessible for a wide range of developers
  • Useful for testing, research, and educational purposes
  • Actively maintained and part of the Ethereum Foundation ecosystem

Cons

  • Not optimized for production use in high-performance environments
  • May lag behind the latest Ethereum protocol updates
  • Limited documentation compared to some other Ethereum implementations
  • Smaller community compared to more established implementations like go-ethereum

Code Examples

  1. Creating and executing a simple contract:
from eth import Chain
from eth.vm.forks.berlin import BerlinVM

chain = Chain.configure(vm_configuration=((0, BerlinVM),))

# Deploy a simple contract
contract_code = b'\x60\x01\x60\x02\x01\x60\x00\x52\x60\x20\x60\x00\xf3'
tx_hash = chain.create_unsigned_transaction(
    nonce=0,
    gas_price=1,
    gas=100000,
    to=b'',
    value=0,
    data=contract_code,
).as_signed_transaction(private_key)

# Execute the contract
result = chain.get_transaction_result(tx_hash)
print(result)  # Should print b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03'
  1. Interacting with a deployed contract:
from eth_utils import to_wei
from eth_keys import keys

# Assume contract is already deployed at `contract_address`
private_key = keys.PrivateKey(b'\x01' * 32)
tx_hash = chain.create_unsigned_transaction(
    nonce=1,
    gas_price=1,
    gas=100000,
    to=contract_address,
    value=to_wei(1, 'ether'),
    data=b'\x00\x00\x00\x01',  # Function selector and arguments
).as_signed_transaction(private_key)

result = chain.get_transaction_result(tx_hash)
print(result)
  1. Inspecting block and state:
# Get the latest block
latest_block = chain.get_canonical_head()

# Inspect block details
print(f"Block number: {latest_block.number}")
print(f"Block hash: {latest_block.hash}")

# Get account balance
account_address = b'\x01' * 20
balance = chain.get_vm().state.get_balance(account_address)
print(f"Account balance: {balance}")

Getting Started

To get started with py-EVM, follow these steps:

  1. Install py-EVM using pip:

    pip install py-evm
    
  2. Import the necessary modules in your Python script:

    from eth import Chain
    from eth.vm.forks.berlin import BerlinVM
    
  3. Create a chain instance:

    chain = Chain.configure(vm_configuration=((0, BerlinVM),))
    
  4. You can now use the chain object to interact with the EVM, deploy contracts, and execute transactions as shown in the code examples above.

Competitor Comparisons

Substrate: The platform for blockchain innovators

Pros of Substrate

  • Written in Rust, offering better performance and memory safety
  • More flexible and modular architecture, allowing for easier customization
  • Supports multiple consensus mechanisms out of the box

Cons of Substrate

  • Steeper learning curve due to Rust and complex architecture
  • Smaller community and ecosystem compared to Ethereum-based projects
  • Less battle-tested in production environments

Code Comparison

py-evm (Python):

class BaseChain(ABC):
    @abstractmethod
    def get_vm_class(self) -> Type[VM]:
        pass

    @abstractmethod
    def get_vm(self, header: BlockHeader) -> VM:
        pass

Substrate (Rust):

pub trait Runtime: frame_system::Config + Sized {
    type Call: Parameter + Dispatchable<Origin = Self::Origin>;
    type Event: Parameter + Member + From<Event<Self>>;
    type Origin: From<frame_system::Origin<Self>> + Into<Result<frame_system::Origin<Self>, Self::Origin>>;
}

The code snippets show the difference in language and structure between py-evm and Substrate. py-evm uses Python's abstract base classes for defining chain interfaces, while Substrate employs Rust traits for runtime configuration. Substrate's code is more complex but offers greater type safety and performance.

1,474

An enterprise-grade Java-based, Apache 2.0 licensed Ethereum client https://wiki.hyperledger.org/display/besu

Pros of Besu

  • Written in Java, offering better performance and enterprise-grade scalability
  • Supports both public and private network deployments
  • More comprehensive documentation and enterprise support

Cons of Besu

  • Larger codebase and potentially steeper learning curve
  • Less focused on research and experimentation compared to py-evm

Code Comparison

py-evm (Python):

class BaseChain(ABC):
    @abstractmethod
    def get_vm_class_for_block_number(self, block_number: BlockNumber) -> Type[VM]:
        pass

    @abstractmethod
    def get_vm(self, header: BlockHeaderAPI = None) -> VM:
        pass

Besu (Java):

public interface Blockchain {
  BlockHeader getChainHeadHeader();
  Optional<BlockHeader> getBlockHeader(final long blockNumber);
  Optional<BlockHeader> getBlockHeader(final Hash blockHash);
  Optional<Block> getBlockByHash(final Hash blockHash);
  Optional<Block> getBlockByNumber(final long blockNumber);
}

The code snippets show different approaches to blockchain implementation. py-evm focuses on VM abstraction, while Besu provides a more comprehensive blockchain interface. This reflects their different design philosophies and target use cases.

(deprecated) The fast, light, and robust client for the Ethereum mainnet.

Pros of OpenEthereum

  • Written in Rust, offering better performance and memory safety
  • More mature and battle-tested implementation
  • Supports light client functionality

Cons of OpenEthereum

  • Larger codebase, potentially more complex to maintain
  • Less accessible for Python developers
  • Development has been discontinued

Code Comparison

OpenEthereum (Rust):

pub fn execute_transaction(
    &mut self,
    t: &SignedTransaction,
    header: &Header,
    last_hashes: &LastHashes,
    engine: &dyn Engine,
) -> Result<Executed, Error> {
    // Implementation details
}

py-evm (Python):

def apply_transaction(
    self,
    transaction: SignedTransactionAPI,
) -> Tuple[ReceiptAPI, ComputationAPI]:
    # Implementation details

Summary

OpenEthereum, written in Rust, offers better performance and a more mature implementation compared to py-evm. It also supports light client functionality. However, OpenEthereum has a larger codebase and is less accessible for Python developers. Additionally, its development has been discontinued.

py-evm, being written in Python, is more accessible for Python developers and has a smaller codebase. It's actively maintained but may not offer the same level of performance as OpenEthereum.

The code comparison shows the difference in language syntax and structure between the two implementations, with OpenEthereum using Rust's static typing and py-evm leveraging Python's dynamic nature.

Go implementation of the Ethereum protocol

Pros of go-ethereum

  • Higher performance and efficiency due to Go's compiled nature
  • More mature and widely adopted implementation
  • Extensive documentation and community support

Cons of go-ethereum

  • Steeper learning curve for developers not familiar with Go
  • Less flexibility for rapid prototyping compared to Python

Code Comparison

go-ethereum (Geth):

func (s *Ethereum) Start() error {
    if err := s.startEthService(); err != nil {
        return err
    }
    return nil
}

py-evm:

def start(self):
    try:
        self.start_eth_service()
    except Exception as e:
        return str(e)
    return None

Summary

go-ethereum (Geth) is the official Go implementation of the Ethereum protocol, known for its performance and widespread adoption. It's the most commonly used Ethereum client, offering robust features and extensive documentation.

py-evm, on the other hand, is a Python implementation of the Ethereum Virtual Machine (EVM). It's more accessible for Python developers and allows for easier experimentation and prototyping. However, it may not match Geth's performance in production environments.

The code comparison shows similar structure but highlights the language differences. Geth's Go code is more concise and type-safe, while py-evm's Python code is more readable and flexible.

DEPRECATED! Java implementation of the Ethereum yellowpaper. For JSON-RPC and other client features check Ethereum Harmony

Pros of ethereumj

  • Written in Java, which may be more familiar to enterprise developers
  • Potentially better performance due to Java's JIT compilation
  • More mature and established project with longer development history

Cons of ethereumj

  • Less actively maintained compared to py-evm
  • Smaller community and fewer contributors
  • May lack some of the latest Ethereum protocol updates

Code Comparison

py-evm (Python):

class BaseChain(ABC):
    @abstractmethod
    def get_vm_class_for_block_number(self, block_number: BlockNumber) -> Type[VirtualMachine]:
        pass

ethereumj (Java):

public interface Blockchain {
    BlockSummary add(Block block);
    ImportResult tryToConnect(Block block);
    Block getBestBlock();
}

Both repositories implement Ethereum clients, but py-evm is written in Python while ethereumj is in Java. py-evm tends to have a more modular and flexible design, while ethereumj may offer better performance in some scenarios. py-evm is more actively maintained and closely follows the latest Ethereum developments, whereas ethereumj has a longer history but less recent activity. The code snippets show different approaches to blockchain implementation, with py-evm using abstract base classes and ethereumj using interfaces.

4,654

A permissioned implementation of Ethereum supporting data privacy

Pros of Quorum

  • Enterprise-focused with enhanced privacy features
  • Higher transaction throughput and performance
  • Permissioned network support for better access control

Cons of Quorum

  • Less decentralized due to its permissioned nature
  • Potentially more complex setup and maintenance
  • Limited compatibility with some Ethereum tools and dApps

Code Comparison

py-evm (Python):

class BaseChain(ABC):
    @abstractmethod
    def get_vm_class(self) -> Type[VirtualMachine]:
        pass

    @abstractmethod
    def get_vm(self) -> VirtualMachine:
        pass

Quorum (Go):

type PrivateStateRepository interface {
    GetPrivateState(root common.Hash) (*state.StateDB, error)
    UpdatePrivateState(privateState *state.StateDB) (common.Hash, error)
    Reset()
}

The code snippets showcase different approaches:

  • py-evm uses abstract base classes for chain implementation
  • Quorum focuses on private state management, reflecting its privacy features

Both projects serve different purposes:

  • py-evm is a Python implementation of the Ethereum protocol
  • Quorum is an enterprise-focused fork of Ethereum with privacy enhancements

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Python Implementation of the Ethereum protocol

Join the conversation on Discord Build Status PyPI version Python versions Docs build

Py-EVM

Py-EVM is an implementation of the Ethereum protocol in Python. It contains the low level primitives for the original proof-of-work (POW), (formerly known as Ethereum 1.0) chain as well as emerging support for the proof-of-stake (POS) (formerly known as Ethereum 2.0) spec.

Goals

Py-EVM aims to eventually become the defacto Python implementation of the Ethereum protocol, enabling a wide array of use cases for both public and private chains.

In particular Py-EVM aims to:

  • be a reference implementation of the Ethereum POW and POS implementations in one of the most widely used and understood languages, Python.

  • be easy to understand and modifiable

  • have clear and simple APIs

  • come with solid, friendly documentation

  • deliver the low level primitives to build various clients on top (including full and light clients)

  • be highly flexible to support both research as well as alternate use cases like private chains.

Quickstart

python -m pip install py-evm

Get started in 5 minutes

Documentation

Check out the documentation on our official website

Developer Setup

If you would like to hack on py-evm, please check out the Snake Charmers Tactical Manual for information on how we do:

  • Testing
  • Pull Requests
  • Documentation

We use pre-commit to maintain consistent code style. Once installed, it will run automatically with every commit. You can also run it manually with make lint. If you need to make a commit that skips the pre-commit checks, you can do so with git commit --no-verify.

Development Environment Setup

git clone git@github.com:ethereum/py-evm.git
cd py-evm
virtualenv -p python3 venv
. venv/bin/activate
python -m pip install -e ".[dev]"
pre-commit install

Release setup

To release a new version:

make release bump=$$VERSION_PART_TO_BUMP$$

To issue the next version in line, specify which part to bump, like make release bump=minor or make release bump=devnum. This is typically done from the main branch, except when releasing a beta (in which case the beta is released from main, and the previous stable branch is released from said branch).

Want to help?

Want to file a bug, contribute some code, or improve documentation? Excellent! Read up on our guidelines for contributing and then check out one of our issues that are labeled Good First Issue.