From ac3d79e50dcdbb0a5d70708d3c7bad49a8835e0b Mon Sep 17 00:00:00 2001 From: George Tsagkarelis Date: Thu, 31 Aug 2023 18:34:18 +0300 Subject: [PATCH] loop+loopd+liquidity: use isWalletAddr flag --- cmd/loop/loopout.go | 3 +++ interface.go | 5 +++++ liquidity/liquidity.go | 1 + liquidity/loopout_builder.go | 2 ++ loopd/swapclient_server.go | 4 ++++ loopout.go | 29 +++++++++++++++++------------ 6 files changed, 32 insertions(+), 12 deletions(-) diff --git a/cmd/loop/loopout.go b/cmd/loop/loopout.go index 688ddeaf2f..5bcd91eafb 100644 --- a/cmd/loop/loopout.go +++ b/cmd/loop/loopout.go @@ -147,9 +147,11 @@ func loopOut(ctx *cli.Context) error { var destAddr string var account string + isWalletAddr := true switch { case ctx.IsSet("addr"): destAddr = ctx.String("addr") + isWalletAddr = false case ctx.IsSet("account"): account = ctx.String("account") @@ -235,6 +237,7 @@ func loopOut(ctx *cli.Context) error { resp, err := client.LoopOut(context.Background(), &looprpc.LoopOutRequest{ Amt: int64(amt), Dest: destAddr, + IsWalletAddr: isWalletAddr, Account: account, AccountAddrType: accountAddrType, MaxMinerFee: int64(limits.maxMinerFee), diff --git a/interface.go b/interface.go index 3476fd757e..2707273dc9 100644 --- a/interface.go +++ b/interface.go @@ -20,6 +20,11 @@ type OutRequest struct { // Destination address for the swap. DestAddr btcutil.Address + // IsWalletAddr indicates whether the provided destination address + // belongs to the underlying wallet. This helps indicate whether the + // sweep of this swap can be batched or not. + IsWalletAddr bool + // MaxSwapRoutingFee is the maximum off-chain fee in msat that may be // paid for payment to the server. This limit is applied during path // finding. Typically this value is taken from the response of the diff --git a/liquidity/liquidity.go b/liquidity/liquidity.go index 64b0a6a160..6df544bc2c 100644 --- a/liquidity/liquidity.go +++ b/liquidity/liquidity.go @@ -454,6 +454,7 @@ func (m *Manager) autoloop(ctx context.Context) error { // outs. if m.params.DestAddr != nil { swap.DestAddr = m.params.DestAddr + swap.IsWalletAddr = false } go m.dispatchStickyLoopOut( diff --git a/liquidity/loopout_builder.go b/liquidity/loopout_builder.go index 658c9407c8..6c27fcf8ca 100644 --- a/liquidity/loopout_builder.go +++ b/liquidity/loopout_builder.go @@ -138,6 +138,7 @@ func (b *loopOutBuilder) buildSwap(ctx context.Context, pubkey route.Vertex, // already validated them. request := loop.OutRequest{ Amount: amount, + IsWalletAddr: true, OutgoingChanSet: chanSet, MaxPrepayRoutingFee: prepayMaxFee, MaxSwapRoutingFee: routeMaxFee, @@ -160,6 +161,7 @@ func (b *loopOutBuilder) buildSwap(ctx context.Context, pubkey route.Vertex, if len(params.Account) > 0 { account = params.Account addrType = params.AccountAddrType + request.IsWalletAddr = false } addr, err := b.cfg.Lnd.WalletKit.NextAddr( diff --git a/loopd/swapclient_server.go b/loopd/swapclient_server.go index 470ac80982..ba158dc158 100644 --- a/loopd/swapclient_server.go +++ b/loopd/swapclient_server.go @@ -96,6 +96,7 @@ func (s *swapClientServer) LoopOut(ctx context.Context, log.Infof("Loop out request received") var sweepAddr btcutil.Address + var isWalletAddr bool var err error //nolint:lll switch { @@ -146,6 +147,8 @@ func (s *swapClientServer) LoopOut(ctx context.Context, if err != nil { return nil, fmt.Errorf("NextAddr error: %v", err) } + + isWalletAddr = true } sweepConfTarget, err := validateLoopOutRequest( @@ -162,6 +165,7 @@ func (s *swapClientServer) LoopOut(ctx context.Context, req := &loop.OutRequest{ Amount: btcutil.Amount(in.Amt), DestAddr: sweepAddr, + IsWalletAddr: isWalletAddr, MaxMinerFee: btcutil.Amount(in.MaxMinerFee), MaxPrepayAmount: btcutil.Amount(in.MaxPrepayAmt), MaxPrepayRoutingFee: btcutil.Amount(in.MaxPrepayRoutingFee), diff --git a/loopout.go b/loopout.go index 109fed2082..4fa049116c 100644 --- a/loopout.go +++ b/loopout.go @@ -174,6 +174,7 @@ func newLoopOutSwap(globalCtx context.Context, cfg *swapConfig, contract := loopdb.LoopOutContract{ SwapInvoice: swapResp.swapInvoice, DestAddr: request.DestAddr, + IsWalletAddr: request.IsWalletAddr, MaxSwapRoutingFee: request.MaxSwapRoutingFee, SweepConfTarget: request.SweepConfTarget, HtlcConfirmations: confs, @@ -1606,19 +1607,23 @@ func (s *loopOutSwap) setStatePreimageRevealed(ctx context.Context) error { func (s *loopOutSwap) sweep(ctx context.Context, htlcOutpoint wire.OutPoint, htlcValue btcutil.Amount) error { - sweep := &sweepbatcher.Sweep{ - SwapHash: s.hash, - Outpoint: htlcOutpoint, - Value: htlcValue, - Timeout: s.CltvExpiry, - InitiationHeight: s.InitiationHeight, - Htlc: *s.htlc, - Preimage: s.Preimage, - Key: s.HtlcKeys.ReceiverScriptKey, - } + // If we're using a wallet address as a destination address then this + // sweep can be batched with other sweeps. + if s.IsWalletAddr { + sweep := &sweepbatcher.Sweep{ + SwapHash: s.hash, + Outpoint: htlcOutpoint, + Value: htlcValue, + Timeout: s.CltvExpiry, + InitiationHeight: s.InitiationHeight, + Htlc: *s.htlc, + Preimage: s.Preimage, + Key: s.HtlcKeys.ReceiverScriptKey, + } - s.executeConfig.batcher.SweepReqs <- sweep - return nil + s.executeConfig.batcher.SweepReqs <- sweep + return nil + } confTarget, canSweep := s.sweepConfTarget() if !canSweep {