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.
type HeightHintCache struct {
cfg CacheConfig
db *channeldb.DB
db kvdb.Backend
}
// Compile-time checks to ensure HeightHintCache satisfies the SpendHintCache
@ -93,7 +93,9 @@ var _ SpendHintCache = (*HeightHintCache)(nil)
var _ ConfirmHintCache = (*HeightHintCache)(nil)
// 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}
if err := cache.initBuckets(); err != nil {
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
// initialized so that we can assume their existence after startup.
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)
if err != nil {
return err
@ -127,7 +129,7 @@ func (c *HeightHintCache) CommitSpendHint(height uint32,
Log.Tracef("Updating spend hint to height %d for %v", height,
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)
if spendHints == nil {
return ErrCorruptedHeightHintCache
@ -197,7 +199,7 @@ func (c *HeightHintCache) PurgeSpendHint(spendRequests ...SpendRequest) error {
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)
if spendHints == nil {
return ErrCorruptedHeightHintCache
@ -228,7 +230,7 @@ func (c *HeightHintCache) CommitConfirmHint(height uint32,
Log.Tracef("Updating confirm hints to height %d for %v", height,
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)
if confirmHints == nil {
return ErrCorruptedHeightHintCache
@ -299,7 +301,7 @@ func (c *HeightHintCache) PurgeConfirmHint(confRequests ...ConfRequest) error {
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)
if confirmHints == nil {
return ErrCorruptedHeightHintCache

View File

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

View File

@ -91,6 +91,10 @@ type DatabaseBackends struct {
// ChanStateDB points to a possibly networked replicated backend that
// contains the critical channel state related data.
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.
@ -124,8 +128,9 @@ func (db *DB) GetBackends(ctx context.Context, dbPath string) (
}
return &DatabaseBackends{
GraphDB: localDB,
ChanStateDB: remoteDB,
GraphDB: localDB,
ChanStateDB: remoteDB,
HeightHintDB: localDB,
}, nil
}

12
lnd.go
View File

@ -43,6 +43,7 @@ import (
"github.com/lightningnetwork/lnd/chanacceptor"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lncfg"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnwallet"
@ -706,7 +707,7 @@ func Main(cfg *Config, lisCfg ListenerCfg, interceptor signal.Interceptor) error
LitecoindMode: cfg.LitecoindMode,
BtcdMode: cfg.BtcdMode,
LtcdMode: cfg.LtcdMode,
HeightHintDB: dbs.graphDB,
HeightHintDB: dbs.heightHintDB,
ChanStateDB: dbs.chanStateDB,
PrivateWalletPw: privateWalletPw,
PublicWalletPw: publicWalletPw,
@ -1624,8 +1625,9 @@ func waitForWalletPassword(cfg *Config,
// databaseInstances is a struct that holds all instances to the actual
// databases that are used in lnd.
type databaseInstances struct {
graphDB *channeldb.DB
chanStateDB *channeldb.DB
graphDB *channeldb.DB
chanStateDB *channeldb.DB
heightHintDB kvdb.Backend
}
// 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,
// having the remote and local pointer be the exact same instance.
var (
dbs = &databaseInstances{}
dbs = &databaseInstances{
heightHintDB: databaseBackends.HeightHintDB,
}
closeFuncs []func()
)
if databaseBackends.ChanStateDB == nil {