Rename ethdb/badgerdb to ethdb/zapdb — uses luxfi/zapdb/v4

Replaced dgraph-io/badger/v4 with luxfi/zapdb/v4 (our fork).
Renamed ethdb/badgerdb/ directory to ethdb/zapdb/ for clarity.
Updated all imports in core/, node/, p2p/.
This commit is contained in:
Hanzo AI
2026-03-29 16:29:55 -07:00
parent 4a606df74a
commit 59639b8cf5
7 changed files with 61 additions and 89 deletions
+5 -5
View File
@@ -27,7 +27,7 @@ import (
"github.com/luxfi/geth/core/rawdb"
"github.com/luxfi/geth/core/types"
"github.com/luxfi/geth/ethdb"
"github.com/luxfi/geth/ethdb/badgerdb"
zapdb "github.com/luxfi/geth/ethdb/zapdb"
"github.com/luxfi/geth/params"
"github.com/luxfi/math"
)
@@ -182,7 +182,7 @@ func benchInsertChain(b *testing.B, disk bool, gen func(int, *BlockGen)) {
if !disk {
db = rawdb.NewMemoryDatabase()
} else {
pdb, err := badgerdb.New(b.TempDir(), 128, 128, "", false)
pdb, err := zapdb.New(b.TempDir(), 128, 128, "", false)
if err != nil {
b.Fatalf("cannot create temporary database: %v", err)
}
@@ -302,7 +302,7 @@ func makeChainForBench(db ethdb.Database, genesis *Genesis, full bool, count uin
func benchWriteChain(b *testing.B, full bool, count uint64) {
genesis := &Genesis{Config: params.AllEthashProtocolChanges}
for b.Loop() {
pdb, err := badgerdb.New(b.TempDir(), 1024, 128, "", false)
pdb, err := zapdb.New(b.TempDir(), 1024, 128, "", false)
if err != nil {
b.Fatalf("error opening database: %v", err)
}
@@ -315,7 +315,7 @@ func benchWriteChain(b *testing.B, full bool, count uint64) {
func benchReadChain(b *testing.B, full bool, count uint64) {
dir := b.TempDir()
pdb, err := badgerdb.New(dir, 1024, 128, "", false)
pdb, err := zapdb.New(dir, 1024, 128, "", false)
if err != nil {
b.Fatalf("error opening database: %v", err)
}
@@ -327,7 +327,7 @@ func benchReadChain(b *testing.B, full bool, count uint64) {
options := DefaultConfig().WithArchive(true)
b.ReportAllocs()
for b.Loop() {
pdb, err = badgerdb.New(dir, 1024, 128, "", false)
pdb, err = zapdb.New(dir, 1024, 128, "", false)
if err != nil {
b.Fatalf("error opening database: %v", err)
}
+5 -5
View File
@@ -30,7 +30,7 @@ import (
"github.com/luxfi/geth/consensus/ethash"
"github.com/luxfi/geth/core/rawdb"
"github.com/luxfi/geth/core/types"
"github.com/luxfi/geth/ethdb/badgerdb"
zapdb "github.com/luxfi/geth/ethdb/zapdb"
"github.com/luxfi/geth/params"
)
@@ -1764,7 +1764,7 @@ func testRepairWithScheme(t *testing.T, tt *rewindTest, snapshots bool, scheme s
datadir := t.TempDir()
ancient := filepath.Join(datadir, "ancient")
pdb, err := badgerdb.New(datadir, 0, 0, "", false)
pdb, err := zapdb.New(datadir, 0, 0, "", false)
if err != nil {
t.Fatalf("Failed to create persistent key-value database: %v", err)
}
@@ -1850,7 +1850,7 @@ func testRepairWithScheme(t *testing.T, tt *rewindTest, snapshots bool, scheme s
chain.stopWithoutSaving()
// Start a new blockchain back up and see where the repair leads us
pdb, err = badgerdb.New(datadir, 0, 0, "", false)
pdb, err = zapdb.New(datadir, 0, 0, "", false)
if err != nil {
t.Fatalf("Failed to reopen persistent key-value database: %v", err)
}
@@ -1915,7 +1915,7 @@ func testIssue23496(t *testing.T, scheme string) {
datadir := t.TempDir()
ancient := filepath.Join(datadir, "ancient")
pdb, err := badgerdb.New(datadir, 0, 0, "", false)
pdb, err := zapdb.New(datadir, 0, 0, "", false)
if err != nil {
t.Fatalf("Failed to create persistent key-value database: %v", err)
}
@@ -1976,7 +1976,7 @@ func testIssue23496(t *testing.T, scheme string) {
chain.stopWithoutSaving()
// Start a new blockchain back up and see where the repair leads us
pdb, err = badgerdb.New(datadir, 0, 0, "", false)
pdb, err = zapdb.New(datadir, 0, 0, "", false)
if err != nil {
t.Fatalf("Failed to reopen persistent key-value database: %v", err)
}
+2 -2
View File
@@ -32,7 +32,7 @@ import (
"github.com/luxfi/geth/core/rawdb"
"github.com/luxfi/geth/core/state"
"github.com/luxfi/geth/core/types"
"github.com/luxfi/geth/ethdb/badgerdb"
zapdb "github.com/luxfi/geth/ethdb/zapdb"
"github.com/luxfi/geth/params"
"github.com/luxfi/geth/triedb"
"github.com/luxfi/geth/triedb/hashdb"
@@ -1968,7 +1968,7 @@ func testSetHeadWithScheme(t *testing.T, tt *rewindTest, snapshots bool, scheme
datadir := t.TempDir()
ancient := filepath.Join(datadir, "ancient")
pdb, err := badgerdb.New(datadir, 0, 0, "", false)
pdb, err := zapdb.New(datadir, 0, 0, "", false)
if err != nil {
t.Fatalf("Failed to create persistent key-value database: %v", err)
}
+3 -3
View File
@@ -34,7 +34,7 @@ import (
"github.com/luxfi/geth/core/rawdb"
"github.com/luxfi/geth/core/types"
"github.com/luxfi/geth/ethdb"
"github.com/luxfi/geth/ethdb/badgerdb"
zapdb "github.com/luxfi/geth/ethdb/zapdb"
"github.com/luxfi/geth/params"
)
@@ -65,7 +65,7 @@ func (basic *snapshotTestBasic) prepare(t *testing.T) (*BlockChain, []*types.Blo
datadir := t.TempDir()
ancient := filepath.Join(datadir, "ancient")
pdb, err := badgerdb.New(datadir, 0, 0, "", false)
pdb, err := zapdb.New(datadir, 0, 0, "", false)
if err != nil {
t.Fatalf("Failed to create persistent key-value database: %v", err)
}
@@ -260,7 +260,7 @@ func (snaptest *crashSnapshotTest) test(t *testing.T) {
chain.triedb.Close()
// Start a new blockchain back up and see where the repair leads us
pdb, err := badgerdb.New(snaptest.datadir, 0, 0, "", false)
pdb, err := zapdb.New(snaptest.datadir, 0, 0, "", false)
if err != nil {
t.Fatalf("Failed to create persistent key-value database: %v", err)
}
+2 -2
View File
@@ -45,7 +45,7 @@ import (
"github.com/luxfi/geth/core/vm/program"
"github.com/luxfi/geth/eth/tracers/logger"
"github.com/luxfi/geth/ethdb"
"github.com/luxfi/geth/ethdb/badgerdb"
zapdb "github.com/luxfi/geth/ethdb/zapdb"
"github.com/luxfi/geth/params"
"github.com/luxfi/geth/trie"
"github.com/stretchr/testify/assert"
@@ -2511,7 +2511,7 @@ func testSideImportPrunedBlocks(t *testing.T, scheme string) {
datadir := t.TempDir()
ancient := path.Join(datadir, "ancient")
pdb, err := badgerdb.New(datadir, 0, 0, "", false)
pdb, err := zapdb.New(datadir, 0, 0, "", false)
if err != nil {
t.Fatalf("Failed to create persistent key-value database: %v", err)
}
@@ -1,13 +1,13 @@
// Copyright (C) 2025, Lux Industries, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package badgerdb
package zapdb
import (
"errors"
"fmt"
badger "github.com/dgraph-io/badger/v4"
badger "github.com/luxfi/zapdb/v4"
"github.com/luxfi/geth/ethdb"
)
@@ -16,16 +16,28 @@ var (
errNotSupported = errors.New("this operation is not supported")
)
// Database is a badgerdb implementation of ethdb.Database
// Database is a zapdb (badgerdb fork) implementation of ethdb.Database
type Database struct {
db *badger.DB
}
// New creates a new badgerdb database
// New creates a new zapdb-backed database
func New(path string, cache int, handles int, namespace string, readonly bool) (ethdb.Database, error) {
opts := badger.DefaultOptions(path)
opts.ReadOnly = readonly
opts.Logger = nil // Disable badger logging
opts.Logger = nil // Disable logging
// Performance tuning for EVM workloads
opts.SyncWrites = false
opts.NumCompactors = 4
opts.NumMemtables = 5
opts.MemTableSize = 64 << 20 // 64 MB
opts.BlockCacheSize = 256 << 20 // 256 MB
opts.IndexCacheSize = 100 << 20 // 100 MB
opts.BloomFalsePositive = 0.01
opts.DetectConflicts = false
opts.NumVersionsToKeep = 1
opts.CompactL0OnClose = true
db, err := badger.Open(opts)
if err != nil {
@@ -95,13 +107,11 @@ func (d *Database) NewBatchWithSize(size int) ethdb.Batch {
// NewIterator creates a new iterator
func (d *Database) NewIterator(prefix []byte, start []byte) ethdb.Iterator {
if d.db == nil {
// Return empty iterator if database is nil
return &iterator{iter: nil, txn: nil}
}
txn := d.db.NewTransaction(false)
if txn == nil {
// Return empty iterator if transaction creation failed
return &iterator{iter: nil, txn: nil}
}
@@ -120,7 +130,7 @@ func (d *Database) NewIterator(prefix []byte, start []byte) ethdb.Iterator {
return &iterator{
iter: iter,
txn: txn,
first: true, // First call to Next() should check Valid() without advancing
first: true,
}
}
@@ -165,7 +175,6 @@ func (b *batch) Write() error {
if err := b.wb.Flush(); err != nil {
return err
}
// CRITICAL: Sync to disk to ensure data persists
return b.db.Sync()
}
@@ -176,12 +185,10 @@ func (b *batch) Reset() {
}
func (b *batch) DeleteRange(start, end []byte) error {
// Not efficiently supported by BadgerDB batches
return nil
}
func (b *batch) Replay(w ethdb.KeyValueWriter) error {
// BadgerDB doesn't support replay
return nil
}
@@ -189,20 +196,17 @@ func (b *batch) Replay(w ethdb.KeyValueWriter) error {
type iterator struct {
iter *badger.Iterator
txn *badger.Txn
first bool // true if we haven't returned the first item yet
first bool
}
func (i *iterator) Next() bool {
if i.iter == nil {
return false
}
// On first call after NewIterator, the iterator is already positioned
// at the first item (via Rewind/Seek), so just check Valid()
if i.first {
i.first = false
return i.iter.Valid()
}
// On subsequent calls, only advance if currently valid
if !i.iter.Valid() {
return false
}
@@ -238,8 +242,7 @@ func (i *iterator) Release() {
}
}
// Ancients/Freezer stubs
// Snapshot support
type Snapshot interface {
Has(key []byte) (bool, error)
Get(key []byte) ([]byte, error)
@@ -297,75 +300,44 @@ func (d *Database) DeleteRange(start, end []byte) error {
})
}
func (d *Database) AncientDatadir() (string, error) {
return "", nil
// Sync flushes all pending writes to disk
func (d *Database) Sync() error {
return d.db.Sync()
}
func (d *Database) ReadAncients(fn func(ethdb.AncientReaderOp) error) error {
return nil
func (d *Database) SyncKeyValue() error {
return d.db.Sync()
}
// Ancient/Freezer stubs (zapdb doesn't use freezer)
func (d *Database) AncientDatadir() (string, error) { return "", nil }
func (d *Database) ReadAncients(fn func(ethdb.AncientReaderOp) error) error { return nil }
func (d *Database) ModifyAncients(fn func(ethdb.AncientWriteOp) error) (int64, error) {
return 0, nil
}
func (d *Database) TruncateHead(n uint64) (uint64, error) {
return 0, nil
}
func (d *Database) TruncateHead(n uint64) (uint64, error) { return 0, nil }
func (d *Database) TruncateTail(n uint64) (uint64, error) { return 0, nil }
func (d *Database) SyncAncient() error { return nil }
func (d *Database) TruncateTail(n uint64) (uint64, error) {
return 0, nil
}
func (d *Database) MigrateTable(s string, f func([]byte) ([]byte, error)) error { return nil }
func (d *Database) Sync() error {
return d.db.Sync()
}
func (d *Database) SyncAncient() error {
return nil
}
func (d *Database) SyncKeyValue() error {
return nil
}
func (d *Database) MigrateTable(s string, f func([]byte) ([]byte, error)) error {
return nil
}
func (d *Database) AncientOffSet() uint64 {
return 0
}
func (d *Database) Ancients() (uint64, error) {
return 0, nil
}
func (d *Database) Tail() (uint64, error) {
return 0, nil
}
func (d *Database) AncientSize(kind string) (uint64, error) {
return 0, nil
}
func (d *Database) AncientOffSet() uint64 { return 0 }
func (d *Database) Ancients() (uint64, error) { return 0, nil }
func (d *Database) Tail() (uint64, error) { return 0, nil }
func (d *Database) AncientSize(kind string) (uint64, error) { return 0, nil }
func (d *Database) AncientRange(kind string, start, count, maxBytes uint64) ([][]byte, error) {
return nil, errNotSupported
}
// AncientBytes retrieves the value segment of the element specified by the id and value offsets.
func (d *Database) AncientBytes(kind string, id, offset, length uint64) ([]byte, error) {
return nil, errNotSupported
}
func (d *Database) HasAncient(kind string, number uint64) (bool, error) {
return false, nil
}
func (d *Database) Ancient(kind string, number uint64) ([]byte, error) {
return nil, errNotSupported
}
func (d *Database) AncientBatch() ethdb.AncientWriteOp {
return nil
}
func (d *Database) HasAncient(kind string, number uint64) (bool, error) { return false, nil }
func (d *Database) Ancient(kind string, number uint64) ([]byte, error) { return nil, errNotSupported }
func (d *Database) AncientBatch() ethdb.AncientWriteOp { return nil }
+2 -2
View File
@@ -22,7 +22,7 @@ import (
"github.com/luxfi/geth/core/rawdb"
"github.com/luxfi/geth/ethdb"
"github.com/luxfi/geth/ethdb/badgerdb"
zapdb "github.com/luxfi/geth/ethdb/zapdb"
log "github.com/luxfi/log"
)
@@ -137,7 +137,7 @@ func openKeyValueDatabase(o internalOpenOptions) (ethdb.KeyValueStore, error) {
// newBadgerDBDatabase creates a persistent key-value database without a freezer
// moving immutable chain segments into cold storage.
func newBadgerDBDatabase(file string, cache int, handles int, namespace string, readonly bool) (ethdb.KeyValueStore, error) {
db, err := badgerdb.New(file, cache, handles, namespace, readonly)
db, err := zapdb.New(file, cache, handles, namespace, readonly)
if err != nil {
return nil, err
}