mirror of
https://github.com/luxfi/node.git
synced 2026-10-30 15:24:05 +00:00
The module already called itself github.com/luxfi/node while containing no node in Go: cmd/luxd was twenty-one lines that printed the size of a few types, and the Go column of the matrix measured a VM plugin. A repo that fetches the thing it claims to be has not replaced it, so the Go sources move in — 1515 files, the daemon and everything under it — and the build reaches outside this module for nothing. Rust and C++ sit beside it unchanged and are still measured against it by the differential. That is the point of one repo: the reference implementation and the two ports are the same checkout, so parity is a thing you can run rather than a thing you assert. Verified: builds, and the closure carries no lux-private — checked at build time rather than believed. The daemon starts twelve chains on a local network (P X C Q Z A B G K M F, plus D through its plugin) and reports healthy.
90 lines
2.4 KiB
Go
90 lines
2.4 KiB
Go
// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package chains
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
|
|
"github.com/luxfi/database"
|
|
"github.com/luxfi/database/prefixdb"
|
|
"github.com/luxfi/ids"
|
|
"github.com/luxfi/log"
|
|
)
|
|
|
|
// ChainDBManager manages chain database access using a single global ZapDB.
|
|
// All chains share one ZapDB instance with prefix-based isolation:
|
|
// 1. Single database - easier to manage, backup, and query across chains
|
|
// 2. Prefix isolation - each chain's data is prefixed by its chainID
|
|
// 3. G-Chain compatible - dgraph can index the entire database for GraphQL queries
|
|
type ChainDBManager struct {
|
|
mu sync.RWMutex
|
|
|
|
// Global shared database (ZapDB)
|
|
db database.Database
|
|
|
|
// Cached prefixed databases per chain
|
|
chainDBs map[ids.ID]database.Database
|
|
|
|
log log.Logger
|
|
}
|
|
|
|
// ChainDBManagerConfig holds configuration for the chain database manager
|
|
type ChainDBManagerConfig struct {
|
|
// DB is the global shared database (ZapDB)
|
|
DB database.Database
|
|
|
|
Log log.Logger
|
|
}
|
|
|
|
// NewChainDBManager creates a new chain database manager using a single global ZapDB
|
|
func NewChainDBManager(config ChainDBManagerConfig) *ChainDBManager {
|
|
return &ChainDBManager{
|
|
db: config.DB,
|
|
chainDBs: make(map[ids.ID]database.Database),
|
|
log: config.Log,
|
|
}
|
|
}
|
|
|
|
// chainDB returns a prefixed database for the given chain.
|
|
// Uses prefix-based isolation on the single global ZapDB.
|
|
func (m *ChainDBManager) chainDB(chainID ids.ID, chainAlias string) (database.Database, error) {
|
|
if m.db == nil {
|
|
return nil, fmt.Errorf("global database not initialized")
|
|
}
|
|
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
|
|
// Check cache first
|
|
if db, exists := m.chainDBs[chainID]; exists {
|
|
return db, nil
|
|
}
|
|
|
|
// Create prefixed database for this chain
|
|
chainDB := prefixdb.New(chainID[:], m.db)
|
|
m.chainDBs[chainID] = chainDB
|
|
|
|
if !m.log.IsZero() {
|
|
m.log.Info("Created prefixed database for chain",
|
|
log.Stringer("chainID", chainID),
|
|
log.String("alias", chainAlias),
|
|
)
|
|
}
|
|
|
|
return chainDB, nil
|
|
}
|
|
|
|
// GetVMDatabase returns a VM-prefixed database for the given chain.
|
|
// Adds a "vm" prefix within the chain's prefix for VM-specific data.
|
|
func (m *ChainDBManager) GetVMDatabase(chainID ids.ID, chainAlias string) (database.Database, error) {
|
|
chainDB, err := m.chainDB(chainID, chainAlias)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Add VM prefix to isolate VM data from other chain data
|
|
return prefixdb.New(VMDBPrefix, chainDB), nil
|
|
}
|