loopd+lndclient: extract config parameters into struct

pull/181/head
Oliver Gugger 4 years ago
parent 899d04470b
commit a8e6118cfb
No known key found for this signature in database
GPG Key ID: 8E4256593F177720

@ -17,6 +17,30 @@ import (
var rpcTimeout = 30 * time.Second var rpcTimeout = 30 * time.Second
// LndServicesConfig holds all configuration settings that are needed to connect
// to an lnd node.
type LndServicesConfig struct {
// LndAddress is the network address (host:port) of the lnd node to
// connect to.
LndAddress string
// Network is the bitcoin network we expect the lnd node to operate on.
Network string
// MacaroonDir is the directory where all lnd macaroons can be found.
MacaroonDir string
// TLSPath is the path to lnd's TLS certificate file.
TLSPath string
// Dialer is an optional dial function that can be passed in if the
// default lncfg.ClientAddressDialer should not be used.
Dialer DialerFunc
}
// DialerFunc is a function that is used as grpc.WithContextDialer().
type DialerFunc func(context.Context, string) (net.Conn, error)
// LndServices constitutes a set of required services. // LndServices constitutes a set of required services.
type LndServices struct { type LndServices struct {
Client LightningClient Client LightningClient
@ -40,27 +64,18 @@ type GrpcLndServices struct {
// NewLndServices creates creates a connection to the given lnd instance and // NewLndServices creates creates a connection to the given lnd instance and
// creates a set of required RPC services. // creates a set of required RPC services.
func NewLndServices(lndAddress, network, macaroonDir, tlsPath string) ( func NewLndServices(cfg *LndServicesConfig) (*GrpcLndServices, error) {
*GrpcLndServices, error) {
// We need to use a custom dialer so we can also connect to unix // We need to use a custom dialer so we can also connect to unix
// sockets and not just TCP addresses. // sockets and not just TCP addresses.
dialer := lncfg.ClientAddressDialer(defaultRPCPort) if cfg.Dialer == nil {
cfg.Dialer = lncfg.ClientAddressDialer(defaultRPCPort)
return NewLndServicesWithDialer( }
dialer, lndAddress, network, macaroonDir, tlsPath,
)
}
// NewLndServices creates a set of required RPC services by connecting to lnd
// using the given dialer.
func NewLndServicesWithDialer(dialer dialerFunc, lndAddress, network,
macaroonDir, tlsPath string) (*GrpcLndServices, error) {
// Based on the network, if the macaroon directory isn't set, then // Based on the network, if the macaroon directory isn't set, then
// we'll use the expected default locations. // we'll use the expected default locations.
macaroonDir := cfg.MacaroonDir
if macaroonDir == "" { if macaroonDir == "" {
switch network { switch cfg.Network {
case "testnet": case "testnet":
macaroonDir = filepath.Join( macaroonDir = filepath.Join(
defaultLndDir, defaultDataDir, defaultLndDir, defaultDataDir,
@ -87,20 +102,20 @@ func NewLndServicesWithDialer(dialer dialerFunc, lndAddress, network,
default: default:
return nil, fmt.Errorf("unsupported network: %v", return nil, fmt.Errorf("unsupported network: %v",
network) cfg.Network)
} }
} }
// Setup connection with lnd // Setup connection with lnd
log.Infof("Creating lnd connection to %v", lndAddress) log.Infof("Creating lnd connection to %v", cfg.LndAddress)
conn, err := getClientConn(dialer, lndAddress, tlsPath) conn, err := getClientConn(cfg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
log.Infof("Connected to lnd") log.Infof("Connected to lnd")
chainParams, err := swap.ChainParamsFromNetwork(network) chainParams, err := swap.ChainParamsFromNetwork(cfg.Network)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -117,7 +132,7 @@ func NewLndServicesWithDialer(dialer dialerFunc, lndAddress, network,
if err != nil { if err != nil {
return nil, err return nil, err
} }
err = checkLndCompatibility(conn, chainParams, readonlyMac, network) err = checkLndCompatibility(conn, chainParams, readonlyMac, cfg.Network)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -176,7 +191,7 @@ func NewLndServicesWithDialer(dialer dialerFunc, lndAddress, network,
cleanup: cleanup, cleanup: cleanup,
} }
log.Infof("Using network %v", network) log.Infof("Using network %v", cfg.Network)
return services, nil return services, nil
} }
@ -238,13 +253,11 @@ var (
maxMsgRecvSize = grpc.MaxCallRecvMsgSize(1 * 1024 * 1024 * 200) maxMsgRecvSize = grpc.MaxCallRecvMsgSize(1 * 1024 * 1024 * 200)
) )
type dialerFunc func(context.Context, string) (net.Conn, error) func getClientConn(cfg *LndServicesConfig) (*grpc.ClientConn, error) {
func getClientConn(dialer dialerFunc, address string, tlsPath string) (
*grpc.ClientConn, error) {
// Load the specified TLS certificate and build transport credentials // Load the specified TLS certificate and build transport credentials
// with it. // with it.
tlsPath := cfg.TLSPath
if tlsPath == "" { if tlsPath == "" {
tlsPath = defaultTLSCertPath tlsPath = defaultTLSCertPath
} }
@ -260,12 +273,13 @@ func getClientConn(dialer dialerFunc, address string, tlsPath string) (
// Use a custom dialer, to allow connections to unix sockets, // Use a custom dialer, to allow connections to unix sockets,
// in-memory listeners etc, and not just TCP addresses. // in-memory listeners etc, and not just TCP addresses.
grpc.WithContextDialer(dialer), grpc.WithContextDialer(cfg.Dialer),
} }
conn, err := grpc.Dial(address, opts...) conn, err := grpc.Dial(cfg.LndAddress, opts...)
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to connect to RPC server: %v", err) return nil, fmt.Errorf("unable to connect to RPC server: %v",
err)
} }
return conn, nil return conn, nil

@ -54,24 +54,23 @@ func newListenerCfg(config *config, rpcCfg RPCConfig) *listenerCfg {
getLnd: func(network string, cfg *lndConfig) ( getLnd: func(network string, cfg *lndConfig) (
*lndclient.GrpcLndServices, error) { *lndclient.GrpcLndServices, error) {
svcCfg := &lndclient.LndServicesConfig{
LndAddress: cfg.Host,
Network: network,
MacaroonDir: cfg.MacaroonDir,
TLSPath: cfg.TLSPath,
}
// If a custom lnd connection is specified we use that // If a custom lnd connection is specified we use that
// directly. // directly.
if rpcCfg.LndConn != nil { if rpcCfg.LndConn != nil {
dialer := func(context.Context, string) ( svcCfg.Dialer = func(context.Context, string) (
net.Conn, error) { net.Conn, error) {
return rpcCfg.LndConn, nil return rpcCfg.LndConn, nil
} }
return lndclient.NewLndServicesWithDialer(
dialer,
rpcCfg.LndConn.RemoteAddr().String(),
network, cfg.MacaroonDir, cfg.TLSPath,
)
} }
return lndclient.NewLndServices( return lndclient.NewLndServices(svcCfg)
cfg.Host, network, cfg.MacaroonDir, cfg.TLSPath,
)
}, },
} }
} }

Loading…
Cancel
Save