|
| 1 | +package liquidity |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/btcsuite/btcd/btcutil" |
| 7 | + "github.com/lightninglabs/lndclient" |
| 8 | + "github.com/lightningnetwork/lnd/lnwire" |
| 9 | + "github.com/lightningnetwork/lnd/routing/route" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +// TestEasyAutoloopExcludedPeers ensures that peers listed in |
| 14 | +// Parameters.EasyAutoloopExcludedPeers are not selected by |
| 15 | +// pickEasyAutoloopChannel even if they would otherwise be preferred. |
| 16 | +func TestEasyAutoloopExcludedPeers(t *testing.T) { |
| 17 | + // Two channels, peer1 has the higher local balance and would be picked |
| 18 | + // if not excluded. |
| 19 | + ch1 := lndclient.ChannelInfo{ |
| 20 | + Active: true, |
| 21 | + ChannelID: lnwire.NewShortChanIDFromInt(11).ToUint64(), |
| 22 | + PubKeyBytes: peer1, |
| 23 | + LocalBalance: 90000, |
| 24 | + RemoteBalance: 0, |
| 25 | + Capacity: 100000, |
| 26 | + } |
| 27 | + ch2 := lndclient.ChannelInfo{ |
| 28 | + Active: true, |
| 29 | + ChannelID: lnwire.NewShortChanIDFromInt(22).ToUint64(), |
| 30 | + PubKeyBytes: peer2, |
| 31 | + LocalBalance: 80000, |
| 32 | + RemoteBalance: 0, |
| 33 | + Capacity: 100000, |
| 34 | + } |
| 35 | + |
| 36 | + params := defaultParameters |
| 37 | + params.Autoloop = true |
| 38 | + params.EasyAutoloop = true |
| 39 | + params.EasyAutoloopTarget = 80000 |
| 40 | + params.ClientRestrictions.Minimum = btcutil.Amount(1) |
| 41 | + params.ClientRestrictions.Maximum = btcutil.Amount(10000) |
| 42 | + // Exclude peer1, even though its channel has more local balance. |
| 43 | + params.EasyAutoloopExcludedPeers = []route.Vertex{peer1} |
| 44 | + |
| 45 | + c := newAutoloopTestCtx( |
| 46 | + t, params, []lndclient.ChannelInfo{ch1, ch2}, testRestrictions, |
| 47 | + ) |
| 48 | + |
| 49 | + // Picking a channel should not pick the excluded peer's channel. |
| 50 | + picked := c.manager.pickEasyAutoloopChannel( |
| 51 | + []lndclient.ChannelInfo{ch1, ch2}, ¶ms.ClientRestrictions, |
| 52 | + nil, nil, 1, |
| 53 | + ) |
| 54 | + require.NotNil(t, picked) |
| 55 | + require.Equal( |
| 56 | + t, ch2.ChannelID, picked.ChannelID, |
| 57 | + "should pick non-excluded peer's channel", |
| 58 | + ) |
| 59 | +} |
| 60 | + |
| 61 | +// TestEasyAutoloopIncludeAllPeers simulates the --includealleasypeers flag by |
| 62 | +// clearing the exclusion list and ensuring a previously excluded peer can be |
| 63 | +// selected again. |
| 64 | +func TestEasyAutoloopIncludeAllPeers(t *testing.T) { |
| 65 | + ch1 := lndclient.ChannelInfo{ |
| 66 | + Active: true, |
| 67 | + ChannelID: lnwire.NewShortChanIDFromInt(33).ToUint64(), |
| 68 | + PubKeyBytes: peer1, |
| 69 | + LocalBalance: 90000, |
| 70 | + RemoteBalance: 0, |
| 71 | + Capacity: 100000, |
| 72 | + } |
| 73 | + ch2 := lndclient.ChannelInfo{ |
| 74 | + Active: true, |
| 75 | + ChannelID: lnwire.NewShortChanIDFromInt(44).ToUint64(), |
| 76 | + PubKeyBytes: peer2, |
| 77 | + LocalBalance: 80000, |
| 78 | + RemoteBalance: 0, |
| 79 | + Capacity: 100000, |
| 80 | + } |
| 81 | + |
| 82 | + params := defaultParameters |
| 83 | + params.Autoloop = true |
| 84 | + params.EasyAutoloop = true |
| 85 | + params.EasyAutoloopTarget = 80000 |
| 86 | + params.ClientRestrictions.Minimum = btcutil.Amount(1) |
| 87 | + params.ClientRestrictions.Maximum = btcutil.Amount(10000) |
| 88 | + params.EasyAutoloopExcludedPeers = []route.Vertex{peer1} |
| 89 | + |
| 90 | + c := newAutoloopTestCtx( |
| 91 | + t, params, []lndclient.ChannelInfo{ch1, ch2}, testRestrictions, |
| 92 | + ) |
| 93 | + |
| 94 | + // With exclusion active, peer1 should not be picked. |
| 95 | + picked := c.manager.pickEasyAutoloopChannel( |
| 96 | + []lndclient.ChannelInfo{ch1, ch2}, ¶ms.ClientRestrictions, |
| 97 | + nil, nil, 1, |
| 98 | + ) |
| 99 | + require.NotNil(t, picked) |
| 100 | + require.Equal(t, ch2.ChannelID, picked.ChannelID) |
| 101 | + |
| 102 | + // Simulate --includealleasypeers by clearing the exclusion list as the |
| 103 | + // CLI does before sending to the server. |
| 104 | + c.manager.params.EasyAutoloopExcludedPeers = nil |
| 105 | + |
| 106 | + picked = c.manager.pickEasyAutoloopChannel( |
| 107 | + []lndclient.ChannelInfo{ch1, ch2}, ¶ms.ClientRestrictions, |
| 108 | + nil, nil, 1, |
| 109 | + ) |
| 110 | + require.NotNil(t, picked) |
| 111 | + require.Equal( |
| 112 | + t, ch1.ChannelID, picked.ChannelID, |
| 113 | + "after include-all, highest local balance should win again", |
| 114 | + ) |
| 115 | +} |
0 commit comments