multi: use kvdb.Backend for height hint DB

In order to separate our databases more clearly, we refactor the height
hint cache DB to use a kvdb backend instead of the channel DB instance
directly.
This commit is contained in:
Oliver Gugger 2021-08-03 09:57:27 +02:00
parent 9138c8abac
commit c4917ae7fc
No known key found for this signature in database
GPG Key ID: 8E4256593F177720
4 changed files with 26 additions and 14 deletions

View File

@ -84,7 +84,7 @@ type ConfirmHintCache interface {
// will be stored. // will be stored.
type HeightHintCache struct { type HeightHintCache struct {
cfg CacheConfig cfg CacheConfig
db *channeldb.DB db kvdb.Backend
} }
// Compile-time checks to ensure HeightHintCache satisfies the SpendHintCache // Compile-time checks to ensure HeightHintCache satisfies the SpendHintCache
@ -93,7 +93,9 @@ var _ SpendHintCache = (*HeightHintCache)(nil)
var _ ConfirmHintCache = (*HeightHintCache)(nil) var _ ConfirmHintCache = (*HeightHintCache)(nil)
// NewHeightHintCache returns a new height hint cache backed by a database. // NewHeightHintCache returns a new height hint cache backed by a database.
func NewHeightHintCache(cfg CacheConfig, db *channeldb.DB) (*HeightHintCache, error) { func NewHeightHintCache(cfg CacheConfig, db kvdb.Backend) (*HeightHintCache,
error) {
cache := &HeightHintCache{cfg, db} cache := &HeightHintCache{cfg, db}
if err := cache.initBuckets(); err != nil { if err := cache.initBuckets(); err != nil {
return nil, err return nil, err
@ -105,7 +107,7 @@ func NewHeightHintCache(cfg CacheConfig, db *channeldb.DB) (*HeightHintCache, er
// initBuckets ensures that the primary buckets used by the circuit are // initBuckets ensures that the primary buckets used by the circuit are
// initialized so that we can assume their existence after startup. // initialized so that we can assume their existence after startup.
func (c *HeightHintCache) initBuckets() error { func (c *HeightHintCache) initBuckets() error {
return kvdb.Batch(c.db.Backend, func(tx kvdb.RwTx) error { return kvdb.Batch(c.db, func(tx kvdb.RwTx) error {
_, err := tx.CreateTopLevelBucket(spendHintBucket) _, err := tx.CreateTopLevelBucket(spendHintBucket)
if err != nil { if err != nil {
return err return err
@ -127,7 +129,7 @@ func (c *HeightHintCache) CommitSpendHint(height uint32,
Log.Tracef("Updating spend hint to height %d for %v", height, Log.Tracef("Updating spend hint to height %d for %v", height,
spendRequests) spendRequests)
return kvdb.Batch(c.db.Backend, func(tx kvdb.RwTx) error { return kvdb.Batch(c.db, func(tx kvdb.RwTx) error {
spendHints := tx.ReadWriteBucket(spendHintBucket) spendHints := tx.ReadWriteBucket(spendHintBucket)
if spendHints == nil { if spendHints == nil {
return ErrCorruptedHeightHintCache return ErrCorruptedHeightHintCache
@ -197,7 +199,7 @@ func (c *HeightHintCache) PurgeSpendHint(spendRequests ...SpendRequest) error {
Log.Tracef("Removing spend hints for %v", spendRequests) Log.Tracef("Removing spend hints for %v", spendRequests)
return kvdb.Batch(c.db.Backend, func(tx kvdb.RwTx) error { return kvdb.Batch(c.db, func(tx kvdb.RwTx) error {
spendHints := tx.ReadWriteBucket(spendHintBucket) spendHints := tx.ReadWriteBucket(spendHintBucket)
if spendHints == nil { if spendHints == nil {
return ErrCorruptedHeightHintCache return ErrCorruptedHeightHintCache
@ -228,7 +230,7 @@ func (c *HeightHintCache) CommitConfirmHint(height uint32,
Log.Tracef("Updating confirm hints to height %d for %v", height, Log.Tracef("Updating confirm hints to height %d for %v", height,
confRequests) confRequests)
return kvdb.Batch(c.db.Backend, func(tx kvdb.RwTx) error { return kvdb.Batch(c.db, func(tx kvdb.RwTx) error {
confirmHints := tx.ReadWriteBucket(confirmHintBucket) confirmHints := tx.ReadWriteBucket(confirmHintBucket)
if confirmHints == nil { if confirmHints == nil {
return ErrCorruptedHeightHintCache return ErrCorruptedHeightHintCache
@ -299,7 +301,7 @@ func (c *HeightHintCache) PurgeConfirmHint(confRequests ...ConfRequest) error {
Log.Tracef("Removing confirm hints for %v", confRequests) Log.Tracef("Removing confirm hints for %v", confRequests)
return kvdb.Batch(c.db.Backend, func(tx kvdb.RwTx) error { return kvdb.Batch(c.db, func(tx kvdb.RwTx) error {
confirmHints := tx.ReadWriteBucket(confirmHintBucket) confirmHints := tx.ReadWriteBucket(confirmHintBucket)
if confirmHints == nil { if confirmHints == nil {
return ErrCorruptedHeightHintCache return ErrCorruptedHeightHintCache

View File

@ -28,6 +28,7 @@ import (
"github.com/lightningnetwork/lnd/htlcswitch" "github.com/lightningnetwork/lnd/htlcswitch"
"github.com/lightningnetwork/lnd/input" "github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/keychain" "github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lncfg" "github.com/lightningnetwork/lnd/lncfg"
"github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwallet/btcwallet" "github.com/lightningnetwork/lnd/lnwallet/btcwallet"
@ -70,7 +71,7 @@ type Config struct {
// HeightHintDB is a pointer to the database that stores the height // HeightHintDB is a pointer to the database that stores the height
// hints. // hints.
HeightHintDB *channeldb.DB HeightHintDB kvdb.Backend
// ChanStateDB is a pointer to the database that stores the channel // ChanStateDB is a pointer to the database that stores the channel
// state. // state.

View File

@ -91,6 +91,10 @@ type DatabaseBackends struct {
// ChanStateDB points to a possibly networked replicated backend that // ChanStateDB points to a possibly networked replicated backend that
// contains the critical channel state related data. // contains the critical channel state related data.
ChanStateDB kvdb.Backend ChanStateDB kvdb.Backend
// HeightHintDB points to a possibly networked replicated backend that
// contains the chain height hint related data.
HeightHintDB kvdb.Backend
} }
// GetBackends returns a set of kvdb.Backends as set in the DB config. // GetBackends returns a set of kvdb.Backends as set in the DB config.
@ -126,6 +130,7 @@ func (db *DB) GetBackends(ctx context.Context, dbPath string) (
return &DatabaseBackends{ return &DatabaseBackends{
GraphDB: localDB, GraphDB: localDB,
ChanStateDB: remoteDB, ChanStateDB: remoteDB,
HeightHintDB: localDB,
}, nil }, nil
} }

8
lnd.go
View File

@ -43,6 +43,7 @@ import (
"github.com/lightningnetwork/lnd/chanacceptor" "github.com/lightningnetwork/lnd/chanacceptor"
"github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/keychain" "github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lncfg" "github.com/lightningnetwork/lnd/lncfg"
"github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwallet"
@ -706,7 +707,7 @@ func Main(cfg *Config, lisCfg ListenerCfg, interceptor signal.Interceptor) error
LitecoindMode: cfg.LitecoindMode, LitecoindMode: cfg.LitecoindMode,
BtcdMode: cfg.BtcdMode, BtcdMode: cfg.BtcdMode,
LtcdMode: cfg.LtcdMode, LtcdMode: cfg.LtcdMode,
HeightHintDB: dbs.graphDB, HeightHintDB: dbs.heightHintDB,
ChanStateDB: dbs.chanStateDB, ChanStateDB: dbs.chanStateDB,
PrivateWalletPw: privateWalletPw, PrivateWalletPw: privateWalletPw,
PublicWalletPw: publicWalletPw, PublicWalletPw: publicWalletPw,
@ -1626,6 +1627,7 @@ func waitForWalletPassword(cfg *Config,
type databaseInstances struct { type databaseInstances struct {
graphDB *channeldb.DB graphDB *channeldb.DB
chanStateDB *channeldb.DB chanStateDB *channeldb.DB
heightHintDB kvdb.Backend
} }
// initializeDatabases extracts the current databases that we'll use for normal // initializeDatabases extracts the current databases that we'll use for normal
@ -1655,7 +1657,9 @@ func initializeDatabases(ctx context.Context,
// If the remoteDB is nil, then we'll just open a local DB as normal, // If the remoteDB is nil, then we'll just open a local DB as normal,
// having the remote and local pointer be the exact same instance. // having the remote and local pointer be the exact same instance.
var ( var (
dbs = &databaseInstances{} dbs = &databaseInstances{
heightHintDB: databaseBackends.HeightHintDB,
}
closeFuncs []func() closeFuncs []func()
) )
if databaseBackends.ChanStateDB == nil { if databaseBackends.ChanStateDB == nil {