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.1.tar.gz (92.0 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.1-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.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (358.8 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

komozoi_evm-0.0.1-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.1-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.1-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.1.tar.gz.

File metadata

  • Download URL: komozoi_evm-0.0.1.tar.gz
  • Upload date:
  • Size: 92.0 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.1.tar.gz
Algorithm Hash digest
SHA256 6659b7a2cc88a9f6a9422452525042f0d7b683c358627853cd3480f08c034ad6
MD5 c395bccab226e097bf724c3fb15d49ae
BLAKE2b-256 19810605b211fa73ef1bd565ebce97d1dd7e2f50e8f6d0539f0eeda55003dd14

See more details on using hashes here.

Provenance

The following attestation bundles were made for komozoi_evm-0.0.1.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.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for komozoi_evm-0.0.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 dfacc0a1d4c66ea7d7296c85758fc288a11d56a57706a24d30f46967ea7e1a1a
MD5 6f86f258c56c9fcb3b0f77ad619cee64
BLAKE2b-256 30bcc413caeae62c188623ef99b903d1f52e95db3f33bf5ff66c144ccee40839

See more details on using hashes here.

Provenance

The following attestation bundles were made for komozoi_evm-0.0.1-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.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for komozoi_evm-0.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 58c7a26ec907d81762f030e33037a668eb7b2ffc846300d561af8bbc1ac8955f
MD5 db6f404dfcf4659824198e159bd3ec77
BLAKE2b-256 54a8f0b6aa14b318116adc6b8aa685a1e4983655d0705cdcadf7d4699fad1b84

See more details on using hashes here.

Provenance

The following attestation bundles were made for komozoi_evm-0.0.1-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.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for komozoi_evm-0.0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b60f2da75ea0cc5d74e3eeeec57d72732097332ae632beb7d2fb7898a4506851
MD5 8854e4112a1105f7fe0b98f18781df96
BLAKE2b-256 fa7ae14783cd8f431ad32915031ed9dc2b601120cb183e64f5da0d331eb39a91

See more details on using hashes here.

Provenance

The following attestation bundles were made for komozoi_evm-0.0.1-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.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for komozoi_evm-0.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6b22e829bf3b2accdb5e6f722579bd0ab55ca1228ebc5f7b67f47e2a63d35fd0
MD5 9fbc919472d64dbd7a187a43d9afeb89
BLAKE2b-256 b9c9d9eda40a4c2da310302879018cc7a2d0ec6a692c3799bc57fa87cd66686f

See more details on using hashes here.

Provenance

The following attestation bundles were made for komozoi_evm-0.0.1-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.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for komozoi_evm-0.0.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 74657d7950b81e222aa543d66f4af7a865f2dd3b9f0067ec5035177dd1139ce0
MD5 bbb0082f7d5626a530c0a945f08f7300
BLAKE2b-256 48f1376c7556c9d90f0ce856438689159e235ebc0d610d4ce4f01fab77d5d08e

See more details on using hashes here.

Provenance

The following attestation bundles were made for komozoi_evm-0.0.1-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