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 Feb 7, 2025
1 parent 1f07c37 commit 4dd3074
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 4 deletions.
19 changes: 15 additions & 4 deletions liquidity/autoloop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1287,9 +1287,20 @@ func TestEasyAutoloop(t *testing.T) {
Capacity: 100000,
}

// The custom channel should be ignored.
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 +1372,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 +1430,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 +1449,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
28 changes: 28 additions & 0 deletions liquidity/liquidity.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,9 @@ func (m *Manager) dispatchBestEasyAutoloopSwap(ctx context.Context) error {

localTotal := btcutil.Amount(0)
for _, channel := range channels {
if channelIsCustom(channel) {
continue
}
localTotal += channel.LocalBalance
}

Expand Down Expand Up @@ -781,6 +784,10 @@ func (m *Manager) SuggestSwaps(ctx context.Context) (
channelPeers := make(map[uint64]route.Vertex)
peerChannels := make(map[route.Vertex]*balances)
for _, channel := range channels {
if channelIsCustom(channel) {
continue
}

channelPeers[channel.ChannelID] = channel.PubKeyBytes

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

channelID := lnwire.NewShortChanIDFromInt(channel.ChannelID)

if channelIsCustom(channel) {
resp.DisqualifiedChans[channelID] =
ReasonCustomChannelData

continue
}

rule, ok := m.params.ChannelRules[channelID]
if !ok {
continue
Expand Down Expand Up @@ -1424,6 +1439,10 @@ 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 channelIsCustom(channel) {
continue
}

shortChanID := lnwire.NewShortChanIDFromInt(channel.ChannelID)

if !channel.Active {
Expand Down Expand Up @@ -1548,3 +1567,12 @@ func satPerKwToSatPerVByte(satPerKw chainfee.SatPerKWeight) int64 {
func ppmToSat(amount btcutil.Amount, ppm uint64) btcutil.Amount {
return btcutil.Amount(uint64(amount) * ppm / FeeBase)
}

// channelIsCustom returns true if the channel has custom channel data.
// we'll want to ignore these channels for autoloop recommendations.
func channelIsCustom(channel lndclient.ChannelInfo) bool {
// 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.
return channel.CustomChannelData != nil
}
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 4dd3074

Please sign in to comment.