Skip to content

Commit 502e473

Browse files
committed
liquidity: add peers to swap interface and include in set reason
This commit adds a peer listing function to our generic swap interface, which we will use to set reasons for swaps that are specified by peer pubkey rather than channel.
1 parent 7ca7a70 commit 502e473

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

liquidity/interface.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/lightninglabs/loop"
66
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
77
"github.com/lightningnetwork/lnd/lnwire"
8+
"github.com/lightningnetwork/lnd/routing/route"
89
)
910

1011
// FeeLimit is an interface implemented by different strategies for limiting
@@ -43,8 +44,17 @@ type swapSuggestion interface {
4344

4445
// channels returns the set of channels involved in the swap.
4546
channels() []lnwire.ShortChannelID
47+
48+
// peers returns the set of peers involved in the swap, taking a map
49+
// of known channel IDs to peers as an argument so that channel peers
50+
// can be looked up.
51+
peers(knownChans map[uint64]route.Vertex) []route.Vertex
4652
}
4753

54+
// Compile-time assertion that loopOutSwapSuggestion satisfies the
55+
// swapSuggestion interface.
56+
var _ swapSuggestion = (*loopOutSwapSuggestion)(nil)
57+
4858
type loopOutSwapSuggestion struct {
4959
loop.OutRequest
5060
}
@@ -69,3 +79,26 @@ func (l *loopOutSwapSuggestion) channels() []lnwire.ShortChannelID {
6979

7080
return channels
7181
}
82+
83+
// peers returns the set of peers that the loop out swap is restricted to.
84+
func (l *loopOutSwapSuggestion) peers(
85+
knownChans map[uint64]route.Vertex) []route.Vertex {
86+
87+
peers := make(map[route.Vertex]struct{}, len(knownChans))
88+
89+
for _, channel := range l.OutgoingChanSet {
90+
peer, ok := knownChans[channel]
91+
if !ok {
92+
log.Warnf("peer for channel: %v unknown", channel)
93+
}
94+
95+
peers[peer] = struct{}{}
96+
}
97+
98+
peerList := make([]route.Vertex, 0, len(peers))
99+
for peer := range peers {
100+
peerList = append(peerList, peer)
101+
}
102+
103+
return peerList
104+
}

liquidity/liquidity.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,8 +685,13 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoloop bool) (
685685
return nil, err
686686
}
687687

688+
// Collect a map of channel IDs to peer pubkeys, and a set of per-peer
689+
// balances which we will use for peer-level liquidity rules.
690+
channelPeers := make(map[uint64]route.Vertex)
688691
peerChannels := make(map[route.Vertex]*balances)
689692
for _, channel := range channels {
693+
channelPeers[channel.ChannelID] = channel.PubKeyBytes
694+
690695
bal, ok := peerChannels[channel.PubKeyBytes]
691696
if !ok {
692697
bal = &balances{}
@@ -777,6 +782,15 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoloop bool) (
777782
// setReason is a helper that adds a swap's channels to our disqualified
778783
// list with the reason provided.
779784
setReason := func(reason Reason, swap swapSuggestion) {
785+
for _, peer := range swap.peers(channelPeers) {
786+
_, ok := m.params.PeerRules[peer]
787+
if !ok {
788+
continue
789+
}
790+
791+
resp.DisqualifiedPeers[peer] = reason
792+
}
793+
780794
for _, channel := range swap.channels() {
781795
_, ok := m.params.ChannelRules[channel]
782796
if !ok {

liquidity/liquidity_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,10 +1198,9 @@ func TestInFlightLimit(t *testing.T) {
11981198
chan1Rec,
11991199
},
12001200
DisqualifiedChans: noneDisqualified,
1201-
// This is a bug, we should list our second
1202-
// peer as disqualified because we've hit our
1203-
// in-flight limit for now.
1204-
DisqualifiedPeers: noPeersDisqualified,
1201+
DisqualifiedPeers: map[route.Vertex]Reason{
1202+
peer2: ReasonInFlight,
1203+
},
12051204
},
12061205
},
12071206
}

0 commit comments

Comments
 (0)