Giter Club home page Giter Club logo

erigon-db's Introduction

Overview

Fully typed access to the Erigon database in rust.

use erigon_db::{Erigon, env_open};
use ethereum_types::Address;

fn main() -> eyre::Result<()> {
    let path = std::path::Path::new(env!("ERIGON_CHAINDATA"));

    // Open an mdbx environment and begin a read-only database transaction
    let env = env_open(path)?;
    let db = Erigon::begin(&env)?;

    // get the canonical head block header
    let head_hash = db.read_head_header_hash()?.unwrap();
    let head_num = db.read_header_number(head_hash)?.unwrap();
    let header = db.read_header((head_num, head_hash))?.unwrap();

    // get the current state of an account
    let contract: Address = "0x0d4c6c6605a729a379216c93e919711a081beba2".parse()?;
    let account = db.read_account(contract)?.unwrap();
    let bytecode = db.read_code(account.codehash)?.unwrap();

    // get all of the contract's populated storage slots
    // (incarnation is an Erigon-specific value related to removal/revival of contracts)
    for read in db.walk_storage(contract, account.incarnation, None)? {
        let (slot, value) = read?;
        println!("The value at slot {} is {}", slot, value);
    }

    // get the state of the account at block 100
    let old_account = db.read_account_hist(contract, 100)?.unwrap_or_default();

    Ok(())
}

Acknowledgements

Much of this code has been taken from the Akula Ethereum client in order to enable its use with the stable rust toolchain. In particular, it repurposes many of Akula's kv utilities and abstractions for working with libmdbx and Ethereum data. These abstractions are extremely high-quality in my opinion, so the primary modifications were increasing the strictness and expressiveness of the accessor types and tailoring to Erigon's data representations and database layout.

Resources

  • Erigon has an excellent doc walking through their database layout, though it may not match the current implementation in some places.
  • Erigon's core/rawdb/accessors_*.go contains many of their low-level database interactions.
  • Some comments from Erigon on the advisability of this endeavor.
  • For some brief info on mdbx, see doc/mdbx.md.
  • The Erigon database layout is defined in src/erigon/tables.rs, and you can see how these tables are read in src/erigon/mod.rs, namely the read_* methods.

erigon-db's People

Contributors

gio256 avatar plotchy avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.