erigon-seg
A Rust library for the Erigon 3 seg state file format. It reads, queries, writes, and merges the file triple Erigon writes for each domain snapshot.
| File | Contents |
|---|---|
.kv |
seg-compressed words. For a domain file: alternating sorted key, value. |
.bt |
B-tree index — an Elias-Fano array of the .kv byte offset of every key. |
.kvei |
Existence (bloom) filter — a negative lookup accelerator. |
Features
- Decompress
seg.kvfiles (both the legacyv0and thev1versioned header). - Point lookups via the
.btElias-Fano index (O(log n)binary search), or an ordered linear scan when no index is present. - Bloom-accelerated negative lookups via
.kvei, including resolving the index salt (known, fromsalt-state.txt, or brute-forced). - Sequential
(key, value)iteration. - Writing: produce valid, erigon-readable
.kv(seg, with optional pattern compression),.bt(legacy or footer layout), and.kvei(holiman bloom) files from sorted pairs. - Merging: k-way newest-wins merge of several domain files into one, with erigon's deletion semantics (empty value dropped only when the merged range starts at step 0).
- Pure-safe Rust apart from the single
mmapcall; nounsafeelsewhere.
Usage
use ;
let mut r = open?;
// Optional: enable the .kvei bloom for fast definite-absent answers.
r.enable_bloom; // or Salt::Known(salt)
if let Some = r.get?
for kv in r.iter
# Ok::
Writing and merging
use ;
// Write a domain file set from sorted, unique (key, value) pairs.
let mut w = create?;
w.add?; // keys must be strictly increasing
w.add?;
let paths = w.finish?; // writes .kv, .bt, and .kvei
// Merge several domain files into one (newest input wins per key).
merge?;
# Ok::
Lower-level building blocks are also public: SegWriter (raw words), build_bt /
BtLayout / BtOptions, and KveiBuilder / build_kvei_from_seg.
Supported layouts
.kv:v0(body at offset 0) andv1([version, feature-flags], optional page-compression byte, optional out-of-band metadata)..bt: legacy (Elias-Fano at offset 0) and footer (trailingerigon\0\0magic locating the Elias-Fano section)..kvei:holiman/bloomfilter/v2(v02\nmagic). The newer "fuse filter" layout is detected and skipped — lookups stay correct, just unaccelerated.
Status
Read, query, write, merge, and pattern compression are all implemented and verified
against real Erigon v1.1 files: re-encoding a real .kv round-trips byte-exact (with and
without compression), a rebuilt .bt matches the real index offset-for-offset, merges
reproduce newest-wins semantics, and compressed output is competitive with erigon's own
(~81% the size on a sample v1.1-accounts file).
License
MIT — see LICENSE.