channeldb+invoices: make htlc cancelation stricter

Previously it was possible to cancel a canceled htlc. This would
subtract the htlc amount from the invoice amount again.
This commit is contained in:
Joost Jager committed 2019-09-16 10:10:13 +02:00
1 parent 35d4652d23
commit 49a20a87a2
2 files changed
+18 -4

No files matched your search

+3 -3
View File
@@ -1224,9 +1224,9 @@ func (d *DB) updateInvoice(hash lntypes.Hash, invoices, settleIndex *bbolt.Bucke
if !ok { if !ok {
return nil, fmt.Errorf("unknown htlc %v", key) return nil, fmt.Errorf("unknown htlc %v", key)
} }
if htlc.State == HtlcStateSettled { if htlc.State != HtlcStateAccepted {
return nil, fmt.Errorf("cannot cancel a " + return nil, fmt.Errorf("can only cancel " +
"settled htlc") "accepted htlcs")
} }
htlc.State = HtlcStateCancelled htlc.State = HtlcStateCancelled
+15 -1
View File
@@ -640,7 +640,21 @@ func (i *InvoiceRegistry) CancelInvoice(payHash lntypes.Hash) error {
canceledHtlcs := make( canceledHtlcs := make(
map[channeldb.CircuitKey]*channeldb.HtlcAcceptDesc, map[channeldb.CircuitKey]*channeldb.HtlcAcceptDesc,
) )
for key := range invoice.Htlcs { for key, htlc := range invoice.Htlcs {
switch htlc.State {
// If we get here, there shouldn't be any settled htlcs.
case channeldb.HtlcStateSettled:
return nil, errors.New("cannot cancel " +
"invoice with settled htlc(s)")
// Don't cancel htlcs that were already cancelled,
// because it would incorrectly modify the invoice paid
// amt.
case channeldb.HtlcStateCancelled:
continue
}
canceledHtlcs[key] = nil canceledHtlcs[key] = nil
} }