Skip to content

Commit faa68a2

Browse files
authored
Merge pull request #9880 from yyforyongyu/accessman
Improve access control in peer connections
2 parents 1756f40 + e470910 commit faa68a2

File tree

15 files changed

+1095
-296
lines changed

15 files changed

+1095
-296
lines changed

accessman.go

Lines changed: 190 additions & 85 deletions
Large diffs are not rendered by default.

accessman_test.go

Lines changed: 434 additions & 17 deletions
Large diffs are not rendered by default.

channeldb/db.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ type ChanCount struct {
743743
func (c *ChannelStateDB) FetchPermAndTempPeers(
744744
chainHash []byte) (map[string]ChanCount, error) {
745745

746-
peerCounts := make(map[string]ChanCount)
746+
peerChanInfo := make(map[string]ChanCount)
747747

748748
err := kvdb.View(c.backend, func(tx kvdb.RTx) error {
749749
openChanBucket := tx.ReadBucket(openChannelBucket)
@@ -829,7 +829,7 @@ func (c *ChannelStateDB) FetchPermAndTempPeers(
829829
HasOpenOrClosedChan: isPermPeer,
830830
PendingOpenCount: pendingOpenCount,
831831
}
832-
peerCounts[string(nodePub)] = peerCount
832+
peerChanInfo[string(nodePub)] = peerCount
833833

834834
return nil
835835
})
@@ -893,15 +893,15 @@ func (c *ChannelStateDB) FetchPermAndTempPeers(
893893
remoteSer := remotePub.SerializeCompressed()
894894
remoteKey := string(remoteSer)
895895

896-
count, exists := peerCounts[remoteKey]
896+
count, exists := peerChanInfo[remoteKey]
897897
if exists {
898898
count.HasOpenOrClosedChan = true
899-
peerCounts[remoteKey] = count
899+
peerChanInfo[remoteKey] = count
900900
} else {
901901
peerCount := ChanCount{
902902
HasOpenOrClosedChan: true,
903903
}
904-
peerCounts[remoteKey] = peerCount
904+
peerChanInfo[remoteKey] = peerCount
905905
}
906906
}
907907

@@ -913,10 +913,10 @@ func (c *ChannelStateDB) FetchPermAndTempPeers(
913913

914914
return nil
915915
}, func() {
916-
clear(peerCounts)
916+
clear(peerChanInfo)
917917
})
918918

919-
return peerCounts, err
919+
return peerChanInfo, err
920920
}
921921

922922
// channelSelector describes a function that takes a chain-hash bucket from

channeldb/db_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -770,16 +770,16 @@ func TestFetchPermTempPeer(t *testing.T) {
770770
)
771771

772772
// Fetch the ChanCount for our peers.
773-
peerCounts, err := cdb.FetchPermAndTempPeers(key[:])
773+
peerChanInfo, err := cdb.FetchPermAndTempPeers(key[:])
774774
require.NoError(t, err, "unable to fetch perm and temp peers")
775775

776776
// There should only be three entries.
777-
require.Len(t, peerCounts, 3)
777+
require.Len(t, peerChanInfo, 3)
778778

779779
// The first entry should have OpenClosed set to true and Pending set
780780
// to 0.
781-
count1, found := peerCounts[string(pubKey1.SerializeCompressed())]
782-
require.True(t, found, "unable to find peer 1 in peerCounts")
781+
count1, found := peerChanInfo[string(pubKey1.SerializeCompressed())]
782+
require.True(t, found, "unable to find peer 1 in peerChanInfo")
783783
require.True(
784784
t, count1.HasOpenOrClosedChan,
785785
"couldn't find peer 1's channels",
@@ -789,15 +789,15 @@ func TestFetchPermTempPeer(t *testing.T) {
789789
"peer 1 doesn't have 0 pending-open",
790790
)
791791

792-
count2, found := peerCounts[string(pubKey2.SerializeCompressed())]
793-
require.True(t, found, "unable to find peer 2 in peerCounts")
792+
count2, found := peerChanInfo[string(pubKey2.SerializeCompressed())]
793+
require.True(t, found, "unable to find peer 2 in peerChanInfo")
794794
require.False(
795795
t, count2.HasOpenOrClosedChan, "found erroneous channels",
796796
)
797797
require.Equal(t, uint64(1), count2.PendingOpenCount)
798798

799-
count3, found := peerCounts[string(pubKey3.SerializeCompressed())]
800-
require.True(t, found, "unable to find peer 3 in peerCounts")
799+
count3, found := peerChanInfo[string(pubKey3.SerializeCompressed())]
800+
require.True(t, found, "unable to find peer 3 in peerChanInfo")
801801
require.True(
802802
t, count3.HasOpenOrClosedChan,
803803
"couldn't find peer 3's channels",

config.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,9 @@ const (
238238
// defaultHTTPHeaderTimeout is the default timeout for HTTP requests.
239239
DefaultHTTPHeaderTimeout = 5 * time.Second
240240

241-
// DefaultNumRestrictedSlots is the default number of restricted slots
242-
// we'll allocate in the server.
241+
// DefaultNumRestrictedSlots is the default max number of incoming
242+
// connections allowed in the server. Outbound connections are not
243+
// restricted.
243244
DefaultNumRestrictedSlots = 100
244245

245246
// BitcoinChainName is a string that represents the Bitcoin blockchain.
@@ -529,9 +530,9 @@ type Config struct {
529530
// before timing out reading the headers of an HTTP request.
530531
HTTPHeaderTimeout time.Duration `long:"http-header-timeout" description:"The maximum duration that the server will wait before timing out reading the headers of an HTTP request."`
531532

532-
// NumRestrictedSlots is the number of restricted slots we'll allocate
533-
// in the server.
534-
NumRestrictedSlots uint64 `long:"num-restricted-slots" description:"The number of restricted slots we'll allocate in the server."`
533+
// NumRestrictedSlots is the max number of incoming connections allowed
534+
// in the server. Outbound connections are not restricted.
535+
NumRestrictedSlots uint64 `long:"num-restricted-slots" description:"The max number of incoming connections allowed in the server. Outbound connections are not restricted."`
535536

536537
// NoDisconnectOnPongFailure controls if we'll disconnect if a peer
537538
// doesn't respond to a pong in time.

docs/release-notes/release-notes-0.19.2.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141

4242
## Functional Updates
4343

44+
- [Improved](https://github.com/lightningnetwork/lnd/pull/9880) the connection
45+
restriction logic enforced by `accessman`. In addition, the restriction placed
46+
on outbound connections is now lifted.
47+
4448
## RPC Updates
4549

4650
## lncli Updates
@@ -71,5 +75,4 @@ much more slowly.
7175
## Tooling and Documentation
7276

7377
# Contributors (Alphabetical Order)
74-
7578
* Yong Yu

funding/manager.go

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ type Config struct {
511511

512512
// NotifyOpenChannelEvent informs the ChannelNotifier when channels
513513
// transition from pending open to open.
514-
NotifyOpenChannelEvent func(wire.OutPoint, *btcec.PublicKey) error
514+
NotifyOpenChannelEvent func(wire.OutPoint, *btcec.PublicKey)
515515

516516
// OpenChannelPredicate is a predicate on the lnwire.OpenChannel message
517517
// and on the requesting node's public key that returns a bool which
@@ -521,13 +521,13 @@ type Config struct {
521521
// NotifyPendingOpenChannelEvent informs the ChannelNotifier when
522522
// channels enter a pending state.
523523
NotifyPendingOpenChannelEvent func(wire.OutPoint,
524-
*channeldb.OpenChannel, *btcec.PublicKey) error
524+
*channeldb.OpenChannel, *btcec.PublicKey)
525525

526526
// NotifyFundingTimeout informs the ChannelNotifier when a pending-open
527527
// channel times out because the funding transaction hasn't confirmed.
528528
// This is only called for the fundee and only if the channel is
529529
// zero-conf.
530-
NotifyFundingTimeout func(wire.OutPoint, *btcec.PublicKey) error
530+
NotifyFundingTimeout func(wire.OutPoint, *btcec.PublicKey)
531531

532532
// EnableUpfrontShutdown specifies whether the upfront shutdown script
533533
// is enabled.
@@ -1319,13 +1319,9 @@ func (f *Manager) advancePendingChannelState(channel *channeldb.OpenChannel,
13191319

13201320
// Inform the ChannelNotifier that the channel has transitioned
13211321
// from pending open to open.
1322-
if err := f.cfg.NotifyOpenChannelEvent(
1322+
f.cfg.NotifyOpenChannelEvent(
13231323
channel.FundingOutpoint, channel.IdentityPub,
1324-
); err != nil {
1325-
log.Errorf("Unable to notify open channel event for "+
1326-
"ChannelPoint(%v): %v",
1327-
channel.FundingOutpoint, err)
1328-
}
1324+
)
13291325

13301326
// Find and close the discoverySignal for this channel such
13311327
// that ChannelReady messages will be processed.
@@ -2666,12 +2662,9 @@ func (f *Manager) fundeeProcessFundingCreated(peer lnpeer.Peer,
26662662

26672663
// Inform the ChannelNotifier that the channel has entered
26682664
// pending open state.
2669-
if err := f.cfg.NotifyPendingOpenChannelEvent(
2665+
f.cfg.NotifyPendingOpenChannelEvent(
26702666
fundingOut, completeChan, completeChan.IdentityPub,
2671-
); err != nil {
2672-
log.Errorf("Unable to send pending-open channel event for "+
2673-
"ChannelPoint(%v) %v", fundingOut, err)
2674-
}
2667+
)
26752668

26762669
// At this point we have sent our last funding message to the
26772670
// initiating peer before the funding transaction will be broadcast.
@@ -2891,13 +2884,9 @@ func (f *Manager) funderProcessFundingSigned(peer lnpeer.Peer,
28912884
case resCtx.updates <- upd:
28922885
// Inform the ChannelNotifier that the channel has entered
28932886
// pending open state.
2894-
if err := f.cfg.NotifyPendingOpenChannelEvent(
2887+
f.cfg.NotifyPendingOpenChannelEvent(
28952888
*fundingPoint, completeChan, completeChan.IdentityPub,
2896-
); err != nil {
2897-
log.Errorf("Unable to send pending-open channel "+
2898-
"event for ChannelPoint(%v) %v", fundingPoint,
2899-
err)
2900-
}
2889+
)
29012890

29022891
case <-f.quit:
29032892
return
@@ -2955,11 +2944,7 @@ func (f *Manager) fundingTimeout(c *channeldb.OpenChannel,
29552944
}
29562945

29572946
// Notify other subsystems about the funding timeout.
2958-
err := f.cfg.NotifyFundingTimeout(c.FundingOutpoint, c.IdentityPub)
2959-
if err != nil {
2960-
log.Errorf("failed to notify of funding timeout for "+
2961-
"ChanPoint(%v): %v", c.FundingOutpoint, err)
2962-
}
2947+
f.cfg.NotifyFundingTimeout(c.FundingOutpoint, c.IdentityPub)
29632948

29642949
timeoutErr := fmt.Errorf("timeout waiting for funding tx (%v) to "+
29652950
"confirm", c.FundingOutpoint)
@@ -3341,13 +3326,9 @@ func (f *Manager) handleFundingConfirmation(
33413326

33423327
// Inform the ChannelNotifier that the channel has transitioned from
33433328
// pending open to open.
3344-
if err := f.cfg.NotifyOpenChannelEvent(
3329+
f.cfg.NotifyOpenChannelEvent(
33453330
completeChan.FundingOutpoint, completeChan.IdentityPub,
3346-
); err != nil {
3347-
log.Errorf("Unable to notify open channel event for "+
3348-
"ChannelPoint(%v): %v", completeChan.FundingOutpoint,
3349-
err)
3350-
}
3331+
)
33513332

33523333
// Close the discoverySignal channel, indicating to a separate
33533334
// goroutine that the channel now is marked as open in the database

funding/manager_test.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -232,29 +232,23 @@ type mockChanEvent struct {
232232
}
233233

234234
func (m *mockChanEvent) NotifyOpenChannelEvent(outpoint wire.OutPoint,
235-
remotePub *btcec.PublicKey) error {
235+
remotePub *btcec.PublicKey) {
236236

237237
m.openEvent <- outpoint
238-
239-
return nil
240238
}
241239

242240
func (m *mockChanEvent) NotifyPendingOpenChannelEvent(outpoint wire.OutPoint,
243241
pendingChannel *channeldb.OpenChannel,
244-
remotePub *btcec.PublicKey) error {
242+
remotePub *btcec.PublicKey) {
245243

246244
m.pendingOpenEvent <- channelnotifier.PendingOpenChannelEvent{
247245
ChannelPoint: &outpoint,
248246
PendingChannel: pendingChannel,
249247
}
250-
251-
return nil
252248
}
253249

254250
func (m *mockChanEvent) NotifyFundingTimeout(outpoint wire.OutPoint,
255-
remotePub *btcec.PublicKey) error {
256-
257-
return nil
251+
remotePub *btcec.PublicKey) {
258252
}
259253

260254
// mockZeroConfAcceptor always accepts the channel open request for zero-conf

itest/list_on_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -691,10 +691,6 @@ var allTestCases = []*lntest.TestCase{
691691
Name: "funding manager funding timeout",
692692
TestFunc: testFundingManagerFundingTimeout,
693693
},
694-
{
695-
Name: "access perm",
696-
TestFunc: testAccessPerm,
697-
},
698694
{
699695
Name: "rbf coop close",
700696
TestFunc: testCoopCloseRbf,
@@ -782,6 +778,9 @@ func init() {
782778
"coop close with external delivery", allTestCases,
783779
coopCloseWithExternalTestCases,
784780
)
781+
allTestCases = appendPrefixed(
782+
"peer conn", allTestCases, peerConnTestCases,
783+
)
785784

786785
// Prepare the test cases for windows to exclude some of the flaky
787786
// ones.

0 commit comments

Comments
 (0)