Skip to content

Commit c70d0de

Browse files
committed
swap_server_client: let the SwapPublicationDeadline be set during LoopOuts
1 parent 2d1e41d commit c70d0de

File tree

4 files changed

+32
-17
lines changed

4 files changed

+32
-17
lines changed

interface.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ type OutRequest struct {
6666
// LoopOutChannel optionally specifies the short channel id of the
6767
// channel to loop out.
6868
LoopOutChannel *uint64
69+
70+
// SwapPublicationDeadline can be set by the client to allow the server
71+
// delaying publication of the swap HTLC to save on chain fees.
72+
SwapPublicationDeadline time.Time
6973
}
7074

7175
// Out contains the full details of a loop out request. This includes things

loopout.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,29 +79,36 @@ func newLoopOutSwap(globalCtx context.Context, cfg *swapConfig,
7979
// the server revocation key and the swap and prepay invoices.
8080
log.Infof("Initiating swap request at height %v", currentHeight)
8181

82-
swapResp, err := cfg.server.NewLoopOutSwap(globalCtx, swapHash,
83-
request.Amount, receiverKey,
82+
// The swap deadline will be given to the server for it to use as the
83+
// latest swap publication time.
84+
swapResp, err := cfg.server.NewLoopOutSwap(
85+
globalCtx, swapHash, request.Amount, receiverKey,
86+
request.SwapPublicationDeadline,
8487
)
8588
if err != nil {
8689
return nil, fmt.Errorf("cannot initiate swap: %v", err)
8790
}
8891

89-
err = validateLoopOutContract(cfg.lnd, currentHeight, request, swapHash, swapResp)
92+
err = validateLoopOutContract(
93+
cfg.lnd, currentHeight, request, swapHash, swapResp,
94+
)
9095
if err != nil {
9196
return nil, err
9297
}
9398

94-
// Instantiate a struct that contains all required data to start the swap.
99+
// Instantiate a struct that contains all required data to start the
100+
// swap.
95101
initiationTime := time.Now()
96102

97103
contract := loopdb.LoopOutContract{
98-
SwapInvoice: swapResp.swapInvoice,
99-
DestAddr: request.DestAddr,
100-
MaxSwapRoutingFee: request.MaxSwapRoutingFee,
101-
SweepConfTarget: request.SweepConfTarget,
102-
UnchargeChannel: request.LoopOutChannel,
103-
PrepayInvoice: swapResp.prepayInvoice,
104-
MaxPrepayRoutingFee: request.MaxPrepayRoutingFee,
104+
SwapInvoice: swapResp.swapInvoice,
105+
DestAddr: request.DestAddr,
106+
MaxSwapRoutingFee: request.MaxSwapRoutingFee,
107+
SweepConfTarget: request.SweepConfTarget,
108+
UnchargeChannel: request.LoopOutChannel,
109+
PrepayInvoice: swapResp.prepayInvoice,
110+
MaxPrepayRoutingFee: request.MaxPrepayRoutingFee,
111+
SwapPublicationDeadline: request.SwapPublicationDeadline,
105112
SwapContract: loopdb.SwapContract{
106113
InitiationHeight: currentHeight,
107114
InitiationTime: initiationTime,

server_mock_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func newServerMock() *serverMock {
5353

5454
func (s *serverMock) NewLoopOutSwap(ctx context.Context,
5555
swapHash lntypes.Hash, amount btcutil.Amount,
56-
receiverKey [33]byte) (
56+
receiverKey [33]byte, _ time.Time) (
5757
*newLoopOutResponse, error) {
5858

5959
_, senderKey := test.CreateKey(100)

swap_server_client.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/hex"
77
"errors"
88
"fmt"
9+
"time"
910

1011
"github.com/lightninglabs/loop/looprpc"
1112
"github.com/lightningnetwork/lnd/lntypes"
@@ -31,7 +32,8 @@ type swapServerClient interface {
3132

3233
NewLoopOutSwap(ctx context.Context,
3334
swapHash lntypes.Hash, amount btcutil.Amount,
34-
receiverKey [33]byte) (
35+
receiverKey [33]byte,
36+
swapPublicationDeadline time.Time) (
3537
*newLoopOutResponse, error)
3638

3739
NewLoopInSwap(ctx context.Context,
@@ -153,15 +155,17 @@ func (s *grpcSwapServerClient) GetLoopInQuote(ctx context.Context,
153155

154156
func (s *grpcSwapServerClient) NewLoopOutSwap(ctx context.Context,
155157
swapHash lntypes.Hash, amount btcutil.Amount,
156-
receiverKey [33]byte) (*newLoopOutResponse, error) {
158+
receiverKey [33]byte, swapPublicationDeadline time.Time) (
159+
*newLoopOutResponse, error) {
157160

158161
rpcCtx, rpcCancel := context.WithTimeout(ctx, serverRPCTimeout)
159162
defer rpcCancel()
160163
swapResp, err := s.server.NewLoopOutSwap(rpcCtx,
161164
&looprpc.ServerLoopOutRequest{
162-
SwapHash: swapHash[:],
163-
Amt: uint64(amount),
164-
ReceiverKey: receiverKey[:],
165+
SwapHash: swapHash[:],
166+
Amt: uint64(amount),
167+
ReceiverKey: receiverKey[:],
168+
SwapPublicationDeadline: swapPublicationDeadline.Unix(),
165169
},
166170
)
167171
if err != nil {

0 commit comments

Comments
 (0)