2019-12-03 14:22:31 +03:00
|
|
|
package invoices
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2019-09-03 13:23:39 +03:00
|
|
|
"github.com/lightningnetwork/lnd/record"
|
2019-12-03 14:22:31 +03:00
|
|
|
)
|
|
|
|
|
2019-12-20 13:25:07 +03:00
|
|
|
// ResolutionResult provides metadata which about an invoice update which can
|
|
|
|
// be used to take custom actions on resolution of the htlc. Only results which
|
|
|
|
// are actionable by the link are exported.
|
|
|
|
type ResolutionResult uint8
|
2019-12-03 14:22:31 +03:00
|
|
|
|
|
|
|
const (
|
2019-12-20 13:25:07 +03:00
|
|
|
resultInvalid ResolutionResult = iota
|
|
|
|
|
|
|
|
// ResultReplayToCanceled is returned when we replay a canceled invoice.
|
|
|
|
ResultReplayToCanceled
|
|
|
|
|
|
|
|
// ResultReplayToAccepted is returned when we replay an accepted invoice.
|
|
|
|
ResultReplayToAccepted
|
|
|
|
|
|
|
|
// ResultReplayToSettled is returned when we replay a settled invoice.
|
|
|
|
ResultReplayToSettled
|
|
|
|
|
|
|
|
// ResultInvoiceAlreadyCanceled is returned when trying to pay an invoice
|
|
|
|
// that is already canceled.
|
|
|
|
ResultInvoiceAlreadyCanceled
|
|
|
|
|
|
|
|
// ResultAmountTooLow is returned when an invoice is underpaid.
|
|
|
|
ResultAmountTooLow
|
|
|
|
|
|
|
|
// ResultExpiryTooSoon is returned when we do not accept an invoice payment
|
|
|
|
// because it expires too soon.
|
|
|
|
ResultExpiryTooSoon
|
|
|
|
|
|
|
|
// ResultDuplicateToAccepted is returned when we accept a duplicate htlc.
|
|
|
|
ResultDuplicateToAccepted
|
|
|
|
|
|
|
|
// ResultDuplicateToSettled is returned when we settle an invoice which has
|
|
|
|
// already been settled at least once.
|
|
|
|
ResultDuplicateToSettled
|
|
|
|
|
|
|
|
// ResultAccepted is returned when we accept a hodl invoice.
|
|
|
|
ResultAccepted
|
|
|
|
|
|
|
|
// ResultSettled is returned when we settle an invoice.
|
|
|
|
ResultSettled
|
|
|
|
|
2019-12-20 13:25:07 +03:00
|
|
|
// ResultCanceled is returned when we cancel an invoice and its associated
|
|
|
|
// htlcs.
|
|
|
|
ResultCanceled
|
|
|
|
|
2019-12-20 13:25:07 +03:00
|
|
|
// ResultInvoiceNotOpen is returned when a mpp invoice is not open.
|
|
|
|
ResultInvoiceNotOpen
|
|
|
|
|
|
|
|
// ResultPartialAccepted is returned when we have partially received
|
|
|
|
// payment.
|
|
|
|
ResultPartialAccepted
|
|
|
|
|
|
|
|
// ResultMppInProgress is returned when we are busy receiving a mpp payment.
|
|
|
|
ResultMppInProgress
|
|
|
|
|
2019-12-20 13:25:07 +03:00
|
|
|
// ResultMppTimeout is returned when an invoice paid with multiple partial
|
|
|
|
// payments times out before it is fully paid.
|
|
|
|
ResultMppTimeout
|
|
|
|
|
2019-12-20 13:25:07 +03:00
|
|
|
// ResultAddressMismatch is returned when the payment address for a mpp
|
|
|
|
// invoice does not match.
|
|
|
|
ResultAddressMismatch
|
|
|
|
|
|
|
|
// ResultHtlcSetTotalMismatch is returned when the amount paid by a htlc
|
|
|
|
// does not match its set total.
|
|
|
|
ResultHtlcSetTotalMismatch
|
|
|
|
|
|
|
|
// ResultHtlcSetTotalTooLow is returned when a mpp set total is too low for
|
|
|
|
// an invoice.
|
|
|
|
ResultHtlcSetTotalTooLow
|
|
|
|
|
|
|
|
// ResultHtlcSetOverpayment is returned when a mpp set is overpaid.
|
|
|
|
ResultHtlcSetOverpayment
|
2019-12-20 13:25:08 +03:00
|
|
|
|
|
|
|
// ResultInvoiceNotFound is returned when an attempt is made to pay an
|
|
|
|
// invoice that is unknown to us.
|
|
|
|
ResultInvoiceNotFound
|
2019-12-03 14:22:31 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// String returns a human-readable representation of the invoice update result.
|
2019-12-20 13:25:07 +03:00
|
|
|
func (u ResolutionResult) String() string {
|
2019-12-03 14:22:31 +03:00
|
|
|
switch u {
|
|
|
|
|
|
|
|
case resultInvalid:
|
|
|
|
return "invalid"
|
|
|
|
|
2019-12-20 13:25:07 +03:00
|
|
|
case ResultReplayToCanceled:
|
2019-12-03 14:22:31 +03:00
|
|
|
return "replayed htlc to canceled invoice"
|
|
|
|
|
2019-12-20 13:25:07 +03:00
|
|
|
case ResultReplayToAccepted:
|
2019-12-03 14:22:31 +03:00
|
|
|
return "replayed htlc to accepted invoice"
|
|
|
|
|
2019-12-20 13:25:07 +03:00
|
|
|
case ResultReplayToSettled:
|
2019-12-03 14:22:31 +03:00
|
|
|
return "replayed htlc to settled invoice"
|
|
|
|
|
2019-12-20 13:25:07 +03:00
|
|
|
case ResultInvoiceAlreadyCanceled:
|
2019-12-03 14:22:31 +03:00
|
|
|
return "invoice already canceled"
|
|
|
|
|
2019-12-20 13:25:07 +03:00
|
|
|
case ResultAmountTooLow:
|
2019-12-03 14:22:31 +03:00
|
|
|
return "amount too low"
|
|
|
|
|
2019-12-20 13:25:07 +03:00
|
|
|
case ResultExpiryTooSoon:
|
2019-12-03 14:22:31 +03:00
|
|
|
return "expiry too soon"
|
|
|
|
|
2019-12-20 13:25:07 +03:00
|
|
|
case ResultDuplicateToAccepted:
|
2019-12-03 14:22:31 +03:00
|
|
|
return "accepting duplicate payment to accepted invoice"
|
|
|
|
|
2019-12-20 13:25:07 +03:00
|
|
|
case ResultDuplicateToSettled:
|
2019-12-03 14:22:31 +03:00
|
|
|
return "accepting duplicate payment to settled invoice"
|
|
|
|
|
2019-12-20 13:25:07 +03:00
|
|
|
case ResultAccepted:
|
2019-12-03 14:22:31 +03:00
|
|
|
return "accepted"
|
|
|
|
|
2019-12-20 13:25:07 +03:00
|
|
|
case ResultSettled:
|
2019-12-03 14:22:31 +03:00
|
|
|
return "settled"
|
|
|
|
|
2019-12-20 13:25:07 +03:00
|
|
|
case ResultInvoiceNotOpen:
|
2019-09-03 13:23:39 +03:00
|
|
|
return "invoice no longer open"
|
|
|
|
|
2019-12-20 13:25:07 +03:00
|
|
|
case ResultPartialAccepted:
|
2019-09-03 13:23:39 +03:00
|
|
|
return "partial payment accepted"
|
|
|
|
|
2019-12-20 13:25:07 +03:00
|
|
|
case ResultMppInProgress:
|
2019-09-03 13:23:39 +03:00
|
|
|
return "mpp reception in progress"
|
|
|
|
|
2019-12-20 13:25:07 +03:00
|
|
|
case ResultAddressMismatch:
|
2019-09-03 13:23:39 +03:00
|
|
|
return "payment address mismatch"
|
|
|
|
|
2019-12-20 13:25:07 +03:00
|
|
|
case ResultHtlcSetTotalMismatch:
|
2019-09-03 13:23:39 +03:00
|
|
|
return "htlc total amt doesn't match set total"
|
|
|
|
|
2019-12-20 13:25:07 +03:00
|
|
|
case ResultHtlcSetTotalTooLow:
|
2019-09-03 13:23:39 +03:00
|
|
|
return "set total too low for invoice"
|
|
|
|
|
2019-12-20 13:25:07 +03:00
|
|
|
case ResultHtlcSetOverpayment:
|
2019-09-03 13:23:39 +03:00
|
|
|
return "mpp is overpaying set total"
|
|
|
|
|
2019-12-03 14:22:31 +03:00
|
|
|
default:
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// invoiceUpdateCtx is an object that describes the context for the invoice
|
|
|
|
// update to be carried out.
|
|
|
|
type invoiceUpdateCtx struct {
|
|
|
|
circuitKey channeldb.CircuitKey
|
|
|
|
amtPaid lnwire.MilliSatoshi
|
|
|
|
expiry uint32
|
|
|
|
currentHeight int32
|
|
|
|
finalCltvRejectDelta int32
|
2019-12-12 02:01:55 +03:00
|
|
|
customRecords record.CustomSet
|
2019-09-03 13:23:39 +03:00
|
|
|
mpp *record.MPP
|
2019-12-03 14:22:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// updateInvoice is a callback for DB.UpdateInvoice that contains the invoice
|
|
|
|
// settlement logic.
|
|
|
|
func updateInvoice(ctx *invoiceUpdateCtx, inv *channeldb.Invoice) (
|
2019-12-20 13:25:07 +03:00
|
|
|
*channeldb.InvoiceUpdateDesc, ResolutionResult, error) {
|
2019-12-03 14:22:31 +03:00
|
|
|
|
|
|
|
// Don't update the invoice when this is a replayed htlc.
|
|
|
|
htlc, ok := inv.Htlcs[ctx.circuitKey]
|
|
|
|
if ok {
|
|
|
|
switch htlc.State {
|
|
|
|
case channeldb.HtlcStateCanceled:
|
2019-12-20 13:25:07 +03:00
|
|
|
return nil, ResultReplayToCanceled, nil
|
2019-12-03 14:22:31 +03:00
|
|
|
|
|
|
|
case channeldb.HtlcStateAccepted:
|
2019-12-20 13:25:07 +03:00
|
|
|
return nil, ResultReplayToAccepted, nil
|
2019-12-03 14:22:31 +03:00
|
|
|
|
|
|
|
case channeldb.HtlcStateSettled:
|
2019-12-20 13:25:07 +03:00
|
|
|
return nil, ResultReplayToSettled, nil
|
2019-12-03 14:22:31 +03:00
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, 0, errors.New("unknown htlc state")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-03 13:23:39 +03:00
|
|
|
if ctx.mpp == nil {
|
|
|
|
return updateLegacy(ctx, inv)
|
|
|
|
}
|
|
|
|
|
|
|
|
return updateMpp(ctx, inv)
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateMpp is a callback for DB.UpdateInvoice that contains the invoice
|
|
|
|
// settlement logic for mpp payments.
|
|
|
|
func updateMpp(ctx *invoiceUpdateCtx, inv *channeldb.Invoice) (
|
2019-12-20 13:25:07 +03:00
|
|
|
*channeldb.InvoiceUpdateDesc, ResolutionResult, error) {
|
2019-09-03 13:23:39 +03:00
|
|
|
|
|
|
|
// Start building the accept descriptor.
|
|
|
|
acceptDesc := &channeldb.HtlcAcceptDesc{
|
|
|
|
Amt: ctx.amtPaid,
|
|
|
|
Expiry: ctx.expiry,
|
|
|
|
AcceptHeight: ctx.currentHeight,
|
|
|
|
MppTotalAmt: ctx.mpp.TotalMsat(),
|
|
|
|
CustomRecords: ctx.customRecords,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only accept payments to open invoices. This behaviour differs from
|
|
|
|
// non-mpp payments that are accepted even after the invoice is settled.
|
|
|
|
// Because non-mpp payments don't have a payment address, this is needed
|
|
|
|
// to thwart probing.
|
|
|
|
if inv.State != channeldb.ContractOpen {
|
2019-12-20 13:25:07 +03:00
|
|
|
return nil, ResultInvoiceNotOpen, nil
|
2019-09-03 13:23:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the payment address that authorizes the payment.
|
|
|
|
if ctx.mpp.PaymentAddr() != inv.Terms.PaymentAddr {
|
2019-12-20 13:25:07 +03:00
|
|
|
return nil, ResultAddressMismatch, nil
|
2019-09-03 13:23:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Don't accept zero-valued sets.
|
|
|
|
if ctx.mpp.TotalMsat() == 0 {
|
2019-12-20 13:25:07 +03:00
|
|
|
return nil, ResultHtlcSetTotalTooLow, nil
|
2019-09-03 13:23:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the total amt of the htlc set is high enough. In case this
|
|
|
|
// is a zero-valued invoice, it will always be enough.
|
|
|
|
if ctx.mpp.TotalMsat() < inv.Terms.Value {
|
2019-12-20 13:25:07 +03:00
|
|
|
return nil, ResultHtlcSetTotalTooLow, nil
|
2019-09-03 13:23:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether total amt matches other htlcs in the set.
|
|
|
|
var newSetTotal lnwire.MilliSatoshi
|
|
|
|
for _, htlc := range inv.Htlcs {
|
|
|
|
// Only consider accepted mpp htlcs. It is possible that there
|
|
|
|
// are htlcs registered in the invoice database that previously
|
|
|
|
// timed out and are in the canceled state now.
|
|
|
|
if htlc.State != channeldb.HtlcStateAccepted {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.mpp.TotalMsat() != htlc.MppTotalAmt {
|
2019-12-20 13:25:07 +03:00
|
|
|
return nil, ResultHtlcSetTotalMismatch, nil
|
2019-09-03 13:23:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
newSetTotal += htlc.Amt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add amount of new htlc.
|
|
|
|
newSetTotal += ctx.amtPaid
|
|
|
|
|
|
|
|
// Make sure the communicated set total isn't overpaid.
|
|
|
|
if newSetTotal > ctx.mpp.TotalMsat() {
|
2019-12-20 13:25:07 +03:00
|
|
|
return nil, ResultHtlcSetOverpayment, nil
|
2019-09-03 13:23:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// The invoice is still open. Check the expiry.
|
|
|
|
if ctx.expiry < uint32(ctx.currentHeight+ctx.finalCltvRejectDelta) {
|
2019-12-20 13:25:07 +03:00
|
|
|
return nil, ResultExpiryTooSoon, nil
|
2019-09-03 13:23:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.expiry < uint32(ctx.currentHeight+inv.Terms.FinalCltvDelta) {
|
2019-12-20 13:25:07 +03:00
|
|
|
return nil, ResultExpiryTooSoon, nil
|
2019-09-03 13:23:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Record HTLC in the invoice database.
|
|
|
|
newHtlcs := map[channeldb.CircuitKey]*channeldb.HtlcAcceptDesc{
|
|
|
|
ctx.circuitKey: acceptDesc,
|
|
|
|
}
|
|
|
|
|
|
|
|
update := channeldb.InvoiceUpdateDesc{
|
|
|
|
AddHtlcs: newHtlcs,
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the invoice cannot be settled yet, only record the htlc.
|
|
|
|
setComplete := newSetTotal == ctx.mpp.TotalMsat()
|
|
|
|
if !setComplete {
|
2019-12-20 13:25:07 +03:00
|
|
|
return &update, ResultPartialAccepted, nil
|
2019-09-03 13:23:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see if we can settle or this is an hold invoice and
|
|
|
|
// we need to wait for the preimage.
|
|
|
|
holdInvoice := inv.Terms.PaymentPreimage == channeldb.UnknownPreimage
|
|
|
|
if holdInvoice {
|
|
|
|
update.State = &channeldb.InvoiceStateUpdateDesc{
|
|
|
|
NewState: channeldb.ContractAccepted,
|
|
|
|
}
|
2019-12-20 13:25:07 +03:00
|
|
|
return &update, ResultAccepted, nil
|
2019-09-03 13:23:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
update.State = &channeldb.InvoiceStateUpdateDesc{
|
|
|
|
NewState: channeldb.ContractSettled,
|
|
|
|
Preimage: inv.Terms.PaymentPreimage,
|
|
|
|
}
|
|
|
|
|
2019-12-20 13:25:07 +03:00
|
|
|
return &update, ResultSettled, nil
|
2019-09-03 13:23:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// updateLegacy is a callback for DB.UpdateInvoice that contains the invoice
|
|
|
|
// settlement logic for legacy payments.
|
|
|
|
func updateLegacy(ctx *invoiceUpdateCtx, inv *channeldb.Invoice) (
|
2019-12-20 13:25:07 +03:00
|
|
|
*channeldb.InvoiceUpdateDesc, ResolutionResult, error) {
|
2019-09-03 13:23:39 +03:00
|
|
|
|
|
|
|
// If the invoice is already canceled, there is no further
|
|
|
|
// checking to do.
|
2019-12-03 14:22:31 +03:00
|
|
|
if inv.State == channeldb.ContractCanceled {
|
2019-12-20 13:25:07 +03:00
|
|
|
return nil, ResultInvoiceAlreadyCanceled, nil
|
2019-12-03 14:22:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If an invoice amount is specified, check that enough is paid. Also
|
|
|
|
// check this for duplicate payments if the invoice is already settled
|
2019-12-03 22:41:46 +03:00
|
|
|
// or accepted. In case this is a zero-valued invoice, it will always be
|
|
|
|
// enough.
|
|
|
|
if ctx.amtPaid < inv.Terms.Value {
|
2019-12-20 13:25:07 +03:00
|
|
|
return nil, ResultAmountTooLow, nil
|
2019-12-03 14:22:31 +03:00
|
|
|
}
|
|
|
|
|
2019-09-03 13:23:39 +03:00
|
|
|
// TODO(joostjager): Check invoice mpp required feature
|
|
|
|
// bit when feature becomes mandatory.
|
|
|
|
|
|
|
|
// Don't allow settling the invoice with an old style
|
|
|
|
// htlc if we are already in the process of gathering an
|
|
|
|
// mpp set.
|
|
|
|
for _, htlc := range inv.Htlcs {
|
|
|
|
if htlc.State == channeldb.HtlcStateAccepted &&
|
|
|
|
htlc.MppTotalAmt > 0 {
|
|
|
|
|
2019-12-20 13:25:07 +03:00
|
|
|
return nil, ResultMppInProgress, nil
|
2019-09-03 13:23:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 14:22:31 +03:00
|
|
|
// The invoice is still open. Check the expiry.
|
|
|
|
if ctx.expiry < uint32(ctx.currentHeight+ctx.finalCltvRejectDelta) {
|
2019-12-20 13:25:07 +03:00
|
|
|
return nil, ResultExpiryTooSoon, nil
|
2019-12-03 14:22:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.expiry < uint32(ctx.currentHeight+inv.Terms.FinalCltvDelta) {
|
2019-12-20 13:25:07 +03:00
|
|
|
return nil, ResultExpiryTooSoon, nil
|
2019-12-03 14:22:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Record HTLC in the invoice database.
|
|
|
|
newHtlcs := map[channeldb.CircuitKey]*channeldb.HtlcAcceptDesc{
|
|
|
|
ctx.circuitKey: {
|
2019-11-19 15:33:05 +03:00
|
|
|
Amt: ctx.amtPaid,
|
|
|
|
Expiry: ctx.expiry,
|
|
|
|
AcceptHeight: ctx.currentHeight,
|
|
|
|
CustomRecords: ctx.customRecords,
|
2019-12-03 14:22:31 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-11-27 16:19:15 +03:00
|
|
|
update := channeldb.InvoiceUpdateDesc{
|
|
|
|
AddHtlcs: newHtlcs,
|
|
|
|
}
|
2019-12-03 14:22:31 +03:00
|
|
|
|
|
|
|
// Don't update invoice state if we are accepting a duplicate payment.
|
|
|
|
// We do accept or settle the HTLC.
|
|
|
|
switch inv.State {
|
|
|
|
case channeldb.ContractAccepted:
|
2019-12-20 13:25:07 +03:00
|
|
|
return &update, ResultDuplicateToAccepted, nil
|
2019-12-03 14:22:31 +03:00
|
|
|
|
|
|
|
case channeldb.ContractSettled:
|
2019-12-20 13:25:07 +03:00
|
|
|
return &update, ResultDuplicateToSettled, nil
|
2019-12-03 14:22:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see if we can settle or this is an hold invoice and we need
|
|
|
|
// to wait for the preimage.
|
|
|
|
holdInvoice := inv.Terms.PaymentPreimage == channeldb.UnknownPreimage
|
|
|
|
if holdInvoice {
|
2019-11-27 15:20:14 +03:00
|
|
|
update.State = &channeldb.InvoiceStateUpdateDesc{
|
|
|
|
NewState: channeldb.ContractAccepted,
|
|
|
|
}
|
2019-12-20 13:25:07 +03:00
|
|
|
return &update, ResultAccepted, nil
|
2019-12-03 14:22:31 +03:00
|
|
|
}
|
|
|
|
|
2019-11-27 15:20:14 +03:00
|
|
|
update.State = &channeldb.InvoiceStateUpdateDesc{
|
|
|
|
NewState: channeldb.ContractSettled,
|
|
|
|
Preimage: inv.Terms.PaymentPreimage,
|
|
|
|
}
|
2019-12-03 14:22:31 +03:00
|
|
|
|
2019-12-20 13:25:07 +03:00
|
|
|
return &update, ResultSettled, nil
|
2019-12-03 14:22:31 +03:00
|
|
|
}
|