Skip to content

Commit

Permalink
autoloop: ignore asset channels
Browse files Browse the repository at this point in the history
  • Loading branch information
sputn1ck committed Jan 31, 2025
1 parent 7b8ce15 commit d971381
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
18 changes: 14 additions & 4 deletions liquidity/autoloop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1287,9 +1287,19 @@ func TestEasyAutoloop(t *testing.T) {
Capacity: 100000,
}

easyChannelCustom := lndclient.ChannelInfo{
Active: true,
ChannelID: chanID1.ToUint64(),
PubKeyBytes: peer1,
LocalBalance: 50000,
RemoteBalance: 0,
Capacity: 100000,
CustomChannelData: []byte("foo"),
}

var (
channels = []lndclient.ChannelInfo{
easyChannel1, easyChannel2,
easyChannel1, easyChannel2, easyChannelCustom,
}

params = Parameters{
Expand Down Expand Up @@ -1361,7 +1371,7 @@ func TestEasyAutoloop(t *testing.T) {
// new context and restart the autolooper.
easyChannel1.LocalBalance -= chan1Swap.Amount
channels = []lndclient.ChannelInfo{
easyChannel1, easyChannel2,
easyChannel1, easyChannel2, easyChannelCustom,
}

// Remove the custom dest address.
Expand Down Expand Up @@ -1419,7 +1429,7 @@ func TestEasyAutoloop(t *testing.T) {
// new context and restart the autolooper.
easyChannel2.LocalBalance -= btcutil.Amount(amt2)
channels = []lndclient.ChannelInfo{
easyChannel1, easyChannel2,
easyChannel1, easyChannel2, easyChannelCustom,
}

c = newAutoloopTestCtx(t, params, channels, testRestrictions)
Expand All @@ -1438,7 +1448,7 @@ func TestEasyAutoloop(t *testing.T) {
// Restore the local balance to a higher value that will trigger a swap.
easyChannel2.LocalBalance = btcutil.Amount(95000)
channels = []lndclient.ChannelInfo{
easyChannel1, easyChannel2,
easyChannel1, easyChannel2, easyChannelCustom,
}

// Override the feeppm with a lower one.
Expand Down
26 changes: 26 additions & 0 deletions liquidity/liquidity.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,12 @@ func (m *Manager) dispatchBestEasyAutoloopSwap(ctx context.Context) error {

localTotal := btcutil.Amount(0)
for _, channel := range channels {
// If the channel has custom channel data, the channel is a
// non-standard channel, such as an asset channel and we
// don't want to consider it for swaps.
if channel.CustomChannelData != nil {
continue
}
localTotal += channel.LocalBalance
}

Expand Down Expand Up @@ -781,6 +787,12 @@ func (m *Manager) SuggestSwaps(ctx context.Context) (
channelPeers := make(map[uint64]route.Vertex)
peerChannels := make(map[route.Vertex]*balances)
for _, channel := range channels {
// If the channel has custom channel data, the channel is a
// non-standard channel, such as an asset channel and we
// don't want to consider it for swaps.
if channel.CustomChannelData != nil {
continue
}
channelPeers[channel.ChannelID] = channel.PubKeyBytes

bal, ok := peerChannels[channel.PubKeyBytes]
Expand Down Expand Up @@ -834,6 +846,13 @@ func (m *Manager) SuggestSwaps(ctx context.Context) (
balance := newBalances(channel)

channelID := lnwire.NewShortChanIDFromInt(channel.ChannelID)
// If the channel has custom channel data, the channel is a
// non-standard channel, such as an asset channel and we
// don't want to consider it for swaps.
if channel.CustomChannelData != nil {
resp.DisqualifiedChans[channelID] = ReasonCustomChannelData
continue
}
rule, ok := m.params.ChannelRules[channelID]
if !ok {
continue
Expand Down Expand Up @@ -1424,6 +1443,13 @@ func (m *Manager) pickEasyAutoloopChannel(channels []lndclient.ChannelInfo,
// Check each channel, since channels are already sorted we return the
// first channel that passes all checks.
for _, channel := range channels {
// If the channel has custom channel data, the channel is a
// non-standard channel, such as an asset channel and we
// don't want to consider it for swaps.
if channel.CustomChannelData != nil {
continue
}

shortChanID := lnwire.NewShortChanIDFromInt(channel.ChannelID)

if !channel.Active {
Expand Down
23 changes: 23 additions & 0 deletions liquidity/liquidity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,29 @@ func TestSuggestSwaps(t *testing.T) {
},
},
},
{

name: "don't consider asset channel",
channels: []lndclient.ChannelInfo{
{
ChannelID: chanID1.ToUint64(),
PubKeyBytes: peer1,
LocalBalance: 10000,
RemoteBalance: 0,
Capacity: 10000,
CustomChannelData: []byte("foo"),
},
},
rules: map[lnwire.ShortChannelID]*SwapRule{
chanID1: chanRule,
},
suggestions: &Suggestions{
DisqualifiedChans: map[lnwire.ShortChannelID]Reason{ // nolint: lll
chanID1: ReasonCustomChannelData,
},
DisqualifiedPeers: noPeersDisqualified,
},
},
}

for _, testCase := range tests {
Expand Down
4 changes: 4 additions & 0 deletions liquidity/reasons.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ const (
// ReasonLoopInUnreachable indicates that the server does not have a
// path to the client, so cannot perform a loop in swap at this time.
ReasonLoopInUnreachable

// ReasonCustomChannelData indicates that the channel is not standard
// and should not be used for swaps.
ReasonCustomChannelData
)

// String returns a string representation of a reason.
Expand Down

0 comments on commit d971381

Please sign in to comment.