Skip to main content

Python wrapper around reth db. Written in Rust.

Project description

reth-db-py

Bare-bones Python package allowing you to interact with the Reth DB via Python. Rust and Pyo3 are used behind the scenes.

This python wrapper can access node data 2x-4x faster than local RPC calls. Using this package, the most recent block hash can be retrieved in ~300μs on a local reth DB.

Installation

This package has been published to PyPi and can be installed using pip:

pip install reth-db-py

Assets

This package only has two assets made available: DbHandler and TableName.

TableName is an enum that contains the names of the supported tables in the Reth DB. This is used to ensure that the table name you are trying to access is valid.

pub enum TableName {
    CanonicalHeaders,
    Headers,
    Transactions,
    TxHashNumber,
}

DbHandler is a struct/class used to interact with the Reth DB. It has two methods, get and list:

  • get - takes a TableName and a key and returns the value associated with that key in the table
  • list - takes a TableName, skip, length, and a reverse boolean and returns a list of keys and values from the table
impl DbHandler {
    pub fn new(db_path: String) -> Self {}
    pub fn list(&self, table_name: TableName, skip: usize, len: usize, reverse: bool) -> PyResult<Vec<(String, String)>> {}
    pub fn get(&self, table_name: TableName, key: String) -> PyResult<String> {}
}
class DbHandler:
    def __init__(self, db_path: str)
    def list(self, table_name: TableName, skip: int, len: int, reverse: bool)
    def get(self, table_name: TableName, key: str)

Usage

Import reth-db-py assets:

from reth_db_py import DbHandler, TableName

Create a DbHandler instance:

handler = DbHandler("/path/to/db/mdbx.dat")

Get a single block hash from the CanonicalHeaders table:

header_hash = handler.get(TableName.CanonicalHeaders, '17000000')

Get 5 most recent block hashes from the CanonicalHeaders table:

header_list = handler.list(TableName.CanonicalHeaders, 0, 5, True)

Table Docs

Goal is to support all tables in Reth (currently only support 4 tables related to headers and transactions). Reth is still in alpha and the database tables are subject to change. Docs on the tables can be found in the reth repo here.

  • CanonicalHeaders - contains the block hash for each block number
  • Headers - contains the header for each block hash
  • Transactions - contains the transaction for each transaction hash
  • TxHashNumber - contains the block number for each transaction hash

Benchmarks

Speed tests between RPC calls vs. reth-db-py calls (direct db interaction) coming soon.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

reth_db_py-0.0.1-cp310-cp310-manylinux_2_34_x86_64.whl (20.8 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.34+ x86-64

Supported by

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