Skip to content

Commit 133f3ca

Browse files
committed
multi: integrate the new htlc v2 scripts to loop in/out
This commit bumps the current protocol version and integrates htlc v2 with loop in/out for new swaps, while keeping htlc v1 for any pending swaps with previous protocol versions.
1 parent 86db43a commit 133f3ca

File tree

9 files changed

+143
-97
lines changed

9 files changed

+143
-97
lines changed

client.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,10 @@ func (s *Client) FetchSwaps() ([]*SwapInfo, error) {
175175

176176
for _, swp := range loopOutSwaps {
177177
htlc, err := swap.NewHtlc(
178-
swap.HtlcV1, swp.Contract.CltvExpiry,
179-
swp.Contract.SenderKey, swp.Contract.ReceiverKey,
180-
swp.Hash, swap.HtlcP2WSH, s.lndServices.ChainParams,
178+
GetHtlcScriptVersion(swp.Contract.ProtocolVersion),
179+
swp.Contract.CltvExpiry, swp.Contract.SenderKey,
180+
swp.Contract.ReceiverKey, swp.Hash, swap.HtlcP2WSH,
181+
s.lndServices.ChainParams,
181182
)
182183
if err != nil {
183184
return nil, err
@@ -195,18 +196,20 @@ func (s *Client) FetchSwaps() ([]*SwapInfo, error) {
195196

196197
for _, swp := range loopInSwaps {
197198
htlcNP2WSH, err := swap.NewHtlc(
198-
swap.HtlcV1, swp.Contract.CltvExpiry,
199-
swp.Contract.SenderKey, swp.Contract.ReceiverKey,
200-
swp.Hash, swap.HtlcNP2WSH, s.lndServices.ChainParams,
199+
GetHtlcScriptVersion(swp.Contract.ProtocolVersion),
200+
swp.Contract.CltvExpiry, swp.Contract.SenderKey,
201+
swp.Contract.ReceiverKey, swp.Hash, swap.HtlcNP2WSH,
202+
s.lndServices.ChainParams,
201203
)
202204
if err != nil {
203205
return nil, err
204206
}
205207

206208
htlcP2WSH, err := swap.NewHtlc(
207-
swap.HtlcV1, swp.Contract.CltvExpiry,
208-
swp.Contract.SenderKey, swp.Contract.ReceiverKey,
209-
swp.Hash, swap.HtlcP2WSH, s.lndServices.ChainParams,
209+
GetHtlcScriptVersion(swp.Contract.ProtocolVersion),
210+
swp.Contract.CltvExpiry, swp.Contract.SenderKey,
211+
swp.Contract.ReceiverKey, swp.Hash, swap.HtlcP2WSH,
212+
s.lndServices.ChainParams,
210213
)
211214
if err != nil {
212215
return nil, err

loopd/view.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func viewOut(swapClient *loop.Client, chainParams *chaincfg.Params) error {
5050

5151
for _, s := range swaps {
5252
htlc, err := swap.NewHtlc(
53-
swap.HtlcV1,
53+
loop.GetHtlcScriptVersion(s.Contract.ProtocolVersion),
5454
s.Contract.CltvExpiry,
5555
s.Contract.SenderKey,
5656
s.Contract.ReceiverKey,
@@ -102,7 +102,7 @@ func viewIn(swapClient *loop.Client, chainParams *chaincfg.Params) error {
102102

103103
for _, s := range swaps {
104104
htlc, err := swap.NewHtlc(
105-
swap.HtlcV1,
105+
loop.GetHtlcScriptVersion(s.Contract.ProtocolVersion),
106106
s.Contract.CltvExpiry,
107107
s.Contract.SenderKey,
108108
s.Contract.ReceiverKey,

loopdb/protocol_version.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,17 @@ const (
3131
// propose a cltv expiry height for loop out.
3232
ProtocolVersionUserExpiryLoopOut ProtocolVersion = 4
3333

34+
// ProtocolVersionHtlcV2 indicates that the client will use the new
35+
// HTLC v2 scrips for swaps.
36+
ProtocolVersionHtlcV2 ProtocolVersion = 5
37+
3438
// ProtocolVersionUnrecorded is set for swaps were created before we
3539
// started saving protocol version with swaps.
3640
ProtocolVersionUnrecorded ProtocolVersion = math.MaxUint32
3741

3842
// CurrentRpcProtocolVersion defines the version of the RPC protocol
3943
// that is currently supported by the loop client.
40-
CurrentRPCProtocolVersion = looprpc.ProtocolVersion_USER_EXPIRY_LOOP_OUT
44+
CurrentRPCProtocolVersion = looprpc.ProtocolVersion_HTLC_V2
4145

4246
// CurrentInteranlProtocolVersionInternal defines the RPC current
4347
// protocol in the internal representation.
@@ -70,6 +74,9 @@ func (p ProtocolVersion) String() string {
7074
case ProtocolVersionUserExpiryLoopOut:
7175
return "User Expiry Loop Out"
7276

77+
case ProtocolVersionHtlcV2:
78+
return "HTLC V2"
79+
7380
default:
7481
return "Unknown"
7582
}

loopdb/protocol_version_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func TestProtocolVersionSanity(t *testing.T) {
1919
ProtocolVersionSegwitLoopIn,
2020
ProtocolVersionPreimagePush,
2121
ProtocolVersionUserExpiryLoopOut,
22+
ProtocolVersionHtlcV2,
2223
}
2324

2425
rpcVersions := [...]looprpc.ProtocolVersion{
@@ -27,6 +28,7 @@ func TestProtocolVersionSanity(t *testing.T) {
2728
looprpc.ProtocolVersion_NATIVE_SEGWIT_LOOP_IN,
2829
looprpc.ProtocolVersion_PREIMAGE_PUSH_LOOP_OUT,
2930
looprpc.ProtocolVersion_USER_EXPIRY_LOOP_OUT,
31+
looprpc.ProtocolVersion_HTLC_V2,
3032
}
3133

3234
require.Equal(t, len(versions), len(rpcVersions))

loopin.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,13 @@ func newLoopInSwap(globalCtx context.Context, cfg *swapConfig,
172172
MaxMinerFee: request.MaxMinerFee,
173173
MaxSwapFee: request.MaxSwapFee,
174174
Label: request.Label,
175+
ProtocolVersion: loopdb.CurrentInternalProtocolVersion,
175176
},
176177
}
177178

178179
swapKit := newSwapKit(
179-
swapHash, swap.TypeIn, cfg, &contract.SwapContract,
180+
swapHash, swap.TypeIn,
181+
cfg, &contract.SwapContract,
180182
)
181183

182184
swapKit.lastUpdateTime = initiationTime
@@ -217,7 +219,8 @@ func resumeLoopInSwap(reqContext context.Context, cfg *swapConfig,
217219
log.Infof("Resuming loop in swap %v", hash)
218220

219221
swapKit := newSwapKit(
220-
hash, swap.TypeIn, cfg, &pend.Contract.SwapContract,
222+
hash, swap.TypeIn, cfg,
223+
&pend.Contract.SwapContract,
221224
)
222225

223226
swap := &loopInSwap{

loopout.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,14 @@ func newLoopOutSwap(globalCtx context.Context, cfg *swapConfig,
171171
MaxMinerFee: request.MaxMinerFee,
172172
MaxSwapFee: request.MaxSwapFee,
173173
Label: request.Label,
174+
ProtocolVersion: loopdb.CurrentInternalProtocolVersion,
174175
},
175176
OutgoingChanSet: chanSet,
176177
}
177178

178179
swapKit := newSwapKit(
179-
swapHash, swap.TypeOut, cfg, &contract.SwapContract,
180+
swapHash, swap.TypeOut,
181+
cfg, &contract.SwapContract,
180182
)
181183

182184
swapKit.lastUpdateTime = initiationTime
@@ -223,7 +225,8 @@ func resumeLoopOutSwap(reqContext context.Context, cfg *swapConfig,
223225
log.Infof("Resuming loop out swap %v", hash)
224226

225227
swapKit := newSwapKit(
226-
hash, swap.TypeOut, cfg, &pend.Contract.SwapContract,
228+
hash, swap.TypeOut, cfg,
229+
&pend.Contract.SwapContract,
227230
)
228231

229232
// Create the htlc.

0 commit comments

Comments
 (0)