From 4da4738a2d3a271e2b9ba6be066f1c38a82de5f7 Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Tue, 23 Jun 2020 11:42:12 +0200 Subject: [PATCH] loopdb: persist htlc tx hash --- loopdb/store.go | 31 ++++++++++++++++++++++++++++++- loopdb/store_test.go | 9 ++++++++- loopdb/swapstate.go | 8 +++++++- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/loopdb/store.go b/loopdb/store.go index 1099ffe..1d48904 100644 --- a/loopdb/store.go +++ b/loopdb/store.go @@ -11,6 +11,7 @@ import ( "time" "github.com/btcsuite/btcd/chaincfg" + "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/coreos/bbolt" "github.com/lightningnetwork/lnd/lntypes" ) @@ -48,6 +49,9 @@ var ( // basicStateKey contains the serialized basic swap state. basicStateKey = []byte{0} + // htlcTxHashKey contains the confirmed htlc tx id. + htlcTxHashKey = []byte{1} + // contractKey is the key that stores the serialized swap contract. It // is nested within the sub-bucket for each active swap. // @@ -284,6 +288,16 @@ func deserializeUpdates(swapBucket *bbolt.Bucket) ([]*LoopEvent, error) { return err } + // Deserialize htlc tx hash if this updates contains one. + htlcTxHashBytes := updateBucket.Get(htlcTxHashKey) + if htlcTxHashBytes != nil { + htlcTxHash, err := chainhash.NewHash(htlcTxHashBytes) + if err != nil { + return err + } + event.HtlcTxHash = htlcTxHash + } + updates = append(updates, event) return nil }) @@ -518,7 +532,22 @@ func (s *boltSwapStore) updateLoop(bucketKey []byte, hash lntypes.Hash, return err } - return nextUpdateBucket.Put(basicStateKey, updateValue) + err = nextUpdateBucket.Put(basicStateKey, updateValue) + if err != nil { + return err + } + + // Write the htlc tx hash if available. + if state.HtlcTxHash != nil { + err := nextUpdateBucket.Put( + htlcTxHashKey, state.HtlcTxHash[:], + ) + if err != nil { + return err + } + } + + return nil }) } diff --git a/loopdb/store_test.go b/loopdb/store_test.go index 799a1d4..d373876 100644 --- a/loopdb/store_test.go +++ b/loopdb/store_test.go @@ -10,10 +10,12 @@ import ( "time" "github.com/btcsuite/btcd/chaincfg" + "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/coreos/bbolt" "github.com/lightninglabs/loop/test" "github.com/lightningnetwork/lnd/lntypes" "github.com/lightningnetwork/lnd/routing/route" + "github.com/stretchr/testify/require" ) var ( @@ -130,6 +132,10 @@ func testLoopOutStore(t *testing.T, pendingSwap *LoopOutContract) { expectedState, swaps[0].State(), ) } + + if expectedState == StatePreimageRevealed { + require.NotNil(t, swaps[0].State().HtlcTxHash) + } } hash := pendingSwap.Preimage.Hash() @@ -152,7 +158,8 @@ func testLoopOutStore(t *testing.T, pendingSwap *LoopOutContract) { err = store.UpdateLoopOut( hash, testTime, SwapStateData{ - State: StatePreimageRevealed, + State: StatePreimageRevealed, + HtlcTxHash: &chainhash.Hash{1, 6, 2}, }, ) if err != nil { diff --git a/loopdb/swapstate.go b/loopdb/swapstate.go index c463966..1227f16 100644 --- a/loopdb/swapstate.go +++ b/loopdb/swapstate.go @@ -1,6 +1,9 @@ package loopdb -import "github.com/btcsuite/btcutil" +import ( + "github.com/btcsuite/btcd/chaincfg/chainhash" + "github.com/btcsuite/btcutil" +) // SwapState indicates the current state of a swap. This enumeration is the // union of loop in and loop out states. A single type is used for both swap @@ -147,4 +150,7 @@ type SwapStateData struct { // Cost are the accrued (final) costs so far. Cost SwapCost + + // HtlcTxHash is the tx id of the confirmed htlc. + HtlcTxHash *chainhash.Hash }