Skip to content

Commit b2b0bad

Browse files
committed
loopin+loopout: move htlc out of swapKit
This commit moves htlc out of swapkit in preparation of adding separate p2wsh and np2wsh htlcs to loop-in swaps.
1 parent 7a44eec commit b2b0bad

File tree

3 files changed

+97
-46
lines changed

3 files changed

+97
-46
lines changed

loopin.go

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,12 @@ var (
4848
type loopInSwap struct {
4949
swapKit
5050

51+
executeConfig
52+
5153
loopdb.LoopInContract
5254

55+
htlc *swap.Htlc
56+
5357
timeoutAddr btcutil.Address
5458
}
5559

@@ -148,19 +152,25 @@ func newLoopInSwap(globalCtx context.Context, cfg *swapConfig,
148152
},
149153
}
150154

151-
swapKit, err := newSwapKit(
155+
swapKit := newSwapKit(
152156
swapHash, swap.TypeIn, cfg, &contract.SwapContract,
153-
swap.HtlcNP2WSH,
154157
)
158+
159+
swapKit.lastUpdateTime = initiationTime
160+
161+
// Create the htlc.
162+
htlc, err := swapKit.getHtlc(swap.HtlcNP2WSH)
155163
if err != nil {
156164
return nil, err
157165
}
158166

159-
swapKit.lastUpdateTime = initiationTime
167+
// Log htlc address for debugging.
168+
swapKit.log.Infof("Htlc address: %v", htlc.Address)
160169

161170
swap := &loopInSwap{
162171
LoopInContract: contract,
163172
swapKit: *swapKit,
173+
htlc: htlc,
164174
}
165175

166176
// Persist the data before exiting this function, so that the caller can
@@ -182,17 +192,23 @@ func resumeLoopInSwap(reqContext context.Context, cfg *swapConfig,
182192

183193
log.Infof("Resuming loop in swap %v", hash)
184194

185-
swapKit, err := newSwapKit(
195+
swapKit := newSwapKit(
186196
hash, swap.TypeIn, cfg, &pend.Contract.SwapContract,
187-
swap.HtlcNP2WSH,
188197
)
198+
199+
// Create the htlc.
200+
htlc, err := swapKit.getHtlc(swap.HtlcNP2WSH)
189201
if err != nil {
190202
return nil, err
191203
}
192204

205+
// Log htlc address for debugging.
206+
swapKit.log.Infof("Htlc address: %v", htlc.Address)
207+
193208
swap := &loopInSwap{
194209
LoopInContract: *pend.Contract,
195210
swapKit: *swapKit,
211+
htlc: htlc,
196212
}
197213

198214
lastUpdate := pend.LastUpdate()
@@ -222,6 +238,22 @@ func validateLoopInContract(lnd *lndclient.LndServices,
222238
return nil
223239
}
224240

241+
// sendUpdate reports an update to the swap state.
242+
func (s *loopInSwap) sendUpdate(ctx context.Context) error {
243+
info := s.swapInfo()
244+
s.log.Infof("Loop in swap state: %v", info.State)
245+
246+
info.HtlcAddress = s.htlc.Address
247+
248+
select {
249+
case s.statusChan <- *info:
250+
case <-ctx.Done():
251+
return ctx.Err()
252+
}
253+
254+
return nil
255+
}
256+
225257
// execute starts/resumes the swap. It is a thin wrapper around executeSwap to
226258
// conveniently handle the error case.
227259
func (s *loopInSwap) execute(mainCtx context.Context,

loopout.go

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ type loopOutSwap struct {
5252

5353
loopdb.LoopOutContract
5454

55+
executeConfig
56+
57+
htlc *swap.Htlc
58+
5559
swapPaymentChan chan lndclient.PaymentResult
5660
prePaymentChan chan lndclient.PaymentResult
5761
}
@@ -134,19 +138,25 @@ func newLoopOutSwap(globalCtx context.Context, cfg *swapConfig,
134138
},
135139
}
136140

137-
swapKit, err := newSwapKit(
141+
swapKit := newSwapKit(
138142
swapHash, swap.TypeOut, cfg, &contract.SwapContract,
139-
swap.HtlcP2WSH,
140143
)
144+
145+
swapKit.lastUpdateTime = initiationTime
146+
147+
// Create the htlc.
148+
htlc, err := swapKit.getHtlc(swap.HtlcP2WSH)
141149
if err != nil {
142150
return nil, err
143151
}
144152

145-
swapKit.lastUpdateTime = initiationTime
153+
// Log htlc address for debugging.
154+
swapKit.log.Infof("Htlc address: %v", htlc.Address)
146155

147156
swap := &loopOutSwap{
148157
LoopOutContract: contract,
149158
swapKit: *swapKit,
159+
htlc: htlc,
150160
}
151161

152162
// Persist the data before exiting this function, so that the caller
@@ -168,17 +178,24 @@ func resumeLoopOutSwap(reqContext context.Context, cfg *swapConfig,
168178

169179
log.Infof("Resuming loop out swap %v", hash)
170180

171-
swapKit, err := newSwapKit(
181+
swapKit := newSwapKit(
172182
hash, swap.TypeOut, cfg, &pend.Contract.SwapContract,
173-
swap.HtlcP2WSH,
174183
)
184+
185+
// Create the htlc.
186+
htlc, err := swapKit.getHtlc(swap.HtlcP2WSH)
175187
if err != nil {
176188
return nil, err
177189
}
178190

191+
// Log htlc address for debugging.
192+
swapKit.log.Infof("Htlc address: %v", htlc.Address)
193+
194+
// Create the swap.
179195
swap := &loopOutSwap{
180196
LoopOutContract: *pend.Contract,
181197
swapKit: *swapKit,
198+
htlc: htlc,
182199
}
183200

184201
lastUpdate := pend.LastUpdate()
@@ -192,6 +209,22 @@ func resumeLoopOutSwap(reqContext context.Context, cfg *swapConfig,
192209
return swap, nil
193210
}
194211

212+
// sendUpdate reports an update to the swap state.
213+
func (s *loopOutSwap) sendUpdate(ctx context.Context) error {
214+
info := s.swapInfo()
215+
s.log.Infof("Loop out swap state: %v", info.State)
216+
217+
info.HtlcAddress = s.htlc.Address
218+
219+
select {
220+
case s.statusChan <- *info:
221+
case <-ctx.Done():
222+
return ctx.Err()
223+
}
224+
225+
return nil
226+
}
227+
195228
// execute starts/resumes the swap. It is a thin wrapper around
196229
// executeAndFinalize to conveniently handle the error case.
197230
func (s *loopOutSwap) execute(mainCtx context.Context,

swap.go

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,59 +11,56 @@ import (
1111
)
1212

1313
type swapKit struct {
14-
htlc *swap.Htlc
1514
hash lntypes.Hash
1615

1716
height int32
1817

1918
log *swap.PrefixLog
2019

2120
lastUpdateTime time.Time
22-
cost loopdb.SwapCost
23-
state loopdb.SwapState
24-
executeConfig
25-
swapConfig
21+
22+
cost loopdb.SwapCost
23+
24+
state loopdb.SwapState
2625

2726
contract *loopdb.SwapContract
27+
2828
swapType swap.Type
29+
30+
swapConfig
2931
}
3032

3133
func newSwapKit(hash lntypes.Hash, swapType swap.Type, cfg *swapConfig,
32-
contract *loopdb.SwapContract, outputType swap.HtlcOutputType) (
33-
*swapKit, error) {
34-
35-
// Compose expected on-chain swap script
36-
htlc, err := swap.NewHtlc(
37-
contract.CltvExpiry, contract.SenderKey,
38-
contract.ReceiverKey, hash, outputType,
39-
cfg.lnd.ChainParams,
40-
)
41-
if err != nil {
42-
return nil, err
43-
}
34+
contract *loopdb.SwapContract) *swapKit {
4435

4536
log := &swap.PrefixLog{
4637
Hash: hash,
4738
Logger: log,
4839
}
4940

50-
// Log htlc address for debugging.
51-
log.Infof("Htlc address: %v", htlc.Address)
52-
5341
return &swapKit{
5442
swapConfig: *cfg,
5543
hash: hash,
5644
log: log,
57-
htlc: htlc,
5845
state: loopdb.StateInitiated,
5946
contract: contract,
6047
swapType: swapType,
61-
}, nil
48+
}
49+
}
50+
51+
// getHtlc composes and returns the on-chain swap script.
52+
func (s *swapKit) getHtlc(outputType swap.HtlcOutputType) (*swap.Htlc, error) {
53+
return swap.NewHtlc(
54+
s.contract.CltvExpiry, s.contract.SenderKey,
55+
s.contract.ReceiverKey, s.hash, outputType,
56+
s.swapConfig.lnd.ChainParams,
57+
)
6258
}
6359

64-
// sendUpdate reports an update to the swap state.
65-
func (s *swapKit) sendUpdate(ctx context.Context) error {
66-
info := &SwapInfo{
60+
// swapInfo constructs and returns a filled SwapInfo from
61+
// the swapKit.
62+
func (s *swapKit) swapInfo() *SwapInfo {
63+
return &SwapInfo{
6764
SwapContract: *s.contract,
6865
SwapHash: s.hash,
6966
SwapType: s.swapType,
@@ -72,18 +69,7 @@ func (s *swapKit) sendUpdate(ctx context.Context) error {
7269
State: s.state,
7370
Cost: s.cost,
7471
},
75-
HtlcAddress: s.htlc.Address,
7672
}
77-
78-
s.log.Infof("state %v", info.State)
79-
80-
select {
81-
case s.statusChan <- *info:
82-
case <-ctx.Done():
83-
return ctx.Err()
84-
}
85-
86-
return nil
8773
}
8874

8975
type genericSwap interface {

0 commit comments

Comments
 (0)