Skip to content

Commit 69716cd

Browse files
committed
multi: make MsgRouter available in the ImplementationCfg
With this commit, we allow the `MsgRouter` to be available in the `ImplementationCfg`. With this, programs outside of lnd itself are able to now hook into the message processing flow to direct handle custom messages, and even normal wire messages.
1 parent 4ced3bc commit 69716cd

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

config_builder.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/lightningnetwork/lnd/chainreg"
3434
"github.com/lightningnetwork/lnd/channeldb"
3535
"github.com/lightningnetwork/lnd/clock"
36+
"github.com/lightningnetwork/lnd/fn"
3637
"github.com/lightningnetwork/lnd/invoices"
3738
"github.com/lightningnetwork/lnd/keychain"
3839
"github.com/lightningnetwork/lnd/kvdb"
@@ -42,6 +43,7 @@ import (
4243
"github.com/lightningnetwork/lnd/lnwallet/btcwallet"
4344
"github.com/lightningnetwork/lnd/lnwallet/rpcwallet"
4445
"github.com/lightningnetwork/lnd/macaroons"
46+
"github.com/lightningnetwork/lnd/msgmux"
4547
"github.com/lightningnetwork/lnd/rpcperms"
4648
"github.com/lightningnetwork/lnd/signal"
4749
"github.com/lightningnetwork/lnd/sqldb"
@@ -118,6 +120,14 @@ type ChainControlBuilder interface {
118120
*btcwallet.Config) (*chainreg.ChainControl, func(), error)
119121
}
120122

123+
// AuxComponents is a set of auxiliary components that can be used by lnd for
124+
// certain custom channel types.
125+
type AuxComponents struct {
126+
// MsgRouter is an optional message router that if set will be used in
127+
// place of a new blank default message router.
128+
MsgRouter fn.Option[msgmux.Router]
129+
}
130+
121131
// ImplementationCfg is a struct that holds all configuration items for
122132
// components that can be implemented outside lnd itself.
123133
type ImplementationCfg struct {
@@ -144,6 +154,10 @@ type ImplementationCfg struct {
144154
// ChainControlBuilder is a type that can provide a custom wallet
145155
// implementation.
146156
ChainControlBuilder
157+
158+
// AuxComponents is a set of auxiliary components that can be used by
159+
// lnd for certain custom channel types.
160+
AuxComponents
147161
}
148162

149163
// DefaultWalletImpl is the default implementation of our normal, btcwallet

lnd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,7 @@ func Main(cfg *Config, lisCfg ListenerCfg, implCfg *ImplementationCfg,
600600
cfg, cfg.Listeners, dbs, activeChainControl, &idKeyDesc,
601601
activeChainControl.Cfg.WalletUnlockParams.ChansToRestore,
602602
multiAcceptor, torController, tlsManager, leaderElector,
603+
implCfg,
603604
)
604605
if err != nil {
605606
return mkErr("unable to create server: %v", err)

peer/brontide.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,11 @@ type Config struct {
387387
// This value will be passed to created links.
388388
MaxFeeExposure lnwire.MilliSatoshi
389389

390+
// MsgRouter is an optional instance of the main message router that
391+
// the peer will use. If None, then a new default version will be used
392+
// in place.
393+
MsgRouter fn.Option[msgmux.Router]
394+
390395
// Quit is the server's quit channel. If this is closed, we halt operation.
391396
Quit chan struct{}
392397
}
@@ -542,6 +547,12 @@ var _ lnpeer.Peer = (*Brontide)(nil)
542547
func NewBrontide(cfg Config) *Brontide {
543548
logPrefix := fmt.Sprintf("Peer(%x):", cfg.PubKeyBytes)
544549

550+
// We'll either use the msg router instance passed in, or create a new
551+
// blank instance.
552+
msgRouter := cfg.MsgRouter.Alt(fn.Some[msgmux.Router](
553+
msgmux.NewMultiMsgRouter(),
554+
))
555+
545556
p := &Brontide{
546557
cfg: cfg,
547558
activeSignal: make(chan struct{}),
@@ -564,9 +575,7 @@ func NewBrontide(cfg Config) *Brontide {
564575
startReady: make(chan struct{}),
565576
quit: make(chan struct{}),
566577
log: build.NewPrefixLog(logPrefix, peerLog),
567-
msgRouter: fn.Some[msgmux.Router](
568-
msgmux.NewMultiMsgRouter(),
569-
),
578+
msgRouter: msgRouter,
570579
}
571580

572581
if cfg.Conn != nil && cfg.Conn.RemoteAddr() != nil {

server.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ type server struct {
160160

161161
cfg *Config
162162

163+
implCfg *ImplementationCfg
164+
163165
// identityECDH is an ECDH capable wrapper for the private key used
164166
// to authenticate any incoming connections.
165167
identityECDH keychain.SingleKeyECDH
@@ -486,7 +488,8 @@ func newServer(cfg *Config, listenAddrs []net.Addr,
486488
chansToRestore walletunlocker.ChannelsToRecover,
487489
chanPredicate chanacceptor.ChannelAcceptor,
488490
torController *tor.Controller, tlsManager *TLSManager,
489-
leaderElector cluster.LeaderElector) (*server, error) {
491+
leaderElector cluster.LeaderElector,
492+
implCfg *ImplementationCfg) (*server, error) {
490493

491494
var (
492495
err error
@@ -571,6 +574,7 @@ func newServer(cfg *Config, listenAddrs []net.Addr,
571574

572575
s := &server{
573576
cfg: cfg,
577+
implCfg: implCfg,
574578
graphDB: dbs.GraphDB.ChannelGraph(),
575579
chanStateDB: dbs.ChanStateDB.ChannelStateDB(),
576580
addrSource: dbs.ChanStateDB,
@@ -3986,6 +3990,7 @@ func (s *server) peerConnected(conn net.Conn, connReq *connmgr.ConnReq,
39863990
DisallowRouteBlinding: s.cfg.ProtocolOptions.NoRouteBlinding(),
39873991
MaxFeeExposure: thresholdMSats,
39883992
Quit: s.quit,
3993+
MsgRouter: s.implCfg.MsgRouter,
39893994
}
39903995

39913996
copy(pCfg.PubKeyBytes[:], peerAddr.IdentityKey.SerializeCompressed())

0 commit comments

Comments
 (0)