Skip to content

Commit

Permalink
Fix #37
Browse files Browse the repository at this point in the history
feat(liquidator.go): add check for swapAmount exceeding maxPendingLocalSats and adjust accordingly (#37)
test(liquidator_test.go): add test case for managing channel liquidity bypassing max pending amount
feat(loop_provider.go): adjust SweepConfTarget and HtlcConfirmations from 2 to 3 for better reliability
style: remove unnecessary blank lines and adjust indentation for better code readability
chore: update loop subproject commit hash
  • Loading branch information
Jossec101 authored Jul 24, 2023
1 parent 8403021 commit 887b2dd
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 41 deletions.
36 changes: 19 additions & 17 deletions liquidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ func startLiquidator() {
log.Fatal("failed to generate context with macaroon")
}


//Get the local node info
nodeInfo, err := getLocalNodeInfo(lightningClient, nodeContext)
if err != nil {
Expand Down Expand Up @@ -349,22 +348,18 @@ func monitorChannel(info MonitorChannelInfo) {

channelRules := info.liquidationRules[info.channel.GetChanId()]

if len(info.channel.PendingHtlcs) == 0 {
//Manage liquidity if there are no pending htlcs
err = manageChannelLiquidity(ManageChannelLiquidityInfo{
channel: info.channel,
channelBalanceRatio: channelBalanceRatio,
channelRules: &channelRules,
swapClientClient: info.swapClient,
nodeguardClient: info.nodeguardClient,
loopProvider: info.loopProvider,
loopdMacaroon: info.loopdMacaroon,
nodeInfo: info.nodeInfo,
ctx: spanCtx,
})
} else {
log.WithField("span", span).Debugf("channel: %v has pending htlcs, skipping liquidity management", info.channel.GetChanId())
}
//Manage liquidity if there are no pending htlcs
err = manageChannelLiquidity(ManageChannelLiquidityInfo{
channel: info.channel,
channelBalanceRatio: channelBalanceRatio,
channelRules: &channelRules,
swapClientClient: info.swapClient,
nodeguardClient: info.nodeguardClient,
loopProvider: info.loopProvider,
loopdMacaroon: info.loopdMacaroon,
nodeInfo: info.nodeInfo,
ctx: spanCtx,
})

if err != nil {

Expand Down Expand Up @@ -448,6 +443,13 @@ func manageChannelLiquidity(info ManageChannelLiquidityInfo) error {
//Calculate the swap amount
swapAmount := helper.AbsInt64((channel.RemoteBalance - swapAmountTarget))

// if the swapAmount is bigger than max pending htlc amount, set it to max pending htlc amount
maxPendingLocalSats := int64(channel.GetLocalConstraints().GetMaxPendingAmtMsat() / 1000)
if swapAmount > maxPendingLocalSats && maxPendingLocalSats > 0 {
swapAmount = maxPendingLocalSats
log.WithField("span",span).Infof("swap amount is bigger than max pending htlc amount, setting it to max pending htlc amount: %v", maxPendingLocalSats)
}

//Request nodeguard a new destination address for the reverse swap
walletRequest := &nodeguard.GetNewWalletAddressRequest{
WalletId: rule.WalletId,
Expand Down
22 changes: 22 additions & 0 deletions liquidator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ func Test_manageChannelLiquidity(t *testing.T) {
LocalBalance: 100,
RemoteBalance: 900,
RemotePubkey: "03485d8dcdd149c87553eeb80586eb2bece874d412e9f117304446ce189955d375",
LocalConstraints: &lnrpc.ChannelConstraints{
CsvDelay: 144,
ChanReserveSat: 250,
DustLimitSat: 300,
MaxPendingAmtMsat: 300*1000,
MinHtlcMsat: 350 * 1000,
MaxAcceptedHtlcs: 30,
},
}

nodeInfo := lnrpc.GetInfoResponse{
Expand All @@ -137,6 +145,20 @@ func Test_manageChannelLiquidity(t *testing.T) {
ctx: context.TODO(),
},
wantErr: false,
},
{
name: "Manage channel liquidity test valid reverse swap bypassing max pending amt",
args: ManageChannelLiquidityInfo{
channel: channelActive,
channelBalanceRatio: 0.1,
channelRules: &[]nodeguard.LiquidityRule{{ChannelId: 123, NodePubkey: "", WalletId: 1, MinimumLocalBalance: 20, MinimumRemoteBalance: 80, RebalanceTarget: 40}},
nodeguardClient: mockNodeGuardClient,
loopProvider: mockProvider,
loopdMacaroon: "0201036c6e6402f801030a10dc64226b045d25f090b114baebcbf04c1201301a160a0761646472657373120472656164120577726974651a130a04696e666f120472656164120577726974651a170a08696e766f69636573120472656164120577726974651a210a086d616361726f6f6e120867656e6572617465120472656164120577726974651a160a076d657373616765120472656164120577726974651a170a086f6666636861696e120472656164120577726974651a160a076f6e636861696e120472656164120577726974651a140a057065657273120472656164120577726974651a180a067369676e6572120867656e657261746512047265616400000620a21b8cc8c071aa5104b706b751aede972f642537c05da31450fb4b02c6da776e",
nodeInfo: nodeInfo,
ctx: context.TODO(),
},
wantErr: false,
},
{
name: "Manage channel liquidity test valid swap",
Expand Down
2 changes: 1 addition & 1 deletion loop
Submodule loop updated 72 files
+4 −1 .github/workflows/main.yml
+1 −1 Dockerfile
+12 −0 Makefile
+11 −13 client.go
+36 −0 cmd/loop/info.go
+35 −13 cmd/loop/liquidity.go
+2 −8 cmd/loop/lsat.go
+1 −0 cmd/loop/main.go
+30 −7 docs/autoloop.md
+55 −28 go.mod
+1,228 −46 go.sum
+18 −0 labels/labels.go
+232 −19 liquidity/autoloop_test.go
+101 −5 liquidity/autoloop_testcontext_test.go
+29 −5 liquidity/fees.go
+1 −1 liquidity/interface.go
+371 −63 liquidity/liquidity.go
+53 −37 liquidity/liquidity_test.go
+6 −2 liquidity/loopin_builder.go
+1 −1 liquidity/loopin_builder_test.go
+6 −2 liquidity/loopout_builder.go
+20 −4 liquidity/parameters.go
+34 −3 loopd/config.go
+18 −5 loopd/daemon.go
+92 −0 loopd/migration.go
+4 −0 loopd/perms/perms.go
+85 −1 loopd/swapclient_server.go
+46 −11 loopd/utils.go
+3 −2 loopd/view.go
+31 −9 loopdb/interface.go
+426 −0 loopdb/migrate.go
+38 −0 loopdb/migrate_test.go
+5 −2 loopdb/migration_04_updates_test.go
+147 −0 loopdb/migrations.go
+135 −0 loopdb/postgres.go
+139 −0 loopdb/postgres_fixture.go
+8 −0 loopdb/schemas.go
+734 −0 loopdb/sql_store.go
+389 −0 loopdb/sql_test.go
+31 −0 loopdb/sqlc/db.go
+35 −0 loopdb/sqlc/liquidity_params.sql.go
+7 −0 loopdb/sqlc/migrations/000001_swap.down.sql
+166 −0 loopdb/sqlc/migrations/000001_swap.up.sql
+1 −0 loopdb/sqlc/migrations/000002_liquidity_params.down.sql
+6 −0 loopdb/sqlc/migrations/000002_liquidity_params.up.sql
+69 −0 loopdb/sqlc/models.go
+26 −0 loopdb/sqlc/querier.go
+10 −0 loopdb/sqlc/queries/liquidity_params.sql
+133 −0 loopdb/sqlc/queries/swaps.sql
+584 −0 loopdb/sqlc/swaps.sql.go
+71 −0 loopdb/sqlerrors.go
+221 −0 loopdb/sqlite.go
+43 −11 loopdb/store.go
+27 −17 loopdb/store_test.go
+13 −0 loopdb/test_postgres.go
+13 −0 loopdb/test_sqlite.go
+46 −49 loopin.go
+2 −1 loopin_test.go
+2 −2 loopout.go
+621 −251 looprpc/client.pb.go
+65 −0 looprpc/client.pb.gw.go
+97 −0 looprpc/client.proto
+103 −0 looprpc/client.swagger.json
+2 −0 looprpc/client.yaml
+40 −0 looprpc/client_grpc.pb.go
+25 −0 looprpc/swapclient.pb.json.go
+1 −1 release.sh
+20 −0 scripts/gen_sqlc_docker.sh
+10 −0 sqlc.yaml
+33 −11 store_mock_test.go
+0 −2 swap/keychain.go
+2 −2 version.go
20 changes: 10 additions & 10 deletions provider/loop_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,16 +234,16 @@ func (l *LoopProvider) RequestReverseSubmarineSwap(ctx context.Context, request

//Use the client to request the swap
resp, err := client.LoopOut(ctx, &looprpc.LoopOutRequest{
Amt: request.SatsAmount,
Dest: request.ReceiverBTCAddress,
MaxMinerFee: int64(limits.maxMinerFee),
MaxPrepayAmt: int64(limits.maxPrepayAmt),
MaxSwapFee: int64(limits.maxSwapFee),
MaxPrepayRoutingFee: int64(limits.maxPrepayRoutingFee),
MaxSwapRoutingFee: int64(limits.maxSwapRoutingFee),
OutgoingChanSet: request.ChannelSet,
SweepConfTarget: 2, //TODO Make this configurable
HtlcConfirmations: 2,
Amt: request.SatsAmount,
Dest: request.ReceiverBTCAddress,
MaxMinerFee: int64(limits.maxMinerFee),
MaxPrepayAmt: int64(limits.maxPrepayAmt),
MaxSwapFee: int64(limits.maxSwapFee),
MaxPrepayRoutingFee: int64(limits.maxPrepayRoutingFee),
MaxSwapRoutingFee: int64(limits.maxSwapRoutingFee),
OutgoingChanSet: request.ChannelSet,
SweepConfTarget: 3, //TODO Make this configurable
HtlcConfirmations: 3,
//The publication deadline is maximum the offset of the swap deadline conf plus the current time
SwapPublicationDeadline: uint64(time.Now().Add(viper.GetDuration("swapPublicationOffset") * time.Minute).Unix()),
Label: fmt.Sprintf("Reverse submarine swap %d sats on date %s", request.SatsAmount, time.Now().Format(time.RFC3339)),
Expand Down
24 changes: 12 additions & 12 deletions provider/loop_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,12 +366,12 @@ func Test_checkSubmarineSwapNotInProgress(t *testing.T) {
swapClientWithOngoingSwaps.EXPECT().ListSwaps(gomock.Any(), gomock.Any()).Return(&looprpc.ListSwapsResponse{
Swaps: []*looprpc.SwapStatus{
{
Amt: 0,
Id: "",
IdBytes: idBytes,
Type: looprpc.SwapType_LOOP_IN,
State: looprpc.SwapState_INITIATED,
FailureReason: 0,
Amt: 0,
Id: "",
IdBytes: idBytes,
Type: looprpc.SwapType_LOOP_IN,
State: looprpc.SwapState_INITIATED,
FailureReason: 0,
//InitiationTime 4 hours ago
InitiationTime: time.Now().Add(-4 * time.Hour).UnixNano(),
LastUpdateTime: time.Now().Add(-4 * time.Hour).UnixNano(),
Expand All @@ -393,12 +393,12 @@ func Test_checkSubmarineSwapNotInProgress(t *testing.T) {
swapClientWithNoOngoingSwaps.EXPECT().ListSwaps(gomock.Any(), gomock.Any()).Return(&looprpc.ListSwapsResponse{
Swaps: []*looprpc.SwapStatus{
{
Amt: 0,
Id: "",
IdBytes: idBytes,
Type: looprpc.SwapType_LOOP_IN,
State: looprpc.SwapState_INITIATED,
FailureReason: 0,
Amt: 0,
Id: "",
IdBytes: idBytes,
Type: looprpc.SwapType_LOOP_IN,
State: looprpc.SwapState_INITIATED,
FailureReason: 0,
//InitiationTime is more than 24 hours ago, stuck but ignored
InitiationTime: time.Now().Add(-25 * time.Hour).UnixNano(),
LastUpdateTime: time.Now().Add(-25 * time.Hour).UnixNano(),
Expand Down
1 change: 0 additions & 1 deletion rpc/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ func TestCreateSwapClientClient(t *testing.T) {
Cert: tlsCertEncoded,
Macaroon: "0201036c6e6402f801030a101ec5b6370c166f6c8e2853164109145a1201301a160a0761646472657373120472656164120577726974651a130a04696e666f120472656164120577726974651a170a08696e766f69636573120472656164120577726974651a210a086d616361726f6f6e120867656e6572617465120472656164120577726974651a160a076d657373616765120472656164120577726974651a170a086f6666636861696e120472656164120577726974651a160a076f6e636861696e120472656164120577726974651a140a057065657273120472656164120577726974651a180a067369676e6572120867656e6572617465120472656164000006208e957e78ec39e7810fad25cfc43850b8e9e7c079843b8ec7bb5522bba12230d6",
},

},
wantErr: false,
},
Expand Down

0 comments on commit 887b2dd

Please sign in to comment.