Skip to content

Latest commit

 

History

History
22 lines (18 loc) · 3.27 KB

database.md

File metadata and controls

22 lines (18 loc) · 3.27 KB

Database

Abstractions

  • We created a Database trait abstraction using Rust Stable GATs which frees us from being bound to a single database implementation. We currently use MDBX, but are exploring redb as an alternative.
  • We then iterated on StageDB as a non-leaky abstraction with helpers for strictly-typed and unit-tested higher-level database abstractions.

Codecs

  • We want Reth's serialized format to be able to trade off read/write speed for size, depending on who the user is.
  • To achieve that, we created the Encode/Decode/Compress/Decompress trais to make the (de)serialization of database Table::Key and Table::Values generic.
  • We implemented that trait for the following encoding formats:
    • Ethereum-specific Compact Encoding: A lot of Ethereum datatypes have unnecessary zeros when serialized, or optional (e.g. on empty hashes) which would be nice not to pay in storage costs.
      • Erigon achieves that by having a bitfield set on Table "PlainState which adds a bitfield to Accounts.
      • Akula expanded it for other tables and datatypes manually. It also saved some more space by storing the length of certain types (U256, u64) using the modular_bitfield crate, which compacts this information.
      • We generalized it for all types, by writing a derive macro that autogenerates code for implementing the trait. It, also generates the interfaces required for fuzzing using ToB/test-fuzz:
    • Scale Encoding
    • Postcard Encoding
    • Passthrough (called no_codec in the codebase)
  • We made implementation of these traits easy via a derive macro called main_codec that delegates to one of Compact (default), Scale, Postcard or Passthrough encoding. This is derived on every struct we need, and lets us experiment with different encoding formats without having to modify the entire codebase each time.