Skip to main content

Python bindings for the Komozoi EVM simulator

Project description

Komozoi EVM — Python Bindings

komozoievm is a Python package that exposes the Komozoi EVM C++ engine for simulating Ethereum transactions and contract execution against either a user-supplied mock chain state or a real chain provider.

The package is built from the same C++ sources as the native library and is distributed as binary wheels on PyPI.

Installation

pip install komozoievm

Building from a source checkout requires a C++17 compiler and CMake:

pip install .

Quick start

import komozoievm as evm

src = "0x0000000000000000000000000000000000000001"
dst = "0x0000000000000000000000000000000000000002"

# An in-memory chain populated by Python code.  Addresses must be valid
# 20-byte hex strings; malformed values raise ``ValueError``.
chain = evm.MockChain()
chain.set_balance(src, 10 * 10**18)
chain.set_code(dst, bytes.fromhex("6001600101"))

# Simulate a call without mutating state.
block = evm.BlockInfo(number=1, timestamp=1_700_000_000, base_fee=10)
result = chain.simulate(dst, src, b"", 0, block)
print(result.success, result.return_data.hex())

# ``execute`` performs the same call but commits the resulting state.
result = chain.execute(dst, src, value=1_000_000_000_000_000_000)

Module layout

komozoievm is a single top-level module. Names listed below are the public Python surface; everything not listed should be treated as internal and is subject to change.

Types

Python type Description
Address 20-byte Ethereum address. Accepts bytes, bytearray, hex str (with or without 0x), None, or another Address. str(address) returns the EIP-55 checksum form.
U256 256-bit unsigned integer. Implicitly converts from Python int; values out of range raise OverflowError.

Address and U256 are thin wrappers; functions that accept them also accept the plain Python equivalents (bytes, str, int).

Data classes

  • BlockInfo(number, timestamp, base_fee=0, gas_limit=30_000_000, chain_id=1, coinbase=..., randao=0) — fields exposed for the block context.
  • AccountInfo(address, balance=0, next_nonce=0) — minimal account state.
  • AccessListEntry(address, storage_keys=()).
  • SimulationResult with attributes: success: bool, return_data: bytes, reason: str, gas_used: int.
  • CallTrace — an aggregating tracer that records every contract call performed during a transaction. Pass an instance as trace= to simulate/execute; the captured entries are available on the calls attribute.
  • CallTraceEntry with attributes: src: Address, dst: Address, calldata: bytes, returndata: bytes, gas_used: int, success: bool.
  • EventTrace with attributes: src: Address, topics: list[int], data: bytes.

Transaction construction

tx = evm.TransactionBuilder(
    to=address,
    nonce=int,
    gas_limit=int,
    value=0,
    gas_price=0,
    max_fee_per_gas=0,
    max_priority_fee_per_gas=0,
    data=b"",
    access_list=[],
).build()

For common contract calls, you can use the Transaction.call helper:

tx = evm.Transaction.call(
    from_=address,
    to=address,
    value=0,
    data=b"",
    gas_limit=100_000
)

For signed transactions, pass a 32-byte private key:

raw = tx.sign_and_encode(private_key=bytes.fromhex(...))

Chain providers

StateProvider is an abstract base class that Python code can subclass to serve as the chain backend. Override:

  • get_account_info(self, address: Address, block_number: int = 0) -> AccountInfo
  • get_contract_code(self, address: Address) -> bytes
  • get_storage_slots(self, address: Address, slot_keys: list[int], block_number: int = 0) -> list[int]
  • update_account(self, address: Address, info: AccountInfo) -> bool
  • save_contract_code(self, address: Address, code: bytes) -> bool
  • update_storage_slots(self, address: Address, entries: dict[int, int]) -> bool

MockChain is a built-in StateProvider implementation backed by in-memory hash tables. It is the recommended starting point for test suites and is what the examples in this document use.

Running the EVM

The high-level entry points on any StateProvider (including MockChain) hide transaction construction behind keyword arguments:

result = chain.simulate(dst, src, data=b"", value=0, block=None,
    gas=3_000_000, trace=None)
result = chain.execute(dst, src, data=b"", value=0, block=None,
    gas=3_000_000, trace=None)
gas = evm.EVM.initial_gas_cost(calldata)

All arguments after src are optional. block defaults to a sensible context; value and gas accept either int or float (e.g. 1e18 for one ETH, 1.5e6 for 1.5M gas) — floats are rounded to the nearest integer for gas and truncated toward zero for value. data defaults to empty calldata, and gas defaults to 3,000,000.

If dst has no contract code installed it is treated as an Externally Owned Account: the call succeeds immediately after any value transfer, without executing any bytecode. Installing code via set_code turns the address into a contract from that point on.

simulate does not mutate the underlying provider. execute writes the resulting state back through the provider's update_* callbacks.

For lower-level control you can build a Transaction (or TransactionBuilder) explicitly and run it through the EVM class:

machine = evm.EVM(chain)
tx = evm.Transaction.call(from_=src, to=dst, data=b"", gas_limit=100_000)
result = machine.simulate(tx, block_info, tracer=None)

Tracing

The simplest way to capture every contract call made by a transaction is evm.CallTrace:

trace = evm.CallTrace()
chain.execute(dst, src, trace=trace)
for entry in trace.calls:
    print(entry.src, "->", entry.dst, entry.success)

For custom logic, subclass evm.Tracer and override the callbacks:

class MyTracer(evm.Tracer):
    def on_contract_call(self, chain, call_trace):
        print(f"Call from {call_trace.src} to {call_trace.dst}")

    def on_event_log(self, chain, event_trace):
        print(f"Log from {event_trace.src} with {len(event_trace.topics)} topics")

chain.execute(dst, src, trace=MyTracer())

Threading and memory

The bindings release the GIL during long-running EVM execution so that Python threads can make progress in parallel. Callbacks into Python (state provider methods, tracers) reacquire the GIL automatically.

Versioning

Wheels are tagged with the upstream Komozoi EVM version followed by a build metadata suffix when applicable. Breaking changes to the Python API are only introduced on a major version bump.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

komozoi_evm-0.0.2.tar.gz (94.2 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

komozoi_evm-0.0.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (358.8 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

komozoi_evm-0.0.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (358.7 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

komozoi_evm-0.0.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (358.6 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

komozoi_evm-0.0.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (357.3 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

komozoi_evm-0.0.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (357.5 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

File details

Details for the file komozoi_evm-0.0.2.tar.gz.

File metadata

  • Download URL: komozoi_evm-0.0.2.tar.gz
  • Upload date:
  • Size: 94.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for komozoi_evm-0.0.2.tar.gz
Algorithm Hash digest
SHA256 e1f2f0996e5e09e0ecfc53415610263db1be5659d857bca4c086b12f0a194601
MD5 62cb8e02c169abfdb14bd78bda55a328
BLAKE2b-256 22832e19060f18098d34d0a0af5ac0c9459e6371b467c579f423763097e5853d

See more details on using hashes here.

Provenance

The following attestation bundles were made for komozoi_evm-0.0.2.tar.gz:

Publisher: release-pypi.yml on komozoi/komozoievm

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file komozoi_evm-0.0.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for komozoi_evm-0.0.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4aa4c1a7a0e8ad9df142ace14902941ce537703b5b627e7bdd16f16e32fd6840
MD5 0da118ff241777730966d863e92b7067
BLAKE2b-256 b7f7b93682382dd213b5189ee5a81b7c3b5846aaeafdbad53a2933a116d49307

See more details on using hashes here.

Provenance

The following attestation bundles were made for komozoi_evm-0.0.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release-pypi.yml on komozoi/komozoievm

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file komozoi_evm-0.0.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for komozoi_evm-0.0.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8bee9b61f424bf5411abf4ea93722c693c8f28ed43d8175c10a994e36ec896bc
MD5 29aa3159db462d48b0fea89eb5cf7aa5
BLAKE2b-256 0427072236148d3af7336151298692a0b54a738aa21b7c75c0a5412e3b0311f6

See more details on using hashes here.

Provenance

The following attestation bundles were made for komozoi_evm-0.0.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release-pypi.yml on komozoi/komozoievm

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file komozoi_evm-0.0.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for komozoi_evm-0.0.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a2a50a269c733d8c424db2ad821b6185dba92f5171cab3b8774b6e6b352b9c93
MD5 b49ed273c7f8385dda28534fa93039ba
BLAKE2b-256 b5ae4453c2c4d95e0b52694b3776ecd68dbf4afb7a8dd4c762dd204de8165c3b

See more details on using hashes here.

Provenance

The following attestation bundles were made for komozoi_evm-0.0.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release-pypi.yml on komozoi/komozoievm

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file komozoi_evm-0.0.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for komozoi_evm-0.0.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d4add7d73e1b42c3c0b4973712ddc1c2fa9f50b518764dc2e32a5e9db295abda
MD5 c722ac7c23d10ba184d98ef20a287d3e
BLAKE2b-256 b8f3841bdb89e2a18f187458a6e5ffdc6f892f3fe453e17cc4e2a9ec3f9f177f

See more details on using hashes here.

Provenance

The following attestation bundles were made for komozoi_evm-0.0.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release-pypi.yml on komozoi/komozoievm

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file komozoi_evm-0.0.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for komozoi_evm-0.0.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bf9460ccca2341e6e59cb22d0088e5aa9991198e55fd9d4994fc69fadad0a862
MD5 4982aa29bbdb155a19b248e73380376e
BLAKE2b-256 103521269bf76041a96d8b745b2656b8966217b64cf7a66c12e2f553e56fda47

See more details on using hashes here.

Provenance

The following attestation bundles were made for komozoi_evm-0.0.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release-pypi.yml on komozoi/komozoievm

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page