From 85aee9b064d857c0a72565e7ceaab9912c31240f Mon Sep 17 00:00:00 2001 From: Andras Banki-Horvath Date: Fri, 15 May 2020 16:59:37 +0200 Subject: [PATCH] kvdb+lncfg: fully move etcd behind build tag This commit separates all etcd related sources (sans a few stubs and config) from the rest of the source tree and makes compilation conditional depending on whether the kvdb_etcd build tag is specified. --- channeldb/kvdb/backend.go | 13 +---- channeldb/kvdb/bbolt.go | 10 ---- channeldb/kvdb/config.go | 33 +++++++++++++ channeldb/kvdb/etcd.go | 10 ---- channeldb/kvdb/etcd/bucket.go | 2 + channeldb/kvdb/etcd/bucket_test.go | 2 + channeldb/kvdb/etcd/db.go | 2 + channeldb/kvdb/etcd/db_test.go | 2 + channeldb/kvdb/etcd/driver.go | 2 + channeldb/kvdb/etcd/driver_test.go | 2 + channeldb/kvdb/etcd/embed.go | 2 + channeldb/kvdb/etcd/fixture_test.go | 2 + channeldb/kvdb/etcd/readwrite_bucket.go | 2 + channeldb/kvdb/etcd/readwrite_bucket_test.go | 2 + channeldb/kvdb/etcd/readwrite_cursor.go | 2 + channeldb/kvdb/etcd/readwrite_cursor_test.go | 2 + channeldb/kvdb/etcd/readwrite_tx.go | 2 + channeldb/kvdb/etcd/readwrite_tx_test.go | 2 + channeldb/kvdb/etcd/stm.go | 2 + channeldb/kvdb/etcd/stm_test.go | 2 + .../kvdb/etcd/walletdb_interface_test.go | 2 + channeldb/kvdb/kvdb_etcd.go | 48 +++++++++++++++++++ channeldb/kvdb/kvdb_no_etcd.go | 24 ++++++++++ channeldb/kvdb/kvdb_test_bdb.go | 5 -- channeldb/kvdb/kvdb_test_etcd.go | 5 -- lncfg/db.go | 40 ++-------------- 26 files changed, 145 insertions(+), 77 deletions(-) delete mode 100644 channeldb/kvdb/bbolt.go create mode 100644 channeldb/kvdb/config.go delete mode 100644 channeldb/kvdb/etcd.go create mode 100644 channeldb/kvdb/kvdb_etcd.go create mode 100644 channeldb/kvdb/kvdb_no_etcd.go delete mode 100644 channeldb/kvdb/kvdb_test_bdb.go delete mode 100644 channeldb/kvdb/kvdb_test_etcd.go diff --git a/channeldb/kvdb/backend.go b/channeldb/kvdb/backend.go index 9e868e52..5f710ed9 100644 --- a/channeldb/kvdb/backend.go +++ b/channeldb/kvdb/backend.go @@ -5,7 +5,7 @@ import ( "os" "path/filepath" - "github.com/lightningnetwork/lnd/channeldb/kvdb/etcd" + _ "github.com/btcsuite/btcwallet/walletdb/bdb" // Import to register backend. ) // fileExists returns true if the file exists, and false otherwise. @@ -63,16 +63,7 @@ func GetTestBackend(path, name string) (Backend, func(), error) { } return db, empty, nil } else if TestBackend == EtcdBackendName { - config, cleanup, err := etcd.NewEmbeddedEtcdInstance(path) - if err != nil { - return nil, nil, err - } - backend, err := Open(EtcdBackendName, *config) - if err != nil { - cleanup() - return nil, nil, err - } - return backend, cleanup, nil + return GetEtcdTestBackend(path, name) } return nil, nil, fmt.Errorf("unknown backend") diff --git a/channeldb/kvdb/bbolt.go b/channeldb/kvdb/bbolt.go deleted file mode 100644 index b249e7db..00000000 --- a/channeldb/kvdb/bbolt.go +++ /dev/null @@ -1,10 +0,0 @@ -package kvdb - -import ( - _ "github.com/btcsuite/btcwallet/walletdb/bdb" // Import to register backend. -) - -// BoltBackendName is the name of the backend that should be passed into -// kvdb.Create to initialize a new instance of kvdb.Backend backed by a live -// instance of bbolt. -const BoltBackendName = "bdb" diff --git a/channeldb/kvdb/config.go b/channeldb/kvdb/config.go new file mode 100644 index 00000000..a4ed68ba --- /dev/null +++ b/channeldb/kvdb/config.go @@ -0,0 +1,33 @@ +package kvdb + +// BoltBackendName is the name of the backend that should be passed into +// kvdb.Create to initialize a new instance of kvdb.Backend backed by a live +// instance of bbolt. +const BoltBackendName = "bdb" + +// EtcdBackendName is the name of the backend that should be passed into +// kvdb.Create to initialize a new instance of kvdb.Backend backed by a live +// instance of etcd. +const EtcdBackendName = "etcd" + +// BoltConfig holds bolt configuration. +type BoltConfig struct { + NoFreeListSync bool `long:"nofreelistsync" description:"If true, prevents the database from syncing its freelist to disk"` +} + +// EtcdConfig holds etcd configuration. +type EtcdConfig struct { + Host string `long:"host" description:"Etcd database host."` + + User string `long:"user" description:"Etcd database user."` + + Pass string `long:"pass" description:"Password for the database user."` + + CertFile string `long:"cert_file" description:"Path to the TLS certificate for etcd RPC."` + + KeyFile string `long:"key_file" description:"Path to the TLS private key for etcd RPC."` + + InsecureSkipVerify bool `long:"insecure_skip_verify" description:"Whether we intend to skip TLS verification"` + + CollectStats bool `long:"collect_stats" description:"Whether to collect etcd commit stats."` +} diff --git a/channeldb/kvdb/etcd.go b/channeldb/kvdb/etcd.go deleted file mode 100644 index 89ec463b..00000000 --- a/channeldb/kvdb/etcd.go +++ /dev/null @@ -1,10 +0,0 @@ -package kvdb - -import ( - _ "github.com/lightningnetwork/lnd/channeldb/kvdb/etcd" // Import to register backend. -) - -// EtcdBackendName is the name of the backend that should be passed into -// kvdb.Create to initialize a new instance of kvdb.Backend backed by a live -// instance of etcd. -const EtcdBackendName = "etcd" diff --git a/channeldb/kvdb/etcd/bucket.go b/channeldb/kvdb/etcd/bucket.go index 48a32b9c..fd4a3701 100644 --- a/channeldb/kvdb/etcd/bucket.go +++ b/channeldb/kvdb/etcd/bucket.go @@ -1,3 +1,5 @@ +// +build kvdb_etcd + package etcd import ( diff --git a/channeldb/kvdb/etcd/bucket_test.go b/channeldb/kvdb/etcd/bucket_test.go index 45d6155d..c9df2847 100644 --- a/channeldb/kvdb/etcd/bucket_test.go +++ b/channeldb/kvdb/etcd/bucket_test.go @@ -1,3 +1,5 @@ +// +build kvdb_etcd + package etcd // bkey is a helper functon used in tests to create a bucket key from passed diff --git a/channeldb/kvdb/etcd/db.go b/channeldb/kvdb/etcd/db.go index 7698ed91..0079d2c9 100644 --- a/channeldb/kvdb/etcd/db.go +++ b/channeldb/kvdb/etcd/db.go @@ -1,3 +1,5 @@ +// +build kvdb_etcd + package etcd import ( diff --git a/channeldb/kvdb/etcd/db_test.go b/channeldb/kvdb/etcd/db_test.go index ecf9b06c..69342207 100644 --- a/channeldb/kvdb/etcd/db_test.go +++ b/channeldb/kvdb/etcd/db_test.go @@ -1,3 +1,5 @@ +// +build kvdb_etcd + package etcd import ( diff --git a/channeldb/kvdb/etcd/driver.go b/channeldb/kvdb/etcd/driver.go index ccdfbf7d..8e313fc6 100644 --- a/channeldb/kvdb/etcd/driver.go +++ b/channeldb/kvdb/etcd/driver.go @@ -1,3 +1,5 @@ +// +build kvdb_etcd + package etcd import ( diff --git a/channeldb/kvdb/etcd/driver_test.go b/channeldb/kvdb/etcd/driver_test.go index ad8578c6..365eda7a 100644 --- a/channeldb/kvdb/etcd/driver_test.go +++ b/channeldb/kvdb/etcd/driver_test.go @@ -1,3 +1,5 @@ +// +build kvdb_etcd + package etcd import ( diff --git a/channeldb/kvdb/etcd/embed.go b/channeldb/kvdb/etcd/embed.go index 8e6ce802..f19363f3 100644 --- a/channeldb/kvdb/etcd/embed.go +++ b/channeldb/kvdb/etcd/embed.go @@ -1,3 +1,5 @@ +// +build kvdb_etcd + package etcd import ( diff --git a/channeldb/kvdb/etcd/fixture_test.go b/channeldb/kvdb/etcd/fixture_test.go index 7082f186..56526906 100644 --- a/channeldb/kvdb/etcd/fixture_test.go +++ b/channeldb/kvdb/etcd/fixture_test.go @@ -1,3 +1,5 @@ +// +build kvdb_etcd + package etcd import ( diff --git a/channeldb/kvdb/etcd/readwrite_bucket.go b/channeldb/kvdb/etcd/readwrite_bucket.go index 4adfa354..60a19cc1 100644 --- a/channeldb/kvdb/etcd/readwrite_bucket.go +++ b/channeldb/kvdb/etcd/readwrite_bucket.go @@ -1,3 +1,5 @@ +// +build kvdb_etcd + package etcd import ( diff --git a/channeldb/kvdb/etcd/readwrite_bucket_test.go b/channeldb/kvdb/etcd/readwrite_bucket_test.go index 8e919403..a3a5d620 100644 --- a/channeldb/kvdb/etcd/readwrite_bucket_test.go +++ b/channeldb/kvdb/etcd/readwrite_bucket_test.go @@ -1,3 +1,5 @@ +// +build kvdb_etcd + package etcd import ( diff --git a/channeldb/kvdb/etcd/readwrite_cursor.go b/channeldb/kvdb/etcd/readwrite_cursor.go index da30cb6f..98965693 100644 --- a/channeldb/kvdb/etcd/readwrite_cursor.go +++ b/channeldb/kvdb/etcd/readwrite_cursor.go @@ -1,3 +1,5 @@ +// +build kvdb_etcd + package etcd // readWriteCursor holds a reference to the cursors bucket, the value diff --git a/channeldb/kvdb/etcd/readwrite_cursor_test.go b/channeldb/kvdb/etcd/readwrite_cursor_test.go index fd8ca01d..c14de7aa 100644 --- a/channeldb/kvdb/etcd/readwrite_cursor_test.go +++ b/channeldb/kvdb/etcd/readwrite_cursor_test.go @@ -1,3 +1,5 @@ +// +build kvdb_etcd + package etcd import ( diff --git a/channeldb/kvdb/etcd/readwrite_tx.go b/channeldb/kvdb/etcd/readwrite_tx.go index 88e0a273..37914e8c 100644 --- a/channeldb/kvdb/etcd/readwrite_tx.go +++ b/channeldb/kvdb/etcd/readwrite_tx.go @@ -1,3 +1,5 @@ +// +build kvdb_etcd + package etcd import ( diff --git a/channeldb/kvdb/etcd/readwrite_tx_test.go b/channeldb/kvdb/etcd/readwrite_tx_test.go index 14a904c5..f65faa54 100644 --- a/channeldb/kvdb/etcd/readwrite_tx_test.go +++ b/channeldb/kvdb/etcd/readwrite_tx_test.go @@ -1,3 +1,5 @@ +// +build kvdb_etcd + package etcd import ( diff --git a/channeldb/kvdb/etcd/stm.go b/channeldb/kvdb/etcd/stm.go index a79cb9ba..a3f8c223 100644 --- a/channeldb/kvdb/etcd/stm.go +++ b/channeldb/kvdb/etcd/stm.go @@ -1,3 +1,5 @@ +// +build kvdb_etcd + package etcd import ( diff --git a/channeldb/kvdb/etcd/stm_test.go b/channeldb/kvdb/etcd/stm_test.go index 80b2ef1d..767963d4 100644 --- a/channeldb/kvdb/etcd/stm_test.go +++ b/channeldb/kvdb/etcd/stm_test.go @@ -1,3 +1,5 @@ +// +build kvdb_etcd + package etcd import ( diff --git a/channeldb/kvdb/etcd/walletdb_interface_test.go b/channeldb/kvdb/etcd/walletdb_interface_test.go index fe674acd..7d4ef023 100644 --- a/channeldb/kvdb/etcd/walletdb_interface_test.go +++ b/channeldb/kvdb/etcd/walletdb_interface_test.go @@ -1,3 +1,5 @@ +// +build kvdb_etcd + package etcd import ( diff --git a/channeldb/kvdb/kvdb_etcd.go b/channeldb/kvdb/kvdb_etcd.go new file mode 100644 index 00000000..b2662e09 --- /dev/null +++ b/channeldb/kvdb/kvdb_etcd.go @@ -0,0 +1,48 @@ +// +build kvdb_etcd + +package kvdb + +import ( + "github.com/lightningnetwork/lnd/channeldb/kvdb/etcd" +) + +// TestBackend is conditionally set to etcd when the kvdb_etcd build tag is +// defined, allowing testing our database code with etcd backend. +const TestBackend = EtcdBackendName + +// GetEtcdBackend returns an etcd backend configured according to the +// passed etcdConfig. +func GetEtcdBackend(etcdConfig *EtcdConfig) (Backend, error) { + // Config translation is needed here in order to keep the + // etcd package fully independent from the rest of the source tree. + backendConfig := etcd.BackendConfig{ + Host: etcdConfig.Host, + User: etcdConfig.User, + Pass: etcdConfig.Pass, + CertFile: etcdConfig.CertFile, + KeyFile: etcdConfig.KeyFile, + InsecureSkipVerify: etcdConfig.InsecureSkipVerify, + CollectCommitStats: etcdConfig.CollectStats, + } + + return Open(EtcdBackendName, backendConfig) +} + +// GetEtcdTestBackend creates an embedded etcd backend for testing +// storig the database at the passed path. +func GetEtcdTestBackend(path, name string) (Backend, func(), error) { + empty := func() {} + + config, cleanup, err := etcd.NewEmbeddedEtcdInstance(path) + if err != nil { + return nil, empty, err + } + + backend, err := Open(EtcdBackendName, *config) + if err != nil { + cleanup() + return nil, empty, err + } + + return backend, cleanup, nil +} diff --git a/channeldb/kvdb/kvdb_no_etcd.go b/channeldb/kvdb/kvdb_no_etcd.go new file mode 100644 index 00000000..1758551f --- /dev/null +++ b/channeldb/kvdb/kvdb_no_etcd.go @@ -0,0 +1,24 @@ +// +build !kvdb_etcd + +package kvdb + +import ( + "fmt" +) + +// TestBackend is conditionally set to bdb when the kvdb_etcd build tag is +// not defined, allowing testing our database code with bolt backend. +const TestBackend = BoltBackendName + +var errEtcdNotAvailable = fmt.Errorf("etcd backend not available") + +// GetEtcdBackend is a stub returning nil and errEtcdNotAvailable error. +func GetEtcdBackend(etcdConfig *EtcdConfig) (Backend, error) { + return nil, errEtcdNotAvailable +} + +// GetTestEtcdBackend is a stub returning nil, an empty closure and an +// errEtcdNotAvailable error. +func GetEtcdTestBackend(path, name string) (Backend, func(), error) { + return nil, func() {}, errEtcdNotAvailable +} diff --git a/channeldb/kvdb/kvdb_test_bdb.go b/channeldb/kvdb/kvdb_test_bdb.go deleted file mode 100644 index fcd08311..00000000 --- a/channeldb/kvdb/kvdb_test_bdb.go +++ /dev/null @@ -1,5 +0,0 @@ -// +build !kvdb_etcd - -package kvdb - -const TestBackend = "bdb" diff --git a/channeldb/kvdb/kvdb_test_etcd.go b/channeldb/kvdb/kvdb_test_etcd.go deleted file mode 100644 index c6e5070a..00000000 --- a/channeldb/kvdb/kvdb_test_etcd.go +++ /dev/null @@ -1,5 +0,0 @@ -// +build kvdb_etcd - -package kvdb - -const TestBackend = "etcd" diff --git a/lncfg/db.go b/lncfg/db.go index d303e58a..c0f47fc7 100644 --- a/lncfg/db.go +++ b/lncfg/db.go @@ -4,7 +4,6 @@ import ( "fmt" "github.com/lightningnetwork/lnd/channeldb/kvdb" - "github.com/lightningnetwork/lnd/channeldb/kvdb/etcd" ) const ( @@ -13,42 +12,20 @@ const ( etcdBackend = "etcd" ) -// BoltDB holds bolt configuration. -type BoltDB struct { - NoFreeListSync bool `long:"nofreelistsync" description:"If true, prevents the database from syncing its freelist to disk"` -} - -// EtcdDB hold etcd configuration. -type EtcdDB struct { - Host string `long:"host" description:"Etcd database host."` - - User string `long:"user" description:"Etcd database user."` - - Pass string `long:"pass" description:"Password for the database user."` - - CertFile string `long:"cert_file" description:"Path to the TLS certificate for etcd RPC."` - - KeyFile string `long:"key_file" description:"Path to the TLS private key for etcd RPC."` - - InsecureSkipVerify bool `long:"insecure_skip_verify" description:"Whether we intend to skip TLS verification"` - - CollectStats bool `long:"collect_stats" description:"Wheter to collect etcd commit stats."` -} - // DB holds database configuration for LND. type DB struct { Backend string `long:"backend" description:"The selected database backend."` - Etcd *EtcdDB `group:"etcd" namespace:"etcd" description:"Etcd settings."` + Etcd *kvdb.EtcdConfig `group:"etcd" namespace:"etcd" description:"Etcd settings."` - Bolt *BoltDB `group:"bolt" namespace:"bolt" description:"Bolt settings."` + Bolt *kvdb.BoltConfig `group:"bolt" namespace:"bolt" description:"Bolt settings."` } // NewDB creates and returns a new default DB config. func DefaultDB() *DB { return &DB{ Backend: boltBackend, - Bolt: &BoltDB{ + Bolt: &kvdb.BoltConfig{ NoFreeListSync: true, }, } @@ -75,16 +52,7 @@ func (db *DB) Validate() error { // GetBackend returns a kvdb.Backend as set in the DB config. func (db *DB) GetBackend(path string) (kvdb.Backend, error) { if db.Backend == etcdBackend { - backendConfig := etcd.BackendConfig{ - Host: db.Etcd.Host, - User: db.Etcd.User, - Pass: db.Etcd.Pass, - CertFile: db.Etcd.CertFile, - KeyFile: db.Etcd.KeyFile, - InsecureSkipVerify: db.Etcd.InsecureSkipVerify, - CollectCommitStats: db.Etcd.CollectStats, - } - return kvdb.Open(kvdb.EtcdBackendName, backendConfig) + return kvdb.GetEtcdBackend(db.Etcd) } return kvdb.GetBoltBackend(path, dbName, db.Bolt.NoFreeListSync)