Browse Source

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 7 months ago
parent
commit
a3b7fa5977
No known key found for this signature in database GPG Key ID: A61B9D4C393C59C7
3 changed files with 29 additions and 0 deletions
  1. +7
    -0
      loopdb/swapstate.go
  2. +7
    -0
      loopin.go
  3. +15
    -0
      loopin_test.go

+ 7
- 0
loopdb/swapstate.go View File

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

+ 7
- 0
loopin.go View File

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

+ 15
- 0
loopin_test.go View File

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