From 644b5b16f70ce60ebb86001a4d39b4b7ec8e471d Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Fri, 3 Jan 2020 14:01:31 +0100 Subject: [PATCH 1/9] cmd+loopd: move loopd to new folder loopd --- .gitignore | 3 --- {cmd/loopd => loopd}/config.go | 2 +- {cmd/loopd => loopd}/daemon.go | 2 +- {cmd/loopd => loopd}/log.go | 2 +- {cmd/loopd => loopd}/main.go | 2 +- {cmd/loopd => loopd}/swapclient_server.go | 2 +- {cmd/loopd => loopd}/utils.go | 2 +- {cmd/loopd => loopd}/view.go | 2 +- 8 files changed, 7 insertions(+), 10 deletions(-) rename {cmd/loopd => loopd}/config.go (99%) rename {cmd/loopd => loopd}/daemon.go (99%) rename {cmd/loopd => loopd}/log.go (98%) rename {cmd/loopd => loopd}/main.go (99%) rename {cmd/loopd => loopd}/swapclient_server.go (99%) rename {cmd/loopd => loopd}/utils.go (98%) rename {cmd/loopd => loopd}/view.go (99%) diff --git a/.gitignore b/.gitignore index ec3842d..ad90636 100644 --- a/.gitignore +++ b/.gitignore @@ -29,9 +29,6 @@ output*.log loop !cmd/loop/ -loopd -!cmd/loopd/ - *.key *.hex diff --git a/cmd/loopd/config.go b/loopd/config.go similarity index 99% rename from cmd/loopd/config.go rename to loopd/config.go index 690665b..fdeb496 100644 --- a/cmd/loopd/config.go +++ b/loopd/config.go @@ -1,4 +1,4 @@ -package main +package loopd import ( "path/filepath" diff --git a/cmd/loopd/daemon.go b/loopd/daemon.go similarity index 99% rename from cmd/loopd/daemon.go rename to loopd/daemon.go index 11f0790..eb06360 100644 --- a/cmd/loopd/daemon.go +++ b/loopd/daemon.go @@ -1,4 +1,4 @@ -package main +package loopd import ( "context" diff --git a/cmd/loopd/log.go b/loopd/log.go similarity index 98% rename from cmd/loopd/log.go rename to loopd/log.go index d0ff029..dd1811a 100644 --- a/cmd/loopd/log.go +++ b/loopd/log.go @@ -1,4 +1,4 @@ -package main +package loopd import ( "github.com/btcsuite/btclog" diff --git a/cmd/loopd/main.go b/loopd/main.go similarity index 99% rename from cmd/loopd/main.go rename to loopd/main.go index 3be4345..4398334 100644 --- a/cmd/loopd/main.go +++ b/loopd/main.go @@ -1,4 +1,4 @@ -package main +package loopd import ( "fmt" diff --git a/cmd/loopd/swapclient_server.go b/loopd/swapclient_server.go similarity index 99% rename from cmd/loopd/swapclient_server.go rename to loopd/swapclient_server.go index 4c1ae4d..232061f 100644 --- a/cmd/loopd/swapclient_server.go +++ b/loopd/swapclient_server.go @@ -1,4 +1,4 @@ -package main +package loopd import ( "context" diff --git a/cmd/loopd/utils.go b/loopd/utils.go similarity index 98% rename from cmd/loopd/utils.go rename to loopd/utils.go index 601957e..a2c44af 100644 --- a/cmd/loopd/utils.go +++ b/loopd/utils.go @@ -1,4 +1,4 @@ -package main +package loopd import ( "os" diff --git a/cmd/loopd/view.go b/loopd/view.go similarity index 99% rename from cmd/loopd/view.go rename to loopd/view.go index 50e3f4e..dabe82c 100644 --- a/cmd/loopd/view.go +++ b/loopd/view.go @@ -1,4 +1,4 @@ -package main +package loopd import ( "fmt" From 704bfff8f64882e4588e3b95bbe57d2a4e4f71cc Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Fri, 3 Jan 2020 14:01:31 +0100 Subject: [PATCH 2/9] cmd+loopd: new cmd/loopd main method replacing the one now in ./loopd This allows loopd to be used as a library by importing it and calling Start. --- cmd/loopd/main.go | 14 ++++++++++++++ loopd/{main.go => start.go} | 9 +-------- 2 files changed, 15 insertions(+), 8 deletions(-) create mode 100644 cmd/loopd/main.go rename loopd/{main.go => start.go} (96%) diff --git a/cmd/loopd/main.go b/cmd/loopd/main.go new file mode 100644 index 0000000..dac8935 --- /dev/null +++ b/cmd/loopd/main.go @@ -0,0 +1,14 @@ +package main + +import ( + "fmt" + + "github.com/lightninglabs/loop/loopd" +) + +func main() { + err := loopd.Start() + if err != nil { + fmt.Println(err) + } +} diff --git a/loopd/main.go b/loopd/start.go similarity index 96% rename from loopd/main.go rename to loopd/start.go index 4398334..2178b68 100644 --- a/loopd/main.go +++ b/loopd/start.go @@ -26,14 +26,7 @@ var ( swapsLock sync.Mutex ) -func main() { - err := start() - if err != nil { - fmt.Println(err) - } -} - -func start() error { +func Start() error { config := defaultConfig // Parse command line flags. From 80b071a2e651c054599521da2c0435538ec22963 Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Fri, 3 Jan 2020 14:01:31 +0100 Subject: [PATCH 3/9] mod+lndclient: update to lnd version with non-deprecated dialer --- go.mod | 2 +- go.sum | 9 +++++---- lndclient/basic_client.go | 2 +- lndclient/lnd_services.go | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index a0ad3d9..9a3bfd3 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/google/go-cmp v0.3.1 // indirect github.com/grpc-ecosystem/grpc-gateway v1.10.0 github.com/jessevdk/go-flags v1.4.0 - github.com/lightningnetwork/lnd v0.8.0-beta-rc3.0.20191212054348-dca31c2bf8be + github.com/lightningnetwork/lnd v0.8.0-beta-rc3.0.20200103000305-22e1f006b194 github.com/lightningnetwork/lnd/queue v1.0.2 github.com/urfave/cli v1.20.0 golang.org/x/crypto v0.0.0-20190829043050-9756ffdc2472 // indirect diff --git a/go.sum b/go.sum index c1dbd35..f77ece8 100644 --- a/go.sum +++ b/go.sum @@ -136,10 +136,11 @@ github.com/lightninglabs/gozmq v0.0.0-20191113021534-d20a764486bf h1:HZKvJUHlcXI github.com/lightninglabs/gozmq v0.0.0-20191113021534-d20a764486bf/go.mod h1:vxmQPeIQxPf6Jf9rM8R+B4rKBqLA2AjttNxkFBL2Plk= github.com/lightninglabs/neutrino v0.11.0 h1:lPpYFCtsfJX2W5zI4pWycPmbbBdr7zU+BafYdLoD6k0= github.com/lightninglabs/neutrino v0.11.0/go.mod h1:CuhF0iuzg9Sp2HO6ZgXgayviFTn1QHdSTJlMncK80wg= -github.com/lightningnetwork/lightning-onion v0.0.0-20190909101754-850081b08b6a h1:GoWPN4i4jTKRxhVNh9a2vvBBO1Y2seiJB+SopUYoKyo= -github.com/lightningnetwork/lightning-onion v0.0.0-20190909101754-850081b08b6a/go.mod h1:rigfi6Af/KqsF7Za0hOgcyq2PNH4AN70AaMRxcJkff4= -github.com/lightningnetwork/lnd v0.8.0-beta-rc3.0.20191212054348-dca31c2bf8be h1:CX8ScoYxJju6AcceHY6nATfRLThZpewqHcgn4sGMdZE= -github.com/lightningnetwork/lnd v0.8.0-beta-rc3.0.20191212054348-dca31c2bf8be/go.mod h1:60/zDjDYaYPmISAm3J1WWKyyt+ZiAvuET1XzglO8s70= +github.com/lightninglabs/protobuf-hex-display v1.3.3-0.20191212020323-b444784ce75d/go.mod h1:KDb67YMzoh4eudnzClmvs2FbiLG9vxISmLApUkCa4uI= +github.com/lightningnetwork/lightning-onion v0.0.0-20191214001659-f34e9dc1651d h1:U50MHOOeL6gR3Ee/l0eMvZMpmRo+ydzmlQuIruCyCsA= +github.com/lightningnetwork/lightning-onion v0.0.0-20191214001659-f34e9dc1651d/go.mod h1:rigfi6Af/KqsF7Za0hOgcyq2PNH4AN70AaMRxcJkff4= +github.com/lightningnetwork/lnd v0.8.0-beta-rc3.0.20200103000305-22e1f006b194 h1:PCzjJcVWcMbkiQvzFNc3ta0JmiMprFDqzMZsSpd/km8= +github.com/lightningnetwork/lnd v0.8.0-beta-rc3.0.20200103000305-22e1f006b194/go.mod h1:WHK90FD3m2n6OyWzondS7ho0Uhtgfp30Nxvj24lQYX4= github.com/lightningnetwork/lnd/cert v1.0.0 h1:J0gtf2UNQX2U+/j5cXnX2wIMSTuJuwrXv7m9qJr2wtw= github.com/lightningnetwork/lnd/cert v1.0.0/go.mod h1:fmtemlSMf5t4hsQmcprSoOykypAPp+9c+0d0iqTScMo= github.com/lightningnetwork/lnd/queue v1.0.1 h1:jzJKcTy3Nj5lQrooJ3aaw9Lau3I0IwvQR5sqtjdv2R0= diff --git a/lndclient/basic_client.go b/lndclient/basic_client.go index 917e9f1..f4f18c1 100644 --- a/lndclient/basic_client.go +++ b/lndclient/basic_client.go @@ -103,7 +103,7 @@ func NewBasicClient(lndHost, tlsPath, macDir, network string, basicOptions ...Ba // We need to use a custom dialer so we can also connect to unix sockets // and not just TCP addresses. opts = append( - opts, grpc.WithDialer( + opts, grpc.WithContextDialer( lncfg.ClientAddressDialer(defaultRPCPort), ), ) diff --git a/lndclient/lnd_services.go b/lndclient/lnd_services.go index a7b1678..3aa5a5c 100644 --- a/lndclient/lnd_services.go +++ b/lndclient/lnd_services.go @@ -211,7 +211,7 @@ func getClientConn(address string, network string, tlsPath string) ( // We need to use a custom dialer so we can also connect to unix sockets // and not just TCP addresses. opts = append( - opts, grpc.WithDialer( + opts, grpc.WithContextDialer( lncfg.ClientAddressDialer(defaultRPCPort), ), ) From 2334816a598df0369e14ecc7132c7d62441f681d Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Fri, 3 Jan 2020 14:01:31 +0100 Subject: [PATCH 4/9] lndclient: split NewLndServices to take gRPC dialer --- lndclient/lnd_services.go | 39 +++++++++++++++++++++++++++------------ loopd/utils.go | 2 +- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/lndclient/lnd_services.go b/lndclient/lnd_services.go index 3aa5a5c..c6009ac 100644 --- a/lndclient/lnd_services.go +++ b/lndclient/lnd_services.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "net" "path/filepath" "time" @@ -38,9 +39,24 @@ type GrpcLndServices struct { cleanup func() } -// NewLndServices creates a set of required RPC services. -func NewLndServices(lndAddress, application, network, macaroonDir, - tlsPath string) (*GrpcLndServices, error) { +// NewLndServices creates creates a connection to the given lnd instance and +// creates a set of required RPC services. +func NewLndServices(lndAddress, network, macaroonDir, tlsPath string) ( + *GrpcLndServices, error) { + + // We need to use a custom dialer so we can also connect to unix + // sockets and not just TCP addresses. + 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 // we'll use the expected default locations. @@ -85,7 +101,7 @@ func NewLndServices(lndAddress, application, network, macaroonDir, // Setup connection with lnd log.Infof("Creating lnd connection to %v", lndAddress) - conn, err := getClientConn(lndAddress, network, tlsPath) + conn, err := getClientConn(dialer, lndAddress, tlsPath) if err != nil { return nil, err } @@ -189,7 +205,9 @@ var ( maxMsgRecvSize = grpc.MaxCallRecvMsgSize(1 * 1024 * 1024 * 200) ) -func getClientConn(address string, network string, tlsPath string) ( +type dialerFunc func(context.Context, string) (net.Conn, error) + +func getClientConn(dialer dialerFunc, address string, tlsPath string) ( *grpc.ClientConn, error) { // Load the specified TLS certificate and build transport credentials @@ -206,15 +224,12 @@ func getClientConn(address string, network string, tlsPath string) ( // Create a dial options array. opts := []grpc.DialOption{ grpc.WithTransportCredentials(creds), + + // Use a custom dialer, to allow connections to unix sockets, + // in-memory listeners etc, and not just TCP addresses. + grpc.WithContextDialer(dialer), } - // We need to use a custom dialer so we can also connect to unix sockets - // and not just TCP addresses. - opts = append( - opts, grpc.WithContextDialer( - lncfg.ClientAddressDialer(defaultRPCPort), - ), - ) conn, err := grpc.Dial(address, opts...) if err != nil { return nil, fmt.Errorf("unable to connect to RPC server: %v", err) diff --git a/loopd/utils.go b/loopd/utils.go index a2c44af..10bb449 100644 --- a/loopd/utils.go +++ b/loopd/utils.go @@ -11,7 +11,7 @@ import ( // getLnd returns an instance of the lnd services proxy. func getLnd(network string, cfg *lndConfig) (*lndclient.GrpcLndServices, error) { return lndclient.NewLndServices( - cfg.Host, "client", network, cfg.MacaroonDir, cfg.TLSPath, + cfg.Host, network, cfg.MacaroonDir, cfg.TLSPath, ) } From 391ae9dc6ba54b12a800d7f52663fe53b5832c3b Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Fri, 3 Jan 2020 14:01:31 +0100 Subject: [PATCH 5/9] loopd/daemon: handle proxy error Check returned error to make linter happy. --- loopd/daemon.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/loopd/daemon.go b/loopd/daemon.go index eb06360..d6247b5 100644 --- a/loopd/daemon.go +++ b/loopd/daemon.go @@ -103,7 +103,15 @@ func daemon(config *config) error { } defer restListener.Close() proxy := &http.Server{Handler: mux} - go proxy.Serve(restListener) + + go func() { + err := proxy.Serve(restListener) + // ErrServerClosed is always returned when the proxy is shut + // down, so don't log it. + if err != nil && err != http.ErrServerClosed { + log.Error(err) + } + }() statusChan := make(chan loop.SwapInfo) From 0f0573c0475540fadf4d2b703516e7a0457c8e99 Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Fri, 3 Jan 2020 14:01:31 +0100 Subject: [PATCH 6/9] loopd/daemon: make server gRPC/REST listeners configurable --- loopd/daemon.go | 15 ++++++++++++--- loopd/start.go | 17 ++++++++++++++++- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/loopd/daemon.go b/loopd/daemon.go index d6247b5..f567c57 100644 --- a/loopd/daemon.go +++ b/loopd/daemon.go @@ -18,9 +18,18 @@ import ( "google.golang.org/grpc" ) +// listenerCfg holds closures used to retrieve listeners for the gRPC services. +type listenerCfg struct { + // grpcListener returns a listener to use for the gRPC server. + grpcListener func() (net.Listener, error) + + // restListener returns a listener to use for the REST proxy. + restListener func() (net.Listener, error) +} + // daemon runs loopd in daemon mode. It will listen for grpc connections, // execute commands and pass back swap status information. -func daemon(config *config) error { +func daemon(config *config, lisCfg *listenerCfg) error { lnd, err := getLnd(config.Network, config.Lnd) if err != nil { return err @@ -74,7 +83,7 @@ func daemon(config *config) error { // Next, start the gRPC server listening for HTTP/2 connections. log.Infof("Starting gRPC listener") - grpcListener, err := net.Listen("tcp", config.RPCListen) + grpcListener, err := lisCfg.grpcListener() if err != nil { return fmt.Errorf("RPC server unable to listen on %s", config.RPCListen) @@ -96,7 +105,7 @@ func daemon(config *config) error { } log.Infof("Starting REST proxy listener") - restListener, err := net.Listen("tcp", config.RESTListen) + restListener, err := lisCfg.restListener() if err != nil { return fmt.Errorf("REST proxy unable to listen on %s", config.RESTListen) diff --git a/loopd/start.go b/loopd/start.go index 2178b68..7f3bbb6 100644 --- a/loopd/start.go +++ b/loopd/start.go @@ -2,6 +2,7 @@ package loopd import ( "fmt" + "net" "os" "path/filepath" "strings" @@ -26,6 +27,18 @@ var ( swapsLock sync.Mutex ) +// newListenerCfg creates and returns a new listenerCfg from the passed config. +func newListenerCfg(config *config) *listenerCfg { + return &listenerCfg{ + grpcListener: func() (net.Listener, error) { + return net.Listen("tcp", config.RPCListen) + }, + restListener: func() (net.Listener, error) { + return net.Listen("tcp", config.RESTListen) + }, + } +} + func Start() error { config := defaultConfig @@ -99,9 +112,11 @@ func Start() error { // Print the version before executing either primary directive. log.Infof("Version: %v", loop.Version()) + lisCfg := newListenerCfg(&config) + // Execute command. if parser.Active == nil { - return daemon(&config) + return daemon(&config, lisCfg) } if parser.Active.Name == "view" { From c7e8c9f964034f0ef98358c4206892a1799e1d8c Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Fri, 3 Jan 2020 14:01:32 +0100 Subject: [PATCH 7/9] loopd: make RPC listener configurable Allows to use external listener, such as in-memory. --- cmd/loopd/main.go | 3 ++- loopd/daemon.go | 34 ++++++++++++++++++++++------------ loopd/start.go | 30 ++++++++++++++++++++++++++---- 3 files changed, 50 insertions(+), 17 deletions(-) diff --git a/cmd/loopd/main.go b/cmd/loopd/main.go index dac8935..0310198 100644 --- a/cmd/loopd/main.go +++ b/cmd/loopd/main.go @@ -7,7 +7,8 @@ import ( ) func main() { - err := loopd.Start() + cfg := loopd.RPCConfig{} + err := loopd.Start(cfg) if err != nil { fmt.Println(err) } diff --git a/loopd/daemon.go b/loopd/daemon.go index f567c57..2695482 100644 --- a/loopd/daemon.go +++ b/loopd/daemon.go @@ -104,23 +104,30 @@ func daemon(config *config, lisCfg *listenerCfg) error { return err } - log.Infof("Starting REST proxy listener") restListener, err := lisCfg.restListener() if err != nil { return fmt.Errorf("REST proxy unable to listen on %s", config.RESTListen) } - defer restListener.Close() - proxy := &http.Server{Handler: mux} - go func() { - err := proxy.Serve(restListener) - // ErrServerClosed is always returned when the proxy is shut - // down, so don't log it. - if err != nil && err != http.ErrServerClosed { - log.Error(err) - } - }() + // A nil listener indicates REST is disabled. + if restListener != nil { + log.Infof("Starting REST proxy listener") + + defer restListener.Close() + proxy := &http.Server{Handler: mux} + + go func() { + err := proxy.Serve(restListener) + // ErrServerClosed is always returned when the proxy is + // shut down, so don't log it. + if err != nil && err != http.ErrServerClosed { + log.Error(err) + } + }() + } else { + log.Infof("REST proxy disabled") + } statusChan := make(chan loop.SwapInfo) @@ -178,7 +185,10 @@ func daemon(config *config, lisCfg *listenerCfg) error { defer wg.Done() log.Infof("RPC server listening on %s", grpcListener.Addr()) - log.Infof("REST proxy listening on %s", restListener.Addr()) + + if restListener != nil { + log.Infof("REST proxy listening on %s", restListener.Addr()) + } err = grpcServer.Serve(grpcListener) if err != nil { diff --git a/loopd/start.go b/loopd/start.go index 7f3bbb6..6cd5db6 100644 --- a/loopd/start.go +++ b/loopd/start.go @@ -27,19 +27,41 @@ var ( swapsLock sync.Mutex ) -// newListenerCfg creates and returns a new listenerCfg from the passed config. -func newListenerCfg(config *config) *listenerCfg { +// RPCConfig holds optional options that can be used to make the loop daemon +// communicate on custom connections. +type RPCConfig struct { + // RPCListener is an optional listener that if set will override the + // daemon's gRPC settings, and make the gRPC server listen on this + // listener. + // Note that setting this will also disable REST. + RPCListener net.Listener +} + +// newListenerCfg creates and returns a new listenerCfg from the passed config +// and RPCConfig. +func newListenerCfg(config *config, rpcCfg RPCConfig) *listenerCfg { return &listenerCfg{ grpcListener: func() (net.Listener, error) { + // If a custom RPC listener is set, we will listen on + // it instead of the regular tcp socket. + if rpcCfg.RPCListener != nil { + return rpcCfg.RPCListener, nil + } + return net.Listen("tcp", config.RPCListen) }, restListener: func() (net.Listener, error) { + // If a custom RPC listener is set, we disable REST. + if rpcCfg.RPCListener != nil { + return nil, nil + } + return net.Listen("tcp", config.RESTListen) }, } } -func Start() error { +func Start(rpcCfg RPCConfig) error { config := defaultConfig // Parse command line flags. @@ -112,7 +134,7 @@ func Start() error { // Print the version before executing either primary directive. log.Infof("Version: %v", loop.Version()) - lisCfg := newListenerCfg(&config) + lisCfg := newListenerCfg(&config, rpcCfg) // Execute command. if parser.Active == nil { From 34c2e71f9ee78cb65515d0cc4caf0e2de949cf17 Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Fri, 3 Jan 2020 14:01:32 +0100 Subject: [PATCH 8/9] loopd: move lnd services init to config This allows us to supply custom lnd connections to the daemon. --- loopd/daemon.go | 6 +++++- loopd/start.go | 10 +++++++++- loopd/utils.go | 7 ------- loopd/view.go | 4 ++-- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/loopd/daemon.go b/loopd/daemon.go index 2695482..a72219e 100644 --- a/loopd/daemon.go +++ b/loopd/daemon.go @@ -14,6 +14,7 @@ import ( proxy "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/lightninglabs/loop" + "github.com/lightninglabs/loop/lndclient" "github.com/lightninglabs/loop/looprpc" "google.golang.org/grpc" ) @@ -25,12 +26,15 @@ type listenerCfg struct { // restListener returns a listener to use for the REST proxy. restListener func() (net.Listener, error) + + // getLnd returns a grpc connection to an lnd instance. + getLnd func(string, *lndConfig) (*lndclient.GrpcLndServices, error) } // daemon runs loopd in daemon mode. It will listen for grpc connections, // execute commands and pass back swap status information. func daemon(config *config, lisCfg *listenerCfg) error { - lnd, err := getLnd(config.Network, config.Lnd) + lnd, err := lisCfg.getLnd(config.Network, config.Lnd) if err != nil { return err } diff --git a/loopd/start.go b/loopd/start.go index 6cd5db6..ab03fea 100644 --- a/loopd/start.go +++ b/loopd/start.go @@ -10,6 +10,7 @@ import ( "github.com/jessevdk/go-flags" "github.com/lightninglabs/loop" + "github.com/lightninglabs/loop/lndclient" "github.com/lightningnetwork/lnd/build" "github.com/lightningnetwork/lnd/lntypes" ) @@ -58,6 +59,13 @@ func newListenerCfg(config *config, rpcCfg RPCConfig) *listenerCfg { return net.Listen("tcp", config.RESTListen) }, + getLnd: func(network string, cfg *lndConfig) ( + *lndclient.GrpcLndServices, error) { + + return lndclient.NewLndServices( + cfg.Host, network, cfg.MacaroonDir, cfg.TLSPath, + ) + }, } } @@ -142,7 +150,7 @@ func Start(rpcCfg RPCConfig) error { } if parser.Active.Name == "view" { - return view(&config) + return view(&config, lisCfg) } return fmt.Errorf("unimplemented command %v", parser.Active.Name) diff --git a/loopd/utils.go b/loopd/utils.go index 10bb449..9b66685 100644 --- a/loopd/utils.go +++ b/loopd/utils.go @@ -8,13 +8,6 @@ import ( "github.com/lightninglabs/loop/lndclient" ) -// getLnd returns an instance of the lnd services proxy. -func getLnd(network string, cfg *lndConfig) (*lndclient.GrpcLndServices, error) { - return lndclient.NewLndServices( - cfg.Host, network, cfg.MacaroonDir, cfg.TLSPath, - ) -} - // getClient returns an instance of the swap client. func getClient(network, swapServer string, insecure bool, tlsPathServer string, lnd *lndclient.LndServices) (*loop.Client, func(), error) { diff --git a/loopd/view.go b/loopd/view.go index dabe82c..b9a6b30 100644 --- a/loopd/view.go +++ b/loopd/view.go @@ -11,13 +11,13 @@ import ( ) // view prints all swaps currently in the database. -func view(config *config) error { +func view(config *config, lisCfg *listenerCfg) error { chainParams, err := swap.ChainParamsFromNetwork(config.Network) if err != nil { return err } - lnd, err := getLnd(config.Network, config.Lnd) + lnd, err := lisCfg.getLnd(config.Network, config.Lnd) if err != nil { return err } From 0a51bf4d2de8fb3fa8ba49f614295880e5445fea Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Fri, 3 Jan 2020 14:01:32 +0100 Subject: [PATCH 9/9] loopd: make lnd conn configurable --- loopd/start.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/loopd/start.go b/loopd/start.go index ab03fea..321ed5c 100644 --- a/loopd/start.go +++ b/loopd/start.go @@ -1,6 +1,7 @@ package loopd import ( + "context" "fmt" "net" "os" @@ -36,6 +37,10 @@ type RPCConfig struct { // listener. // Note that setting this will also disable REST. RPCListener net.Listener + + // LndConn is an optional connection to an lnd instance. If set it will + // override the TCP connection created from daemon's config. + LndConn net.Conn } // newListenerCfg creates and returns a new listenerCfg from the passed config @@ -62,6 +67,21 @@ func newListenerCfg(config *config, rpcCfg RPCConfig) *listenerCfg { getLnd: func(network string, cfg *lndConfig) ( *lndclient.GrpcLndServices, error) { + // If a custom lnd connection is specified we use that + // directly. + if rpcCfg.LndConn != nil { + dialer := func(context.Context, string) ( + net.Conn, error) { + return rpcCfg.LndConn, nil + } + + return lndclient.NewLndServicesWithDialer( + dialer, + rpcCfg.LndConn.RemoteAddr().String(), + network, cfg.MacaroonDir, cfg.TLSPath, + ) + } + return lndclient.NewLndServices( cfg.Host, network, cfg.MacaroonDir, cfg.TLSPath, )