Skip to content

Commit b9b75c3

Browse files
committed
liquidity: refactor eligible channels logic to ineligible channels
This commit switches up our eligible channels logic to rather return a struct containing information about our current swap traffic. This change is made in preparation for returning reasons indicating why we did not perform a swap for a specific channel - when we only return eligible swaps, we lose the information about why all the excluded channels weren't used.
1 parent d5096cd commit b9b75c3

File tree

1 file changed

+59
-52
lines changed

1 file changed

+59
-52
lines changed

liquidity/liquidity.go

Lines changed: 59 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -652,25 +652,32 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoloop bool) (
652652
return nil, nil
653653
}
654654

655-
eligible, err := m.getEligibleChannels(ctx, loopOut, loopIn)
655+
channels, err := m.cfg.Lnd.Client.ListChannels(ctx)
656656
if err != nil {
657657
return nil, err
658658
}
659659

660+
// Get a summary of the channels and peers that are not eligible due
661+
// to ongoing swaps.
662+
traffic := m.currentSwapTraffic(loopOut, loopIn)
663+
660664
var suggestions []loop.OutRequest
661-
for _, channel := range eligible {
662-
channelID := lnwire.NewShortChanIDFromInt(channel.ChannelID)
663-
rule, ok := m.params.ChannelRules[channelID]
665+
666+
for _, channel := range channels {
667+
balance := newBalances(channel)
668+
669+
rule, ok := m.params.ChannelRules[balance.channelID]
664670
if !ok {
665671
continue
666672
}
667673

668-
balance := newBalances(channel)
669-
670-
suggestion := rule.suggestSwap(balance, restrictions)
674+
if !traffic.maySwap(channel.PubKeyBytes, balance.channelID) {
675+
continue
676+
}
671677

672678
// We can have nil suggestions in the case where no action is
673679
// required, so we skip over them.
680+
suggestion := rule.suggestSwap(balance, restrictions)
674681
if suggestion == nil {
675682
continue
676683
}
@@ -928,17 +935,13 @@ func (m *Manager) checkExistingAutoLoops(ctx context.Context,
928935
return &summary, nil
929936
}
930937

931-
// getEligibleChannels takes lists of our existing loop out and in swaps, and
932-
// gets a list of channels that are not currently being utilized for a swap.
933-
func (m *Manager) getEligibleChannels(ctx context.Context,
934-
loopOut []*loopdb.LoopOut, loopIn []*loopdb.LoopIn) (
935-
[]lndclient.ChannelInfo, error) {
938+
// currentSwapTraffic examines our existing swaps and returns a summary of the
939+
// current activity which can be used to determine whether we should perform
940+
// any swaps.
941+
func (m *Manager) currentSwapTraffic(loopOut []*loopdb.LoopOut,
942+
loopIn []*loopdb.LoopIn) *swapTraffic {
936943

937-
var (
938-
existingOut = make(map[lnwire.ShortChannelID]bool)
939-
existingIn = make(map[route.Vertex]bool)
940-
failedOut = make(map[lnwire.ShortChannelID]time.Time)
941-
)
944+
traffic := newSwapTraffic()
942945

943946
// Failure cutoff is the most recent failure timestamp we will still
944947
// consider a channel eligible. Any channels involved in swaps that have
@@ -971,7 +974,7 @@ func (m *Manager) getEligibleChannels(ctx context.Context,
971974
id,
972975
)
973976

974-
failedOut[chanID] = failedAt
977+
traffic.failedLoopOut[chanID] = failedAt
975978
}
976979
}
977980
}
@@ -988,7 +991,7 @@ func (m *Manager) getEligibleChannels(ctx context.Context,
988991

989992
for _, id := range chanSet {
990993
chanID := lnwire.NewShortChanIDFromInt(id)
991-
existingOut[chanID] = true
994+
traffic.ongoingLoopOut[chanID] = true
992995
}
993996
}
994997

@@ -1003,51 +1006,55 @@ func (m *Manager) getEligibleChannels(ctx context.Context,
10031006
continue
10041007
}
10051008

1006-
existingIn[*in.Contract.LastHop] = true
1009+
traffic.ongoingLoopIn[*in.Contract.LastHop] = true
10071010
}
10081011

1009-
channels, err := m.cfg.Lnd.Client.ListChannels(ctx)
1010-
if err != nil {
1011-
return nil, err
1012-
}
1012+
return traffic
1013+
}
10131014

1014-
// Run through our set of channels and skip over any channels that
1015-
// are currently being utilized by a restricted swap (where restricted
1016-
// means that a loop out limited channels, or a loop in limited last
1017-
// hop).
1018-
var eligible []lndclient.ChannelInfo
1019-
for _, channel := range channels {
1020-
shortID := lnwire.NewShortChanIDFromInt(channel.ChannelID)
1015+
// swapTraffic contains a summary of our current and previously failed swaps.
1016+
type swapTraffic struct {
1017+
ongoingLoopOut map[lnwire.ShortChannelID]bool
1018+
ongoingLoopIn map[route.Vertex]bool
1019+
failedLoopOut map[lnwire.ShortChannelID]time.Time
1020+
}
10211021

1022-
lastFail, recentFail := failedOut[shortID]
1023-
if recentFail {
1024-
log.Debugf("Channel: %v not eligible for "+
1025-
"suggestions, was part of a failed swap at: %v",
1026-
channel.ChannelID, lastFail)
1022+
func newSwapTraffic() *swapTraffic {
1023+
return &swapTraffic{
1024+
ongoingLoopOut: make(map[lnwire.ShortChannelID]bool),
1025+
ongoingLoopIn: make(map[route.Vertex]bool),
1026+
failedLoopOut: make(map[lnwire.ShortChannelID]time.Time),
1027+
}
1028+
}
10271029

1028-
continue
1029-
}
1030+
// maySwap returns a boolean that indicates whether we may perform a swap for a
1031+
// peer and its set of channels.
1032+
func (s *swapTraffic) maySwap(peer route.Vertex,
1033+
chanID lnwire.ShortChannelID) bool {
10301034

1031-
if existingOut[shortID] {
1032-
log.Debugf("Channel: %v not eligible for "+
1033-
"suggestions, ongoing loop out utilizing "+
1034-
"channel", channel.ChannelID)
1035+
lastFail, recentFail := s.failedLoopOut[chanID]
1036+
if recentFail {
1037+
log.Debugf("Channel: %v not eligible for suggestions, was "+
1038+
"part of a failed swap at: %v", chanID, lastFail)
10351039

1036-
continue
1037-
}
1040+
return false
1041+
}
10381042

1039-
if existingIn[channel.PubKeyBytes] {
1040-
log.Debugf("Channel: %v not eligible for "+
1041-
"suggestions, ongoing loop in utilizing "+
1042-
"peer", channel.ChannelID)
1043+
if s.ongoingLoopOut[chanID] {
1044+
log.Debugf("Channel: %v not eligible for suggestions, "+
1045+
"ongoing loop out utilizing channel", chanID)
10431046

1044-
continue
1045-
}
1047+
return false
1048+
}
1049+
1050+
if s.ongoingLoopIn[peer] {
1051+
log.Debugf("Peer: %x not eligible for suggestions ongoing "+
1052+
"loop in utilizing peer", peer)
10461053

1047-
eligible = append(eligible, channel)
1054+
return false
10481055
}
10491056

1050-
return eligible, nil
1057+
return true
10511058
}
10521059

10531060
// checkFeeLimits takes a set of fees for a swap and checks whether they exceed

0 commit comments

Comments
 (0)