py-evm (Python Ethereum Virtual Machine)

0.12.1b1 · abandoned · verified Sat Apr 11

Py-EVM is a Python implementation of the Ethereum Virtual Machine (EVM) designed for readability, flexibility for research and experimentation, and performance in testing. It aims to be a reference implementation for the Ethereum execution layer specifications. **As of May 2025, Py-EVM has been archived and is now read-only, with no further development planned. The last supported Ethereum fork is Prague.**

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a basic Ethereum chain and Virtual Machine (VM) using Py-EVM. It sets up an in-memory database, pre-funds a mock address, and then retrieves the current block number and the balance of the pre-funded address. This provides a minimal runnable example of interacting with Py-EVM's core components for simulating EVM behavior.

import os

from eth import constants
from eth.chains.mainnet import MainnetChain
from eth.db.atomic import AtomicDB
from eth_utils import to_wei, encode_hex
from eth_typing import Address

def run_evm_example():
    # Setup a mock address and initial balance
    MOCK_ADDRESS = Address(b'\x00' * 19 + b'\x01')  # An arbitrary address
    PREFUND_AMOUNT = to_wei(100, 'ether')

    # Initialize a new chain with an atomic database
    # AtomicDB is an in-memory database suitable for tests and examples
    chain = MainnetChain.from_genesis(
        AtomicDB(),
        genesis_state={
            MOCK_ADDRESS: {
                'balance': PREFUND_AMOUNT,
                'nonce': 0,
                'code': b'',
                'storage': {}
            }
        },
        genesis_header_params={
            'gas_limit': constants.GAS_LIMIT,
            'difficulty': 1  # Simplified difficulty for non-PoW chain
        }
    )

    # Get the current VM from the chain
    vm = chain.get_vm()

    print(f"Chain created successfully. Current block number: {vm.get_block().header.block_number}")
    print(f"Balance of {encode_hex(MOCK_ADDRESS)}: {vm.get_balance(MOCK_ADDRESS)} Wei")

    # Example: Accessing a constant
    print(f"Default transaction gas limit: {constants.GAS_LIMIT}")

if __name__ == "__main__":
    run_evm_example()

view raw JSON →