2020-03-13 19:05:28 +03:00
|
|
|
package lncfg
|
|
|
|
|
|
|
|
import (
|
2020-05-25 19:08:58 +03:00
|
|
|
"context"
|
2020-03-13 19:05:28 +03:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/channeldb/kvdb"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
dbName = "channel.db"
|
2020-08-05 01:34:29 +03:00
|
|
|
BoltBackend = "bolt"
|
|
|
|
EtcdBackend = "etcd"
|
2020-03-13 19:05:28 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// DB holds database configuration for LND.
|
|
|
|
type DB struct {
|
|
|
|
Backend string `long:"backend" description:"The selected database backend."`
|
|
|
|
|
2020-05-15 17:59:37 +03:00
|
|
|
Etcd *kvdb.EtcdConfig `group:"etcd" namespace:"etcd" description:"Etcd settings."`
|
2020-03-13 19:05:28 +03:00
|
|
|
|
2020-05-15 17:59:37 +03:00
|
|
|
Bolt *kvdb.BoltConfig `group:"bolt" namespace:"bolt" description:"Bolt settings."`
|
2020-03-13 19:05:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDB creates and returns a new default DB config.
|
|
|
|
func DefaultDB() *DB {
|
|
|
|
return &DB{
|
2020-08-05 01:34:29 +03:00
|
|
|
Backend: BoltBackend,
|
|
|
|
Bolt: &kvdb.BoltConfig{},
|
2020-03-13 19:05:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate validates the DB config.
|
|
|
|
func (db *DB) Validate() error {
|
|
|
|
switch db.Backend {
|
2020-08-05 01:34:29 +03:00
|
|
|
case BoltBackend:
|
2020-03-13 19:05:28 +03:00
|
|
|
|
2020-08-05 01:34:29 +03:00
|
|
|
case EtcdBackend:
|
2020-03-13 19:05:28 +03:00
|
|
|
if db.Etcd.Host == "" {
|
|
|
|
return fmt.Errorf("etcd host must be set")
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown backend, must be either \"%v\" or \"%v\"",
|
2020-08-05 01:34:29 +03:00
|
|
|
BoltBackend, EtcdBackend)
|
2020-03-13 19:05:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBackend returns a kvdb.Backend as set in the DB config.
|
2020-05-25 19:08:58 +03:00
|
|
|
func (db *DB) GetBackend(ctx context.Context, dbPath string,
|
|
|
|
networkName string) (kvdb.Backend, error) {
|
2020-05-20 15:04:34 +03:00
|
|
|
|
2020-08-05 01:34:29 +03:00
|
|
|
if db.Backend == EtcdBackend {
|
2020-05-12 17:02:46 +03:00
|
|
|
// Prefix will separate key/values in the db.
|
2020-05-25 19:08:58 +03:00
|
|
|
return kvdb.GetEtcdBackend(ctx, networkName, db.Etcd)
|
2020-03-13 19:05:28 +03:00
|
|
|
}
|
|
|
|
|
2020-08-05 01:34:29 +03:00
|
|
|
// The implementation by walletdb accepts "noFreelistSync" as the
|
|
|
|
// second parameter, so we negate here.
|
|
|
|
return kvdb.GetBoltBackend(dbPath, dbName, !db.Bolt.SyncFreelist)
|
2020-03-13 19:05:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compile-time constraint to ensure Workers implements the Validator interface.
|
|
|
|
var _ Validator = (*DB)(nil)
|