Skip to content

Commit 79e6ff8

Browse files
committed
peer+protofsm: move MsgRouter to new protofsm package
Without this, any other sub-system that wants to use the interface may run into an import cycle.
1 parent 336b62a commit 79e6ff8

File tree

4 files changed

+61
-15
lines changed

4 files changed

+61
-15
lines changed

peer/brontide.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
"github.com/lightningnetwork/lnd/lnwire"
4343
"github.com/lightningnetwork/lnd/netann"
4444
"github.com/lightningnetwork/lnd/pool"
45+
"github.com/lightningnetwork/lnd/protofsm"
4546
"github.com/lightningnetwork/lnd/queue"
4647
"github.com/lightningnetwork/lnd/subscribe"
4748
"github.com/lightningnetwork/lnd/ticker"
@@ -367,7 +368,7 @@ type Config struct {
367368
// MsgRouter is an optional instance of the main message router that
368369
// the peer will use. If None, then a new default version will be used
369370
// in place.
370-
MsgRouter fn.Option[MsgRouter]
371+
MsgRouter fn.Option[protofsm.MsgRouter]
371372

372373
// Quit is the server's quit channel. If this is closed, we halt operation.
373374
Quit chan struct{}
@@ -490,7 +491,7 @@ type Brontide struct {
490491

491492
// msgRouter is an instance of the MsgRouter which is used to send off
492493
// new wire messages for handing.
493-
msgRouter fn.Option[MsgRouter]
494+
msgRouter fn.Option[protofsm.MsgRouter]
494495

495496
startReady chan struct{}
496497
quit chan struct{}
@@ -512,7 +513,7 @@ func NewBrontide(cfg Config) *Brontide {
512513
//
513514
// TODO(roasbeef): extend w/ source peer info?
514515
msgRouter := cfg.MsgRouter.Alt(
515-
fn.Some[MsgRouter](NewMultiMsgRouter()),
516+
fn.Some[protofsm.MsgRouter](protofsm.NewMultiMsgRouter()),
516517
)
517518

518519
p := &Brontide{
@@ -714,7 +715,7 @@ func (p *Brontide) Start() error {
714715

715716
// Register the message router now as we may need to register some
716717
// endpoints while loading the channels below.
717-
p.msgRouter.WhenSome(func(router MsgRouter) {
718+
p.msgRouter.WhenSome(func(router protofsm.MsgRouter) {
718719
router.Start()
719720
})
720721

@@ -1265,7 +1266,7 @@ func (p *Brontide) Disconnect(reason error) {
12651266
err)
12661267
}
12671268

1268-
p.msgRouter.WhenSome(func(router MsgRouter) {
1269+
p.msgRouter.WhenSome(func(router protofsm.MsgRouter) {
12691270
router.Stop()
12701271
})
12711272
}
@@ -1710,7 +1711,7 @@ out:
17101711
// If a message router is active, then we'll try to have it
17111712
// handle this message. If it can, then we're able to skip the
17121713
// rest of the message handling logic.
1713-
ok := fn.MapOptionZ(p.msgRouter, func(r MsgRouter) error {
1714+
ok := fn.MapOptionZ(p.msgRouter, func(r protofsm.MsgRouter) error {
17141715
return r.RouteMsg(nextMsg)
17151716
})
17161717

protofsm/log.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package protofsm
2+
3+
import (
4+
"github.com/btcsuite/btclog"
5+
"github.com/lightningnetwork/lnd/build"
6+
)
7+
8+
// log is a logger that is initialized with no output filters. This
9+
// means the package will not perform any logging by default until the caller
10+
// requests it.
11+
var log btclog.Logger
12+
13+
// The default amount of logging is none.
14+
func init() {
15+
UseLogger(build.NewSubLogger("PFSM", nil))
16+
}
17+
18+
// DisableLog disables all library log output. Logging output is disabled
19+
// by default until UseLogger is called.
20+
func DisableLog() {
21+
UseLogger(btclog.Disabled)
22+
}
23+
24+
// UseLogger uses a specified Logger to output package logging info.
25+
// This should be used in preference to SetLogWriter if the caller is also
26+
// using btclog.
27+
func UseLogger(logger btclog.Logger) {
28+
log = logger
29+
}
30+
31+
// logClosure is used to provide a closure over expensive logging operations
32+
// so they aren't performed when the logging level doesn't warrant it.
33+
type logClosure func() string
34+
35+
// String invokes the underlying function and returns the result.
36+
func (c logClosure) String() string {
37+
return c()
38+
}
39+
40+
// newLogClosure returns a new closure over a function that returns a string
41+
// which itself provides a Stringer interface so that it can be used with the
42+
// logging system.
43+
func newLogClosure(c func() string) logClosure {
44+
return logClosure(c)
45+
}

peer/msg_router.go renamed to protofsm/msg_router.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package peer
1+
package protofsm
22

33
import (
44
"fmt"
@@ -152,7 +152,7 @@ func NewMultiMsgRouter() *MultiMsgRouter {
152152

153153
// Start starts the peer message router.
154154
func (p *MultiMsgRouter) Start() {
155-
peerLog.Infof("Starting MsgRouter")
155+
log.Infof("Starting MsgRouter")
156156

157157
p.startOnce.Do(func() {
158158
p.wg.Add(1)
@@ -162,7 +162,7 @@ func (p *MultiMsgRouter) Start() {
162162

163163
// Stop stops the peer message router.
164164
func (p *MultiMsgRouter) Stop() {
165-
peerLog.Infof("Stopping MsgRouter")
165+
log.Infof("Stopping MsgRouter")
166166

167167
p.stopOnce.Do(func() {
168168
close(p.quit)
@@ -214,13 +214,13 @@ func (p *MultiMsgRouter) msgRouter() {
214214
case newEndpointMsg := <-p.registerChan:
215215
endpoint := newEndpointMsg.query
216216

217-
peerLog.Infof("MsgRouter: registering new "+
217+
log.Infof("MsgRouter: registering new "+
218218
"MsgEndpoint(%s)", endpoint.Name())
219219

220220
// If this endpoint already exists, then we'll return
221221
// an error as we require unique names.
222222
if _, ok := endpoints[endpoint.Name()]; ok {
223-
peerLog.Errorf("MsgRouter: rejecting "+
223+
log.Errorf("MsgRouter: rejecting "+
224224
"duplicate endpoint: %v",
225225
endpoint.Name())
226226

@@ -243,7 +243,7 @@ func (p *MultiMsgRouter) msgRouter() {
243243
case endpointName := <-p.unregisterChan:
244244
delete(endpoints, endpointName.query)
245245

246-
peerLog.Infof("MsgRouter: unregistering "+
246+
log.Infof("MsgRouter: unregistering "+
247247
"MsgEndpoint(%s)", endpointName.query)
248248

249249
endpointName.respChan <- fn.NewRight[error, error](
@@ -260,7 +260,7 @@ func (p *MultiMsgRouter) msgRouter() {
260260
var couldSend bool
261261
for _, endpoint := range endpoints {
262262
if endpoint.CanHandle(msg) {
263-
peerLog.Tracef("MsgRouter: sending "+
263+
log.Tracef("MsgRouter: sending "+
264264
"msg %T to endpoint %s", msg,
265265
endpoint.Name())
266266

@@ -271,7 +271,7 @@ func (p *MultiMsgRouter) msgRouter() {
271271

272272
var err error
273273
if !couldSend {
274-
peerLog.Tracef("MsgRouter: unable to route "+
274+
log.Tracef("MsgRouter: unable to route "+
275275
"msg %T", msg)
276276

277277
err = ErrUnableToRouteMsg

peer/msg_router_test.go renamed to protofsm/msg_router_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package peer
1+
package protofsm
22

33
import (
44
"testing"

0 commit comments

Comments
 (0)