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.
pull/240/head
Joost Jager 4 years ago
parent e33d2fb762
commit a3b7fa5977
No known key found for this signature in database
GPG Key ID: A61B9D4C393C59C7

@ -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…
Cancel
Save