mirror of
https://github.com/lightninglabs/loop.git
synced 2026-08-13 12:33:03 +02:00
loopdb: persist htlc tx hash
This commit is contained in:
parent
12a7b34d88
commit
4da4738a2d
3 changed files with 45 additions and 3 deletions
|
|
@ -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
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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…
Add table
Add a link
Reference in a new issue