Lux Docs
Lux Skills Reference

Lux Database

Blockchain Storage Abstraction Layer

Overview

Lux Database (github.com/luxfi/database) is the Go storage abstraction layer for the Lux blockchain. It defines a unified key-value Database interface modeled after Ethereum's database API (enabling direct use of Geth/EVM code) and provides multiple pluggable backends: ZapDB (BadgerDB-based, default), PebbleDB (CockroachDB), LevelDB (Google), and in-memory. Also includes database wrappers for encryption, corruption detection, hooking, prefixing, versioning, metering, and linked-list storage. Used by github.com/luxfi/node for all persistent state.

Repository

ItemValue
Modulegithub.com/luxfi/database
Local~/work/lux/database/
Go1.26.1
LicenseSee LICENSE
Buildgo build ./...
Testgo test ./...

Core Dependencies

PackageVersionPurpose
github.com/luxfi/zapdb/v4v4.9.2Default KV backend (BadgerDB-based)
github.com/cockroachdb/pebblev1.1.5PebbleDB backend (build tag)
github.com/syndtr/goleveldbv1.0.1LevelDB backend (build tag)
github.com/luxfi/codecv1.1.3Binary serialization
github.com/luxfi/cryptov1.17.39Encryption for encdb
github.com/luxfi/idsv1.2.9ID types
github.com/luxfi/logv1.4.1Structured logging
github.com/luxfi/metricv1.5.0Prometheus metrics
github.com/luxfi/cachev1.2.1Caching layer
github.com/luxfi/compressv0.0.5Data compression
github.com/luxfi/concurrentv0.0.3Concurrency utilities
github.com/luxfi/mathv1.2.3Math utilities
github.com/stretchr/testifyv1.11.1Testing assertions
go.uber.org/mockv0.6.0Mock generation
golang.org/x/cryptov0.48.0Cryptographic primitives
golang.org/x/syncv0.19.0Sync utilities
github.com/klauspost/compressv1.18.4Fast compression

Core Interface

The Database interface in database.go composes:

type Database interface {
    KeyValueReaderWriterDeleter  // Has, Get, Put, Delete
    Batcher                     // NewBatch
    Iteratee                    // NewIterator variants
    Compacter                   // Compact(start, limit)
    Syncer                      // Sync() - flush to disk
    io.Closer                   // Close
    HealthCheck(context.Context) (interface{}, error)
    Backup(w io.Writer, since uint64) (uint64, error)  // Incremental backup
    Load(r io.Reader) error                             // Restore from backup
}

Sub-Interfaces

InterfaceMethodsPurpose
KeyValueReaderHas(key), Get(key)Read access
KeyValueWriterPut(key, value)Write access
KeyValueDeleterDelete(key)Delete access
KeyValueReaderWriterReader + WriterRead/write
KeyValueWriterDeleterWriter + DeleterWrite/delete
KeyValueReaderWriterDeleterAll threeFull access
CompacterCompact(start, limit)Storage compaction
SyncerSync()Flush to persistent storage
BatchSize, Write, Reset, Replay, InnerAtomic batch writes
BatcherNewBatch()Batch factory
IteratorNext, Error, Key, Value, ReleaseKey-value iteration
IterateeNewIterator[WithStart][WithPrefix]Iterator factory
HeightIndexPut(height, value), Get(height), Has, Delete, CloseHeight-based blockchain state

Error Types

var ErrClosed   = errors.New("closed")
var ErrNotFound = errors.New("not found")
type ErrBackendDisabled struct { Backend string }

Memory Management

memory.go provides MemoryManager for controlling database memory usage:

  • Configurable percentage of system memory
  • Reserve allocation for critical operations
  • Prometheus gauge metrics (usage, available, remaining)

Constants

MaxExcessCapacityFactor = 4  // Above which capacity is excessive
CapacityReductionFactor = 2  // Reduction factor for excess capacity

Storage Backends

ZapDB (Default)

ItemValue
Packagegithub.com/luxfi/zapdb/v4
Wrapperdatabase/badgerdb/ (compatibility)
Based onBadgerDB
Build tagNone (always available)

The badgerdb/ package is a thin compatibility wrapper that delegates to zapdb.New(). Registered as the default backend in manager/manager.go via init().

PebbleDB

ItemValue
Packagedatabase/pebbledb/
Based onCockroachDB Pebble
Build tag-tags=pebbledb
FeaturesBloom filters, batch operations, custom iterators

8 source files: db.go, batch.go, iterator.go, constants.go, errors.go, set.go, db_test.go, batch_test.go.

LevelDB

ItemValue
Packagedatabase/leveldb/
Based onGoogle LevelDB (syndtr/goleveldb)
Build tag-tags=leveldb
FeaturesMetrics collection, configurable constants

5 source files: db.go, constants.go, errors.go, metrics.go, db_test.go.

In-Memory (memdb)

ItemValue
Packagedatabase/memdb/
PurposeTesting, ephemeral storage
Build tagNone (always available)

4 source files: db.go, constants.go, iterator.go, db_test.go.

Database Wrappers

PackagePurposeKey Feature
encdb/Encrypted databaseAES encryption via golang.org/x/crypto
corruptabledb/Corruption injectionTesting fault tolerance
hookdb/Event hooksNotifiers on write/delete operations
linkeddb/Linked-list storageOrdered traversal over key-value pairs
heightindexdb/Height-indexed storageBlockchain state by block height (big-endian uint64 keys)
codec/Serialization layerBinary codec with codec/linear/ sub-package

Database Infrastructure

Factory (factory/)

Pluggable database creation system:

  • RegisterDatabase(name, factory) -- Register a backend
  • AvailableDatabases() -- List available backends
  • Default: ZapDB + memdb always available
  • PebbleDB/LevelDB available via build tags

Manager (manager/)

Database lifecycle management:

  • NewManager(baseDir, registerer) -- Create manager
  • RegisterDatabaseType(name, creator) -- Register backend type
  • Supports zapdb, pebbledb, leveldb via files manager_zapdb.go, manager_pebbledb.go, manager_leveldb.go
  • Wraps databases with metering, versioning, and prefixing

Test Suite (dbtest/)

Comprehensive test harness:

  • dbtest.go -- Full interface compliance tests
  • benchmark.go -- Performance benchmarks
  • testsuite.go -- Reusable test suite for any backend

Mocks (databasemock/)

Generated mock implementations:

  • batch.go -- Mock Batch
  • iterator.go -- Mock Iterator

Project Structure

~/work/lux/database/
  go.mod                 # github.com/luxfi/database, Go 1.26.1
  database.go            # Core Database/HeightIndex interfaces
  batch.go               # Batch/Batcher interfaces and BatchOps
  iterator.go            # Iterator/Iteratee interfaces
  errors.go              # ErrClosed, ErrNotFound, ErrBackendDisabled
  memory.go              # MemoryManager (system memory control)
  helpers.go             # Utility functions
  database_util.go       # Additional utilities
  Makefile               # Build/test targets

  badgerdb/              # ZapDB compatibility wrapper
  pebbledb/              # PebbleDB backend (build tag)
  leveldb/               # LevelDB backend (build tag)
  memdb/                 # In-memory backend
  encdb/                 # Encrypted database wrapper
  corruptabledb/         # Corruption testing wrapper
  hookdb/                # Event hook wrapper
  linkeddb/              # Linked-list storage
  heightindexdb/         # Height-indexed storage
  codec/                 # Serialization (with linear/ sub-pkg)
  factory/               # Pluggable backend factory
  manager/               # Database lifecycle management
  dbtest/                # Test suite and benchmarks
  databasemock/          # Generated mocks
  bin/                   # Build output

Development

cd ~/work/lux/database

# Build all packages
go build ./...

# Run all tests
go test ./...

# Run with race detector
go test -race ./...

# Build with PebbleDB support
go build -tags=pebbledb ./...

# Build with LevelDB support
go build -tags=leveldb ./...

# Run benchmarks
go test -bench=. ./dbtest/

# Run specific backend tests
go test ./pebbledb/ -tags=pebbledb -v
go test ./leveldb/ -tags=leveldb -v
go test ./memdb/ -v

Key Design Decisions

  1. Ethereum-compatible interface: Matches go-ethereum's database API for seamless EVM integration
  2. Build-tag backends: PebbleDB and LevelDB are compile-time optional to minimize binary size
  3. ZapDB as default: BadgerDB-based ZapDB is always available, registered in init()
  4. Wrapper composition: Encryption, metering, versioning are separate wrappers that can be composed
  5. HeightIndex: Specialized interface for blockchain state indexed by block height using big-endian uint64 keys for efficient range queries
  6. Backup/Restore: Built into the core Database interface with incremental backup support
  7. Memory management: System-level memory control with configurable limits and Prometheus metrics
PackageModulePurpose
github.com/luxfi/zapdbv4.9.2Default storage engine
github.com/luxfi/cachev1.2.1In-memory caching layer
github.com/luxfi/compressv0.0.5Data compression
github.com/luxfi/codecv1.1.3Binary serialization
github.com/luxfi/nodeLatestPrimary consumer of this package

On this page