Skip to content

Commit 852f459

Browse files
committed
multi: add htlc confirmations parameter to client loop out request
1 parent 1877b7f commit 852f459

File tree

7 files changed

+175
-127
lines changed

7 files changed

+175
-127
lines changed

client_test.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var (
2727
Amount: btcutil.Amount(50000),
2828
DestAddr: testAddr,
2929
MaxMinerFee: 50000,
30+
HtlcConfirmations: defaultConfirmations,
3031
SweepConfTarget: 2,
3132
MaxSwapFee: 1050,
3233
MaxPrepayAmount: 100,
@@ -40,14 +41,18 @@ var (
4041
defaultConfirmations = int32(loopdb.DefaultLoopOutHtlcConfirmations)
4142
)
4243

43-
// TestSuccess tests the loop out happy flow.
44+
// TestSuccess tests the loop out happy flow, using a custom htlc confirmation
45+
// target.
4446
func TestSuccess(t *testing.T) {
4547
defer test.Guard(t)()
4648

4749
ctx := createClientTestContext(t, nil)
4850

51+
req := *testRequest
52+
req.HtlcConfirmations = 2
53+
4954
// Initiate loop out.
50-
info, err := ctx.swapClient.LoopOut(context.Background(), testRequest)
55+
info, err := ctx.swapClient.LoopOut(context.Background(), &req)
5156
if err != nil {
5257
t.Fatal(err)
5358
}
@@ -59,7 +64,7 @@ func TestSuccess(t *testing.T) {
5964
signalPrepaymentResult := ctx.AssertPaid(prepayInvoiceDesc)
6065

6166
// Expect client to register for conf.
62-
confIntent := ctx.AssertRegisterConf(false, defaultConfirmations)
67+
confIntent := ctx.AssertRegisterConf(false, req.HtlcConfirmations)
6368

6469
testSuccess(ctx, testRequest.Amount, info.SwapHash,
6570
signalPrepaymentResult, signalSwapPaymentResult, false,
@@ -143,18 +148,25 @@ func TestFailWrongAmount(t *testing.T) {
143148
func TestResume(t *testing.T) {
144149
defer test.Guard(t)()
145150

151+
defaultConfs := loopdb.DefaultLoopOutHtlcConfirmations
152+
146153
t.Run("not expired", func(t *testing.T) {
147-
testResume(t, false, false, true)
154+
testResume(t, defaultConfs, false, false, true)
155+
})
156+
t.Run("not expired, custom confirmations", func(t *testing.T) {
157+
testResume(t, 3, false, false, true)
148158
})
149159
t.Run("expired not revealed", func(t *testing.T) {
150-
testResume(t, true, false, false)
160+
testResume(t, defaultConfs, true, false, false)
151161
})
152162
t.Run("expired revealed", func(t *testing.T) {
153-
testResume(t, true, true, true)
163+
testResume(t, defaultConfs, true, true, true)
154164
})
155165
}
156166

157-
func testResume(t *testing.T, expired, preimageRevealed, expectSuccess bool) {
167+
func testResume(t *testing.T, confs uint32, expired, preimageRevealed,
168+
expectSuccess bool) {
169+
158170
defer test.Guard(t)()
159171

160172
preimage := testPreimage
@@ -193,12 +205,13 @@ func testResume(t *testing.T, expired, preimageRevealed, expectSuccess bool) {
193205
update.HtlcTxHash = &chainhash.Hash{1, 2, 6}
194206
}
195207

208+
// Create a pending swap with our custom number of confirmations.
196209
pendingSwap := &loopdb.LoopOut{
197210
Contract: &loopdb.LoopOutContract{
198211
DestAddr: dest,
199212
SwapInvoice: swapPayReq,
200213
SweepConfTarget: 2,
201-
HtlcConfirmations: loopdb.DefaultLoopOutHtlcConfirmations,
214+
HtlcConfirmations: confs,
202215
MaxSwapRoutingFee: 70000,
203216
PrepayInvoice: prePayReq,
204217
SwapContract: loopdb.SwapContract{
@@ -234,10 +247,8 @@ func testResume(t *testing.T, expired, preimageRevealed, expectSuccess bool) {
234247
signalSwapPaymentResult := ctx.AssertPaid(swapInvoiceDesc)
235248
signalPrepaymentResult := ctx.AssertPaid(prepayInvoiceDesc)
236249

237-
// Expect client to register for conf.
238-
confIntent := ctx.AssertRegisterConf(
239-
preimageRevealed, defaultConfirmations,
240-
)
250+
// Expect client to register for our expected number of confirmations.
251+
confIntent := ctx.AssertRegisterConf(preimageRevealed, int32(confs))
241252

242253
signalSwapPaymentResult(nil)
243254
signalPrepaymentResult(nil)

interface.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ type OutRequest struct {
6464
// client sweep tx.
6565
SweepConfTarget int32
6666

67+
// HtlcConfirmations specifies the number of confirmations we require
68+
// for on chain loop out htlcs.
69+
HtlcConfirmations int32
70+
6771
// OutgoingChanSet optionally specifies the short channel ids of the
6872
// channels that may be used to loop out.
6973
OutgoingChanSet loopdb.ChannelSet

loopd/swapclient_server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ func (s *swapClientServer) LoopOut(ctx context.Context,
8686
MaxSwapRoutingFee: btcutil.Amount(in.MaxSwapRoutingFee),
8787
MaxSwapFee: btcutil.Amount(in.MaxSwapFee),
8888
SweepConfTarget: sweepConfTarget,
89+
HtlcConfirmations: in.HtlcConfirmations,
8990
SwapPublicationDeadline: time.Unix(
9091
int64(in.SwapPublicationDeadline), 0,
9192
),

loopout.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,15 @@ func newLoopOutSwap(globalCtx context.Context, cfg *swapConfig,
138138
return nil, err
139139
}
140140

141+
// If a htlc confirmation target was not provided, we use the default
142+
// number of confirmations. We overwrite this value rather than failing
143+
// it because the field is a new addition to the rpc, and we don't want
144+
// to break older clients that are not aware of this new field.
145+
confs := uint32(request.HtlcConfirmations)
146+
if confs == 0 {
147+
confs = loopdb.DefaultLoopOutHtlcConfirmations
148+
}
149+
141150
// Instantiate a struct that contains all required data to start the
142151
// swap.
143152
initiationTime := time.Now()
@@ -147,7 +156,7 @@ func newLoopOutSwap(globalCtx context.Context, cfg *swapConfig,
147156
DestAddr: request.DestAddr,
148157
MaxSwapRoutingFee: request.MaxSwapRoutingFee,
149158
SweepConfTarget: request.SweepConfTarget,
150-
HtlcConfirmations: loopdb.DefaultLoopOutHtlcConfirmations,
159+
HtlcConfirmations: confs,
151160
PrepayInvoice: swapResp.prepayInvoice,
152161
MaxPrepayRoutingFee: request.MaxPrepayRoutingFee,
153162
SwapPublicationDeadline: request.SwapPublicationDeadline,

0 commit comments

Comments
 (0)