Browse Source

loopdb: persist htlc tx hash

pull/238/head
Joost Jager 9 months ago
parent
commit
4da4738a2d
No known key found for this signature in database GPG Key ID: A61B9D4C393C59C7
3 changed files with 45 additions and 3 deletions
  1. +30
    -1
      loopdb/store.go
  2. +8
    -1
      loopdb/store_test.go
  3. +7
    -1
      loopdb/swapstate.go

+ 30
- 1
loopdb/store.go View File

@ -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
})
}

+ 8
- 1
loopdb/store_test.go View File

@ -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 {

+ 7
- 1
loopdb/swapstate.go View File

@ -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
}

Loading…
Cancel
Save