mirror of
https://github.com/lightninglabs/loop.git
synced 2026-08-13 12:33:03 +02:00
loopin: fail swap when htlc amount is incorrect
Previously the swap would get stuck in a state where it wouldn't ever progress because the server rejected the htlc.
This commit is contained in:
parent
e33d2fb762
commit
a3b7fa5977
3 changed files with 29 additions and 0 deletions
|
|
@ -60,6 +60,10 @@ const (
|
|||
// StateInvoiceSettled means that the swap invoice has been paid by the
|
||||
// server.
|
||||
StateInvoiceSettled SwapState = 9
|
||||
|
||||
// StateFailIncorrectHtlcAmt indicates that the amount of an externally
|
||||
// published loop in htlc didn't match the swap amount.
|
||||
StateFailIncorrectHtlcAmt SwapState = 10
|
||||
)
|
||||
|
||||
// SwapStateType defines the types of swap states that exist. Every swap state
|
||||
|
|
@ -127,6 +131,9 @@ func (s SwapState) String() string {
|
|||
case StateInvoiceSettled:
|
||||
return "InvoiceSettled"
|
||||
|
||||
case StateFailIncorrectHtlcAmt:
|
||||
return "IncorrectHtlcAmt"
|
||||
|
||||
default:
|
||||
return "Unknown"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -363,6 +363,13 @@ func (s *loopInSwap) executeSwap(globalCtx context.Context) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// Verify that the confirmed (external) htlc value matches the swap
|
||||
// amount. Otherwise fail the swap immediately.
|
||||
if htlcValue != s.LoopInContract.AmountRequested {
|
||||
s.setState(loopdb.StateFailIncorrectHtlcAmt)
|
||||
return s.persistAndAnnounceState(globalCtx)
|
||||
}
|
||||
|
||||
// TODO: Add miner fee of htlc tx to swap cost balance.
|
||||
|
||||
// The server is expected to see the htlc on-chain and knowing that it
|
||||
|
|
|
|||
|
|
@ -209,6 +209,20 @@ func testLoopInTimeout(t *testing.T,
|
|||
Tx: &htlcTx,
|
||||
}
|
||||
|
||||
// Assert that the swap is failed in case of an invalid amount.
|
||||
invalidAmt := externalValue != 0 && externalValue != int64(req.Amount)
|
||||
if invalidAmt {
|
||||
ctx.assertState(loopdb.StateFailIncorrectHtlcAmt)
|
||||
ctx.store.assertLoopInState(loopdb.StateFailIncorrectHtlcAmt)
|
||||
|
||||
err = <-errChan
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Client starts listening for spend of htlc.
|
||||
<-ctx.lnd.RegisterSpendChannel
|
||||
|
||||
|
|
@ -380,6 +394,7 @@ func testLoopInResume(t *testing.T, state loopdb.SwapState, expired bool) {
|
|||
|
||||
htlcTx.AddTxOut(&wire.TxOut{
|
||||
PkScript: htlc.PkScript,
|
||||
Value: int64(contract.AmountRequested),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue