diff --git a/config_builder.go b/config_builder.go index e5839208d..a1658fd67 100644 --- a/config_builder.go +++ b/config_builder.go @@ -44,7 +44,7 @@ import ( "github.com/lightningnetwork/lnd/lnwallet/btcwallet" "github.com/lightningnetwork/lnd/lnwallet/rpcwallet" "github.com/lightningnetwork/lnd/macaroons" - "github.com/lightningnetwork/lnd/peer" + "github.com/lightningnetwork/lnd/protofsm" "github.com/lightningnetwork/lnd/rpcperms" "github.com/lightningnetwork/lnd/signal" "github.com/lightningnetwork/lnd/sqldb" @@ -161,7 +161,7 @@ type AuxComponents struct { // MsgRouter is an optional message router that if set will be used in // place of a new balnk default message router. - MsgRouter fn.Option[peer.MsgRouter] + MsgRouter fn.Option[protofsm.MsgRouter] } // DefaultWalletImpl is the default implementation of our normal, btcwallet diff --git a/peer/brontide.go b/peer/brontide.go index b86a2db62..779206038 100644 --- a/peer/brontide.go +++ b/peer/brontide.go @@ -42,6 +42,7 @@ import ( "github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/netann" "github.com/lightningnetwork/lnd/pool" + "github.com/lightningnetwork/lnd/protofsm" "github.com/lightningnetwork/lnd/queue" "github.com/lightningnetwork/lnd/subscribe" "github.com/lightningnetwork/lnd/ticker" @@ -377,7 +378,7 @@ type Config struct { // MsgRouter is an optional instance of the main message router that // the peer will use. If None, then a new default version will be used // in place. - MsgRouter fn.Option[MsgRouter] + MsgRouter fn.Option[protofsm.MsgRouter] // Quit is the server's quit channel. If this is closed, we halt operation. Quit chan struct{} @@ -500,7 +501,7 @@ type Brontide struct { // msgRouter is an instance of the MsgRouter which is used to send off // new wire messages for handing. - msgRouter fn.Option[MsgRouter] + msgRouter fn.Option[protofsm.MsgRouter] startReady chan struct{} quit chan struct{} @@ -522,7 +523,7 @@ func NewBrontide(cfg Config) *Brontide { // // TODO(roasbeef): extend w/ source peer info? msgRouter := cfg.MsgRouter.Alt( - fn.Some[MsgRouter](NewMultiMsgRouter()), + fn.Some[protofsm.MsgRouter](protofsm.NewMultiMsgRouter()), ) p := &Brontide{ @@ -724,7 +725,7 @@ func (p *Brontide) Start() error { // Register the message router now as we may need to register some // endpoints while loading the channels below. - p.msgRouter.WhenSome(func(router MsgRouter) { + p.msgRouter.WhenSome(func(router protofsm.MsgRouter) { router.Start() }) @@ -1294,7 +1295,7 @@ func (p *Brontide) Disconnect(reason error) { err) } - p.msgRouter.WhenSome(func(router MsgRouter) { + p.msgRouter.WhenSome(func(router protofsm.MsgRouter) { router.Stop() }) } @@ -1739,7 +1740,7 @@ out: // If a message router is active, then we'll try to have it // handle this message. If it can, then we're able to skip the // rest of the message handling logic. - ok := fn.MapOptionZ(p.msgRouter, func(r MsgRouter) error { + ok := fn.MapOptionZ(p.msgRouter, func(r protofsm.MsgRouter) error { return r.RouteMsg(nextMsg) }) diff --git a/protofsm/log.go b/protofsm/log.go new file mode 100644 index 000000000..368abfab2 --- /dev/null +++ b/protofsm/log.go @@ -0,0 +1,45 @@ +package protofsm + +import ( + "github.com/btcsuite/btclog" + "github.com/lightningnetwork/lnd/build" +) + +// log is a logger that is initialized with no output filters. This +// means the package will not perform any logging by default until the caller +// requests it. +var log btclog.Logger + +// The default amount of logging is none. +func init() { + UseLogger(build.NewSubLogger("PFSM", nil)) +} + +// DisableLog disables all library log output. Logging output is disabled +// by default until UseLogger is called. +func DisableLog() { + UseLogger(btclog.Disabled) +} + +// UseLogger uses a specified Logger to output package logging info. +// This should be used in preference to SetLogWriter if the caller is also +// using btclog. +func UseLogger(logger btclog.Logger) { + log = logger +} + +// logClosure is used to provide a closure over expensive logging operations +// so they aren't performed when the logging level doesn't warrant it. +type logClosure func() string + +// String invokes the underlying function and returns the result. +func (c logClosure) String() string { + return c() +} + +// newLogClosure returns a new closure over a function that returns a string +// which itself provides a Stringer interface so that it can be used with the +// logging system. +func newLogClosure(c func() string) logClosure { + return logClosure(c) +} diff --git a/peer/msg_router.go b/protofsm/msg_router.go similarity index 96% rename from peer/msg_router.go rename to protofsm/msg_router.go index 8bf9b4baf..176cda5b4 100644 --- a/peer/msg_router.go +++ b/protofsm/msg_router.go @@ -1,4 +1,4 @@ -package peer +package protofsm import ( "fmt" @@ -152,7 +152,7 @@ func NewMultiMsgRouter() *MultiMsgRouter { // Start starts the peer message router. func (p *MultiMsgRouter) Start() { - peerLog.Infof("Starting MsgRouter") + log.Infof("Starting MsgRouter") p.startOnce.Do(func() { p.wg.Add(1) @@ -162,7 +162,7 @@ func (p *MultiMsgRouter) Start() { // Stop stops the peer message router. func (p *MultiMsgRouter) Stop() { - peerLog.Infof("Stopping MsgRouter") + log.Infof("Stopping MsgRouter") p.stopOnce.Do(func() { close(p.quit) @@ -214,13 +214,13 @@ func (p *MultiMsgRouter) msgRouter() { case newEndpointMsg := <-p.registerChan: endpoint := newEndpointMsg.query - peerLog.Infof("MsgRouter: registering new "+ + log.Infof("MsgRouter: registering new "+ "MsgEndpoint(%s)", endpoint.Name()) // If this endpoint already exists, then we'll return // an error as we require unique names. if _, ok := endpoints[endpoint.Name()]; ok { - peerLog.Errorf("MsgRouter: rejecting "+ + log.Errorf("MsgRouter: rejecting "+ "duplicate endpoint: %v", endpoint.Name()) @@ -243,7 +243,7 @@ func (p *MultiMsgRouter) msgRouter() { case endpointName := <-p.unregisterChan: delete(endpoints, endpointName.query) - peerLog.Infof("MsgRouter: unregistering "+ + log.Infof("MsgRouter: unregistering "+ "MsgEndpoint(%s)", endpointName.query) endpointName.respChan <- fn.NewRight[error, error]( @@ -260,7 +260,7 @@ func (p *MultiMsgRouter) msgRouter() { var couldSend bool for _, endpoint := range endpoints { if endpoint.CanHandle(msg) { - peerLog.Tracef("MsgRouter: sending "+ + log.Tracef("MsgRouter: sending "+ "msg %T to endpoint %s", msg, endpoint.Name()) @@ -271,7 +271,7 @@ func (p *MultiMsgRouter) msgRouter() { var err error if !couldSend { - peerLog.Tracef("MsgRouter: unable to route "+ + log.Tracef("MsgRouter: unable to route "+ "msg %T", msg) err = ErrUnableToRouteMsg diff --git a/peer/msg_router_test.go b/protofsm/msg_router_test.go similarity index 99% rename from peer/msg_router_test.go rename to protofsm/msg_router_test.go index ee52968eb..b5e56e1e3 100644 --- a/peer/msg_router_test.go +++ b/protofsm/msg_router_test.go @@ -1,4 +1,4 @@ -package peer +package protofsm import ( "testing"