Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include the batch sweep transaction hash to SwapStatus #805

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ func (s *Client) FetchSwaps(ctx context.Context) ([]*SwapInfo, error) {
SwapStateData: swp.State(),
SwapHash: swp.Hash,
LastUpdate: swp.LastUpdateTime(),
SweepTxHash: swp.State().SweepTxHash,
}

htlc, err := utils.GetHtlc(
Expand Down
3 changes: 3 additions & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/lightninglabs/loop/loopdb"
"github.com/lightninglabs/loop/swap"
"github.com/lightningnetwork/lnd/lntypes"
Expand Down Expand Up @@ -382,6 +383,8 @@ type SwapInfo struct {
// channels that may be used to loop out. On a loop in this field
// is nil.
OutgoingChanSet loopdb.ChannelSet

SweepTxHash *chainhash.Hash
}

// LastUpdate returns the last update time of the swap.
Expand Down
12 changes: 9 additions & 3 deletions loopd/swapclient_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,15 @@ func (s *swapClientServer) marshallSwap(loopSwap *loop.SwapInfo) (
state = clientrpc.SwapState_FAILED
}

var swapType clientrpc.SwapType
var (
htlcAddress string
htlcAddressP2TR string
htlcAddressP2WSH string
outGoingChanSet []uint64
lastHop []byte
swapType clientrpc.SwapType
sweepTxHash string
)
var outGoingChanSet []uint64
var lastHop []byte

switch loopSwap.SwapType {
case swap.TypeIn:
Expand Down Expand Up @@ -369,6 +370,10 @@ func (s *swapClientServer) marshallSwap(loopSwap *loop.SwapInfo) (

outGoingChanSet = loopSwap.OutgoingChanSet

if loopSwap.SweepTxHash != nil {
sweepTxHash = loopSwap.SweepTxHash.String()
}

default:
return nil, errors.New("unknown swap type")
}
Expand All @@ -391,6 +396,7 @@ func (s *swapClientServer) marshallSwap(loopSwap *loop.SwapInfo) (
Label: loopSwap.Label,
LastHop: lastHop,
OutgoingChanSet: outGoingChanSet,
SweepTxHash: sweepTxHash,
}, nil
}

Expand Down
15 changes: 14 additions & 1 deletion loopdb/sql_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,10 @@ func (db *BaseDB) updateLoop(ctx context.Context, hash lntypes.Hash,
if state.HtlcTxHash != nil {
updateParams.HtlcTxhash = state.HtlcTxHash.String()
}

if state.SweepTxHash != nil {
updateParams.SweepTxHash = state.SweepTxHash.String()
}
// First we insert the swap update.
err := tx.InsertSwapUpdate(ctx, updateParams)
if err != nil {
Expand Down Expand Up @@ -610,7 +614,7 @@ func ConvertLoopOutRow(network *chaincfg.Params, row sqlc.GetLoopOutSwapRow,
loopOut.Contract.OutgoingChanSet = chanSet
}

// If we don't have any updates yet we can return early
// If we don't have any updates yet we can return early.
if len(updates) == 0 {
return loopOut, nil
}
Expand Down Expand Up @@ -720,6 +724,15 @@ func getSwapEvents(updates []sqlc.SwapUpdate) ([]*LoopEvent, error) {

events[i].HtlcTxHash = chainHash
}

if updates[i].SweepTxHash != "" {
chainHash, err := chainhash.NewHashFromStr(updates[i].SweepTxHash)
if err != nil {
return nil, err
}

events[i].SweepTxHash = chainHash
}
}

return events, nil
Expand Down
2 changes: 2 additions & 0 deletions loopdb/sqlc/migrations/000009_sweep_tx_hash.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- sweep_tx_hash is the hash of the transaction that sweeps the htlc.
ALTER TABLE swap_updates DROP COLUMN sweep_tx_hash;
2 changes: 2 additions & 0 deletions loopdb/sqlc/migrations/000009_sweep_tx_hash.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- sweep_tx_hash is the hash of the transaction that sweeps the htlc.
ALTER TABLE swap_updates ADD sweep_tx_hash TEXT NOT NULL;
1 change: 1 addition & 0 deletions loopdb/sqlc/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions loopdb/sqlc/queries/swaps.sql
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,10 @@ INSERT INTO swap_updates (
htlc_txhash,
server_cost,
onchain_cost,
offchain_cost
offchain_cost,
sweep_tx_hash
) VALUES (
$1, $2, $3, $4, $5, $6, $7
$1, $2, $3, $4, $5, $6, $7, $8
);

-- name: InsertLoopOut :exec
Expand Down
10 changes: 7 additions & 3 deletions loopdb/sqlc/swaps.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions loopdb/swapstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,7 @@ type SwapStateData struct {

// HtlcTxHash is the tx id of the confirmed htlc.
HtlcTxHash *chainhash.Hash

// SweepTxHash is the tx id of the confirmed htlc.
SweepTxHash *chainhash.Hash
}
35 changes: 29 additions & 6 deletions loopout.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ type loopOutSwap struct {
// htlcTxHash is the confirmed htlc tx id.
htlcTxHash *chainhash.Hash

sweepTxHash *chainhash.Hash

swapInvoicePaymentAddr [32]byte

// prepayAmount holds the amount of the prepay invoice. We use this
Expand Down Expand Up @@ -301,6 +303,7 @@ func resumeLoopOutSwap(cfg *swapConfig, pend *loopdb.LoopOut,
swap.state = lastUpdate.State
swap.lastUpdateTime = lastUpdate.Time
swap.htlcTxHash = lastUpdate.HtlcTxHash
swap.sweepTxHash = lastUpdate.SweepTxHash
}

return swap, nil
Expand All @@ -326,6 +329,10 @@ func (s *loopOutSwap) sendUpdate(ctx context.Context) error {
info.OutgoingChanSet = outgoingChanSet
}

if s.sweepTxHash != nil {
info.SweepTxHash = s.sweepTxHash
}

select {
case s.statusChan <- *info:
case <-ctx.Done():
Expand Down Expand Up @@ -597,9 +604,10 @@ func (s *loopOutSwap) persistState(ctx context.Context) error {
err := s.store.UpdateLoopOut(
ctx, s.hash, updateTime,
loopdb.SwapStateData{
State: s.state,
Cost: s.cost,
HtlcTxHash: s.htlcTxHash,
State: s.state,
Cost: s.cost,
HtlcTxHash: s.htlcTxHash,
SweepTxHash: s.sweepTxHash,
},
)
if err != nil {
Expand Down Expand Up @@ -1071,15 +1079,17 @@ func (s *loopOutSwap) waitForHtlcSpendConfirmedV2(globalCtx context.Context,
spendChan := make(chan *sweepbatcher.SpendDetail)
spendErrChan := make(chan error, 1)
quitChan := make(chan bool, 1)
sweepTxHashChan := make(chan *chainhash.Hash, 1)

defer func() {
quitChan <- true
}()

notifier := sweepbatcher.SpendNotifier{
SpendChan: spendChan,
SpendErrChan: spendErrChan,
QuitChan: quitChan,
SpendChan: spendChan,
SpendErrChan: spendErrChan,
QuitChan: quitChan,
SweepTxHashChan: sweepTxHashChan,
}

sweepReq := sweepbatcher.SweepRequest{
Expand Down Expand Up @@ -1124,6 +1134,19 @@ func (s *loopOutSwap) waitForHtlcSpendConfirmedV2(globalCtx context.Context,
case err := <-spendErrChan:
return nil, err

case sweepTxHash := <-sweepTxHashChan:
s.sweepTxHash = sweepTxHash
err = s.persistState(ctx)
if err != nil {
log.Warnf("unable to persist sweep tx hash "+
"%v", sweepTxHash)
}

err = s.sendUpdate(ctx)
if err != nil {
log.Warnf("unable to send update")
}

// Receive status updates for our payment so that we can detect
// whether we've successfully pushed our preimage.
case status, ok := <-trackChan:
Expand Down
Loading
Loading