From 5dfc5f4b4236fb4b477a0db272d3be91e5e13dee Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Sat, 31 May 2025 01:39:09 +0800 Subject: [PATCH] lncfg+lnd: add new dev config `unsafeconnect` This flag is added so we can use it in the itest to mimic racing inbound and outbound connections. --- lncfg/dev.go | 6 ++++++ lncfg/dev_integration.go | 6 ++++++ server.go | 6 +++++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lncfg/dev.go b/lncfg/dev.go index 6b2471024..f048d69b7 100644 --- a/lncfg/dev.go +++ b/lncfg/dev.go @@ -52,3 +52,9 @@ func (d *DevConfig) GetZombieSweeperInterval() time.Duration { func (d *DevConfig) GetMaxWaitNumBlocksFundingConf() uint32 { return DefaultMaxWaitNumBlocksFundingConf } + +// GetUnsafeConnect returns the config value `UnsafeConnect`, which is always +// false for production build. +func (d *DevConfig) GetUnsafeConnect() bool { + return false +} diff --git a/lncfg/dev_integration.go b/lncfg/dev_integration.go index 206a1bcca..8ac85f5d9 100644 --- a/lncfg/dev_integration.go +++ b/lncfg/dev_integration.go @@ -26,6 +26,7 @@ type DevConfig struct { ZombieSweeperInterval time.Duration `long:"zombiesweeperinterval" description:"The time interval at which channel opening flows are evaluated for zombie status."` UnsafeDisconnect bool `long:"unsafedisconnect" description:"Allows the rpcserver to intentionally disconnect from peers with open channels."` MaxWaitNumBlocksFundingConf uint32 `long:"maxwaitnumblocksfundingconf" description:"Maximum blocks to wait for funding confirmation before discarding non-initiated channels."` + UnsafeConnect bool `long:"unsafeconnect" description:"Allow the rpcserver to connect to a peer even if there's already a connection."` } // ChannelReadyWait returns the config value `ProcessChannelReadyWait`. @@ -65,3 +66,8 @@ func (d *DevConfig) GetMaxWaitNumBlocksFundingConf() uint32 { return d.MaxWaitNumBlocksFundingConf } + +// GetUnsafeConnect returns the config value `UnsafeConnect`. +func (d *DevConfig) GetUnsafeConnect() bool { + return d.UnsafeConnect +} diff --git a/server.go b/server.go index 81cffee3e..5ffaf13a9 100644 --- a/server.go +++ b/server.go @@ -4980,7 +4980,11 @@ func (s *server) ConnectToPeer(addr *lnwire.NetAddress, // Ensure we're not already connected to this peer. peer, err := s.findPeerByPubStr(targetPub) - if err == nil { + + // When there's no error it means we already have a connection with this + // peer. If this is a dev environment with the `--unsafeconnect` flag + // set, we will ignore the existing connection and continue. + if err == nil && !s.cfg.Dev.GetUnsafeConnect() { s.mu.Unlock() return &errPeerAlreadyConnected{peer: peer} }