2020-05-15 17:59:37 +03:00
|
|
|
// +build kvdb_etcd
|
|
|
|
|
|
|
|
package kvdb
|
|
|
|
|
|
|
|
import (
|
2020-05-25 19:08:58 +03:00
|
|
|
"context"
|
|
|
|
|
2020-05-15 17:59:37 +03:00
|
|
|
"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.
|
2020-05-25 19:08:58 +03:00
|
|
|
func GetEtcdBackend(ctx context.Context, prefix string,
|
|
|
|
etcdConfig *EtcdConfig) (Backend, error) {
|
|
|
|
|
2020-05-15 17:59:37 +03:00
|
|
|
// Config translation is needed here in order to keep the
|
|
|
|
// etcd package fully independent from the rest of the source tree.
|
|
|
|
backendConfig := etcd.BackendConfig{
|
2020-05-25 19:08:58 +03:00
|
|
|
Ctx: ctx,
|
2020-05-15 17:59:37 +03:00
|
|
|
Host: etcdConfig.Host,
|
|
|
|
User: etcdConfig.User,
|
|
|
|
Pass: etcdConfig.Pass,
|
|
|
|
CertFile: etcdConfig.CertFile,
|
|
|
|
KeyFile: etcdConfig.KeyFile,
|
|
|
|
InsecureSkipVerify: etcdConfig.InsecureSkipVerify,
|
2020-05-12 17:02:46 +03:00
|
|
|
Prefix: prefix,
|
2020-12-16 20:23:06 +03:00
|
|
|
Namespace: etcdConfig.Namespace,
|
2020-05-15 17:59:37 +03:00
|
|
|
CollectCommitStats: etcdConfig.CollectStats,
|
|
|
|
}
|
|
|
|
|
|
|
|
return Open(EtcdBackendName, backendConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetEtcdTestBackend creates an embedded etcd backend for testing
|
|
|
|
// storig the database at the passed path.
|
2021-01-06 21:40:30 +03:00
|
|
|
func GetEtcdTestBackend(path string, clientPort, peerPort uint16) (
|
|
|
|
Backend, func(), error) {
|
|
|
|
|
2020-05-15 17:59:37 +03:00
|
|
|
empty := func() {}
|
|
|
|
|
2021-01-06 21:40:30 +03:00
|
|
|
config, cleanup, err := etcd.NewEmbeddedEtcdInstance(
|
|
|
|
path, clientPort, peerPort,
|
|
|
|
)
|
2020-05-15 17:59:37 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, empty, err
|
|
|
|
}
|
|
|
|
|
|
|
|
backend, err := Open(EtcdBackendName, *config)
|
|
|
|
if err != nil {
|
|
|
|
cleanup()
|
|
|
|
return nil, empty, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return backend, cleanup, nil
|
|
|
|
}
|