mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-28 14:40:51 +02:00
multi: make macaroon DB remote compatible
The macaroon root keys should also be stored to the remote database if a replicated backend such as etcd is used. This commit refactors the macaroons service and wallet unlocker to accept a kvdb backend directly instead of creating the bolt instance automatically.
This commit is contained in:
48
lncfg/db.go
48
lncfg/db.go
@@ -10,7 +10,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
dbName = "channel.db"
|
||||
channelDBName = "channel.db"
|
||||
macaroonDBName = "macaroons.db"
|
||||
|
||||
BoltBackend = "bolt"
|
||||
EtcdBackend = "etcd"
|
||||
@@ -19,6 +20,9 @@ const (
|
||||
// NSChannelDB is the namespace name that we use for the combined graph
|
||||
// and channel state DB.
|
||||
NSChannelDB = "channeldb"
|
||||
|
||||
// NSMacaroonDB is the namespace name that we use for the macaroon DB.
|
||||
NSMacaroonDB = "macaroondb"
|
||||
)
|
||||
|
||||
// DB holds database configuration for LND.
|
||||
@@ -100,6 +104,10 @@ type DatabaseBackends struct {
|
||||
// contains the chain height hint related data.
|
||||
HeightHintDB kvdb.Backend
|
||||
|
||||
// MacaroonDB points to a database backend that stores the macaroon root
|
||||
// keys.
|
||||
MacaroonDB kvdb.Backend
|
||||
|
||||
// Remote indicates whether the database backends are remote, possibly
|
||||
// replicated instances or local bbolt backed databases.
|
||||
Remote bool
|
||||
@@ -110,14 +118,27 @@ type DatabaseBackends struct {
|
||||
}
|
||||
|
||||
// GetBackends returns a set of kvdb.Backends as set in the DB config.
|
||||
func (db *DB) GetBackends(ctx context.Context, dbPath string) (
|
||||
*DatabaseBackends, error) {
|
||||
func (db *DB) GetBackends(ctx context.Context, chanDBPath,
|
||||
walletDBPath string) (*DatabaseBackends, error) {
|
||||
|
||||
// We keep track of all the kvdb backends we actually open and return a
|
||||
// reference to their close function so they can be cleaned up properly
|
||||
// on error or shutdown.
|
||||
closeFuncs := make(map[string]func() error)
|
||||
|
||||
// If we need to return early because of an error, we invoke any close
|
||||
// function that has been initialized so far.
|
||||
returnEarly := true
|
||||
defer func() {
|
||||
if !returnEarly {
|
||||
return
|
||||
}
|
||||
|
||||
for _, closeFunc := range closeFuncs {
|
||||
_ = closeFunc()
|
||||
}
|
||||
}()
|
||||
|
||||
if db.Backend == EtcdBackend {
|
||||
etcdBackend, err := kvdb.Open(
|
||||
kvdb.EtcdBackendName, ctx, db.Etcd,
|
||||
@@ -127,10 +148,12 @@ func (db *DB) GetBackends(ctx context.Context, dbPath string) (
|
||||
}
|
||||
closeFuncs[NSChannelDB] = etcdBackend.Close
|
||||
|
||||
returnEarly = false
|
||||
return &DatabaseBackends{
|
||||
GraphDB: etcdBackend,
|
||||
ChanStateDB: etcdBackend,
|
||||
HeightHintDB: etcdBackend,
|
||||
MacaroonDB: etcdBackend,
|
||||
Remote: true,
|
||||
CloseFuncs: closeFuncs,
|
||||
}, nil
|
||||
@@ -138,8 +161,8 @@ func (db *DB) GetBackends(ctx context.Context, dbPath string) (
|
||||
|
||||
// We're using all bbolt based databases by default.
|
||||
boltBackend, err := kvdb.GetBoltBackend(&kvdb.BoltBackendConfig{
|
||||
DBPath: dbPath,
|
||||
DBFileName: dbName,
|
||||
DBPath: chanDBPath,
|
||||
DBFileName: channelDBName,
|
||||
DBTimeout: db.Bolt.DBTimeout,
|
||||
NoFreelistSync: !db.Bolt.SyncFreelist,
|
||||
AutoCompact: db.Bolt.AutoCompact,
|
||||
@@ -150,10 +173,25 @@ func (db *DB) GetBackends(ctx context.Context, dbPath string) (
|
||||
}
|
||||
closeFuncs[NSChannelDB] = boltBackend.Close
|
||||
|
||||
macaroonBackend, err := kvdb.GetBoltBackend(&kvdb.BoltBackendConfig{
|
||||
DBPath: walletDBPath,
|
||||
DBFileName: macaroonDBName,
|
||||
DBTimeout: db.Bolt.DBTimeout,
|
||||
NoFreelistSync: !db.Bolt.SyncFreelist,
|
||||
AutoCompact: db.Bolt.AutoCompact,
|
||||
AutoCompactMinAge: db.Bolt.AutoCompactMinAge,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error opening macaroon DB: %v", err)
|
||||
}
|
||||
closeFuncs[NSMacaroonDB] = macaroonBackend.Close
|
||||
|
||||
returnEarly = false
|
||||
return &DatabaseBackends{
|
||||
GraphDB: boltBackend,
|
||||
ChanStateDB: boltBackend,
|
||||
HeightHintDB: boltBackend,
|
||||
MacaroonDB: macaroonBackend,
|
||||
CloseFuncs: closeFuncs,
|
||||
}, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user