Skip to content

Commit f91422d

Browse files
committed
loopin: record htlx tx hash
1 parent a3b7fa5 commit f91422d

File tree

3 files changed

+76
-13
lines changed

3 files changed

+76
-13
lines changed

loopin.go

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,16 @@ import (
99

1010
"github.com/btcsuite/btcutil"
1111

12-
"github.com/lightningnetwork/lnd/chainntnfs"
13-
"github.com/lightningnetwork/lnd/channeldb"
14-
"github.com/lightningnetwork/lnd/lnwire"
15-
12+
"github.com/btcsuite/btcd/chaincfg/chainhash"
1613
"github.com/btcsuite/btcd/wire"
17-
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
18-
1914
"github.com/lightninglabs/lndclient"
2015
"github.com/lightninglabs/loop/loopdb"
2116
"github.com/lightninglabs/loop/swap"
17+
"github.com/lightningnetwork/lnd/chainntnfs"
18+
"github.com/lightningnetwork/lnd/channeldb"
19+
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
2220
"github.com/lightningnetwork/lnd/lntypes"
21+
"github.com/lightningnetwork/lnd/lnwire"
2322
)
2423

2524
var (
@@ -58,6 +57,9 @@ type loopInSwap struct {
5857

5958
htlcNP2WSH *swap.Htlc
6059

60+
// htlcTxHash is the confirmed htlc tx id.
61+
htlcTxHash *chainhash.Hash
62+
6163
timeoutAddr btcutil.Address
6264
}
6365

@@ -209,6 +211,7 @@ func resumeLoopInSwap(reqContext context.Context, cfg *swapConfig,
209211
} else {
210212
swap.state = lastUpdate.State
211213
swap.lastUpdateTime = lastUpdate.Time
214+
swap.htlcTxHash = lastUpdate.HtlcTxHash
212215
}
213216

214217
return swap, nil
@@ -394,20 +397,35 @@ func (s *loopInSwap) executeSwap(globalCtx context.Context) error {
394397
func (s *loopInSwap) waitForHtlcConf(globalCtx context.Context) (
395398
*chainntnfs.TxConfirmation, error) {
396399

400+
// Register for confirmation of the htlc. It is essential to specify not
401+
// just the pk script, because an attacker may publish the same htlc
402+
// with a lower value and we don't want to follow through with that tx.
403+
// In the unlikely event that our call to SendOutputs crashes and we
404+
// restart, htlcTxHash will be nil at this point. Then only register
405+
// with PkScript and accept the risk that the call triggers on a
406+
// different htlc outpoint.
407+
s.log.Infof("Register for htlc conf (hh=%v, txid=%v)",
408+
s.InitiationHeight, s.htlcTxHash)
409+
410+
if s.htlcTxHash == nil {
411+
s.log.Warnf("No htlc tx hash available, registering with " +
412+
"just the pkscript")
413+
}
414+
397415
ctx, cancel := context.WithCancel(globalCtx)
398416
defer cancel()
399417

400418
notifier := s.lnd.ChainNotifier
401419

402420
confChanP2WSH, confErrP2WSH, err := notifier.RegisterConfirmationsNtfn(
403-
ctx, nil, s.htlcP2WSH.PkScript, 1, s.InitiationHeight,
421+
ctx, s.htlcTxHash, s.htlcP2WSH.PkScript, 1, s.InitiationHeight,
404422
)
405423
if err != nil {
406424
return nil, err
407425
}
408426

409427
confChanNP2WSH, confErrNP2WSH, err := notifier.RegisterConfirmationsNtfn(
410-
ctx, nil, s.htlcNP2WSH.PkScript, 1, s.InitiationHeight,
428+
ctx, s.htlcTxHash, s.htlcNP2WSH.PkScript, 1, s.InitiationHeight,
411429
)
412430
if err != nil {
413431
return nil, err
@@ -445,6 +463,17 @@ func (s *loopInSwap) waitForHtlcConf(globalCtx context.Context) (
445463
}
446464
}
447465

466+
// Store htlc tx hash for accounting purposes. Usually this call is a
467+
// no-op because the htlc tx hash was already known. Exceptions are:
468+
//
469+
// - Old pending swaps that were initiated before we persisted the htlc
470+
// tx hash directly after publish.
471+
//
472+
// - Swaps that experienced a crash during their call to SendOutputs. In
473+
// that case, we weren't able to record the tx hash.
474+
txHash := conf.Tx.TxHash()
475+
s.htlcTxHash = &txHash
476+
448477
return conf, nil
449478
}
450479

@@ -491,7 +520,20 @@ func (s *loopInSwap) publishOnChainHtlc(ctx context.Context) (bool, error) {
491520
if err != nil {
492521
return false, fmt.Errorf("send outputs: %v", err)
493522
}
494-
s.log.Infof("Published on chain HTLC tx %v", tx.TxHash())
523+
txHash := tx.TxHash()
524+
s.log.Infof("Published on chain HTLC tx %v", txHash)
525+
526+
// Persist the htlc hash so that after a restart we are still waiting
527+
// for our own htlc. We don't need to announce to clients, because the
528+
// state remains unchanged.
529+
//
530+
// TODO(joostjager): Store tx hash before calling SendOutputs. This is
531+
// not yet possible with the current lnd api.
532+
s.htlcTxHash = &txHash
533+
s.lastUpdateTime = time.Now()
534+
if err := s.persistState(); err != nil {
535+
return false, fmt.Errorf("persist htlc tx: %v", err)
536+
}
495537

496538
return true, nil
497539

@@ -507,7 +549,7 @@ func (s *loopInSwap) waitForSwapComplete(ctx context.Context,
507549
rpcCtx, cancel := context.WithCancel(ctx)
508550
defer cancel()
509551
spendChan, spendErr, err := s.lnd.ChainNotifier.RegisterSpendNtfn(
510-
rpcCtx, nil, s.htlc.PkScript, s.InitiationHeight,
552+
rpcCtx, htlcOutpoint, s.htlc.PkScript, s.InitiationHeight,
511553
)
512554
if err != nil {
513555
return fmt.Errorf("register spend ntfn: %v", err)
@@ -714,8 +756,9 @@ func (s *loopInSwap) persistState() error {
714756
return s.store.UpdateLoopIn(
715757
s.hash, s.lastUpdateTime,
716758
loopdb.SwapStateData{
717-
State: s.state,
718-
Cost: s.cost,
759+
State: s.state,
760+
Cost: s.cost,
761+
HtlcTxHash: s.htlcTxHash,
719762
},
720763
)
721764
}

loopin_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/lightninglabs/loop/test"
1111
"github.com/lightningnetwork/lnd/chainntnfs"
1212
"github.com/lightningnetwork/lnd/channeldb"
13+
"github.com/stretchr/testify/require"
1314

1415
"github.com/btcsuite/btcd/wire"
1516
"github.com/btcsuite/btcutil"
@@ -61,6 +62,10 @@ func TestLoopInSuccess(t *testing.T) {
6162
// Expect htlc to be published.
6263
htlcTx := <-ctx.lnd.SendOutputsChannel
6364

65+
// Expect the same state to be written again with the htlc tx hash.
66+
state := ctx.store.assertLoopInState(loopdb.StateHtlcPublished)
67+
require.NotNil(t, state.HtlcTxHash)
68+
6469
// Expect register for htlc conf.
6570
<-ctx.lnd.RegisterConfChannel
6671
<-ctx.lnd.RegisterConfChannel
@@ -182,6 +187,10 @@ func testLoopInTimeout(t *testing.T,
182187
if externalValue == 0 {
183188
// Expect htlc to be published.
184189
htlcTx = <-ctx.lnd.SendOutputsChannel
190+
191+
// Expect the same state to be written again with the htlc tx hash.
192+
state := ctx.store.assertLoopInState(loopdb.StateHtlcPublished)
193+
require.NotNil(t, state.HtlcTxHash)
185194
} else {
186195
// Create an external htlc publish tx.
187196
var pkScript []byte
@@ -389,6 +398,11 @@ func testLoopInResume(t *testing.T, state loopdb.SwapState, expired bool) {
389398

390399
// Expect htlc to be published.
391400
htlcTx = <-ctx.lnd.SendOutputsChannel
401+
402+
// Expect the same state to be written again with the htlc tx
403+
// hash.
404+
state := ctx.store.assertLoopInState(loopdb.StateHtlcPublished)
405+
require.NotNil(t, state.HtlcTxHash)
392406
} else {
393407
ctx.assertState(loopdb.StateHtlcPublished)
394408

store_mock_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,19 @@ func (s *storeMock) assertLoopInStored() {
215215
<-s.loopInStoreChan
216216
}
217217

218-
func (s *storeMock) assertLoopInState(expectedState loopdb.SwapState) {
218+
// assertLoopInState asserts that a specified state transition is persisted to
219+
// disk.
220+
func (s *storeMock) assertLoopInState(
221+
expectedState loopdb.SwapState) loopdb.SwapStateData {
222+
219223
s.t.Helper()
220224

221225
state := <-s.loopInUpdateChan
222226
if state.State != expectedState {
223227
s.t.Fatalf("expected state %v, got %v", expectedState, state)
224228
}
229+
230+
return state
225231
}
226232

227233
func (s *storeMock) assertStorePreimageReveal() {

0 commit comments

Comments
 (0)