Skip to content

Commit 4da4738

Browse files
committed
loopdb: persist htlc tx hash
1 parent 12a7b34 commit 4da4738

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

loopdb/store.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
"github.com/btcsuite/btcd/chaincfg"
14+
"github.com/btcsuite/btcd/chaincfg/chainhash"
1415
"github.com/coreos/bbolt"
1516
"github.com/lightningnetwork/lnd/lntypes"
1617
)
@@ -48,6 +49,9 @@ var (
4849
// basicStateKey contains the serialized basic swap state.
4950
basicStateKey = []byte{0}
5051

52+
// htlcTxHashKey contains the confirmed htlc tx id.
53+
htlcTxHashKey = []byte{1}
54+
5155
// contractKey is the key that stores the serialized swap contract. It
5256
// is nested within the sub-bucket for each active swap.
5357
//
@@ -284,6 +288,16 @@ func deserializeUpdates(swapBucket *bbolt.Bucket) ([]*LoopEvent, error) {
284288
return err
285289
}
286290

291+
// Deserialize htlc tx hash if this updates contains one.
292+
htlcTxHashBytes := updateBucket.Get(htlcTxHashKey)
293+
if htlcTxHashBytes != nil {
294+
htlcTxHash, err := chainhash.NewHash(htlcTxHashBytes)
295+
if err != nil {
296+
return err
297+
}
298+
event.HtlcTxHash = htlcTxHash
299+
}
300+
287301
updates = append(updates, event)
288302
return nil
289303
})
@@ -518,7 +532,22 @@ func (s *boltSwapStore) updateLoop(bucketKey []byte, hash lntypes.Hash,
518532
return err
519533
}
520534

521-
return nextUpdateBucket.Put(basicStateKey, updateValue)
535+
err = nextUpdateBucket.Put(basicStateKey, updateValue)
536+
if err != nil {
537+
return err
538+
}
539+
540+
// Write the htlc tx hash if available.
541+
if state.HtlcTxHash != nil {
542+
err := nextUpdateBucket.Put(
543+
htlcTxHashKey, state.HtlcTxHash[:],
544+
)
545+
if err != nil {
546+
return err
547+
}
548+
}
549+
550+
return nil
522551
})
523552
}
524553

loopdb/store_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ import (
1010
"time"
1111

1212
"github.com/btcsuite/btcd/chaincfg"
13+
"github.com/btcsuite/btcd/chaincfg/chainhash"
1314
"github.com/coreos/bbolt"
1415
"github.com/lightninglabs/loop/test"
1516
"github.com/lightningnetwork/lnd/lntypes"
1617
"github.com/lightningnetwork/lnd/routing/route"
18+
"github.com/stretchr/testify/require"
1719
)
1820

1921
var (
@@ -130,6 +132,10 @@ func testLoopOutStore(t *testing.T, pendingSwap *LoopOutContract) {
130132
expectedState, swaps[0].State(),
131133
)
132134
}
135+
136+
if expectedState == StatePreimageRevealed {
137+
require.NotNil(t, swaps[0].State().HtlcTxHash)
138+
}
133139
}
134140

135141
hash := pendingSwap.Preimage.Hash()
@@ -152,7 +158,8 @@ func testLoopOutStore(t *testing.T, pendingSwap *LoopOutContract) {
152158
err = store.UpdateLoopOut(
153159
hash, testTime,
154160
SwapStateData{
155-
State: StatePreimageRevealed,
161+
State: StatePreimageRevealed,
162+
HtlcTxHash: &chainhash.Hash{1, 6, 2},
156163
},
157164
)
158165
if err != nil {

loopdb/swapstate.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package loopdb
22

3-
import "github.com/btcsuite/btcutil"
3+
import (
4+
"github.com/btcsuite/btcd/chaincfg/chainhash"
5+
"github.com/btcsuite/btcutil"
6+
)
47

58
// SwapState indicates the current state of a swap. This enumeration is the
69
// union of loop in and loop out states. A single type is used for both swap
@@ -147,4 +150,7 @@ type SwapStateData struct {
147150

148151
// Cost are the accrued (final) costs so far.
149152
Cost SwapCost
153+
154+
// HtlcTxHash is the tx id of the confirmed htlc.
155+
HtlcTxHash *chainhash.Hash
150156
}

0 commit comments

Comments
 (0)