2016-09-17 03:23:37 +03:00
|
|
|
package channeldb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-09-21 03:37:26 +03:00
|
|
|
"encoding/binary"
|
2019-01-14 14:03:26 +03:00
|
|
|
"errors"
|
2016-09-24 01:15:22 +03:00
|
|
|
"fmt"
|
2016-09-17 03:23:37 +03:00
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
|
2019-12-13 05:22:19 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb/kvdb"
|
2019-11-19 15:33:05 +03:00
|
|
|
"github.com/lightningnetwork/lnd/htlcswitch/hop"
|
2019-01-15 13:31:22 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2017-08-22 08:51:19 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2019-12-12 02:01:55 +03:00
|
|
|
"github.com/lightningnetwork/lnd/record"
|
2019-08-08 17:58:28 +03:00
|
|
|
"github.com/lightningnetwork/lnd/tlv"
|
2016-09-17 03:23:37 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-04-08 14:47:10 +03:00
|
|
|
// unknownPreimage is an all-zeroes preimage that indicates that the
|
2018-10-05 11:14:56 +03:00
|
|
|
// preimage for this invoice is not yet known.
|
2020-04-08 14:47:10 +03:00
|
|
|
unknownPreimage lntypes.Preimage
|
2018-10-05 11:14:56 +03:00
|
|
|
|
2016-09-17 03:23:37 +03:00
|
|
|
// invoiceBucket is the name of the bucket within the database that
|
|
|
|
// stores all data related to invoices no matter their final state.
|
|
|
|
// Within the invoice bucket, each invoice is keyed by its invoice ID
|
|
|
|
// which is a monotonically increasing uint32.
|
|
|
|
invoiceBucket = []byte("invoices")
|
|
|
|
|
|
|
|
// paymentHashIndexBucket is the name of the sub-bucket within the
|
|
|
|
// invoiceBucket which indexes all invoices by their payment hash. The
|
|
|
|
// payment hash is the sha256 of the invoice's payment preimage. This
|
|
|
|
// index is used to detect duplicates, and also to provide a fast path
|
2017-01-13 08:01:50 +03:00
|
|
|
// for looking up incoming HTLCs to determine if we're able to settle
|
2016-09-17 03:23:37 +03:00
|
|
|
// them fully.
|
2018-04-25 06:50:56 +03:00
|
|
|
//
|
2018-08-11 06:24:04 +03:00
|
|
|
// maps: payHash => invoiceKey
|
2016-09-19 21:46:48 +03:00
|
|
|
invoiceIndexBucket = []byte("paymenthashes")
|
|
|
|
|
2020-05-22 01:37:39 +03:00
|
|
|
// payAddrIndexBucket is the name of the top-level bucket that maps
|
|
|
|
// payment addresses to their invoice number. This can be used
|
|
|
|
// to efficiently query or update non-legacy invoices. Note that legacy
|
|
|
|
// invoices will not be included in this index since they all have the
|
|
|
|
// same, all-zero payment address, however all newly generated invoices
|
|
|
|
// will end up in this index.
|
|
|
|
//
|
|
|
|
// maps: payAddr => invoiceKey
|
|
|
|
payAddrIndexBucket = []byte("pay-addr-index")
|
|
|
|
|
2016-09-19 21:46:48 +03:00
|
|
|
// numInvoicesKey is the name of key which houses the auto-incrementing
|
|
|
|
// invoice ID which is essentially used as a primary key. With each
|
|
|
|
// invoice inserted, the primary key is incremented by one. This key is
|
|
|
|
// stored within the invoiceIndexBucket. Within the invoiceBucket
|
|
|
|
// invoices are uniquely identified by the invoice ID.
|
|
|
|
numInvoicesKey = []byte("nik")
|
2018-04-25 06:57:30 +03:00
|
|
|
|
|
|
|
// addIndexBucket is an index bucket that we'll use to create a
|
|
|
|
// monotonically increasing set of add indexes. Each time we add a new
|
|
|
|
// invoice, this sequence number will be incremented and then populated
|
|
|
|
// within the new invoice.
|
|
|
|
//
|
|
|
|
// In addition to this sequence number, we map:
|
|
|
|
//
|
2018-08-11 06:24:04 +03:00
|
|
|
// addIndexNo => invoiceKey
|
2018-04-25 06:57:30 +03:00
|
|
|
addIndexBucket = []byte("invoice-add-index")
|
|
|
|
|
|
|
|
// settleIndexBucket is an index bucket that we'll use to create a
|
|
|
|
// monotonically increasing integer for tracking a "settle index". Each
|
|
|
|
// time an invoice is settled, this sequence number will be incremented
|
|
|
|
// as populate within the newly settled invoice.
|
|
|
|
//
|
|
|
|
// In addition to this sequence number, we map:
|
|
|
|
//
|
2018-08-11 06:24:04 +03:00
|
|
|
// settleIndexNo => invoiceKey
|
2018-04-25 06:57:30 +03:00
|
|
|
settleIndexBucket = []byte("invoice-settle-index")
|
2019-01-14 14:03:26 +03:00
|
|
|
|
|
|
|
// ErrInvoiceAlreadySettled is returned when the invoice is already
|
|
|
|
// settled.
|
|
|
|
ErrInvoiceAlreadySettled = errors.New("invoice already settled")
|
2019-01-11 13:19:16 +03:00
|
|
|
|
|
|
|
// ErrInvoiceAlreadyCanceled is returned when the invoice is already
|
|
|
|
// canceled.
|
|
|
|
ErrInvoiceAlreadyCanceled = errors.New("invoice already canceled")
|
2019-02-11 14:01:05 +03:00
|
|
|
|
|
|
|
// ErrInvoiceAlreadyAccepted is returned when the invoice is already
|
|
|
|
// accepted.
|
|
|
|
ErrInvoiceAlreadyAccepted = errors.New("invoice already accepted")
|
|
|
|
|
|
|
|
// ErrInvoiceStillOpen is returned when the invoice is still open.
|
|
|
|
ErrInvoiceStillOpen = errors.New("invoice still open")
|
2019-11-27 15:20:14 +03:00
|
|
|
|
|
|
|
// ErrInvoiceCannotOpen is returned when an attempt is made to move an
|
|
|
|
// invoice to the open state.
|
|
|
|
ErrInvoiceCannotOpen = errors.New("cannot move invoice to open")
|
|
|
|
|
|
|
|
// ErrInvoiceCannotAccept is returned when an attempt is made to accept
|
|
|
|
// an invoice while the invoice is not in the open state.
|
|
|
|
ErrInvoiceCannotAccept = errors.New("cannot accept invoice")
|
|
|
|
|
|
|
|
// ErrInvoicePreimageMismatch is returned when the preimage doesn't
|
|
|
|
// match the invoice hash.
|
|
|
|
ErrInvoicePreimageMismatch = errors.New("preimage does not match")
|
2016-09-17 03:23:37 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// MaxMemoSize is maximum size of the memo field within invoices stored
|
|
|
|
// in the database.
|
|
|
|
MaxMemoSize = 1024
|
|
|
|
|
2018-04-18 05:02:04 +03:00
|
|
|
// MaxPaymentRequestSize is the max size of a payment request for
|
2017-09-05 18:59:52 +03:00
|
|
|
// this invoice.
|
|
|
|
// TODO(halseth): determine the max length payment request when field
|
|
|
|
// lengths are final.
|
|
|
|
MaxPaymentRequestSize = 4096
|
2019-08-08 17:58:28 +03:00
|
|
|
|
|
|
|
// A set of tlv type definitions used to serialize invoice htlcs to the
|
|
|
|
// database.
|
2019-11-22 13:24:28 +03:00
|
|
|
//
|
|
|
|
// NOTE: A migration should be added whenever this list changes. This
|
|
|
|
// prevents against the database being rolled back to an older
|
|
|
|
// format where the surrounding logic might assume a different set of
|
|
|
|
// fields are known.
|
2019-08-08 17:58:28 +03:00
|
|
|
chanIDType tlv.Type = 1
|
|
|
|
htlcIDType tlv.Type = 3
|
|
|
|
amtType tlv.Type = 5
|
|
|
|
acceptHeightType tlv.Type = 7
|
|
|
|
acceptTimeType tlv.Type = 9
|
|
|
|
resolveTimeType tlv.Type = 11
|
|
|
|
expiryHeightType tlv.Type = 13
|
2019-11-22 13:24:28 +03:00
|
|
|
htlcStateType tlv.Type = 15
|
2019-09-03 13:23:39 +03:00
|
|
|
mppTotalAmtType tlv.Type = 17
|
2019-11-22 13:24:28 +03:00
|
|
|
|
|
|
|
// A set of tlv type definitions used to serialize invoice bodiees.
|
|
|
|
//
|
|
|
|
// NOTE: A migration should be added whenever this list changes. This
|
|
|
|
// prevents against the database being rolled back to an older
|
|
|
|
// format where the surrounding logic might assume a different set of
|
|
|
|
// fields are known.
|
|
|
|
memoType tlv.Type = 0
|
|
|
|
payReqType tlv.Type = 1
|
|
|
|
createTimeType tlv.Type = 2
|
|
|
|
settleTimeType tlv.Type = 3
|
|
|
|
addIndexType tlv.Type = 4
|
|
|
|
settleIndexType tlv.Type = 5
|
|
|
|
preimageType tlv.Type = 6
|
|
|
|
valueType tlv.Type = 7
|
|
|
|
cltvDeltaType tlv.Type = 8
|
|
|
|
expiryType tlv.Type = 9
|
|
|
|
paymentAddrType tlv.Type = 10
|
|
|
|
featuresType tlv.Type = 11
|
|
|
|
invStateType tlv.Type = 12
|
|
|
|
amtPaidType tlv.Type = 13
|
2020-04-08 14:47:10 +03:00
|
|
|
hodlInvoiceType tlv.Type = 14
|
2016-09-17 03:23:37 +03:00
|
|
|
)
|
|
|
|
|
2020-05-22 01:37:39 +03:00
|
|
|
// InvoiceRef is a composite identifier for invoices. Invoices can be referenced
|
|
|
|
// by various combinations of payment hash and payment addr, in certain contexts
|
|
|
|
// only some of these are known. An InvoiceRef and its constructors thus
|
|
|
|
// encapsulate the valid combinations of query parameters that can be supplied
|
|
|
|
// to LookupInvoice and UpdateInvoice.
|
2020-05-22 01:37:10 +03:00
|
|
|
type InvoiceRef struct {
|
|
|
|
// payHash is the payment hash of the target invoice. All invoices are
|
|
|
|
// currently indexed by payment hash. This value will be used as a
|
|
|
|
// fallback when no payment address is known.
|
|
|
|
payHash lntypes.Hash
|
2020-05-22 01:37:39 +03:00
|
|
|
|
|
|
|
// payAddr is the payment addr of the target invoice. Newer invoices
|
|
|
|
// (0.11 and up) are indexed by payment address in addition to payment
|
|
|
|
// hash, but pre 0.8 invoices do not have one at all. When this value is
|
|
|
|
// known it will be used as the primary identifier, falling back to
|
|
|
|
// payHash if no value is known.
|
|
|
|
payAddr *[32]byte
|
2020-05-22 01:37:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// InvoiceRefByHash creates an InvoiceRef that queries for an invoice only by
|
|
|
|
// its payment hash.
|
|
|
|
func InvoiceRefByHash(payHash lntypes.Hash) InvoiceRef {
|
|
|
|
return InvoiceRef{
|
|
|
|
payHash: payHash,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-22 01:37:39 +03:00
|
|
|
// InvoiceRefByHashAndAddr creates an InvoiceRef that first queries for an
|
|
|
|
// invoice by the provided payment address, falling back to the payment hash if
|
|
|
|
// the payment address is unknown.
|
|
|
|
func InvoiceRefByHashAndAddr(payHash lntypes.Hash,
|
|
|
|
payAddr [32]byte) InvoiceRef {
|
|
|
|
|
|
|
|
return InvoiceRef{
|
|
|
|
payHash: payHash,
|
|
|
|
payAddr: &payAddr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-22 01:37:10 +03:00
|
|
|
// PayHash returns the target invoice's payment hash.
|
|
|
|
func (r InvoiceRef) PayHash() lntypes.Hash {
|
|
|
|
return r.payHash
|
|
|
|
}
|
|
|
|
|
2020-05-22 01:37:39 +03:00
|
|
|
// PayAddr returns the optional payment address of the target invoice.
|
|
|
|
//
|
|
|
|
// NOTE: This value may be nil.
|
|
|
|
func (r InvoiceRef) PayAddr() *[32]byte {
|
|
|
|
if r.payAddr != nil {
|
|
|
|
addr := *r.payAddr
|
|
|
|
return &addr
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-22 01:37:10 +03:00
|
|
|
// String returns a human-readable representation of an InvoiceRef.
|
|
|
|
func (r InvoiceRef) String() string {
|
2020-05-22 01:37:39 +03:00
|
|
|
if r.payAddr != nil {
|
|
|
|
return fmt.Sprintf("(pay_hash=%v, pay_addr=%x)", r.payHash, *r.payAddr)
|
|
|
|
}
|
2020-05-22 01:37:10 +03:00
|
|
|
return fmt.Sprintf("(pay_hash=%v)", r.payHash)
|
|
|
|
}
|
|
|
|
|
2018-12-19 17:56:26 +03:00
|
|
|
// ContractState describes the state the invoice is in.
|
|
|
|
type ContractState uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
// ContractOpen means the invoice has only been created.
|
|
|
|
ContractOpen ContractState = 0
|
|
|
|
|
2019-12-02 15:24:26 +03:00
|
|
|
// ContractSettled means the htlc is settled and the invoice has been paid.
|
2018-12-19 17:56:26 +03:00
|
|
|
ContractSettled ContractState = 1
|
2019-01-11 13:19:16 +03:00
|
|
|
|
|
|
|
// ContractCanceled means the invoice has been canceled.
|
|
|
|
ContractCanceled ContractState = 2
|
2019-02-11 14:01:05 +03:00
|
|
|
|
2019-12-02 15:24:26 +03:00
|
|
|
// ContractAccepted means the HTLC has been accepted but not settled yet.
|
2019-02-11 14:01:05 +03:00
|
|
|
ContractAccepted ContractState = 3
|
2018-12-19 17:56:26 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// String returns a human readable identifier for the ContractState type.
|
|
|
|
func (c ContractState) String() string {
|
|
|
|
switch c {
|
|
|
|
case ContractOpen:
|
|
|
|
return "Open"
|
|
|
|
case ContractSettled:
|
|
|
|
return "Settled"
|
2019-01-11 13:19:16 +03:00
|
|
|
case ContractCanceled:
|
|
|
|
return "Canceled"
|
2019-02-11 14:01:05 +03:00
|
|
|
case ContractAccepted:
|
|
|
|
return "Accepted"
|
2018-12-19 17:56:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return "Unknown"
|
|
|
|
}
|
|
|
|
|
2016-09-19 21:46:48 +03:00
|
|
|
// ContractTerm is a companion struct to the Invoice struct. This struct houses
|
|
|
|
// the necessary conditions required before the invoice can be considered fully
|
|
|
|
// settled by the payee.
|
|
|
|
type ContractTerm struct {
|
2019-11-22 13:25:02 +03:00
|
|
|
// FinalCltvDelta is the minimum required number of blocks before htlc
|
|
|
|
// expiry when the invoice is accepted.
|
|
|
|
FinalCltvDelta int32
|
|
|
|
|
|
|
|
// Expiry defines how long after creation this invoice should expire.
|
|
|
|
Expiry time.Duration
|
|
|
|
|
2016-09-19 21:46:48 +03:00
|
|
|
// PaymentPreimage is the preimage which is to be revealed in the
|
|
|
|
// occasion that an HTLC paying to the hash of this preimage is
|
2020-04-08 14:47:10 +03:00
|
|
|
// extended. Set to nil if the preimage isn't known yet.
|
|
|
|
PaymentPreimage *lntypes.Preimage
|
2016-09-19 21:46:48 +03:00
|
|
|
|
2018-04-25 06:50:56 +03:00
|
|
|
// Value is the expected amount of milli-satoshis to be paid to an HTLC
|
|
|
|
// which can be satisfied by the above preimage.
|
2017-08-22 08:51:19 +03:00
|
|
|
Value lnwire.MilliSatoshi
|
2016-09-19 21:46:48 +03:00
|
|
|
|
2019-11-22 13:24:28 +03:00
|
|
|
// PaymentAddr is a randomly generated value include in the MPP record
|
|
|
|
// by the sender to prevent probing of the receiver.
|
|
|
|
PaymentAddr [32]byte
|
|
|
|
|
|
|
|
// Features is the feature vectors advertised on the payment request.
|
|
|
|
Features *lnwire.FeatureVector
|
2016-09-19 21:46:48 +03:00
|
|
|
}
|
|
|
|
|
2020-04-07 21:54:29 +03:00
|
|
|
// String returns a human-readable description of the prominent contract terms.
|
|
|
|
func (c ContractTerm) String() string {
|
|
|
|
return fmt.Sprintf("amt=%v, expiry=%v, final_cltv_delta=%v", c.Value,
|
|
|
|
c.Expiry, c.FinalCltvDelta)
|
|
|
|
}
|
|
|
|
|
2016-09-17 03:23:37 +03:00
|
|
|
// Invoice is a payment invoice generated by a payee in order to request
|
|
|
|
// payment for some good or service. The inclusion of invoices within Lightning
|
|
|
|
// creates a payment work flow for merchants very similar to that of the
|
|
|
|
// existing financial system within PayPal, etc. Invoices are added to the
|
|
|
|
// database when a payment is requested, then can be settled manually once the
|
|
|
|
// payment is received at the upper layer. For record keeping purposes,
|
|
|
|
// invoices are never deleted from the database, instead a bit is toggled
|
|
|
|
// denoting the invoice has been fully settled. Within the database, all
|
|
|
|
// invoices must have a unique payment hash which is generated by taking the
|
2018-04-25 06:50:56 +03:00
|
|
|
// sha256 of the payment preimage.
|
2016-09-17 03:23:37 +03:00
|
|
|
type Invoice struct {
|
|
|
|
// Memo is an optional memo to be stored along side an invoice. The
|
|
|
|
// memo may contain further details pertaining to the invoice itself,
|
|
|
|
// or any other message which fits within the size constraints.
|
2016-09-24 01:15:22 +03:00
|
|
|
Memo []byte
|
2016-09-17 03:23:37 +03:00
|
|
|
|
2019-12-05 14:55:17 +03:00
|
|
|
// PaymentRequest is the encoded payment request for this invoice. For
|
2020-01-16 15:13:59 +03:00
|
|
|
// spontaneous (keysend) payments, this field will be empty.
|
2017-09-05 18:59:52 +03:00
|
|
|
PaymentRequest []byte
|
|
|
|
|
2016-09-17 03:23:37 +03:00
|
|
|
// CreationDate is the exact time the invoice was created.
|
|
|
|
CreationDate time.Time
|
|
|
|
|
2017-12-05 09:07:21 +03:00
|
|
|
// SettleDate is the exact time the invoice was settled.
|
|
|
|
SettleDate time.Time
|
|
|
|
|
2018-04-25 06:50:56 +03:00
|
|
|
// Terms are the contractual payment terms of the invoice. Once all the
|
|
|
|
// terms have been satisfied by the payer, then the invoice can be
|
|
|
|
// considered fully fulfilled.
|
2016-09-17 03:23:37 +03:00
|
|
|
//
|
|
|
|
// TODO(roasbeef): later allow for multiple terms to fulfill the final
|
|
|
|
// invoice: payment fragmentation, etc.
|
|
|
|
Terms ContractTerm
|
2018-04-25 06:57:30 +03:00
|
|
|
|
|
|
|
// AddIndex is an auto-incrementing integer that acts as a
|
|
|
|
// monotonically increasing sequence number for all invoices created.
|
|
|
|
// Clients can then use this field as a "checkpoint" of sorts when
|
|
|
|
// implementing a streaming RPC to notify consumers of instances where
|
|
|
|
// an invoice has been added before they re-connected.
|
|
|
|
//
|
|
|
|
// NOTE: This index starts at 1.
|
|
|
|
AddIndex uint64
|
|
|
|
|
|
|
|
// SettleIndex is an auto-incrementing integer that acts as a
|
|
|
|
// monotonically increasing sequence number for all settled invoices.
|
|
|
|
// Clients can then use this field as a "checkpoint" of sorts when
|
|
|
|
// implementing a streaming RPC to notify consumers of instances where
|
|
|
|
// an invoice has been settled before they re-connected.
|
|
|
|
//
|
|
|
|
// NOTE: This index starts at 1.
|
|
|
|
SettleIndex uint64
|
|
|
|
|
2019-11-22 13:25:02 +03:00
|
|
|
// State describes the state the invoice is in.
|
|
|
|
State ContractState
|
|
|
|
|
2018-04-25 06:50:56 +03:00
|
|
|
// AmtPaid is the final amount that we ultimately accepted for pay for
|
|
|
|
// this invoice. We specify this value independently as it's possible
|
|
|
|
// that the invoice originally didn't specify an amount, or the sender
|
|
|
|
// overpaid.
|
|
|
|
AmtPaid lnwire.MilliSatoshi
|
2019-08-08 17:58:28 +03:00
|
|
|
|
|
|
|
// Htlcs records all htlcs that paid to this invoice. Some of these
|
2019-10-03 18:22:43 +03:00
|
|
|
// htlcs may have been marked as canceled.
|
2019-08-08 17:58:28 +03:00
|
|
|
Htlcs map[CircuitKey]*InvoiceHTLC
|
2020-04-08 14:47:10 +03:00
|
|
|
|
|
|
|
// HodlInvoice indicates whether the invoice should be held in the
|
|
|
|
// Accepted state or be settled right away.
|
|
|
|
HodlInvoice bool
|
2019-08-08 17:58:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// HtlcState defines the states an htlc paying to an invoice can be in.
|
|
|
|
type HtlcState uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
// HtlcStateAccepted indicates the htlc is locked-in, but not resolved.
|
|
|
|
HtlcStateAccepted HtlcState = iota
|
|
|
|
|
2019-10-03 18:22:43 +03:00
|
|
|
// HtlcStateCanceled indicates the htlc is canceled back to the
|
2019-08-08 17:58:28 +03:00
|
|
|
// sender.
|
2019-10-03 18:22:43 +03:00
|
|
|
HtlcStateCanceled
|
2019-08-08 17:58:28 +03:00
|
|
|
|
|
|
|
// HtlcStateSettled indicates the htlc is settled.
|
|
|
|
HtlcStateSettled
|
|
|
|
)
|
|
|
|
|
|
|
|
// InvoiceHTLC contains details about an htlc paying to this invoice.
|
|
|
|
type InvoiceHTLC struct {
|
|
|
|
// Amt is the amount that is carried by this htlc.
|
|
|
|
Amt lnwire.MilliSatoshi
|
|
|
|
|
2019-09-03 13:23:39 +03:00
|
|
|
// MppTotalAmt is a field for mpp that indicates the expected total
|
|
|
|
// amount.
|
|
|
|
MppTotalAmt lnwire.MilliSatoshi
|
|
|
|
|
2019-08-08 17:58:28 +03:00
|
|
|
// AcceptHeight is the block height at which the invoice registry
|
|
|
|
// decided to accept this htlc as a payment to the invoice. At this
|
|
|
|
// height, the invoice cltv delay must have been met.
|
|
|
|
AcceptHeight uint32
|
|
|
|
|
|
|
|
// AcceptTime is the wall clock time at which the invoice registry
|
|
|
|
// decided to accept the htlc.
|
|
|
|
AcceptTime time.Time
|
|
|
|
|
|
|
|
// ResolveTime is the wall clock time at which the invoice registry
|
|
|
|
// decided to settle the htlc.
|
|
|
|
ResolveTime time.Time
|
|
|
|
|
|
|
|
// Expiry is the expiry height of this htlc.
|
|
|
|
Expiry uint32
|
|
|
|
|
|
|
|
// State indicates the state the invoice htlc is currently in. A
|
2019-10-03 18:22:43 +03:00
|
|
|
// canceled htlc isn't just removed from the invoice htlcs map, because
|
2019-08-14 22:21:39 +03:00
|
|
|
// we need AcceptHeight to properly cancel the htlc back.
|
2019-08-08 17:58:28 +03:00
|
|
|
State HtlcState
|
2019-11-19 15:33:05 +03:00
|
|
|
|
|
|
|
// CustomRecords contains the custom key/value pairs that accompanied
|
|
|
|
// the htlc.
|
2019-12-12 02:01:55 +03:00
|
|
|
CustomRecords record.CustomSet
|
2016-09-17 03:23:37 +03:00
|
|
|
}
|
|
|
|
|
2019-08-09 16:09:57 +03:00
|
|
|
// HtlcAcceptDesc describes the details of a newly accepted htlc.
|
|
|
|
type HtlcAcceptDesc struct {
|
|
|
|
// AcceptHeight is the block height at which this htlc was accepted.
|
|
|
|
AcceptHeight int32
|
|
|
|
|
|
|
|
// Amt is the amount that is carried by this htlc.
|
|
|
|
Amt lnwire.MilliSatoshi
|
|
|
|
|
2019-09-03 13:23:39 +03:00
|
|
|
// MppTotalAmt is a field for mpp that indicates the expected total
|
|
|
|
// amount.
|
|
|
|
MppTotalAmt lnwire.MilliSatoshi
|
|
|
|
|
2019-08-09 16:09:57 +03:00
|
|
|
// Expiry is the expiry height of this htlc.
|
|
|
|
Expiry uint32
|
2019-11-19 15:33:05 +03:00
|
|
|
|
|
|
|
// CustomRecords contains the custom key/value pairs that accompanied
|
|
|
|
// the htlc.
|
2019-12-12 02:01:55 +03:00
|
|
|
CustomRecords record.CustomSet
|
2019-08-09 16:09:57 +03:00
|
|
|
}
|
|
|
|
|
2019-08-09 14:40:34 +03:00
|
|
|
// InvoiceUpdateDesc describes the changes that should be applied to the
|
|
|
|
// invoice.
|
|
|
|
type InvoiceUpdateDesc struct {
|
2019-11-27 15:20:14 +03:00
|
|
|
// State is the new state that this invoice should progress to. If nil,
|
|
|
|
// the state is left unchanged.
|
|
|
|
State *InvoiceStateUpdateDesc
|
2019-08-09 14:40:34 +03:00
|
|
|
|
2019-11-27 16:19:15 +03:00
|
|
|
// CancelHtlcs describes the htlcs that need to be canceled.
|
|
|
|
CancelHtlcs map[CircuitKey]struct{}
|
|
|
|
|
|
|
|
// AddHtlcs describes the newly accepted htlcs that need to be added to
|
|
|
|
// the invoice.
|
|
|
|
AddHtlcs map[CircuitKey]*HtlcAcceptDesc
|
2019-11-27 15:20:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// InvoiceStateUpdateDesc describes an invoice-level state transition.
|
|
|
|
type InvoiceStateUpdateDesc struct {
|
|
|
|
// NewState is the new state that this invoice should progress to.
|
|
|
|
NewState ContractState
|
2019-08-21 16:24:21 +03:00
|
|
|
|
2019-11-27 15:20:14 +03:00
|
|
|
// Preimage must be set to the preimage when NewState is settled.
|
2020-04-08 14:47:10 +03:00
|
|
|
Preimage *lntypes.Preimage
|
2019-08-09 14:40:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// InvoiceUpdateCallback is a callback used in the db transaction to update the
|
|
|
|
// invoice.
|
|
|
|
type InvoiceUpdateCallback = func(invoice *Invoice) (*InvoiceUpdateDesc, error)
|
|
|
|
|
2020-04-08 14:47:10 +03:00
|
|
|
func validateInvoice(i *Invoice, paymentHash lntypes.Hash) error {
|
|
|
|
// Avoid conflicts with all-zeroes magic value in the database.
|
|
|
|
if paymentHash == unknownPreimage.Hash() {
|
|
|
|
return fmt.Errorf("cannot use hash of all-zeroes preimage")
|
|
|
|
}
|
|
|
|
|
2016-09-24 01:15:22 +03:00
|
|
|
if len(i.Memo) > MaxMemoSize {
|
|
|
|
return fmt.Errorf("max length a memo is %v, and invoice "+
|
|
|
|
"of length %v was provided", MaxMemoSize, len(i.Memo))
|
|
|
|
}
|
2017-09-05 18:59:52 +03:00
|
|
|
if len(i.PaymentRequest) > MaxPaymentRequestSize {
|
|
|
|
return fmt.Errorf("max length of payment request is %v, length "+
|
|
|
|
"provided was %v", MaxPaymentRequestSize,
|
|
|
|
len(i.PaymentRequest))
|
|
|
|
}
|
2019-11-22 13:24:28 +03:00
|
|
|
if i.Terms.Features == nil {
|
|
|
|
return errors.New("invoice must have a feature vector")
|
|
|
|
}
|
2020-04-08 14:47:10 +03:00
|
|
|
|
|
|
|
if i.Terms.PaymentPreimage == nil && !i.HodlInvoice {
|
|
|
|
return errors.New("non-hodl invoices must have a preimage")
|
|
|
|
}
|
2016-12-05 14:59:36 +03:00
|
|
|
return nil
|
|
|
|
}
|
2016-09-24 01:15:22 +03:00
|
|
|
|
2019-12-02 15:24:26 +03:00
|
|
|
// IsPending returns ture if the invoice is in ContractOpen state.
|
|
|
|
func (i *Invoice) IsPending() bool {
|
|
|
|
return i.State == ContractOpen || i.State == ContractAccepted
|
|
|
|
}
|
|
|
|
|
2019-01-15 12:06:48 +03:00
|
|
|
// AddInvoice inserts the targeted invoice into the database. If the invoice has
|
|
|
|
// *any* payment hashes which already exists within the database, then the
|
2016-12-05 14:59:36 +03:00
|
|
|
// insertion will be aborted and rejected due to the strict policy banning any
|
2019-01-15 12:06:48 +03:00
|
|
|
// duplicate payment hashes. A side effect of this function is that it sets
|
|
|
|
// AddIndex on newInvoice.
|
2018-10-05 11:14:56 +03:00
|
|
|
func (d *DB) AddInvoice(newInvoice *Invoice, paymentHash lntypes.Hash) (
|
|
|
|
uint64, error) {
|
|
|
|
|
2020-04-08 14:47:10 +03:00
|
|
|
if err := validateInvoice(newInvoice, paymentHash); err != nil {
|
2018-06-30 04:05:51 +03:00
|
|
|
return 0, err
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
2018-04-25 06:57:30 +03:00
|
|
|
|
2018-06-30 04:05:51 +03:00
|
|
|
var invoiceAddIndex uint64
|
2019-12-13 05:22:19 +03:00
|
|
|
err := kvdb.Update(d, func(tx kvdb.RwTx) error {
|
|
|
|
invoices, err := tx.CreateTopLevelBucket(invoiceBucket)
|
2016-09-17 03:23:37 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-04-25 06:57:30 +03:00
|
|
|
invoiceIndex, err := invoices.CreateBucketIfNotExists(
|
|
|
|
invoiceIndexBucket,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
addIndex, err := invoices.CreateBucketIfNotExists(
|
|
|
|
addIndexBucket,
|
|
|
|
)
|
2016-09-17 03:23:37 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that an invoice an identical payment hash doesn't
|
|
|
|
// already exist within the index.
|
|
|
|
if invoiceIndex.Get(paymentHash[:]) != nil {
|
|
|
|
return ErrDuplicateInvoice
|
|
|
|
}
|
|
|
|
|
2020-05-22 01:37:39 +03:00
|
|
|
payAddrIndex := tx.ReadWriteBucket(payAddrIndexBucket)
|
|
|
|
if payAddrIndex.Get(newInvoice.Terms.PaymentAddr[:]) != nil {
|
|
|
|
return ErrDuplicatePayAddr
|
|
|
|
}
|
|
|
|
|
2016-09-17 03:23:37 +03:00
|
|
|
// If the current running payment ID counter hasn't yet been
|
|
|
|
// created, then create it now.
|
|
|
|
var invoiceNum uint32
|
2016-09-19 21:46:48 +03:00
|
|
|
invoiceCounter := invoiceIndex.Get(numInvoicesKey)
|
2016-09-17 03:23:37 +03:00
|
|
|
if invoiceCounter == nil {
|
|
|
|
var scratch [4]byte
|
|
|
|
byteOrder.PutUint32(scratch[:], invoiceNum)
|
2018-04-25 06:57:30 +03:00
|
|
|
err := invoiceIndex.Put(numInvoicesKey, scratch[:])
|
|
|
|
if err != nil {
|
2019-09-09 12:41:43 +03:00
|
|
|
return err
|
2016-09-17 03:23:37 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
invoiceNum = byteOrder.Uint32(invoiceCounter)
|
|
|
|
}
|
|
|
|
|
2018-06-30 04:05:51 +03:00
|
|
|
newIndex, err := putInvoice(
|
2020-05-22 01:37:39 +03:00
|
|
|
invoices, invoiceIndex, payAddrIndex, addIndex,
|
|
|
|
newInvoice, invoiceNum, paymentHash,
|
2018-04-25 06:57:30 +03:00
|
|
|
)
|
2018-06-30 04:05:51 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
invoiceAddIndex = newIndex
|
|
|
|
return nil
|
2018-04-25 06:57:30 +03:00
|
|
|
})
|
|
|
|
if err != nil {
|
2018-06-30 04:05:51 +03:00
|
|
|
return 0, err
|
2018-04-25 06:57:30 +03:00
|
|
|
}
|
|
|
|
|
2018-06-30 04:05:51 +03:00
|
|
|
return invoiceAddIndex, err
|
2018-04-25 06:57:30 +03:00
|
|
|
}
|
2018-04-25 06:59:53 +03:00
|
|
|
|
|
|
|
// InvoicesAddedSince can be used by callers to seek into the event time series
|
|
|
|
// of all the invoices added in the database. The specified sinceAddIndex
|
|
|
|
// should be the highest add index that the caller knows of. This method will
|
|
|
|
// return all invoices with an add index greater than the specified
|
|
|
|
// sinceAddIndex.
|
|
|
|
//
|
|
|
|
// NOTE: The index starts from 1, as a result. We enforce that specifying a
|
|
|
|
// value below the starting index value is a noop.
|
|
|
|
func (d *DB) InvoicesAddedSince(sinceAddIndex uint64) ([]Invoice, error) {
|
|
|
|
var newInvoices []Invoice
|
|
|
|
|
|
|
|
// If an index of zero was specified, then in order to maintain
|
|
|
|
// backwards compat, we won't send out any new invoices.
|
|
|
|
if sinceAddIndex == 0 {
|
|
|
|
return newInvoices, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var startIndex [8]byte
|
|
|
|
byteOrder.PutUint64(startIndex[:], sinceAddIndex)
|
|
|
|
|
2020-05-07 01:45:50 +03:00
|
|
|
err := kvdb.View(d, func(tx kvdb.RTx) error {
|
2019-12-13 05:22:19 +03:00
|
|
|
invoices := tx.ReadBucket(invoiceBucket)
|
2018-04-25 06:59:53 +03:00
|
|
|
if invoices == nil {
|
2020-05-20 06:31:26 +03:00
|
|
|
return nil
|
2018-04-25 06:59:53 +03:00
|
|
|
}
|
|
|
|
|
2019-12-13 05:22:19 +03:00
|
|
|
addIndex := invoices.NestedReadBucket(addIndexBucket)
|
2018-04-25 06:59:53 +03:00
|
|
|
if addIndex == nil {
|
2020-05-20 06:31:26 +03:00
|
|
|
return nil
|
2018-04-25 06:59:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We'll now run through each entry in the add index starting
|
|
|
|
// at our starting index. We'll continue until we reach the
|
|
|
|
// very end of the current key space.
|
2019-12-13 05:22:19 +03:00
|
|
|
invoiceCursor := addIndex.ReadCursor()
|
2018-04-25 06:59:53 +03:00
|
|
|
|
|
|
|
// We'll seek to the starting index, then manually advance the
|
|
|
|
// cursor in order to skip the entry with the since add index.
|
|
|
|
invoiceCursor.Seek(startIndex[:])
|
|
|
|
addSeqNo, invoiceKey := invoiceCursor.Next()
|
|
|
|
|
|
|
|
for ; addSeqNo != nil && bytes.Compare(addSeqNo, startIndex[:]) > 0; addSeqNo, invoiceKey = invoiceCursor.Next() {
|
|
|
|
|
|
|
|
// For each key found, we'll look up the actual
|
|
|
|
// invoice, then accumulate it into our return value.
|
|
|
|
invoice, err := fetchInvoice(invoiceKey, invoices)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
newInvoices = append(newInvoices, invoice)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2016-09-17 03:23:37 +03:00
|
|
|
})
|
2020-05-20 06:31:26 +03:00
|
|
|
if err != nil {
|
2018-04-25 06:59:53 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return newInvoices, nil
|
2016-09-17 03:23:37 +03:00
|
|
|
}
|
|
|
|
|
2018-02-07 06:13:07 +03:00
|
|
|
// LookupInvoice attempts to look up an invoice according to its 32 byte
|
|
|
|
// payment hash. If an invoice which can settle the HTLC identified by the
|
2017-01-08 07:09:08 +03:00
|
|
|
// passed payment hash isn't found, then an error is returned. Otherwise, the
|
2016-09-17 03:23:37 +03:00
|
|
|
// full invoice is returned. Before setting the incoming HTLC, the values
|
|
|
|
// SHOULD be checked to ensure the payer meets the agreed upon contractual
|
|
|
|
// terms of the payment.
|
2020-05-22 01:37:10 +03:00
|
|
|
func (d *DB) LookupInvoice(ref InvoiceRef) (Invoice, error) {
|
2018-04-25 06:57:30 +03:00
|
|
|
var invoice Invoice
|
2020-05-07 01:45:50 +03:00
|
|
|
err := kvdb.View(d, func(tx kvdb.RTx) error {
|
2019-12-13 05:22:19 +03:00
|
|
|
invoices := tx.ReadBucket(invoiceBucket)
|
2016-09-17 03:23:37 +03:00
|
|
|
if invoices == nil {
|
2017-02-24 16:32:33 +03:00
|
|
|
return ErrNoInvoicesCreated
|
2016-09-17 03:23:37 +03:00
|
|
|
}
|
2019-12-13 05:22:19 +03:00
|
|
|
invoiceIndex := invoices.NestedReadBucket(invoiceIndexBucket)
|
2016-09-17 03:23:37 +03:00
|
|
|
if invoiceIndex == nil {
|
2017-02-24 16:32:33 +03:00
|
|
|
return ErrNoInvoicesCreated
|
2016-09-17 03:23:37 +03:00
|
|
|
}
|
2020-05-22 01:37:39 +03:00
|
|
|
payAddrIndex := tx.ReadBucket(payAddrIndexBucket)
|
2016-09-17 03:23:37 +03:00
|
|
|
|
2020-05-22 01:37:10 +03:00
|
|
|
// Retrieve the invoice number for this invoice using the
|
|
|
|
// provided invoice reference.
|
|
|
|
invoiceNum, err := fetchInvoiceNumByRef(
|
2020-05-22 01:37:39 +03:00
|
|
|
invoiceIndex, payAddrIndex, ref,
|
2020-05-22 01:37:10 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-09-17 03:23:37 +03:00
|
|
|
}
|
|
|
|
|
2020-05-22 01:37:10 +03:00
|
|
|
// An invoice was found, retrieve the remainder of the invoice
|
|
|
|
// body.
|
2016-09-17 03:23:37 +03:00
|
|
|
i, err := fetchInvoice(invoiceNum, invoices)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
invoice = i
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2018-04-25 06:57:30 +03:00
|
|
|
return invoice, err
|
2016-09-17 03:23:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return invoice, nil
|
|
|
|
}
|
|
|
|
|
2020-05-22 01:37:10 +03:00
|
|
|
// fetchInvoiceNumByRef retrieve the invoice number for the provided invoice
|
2020-05-22 01:37:39 +03:00
|
|
|
// reference. The payment address will be treated as the primary key, falling
|
|
|
|
// back to the payment hash if nothing is found for the payment address. An
|
|
|
|
// error is returned if the invoice is not found.
|
2020-05-28 06:20:29 +03:00
|
|
|
func fetchInvoiceNumByRef(invoiceIndex, payAddrIndex kvdb.RBucket,
|
2020-05-22 01:37:10 +03:00
|
|
|
ref InvoiceRef) ([]byte, error) {
|
|
|
|
|
|
|
|
payHash := ref.PayHash()
|
2020-05-22 01:37:39 +03:00
|
|
|
payAddr := ref.PayAddr()
|
|
|
|
|
|
|
|
var (
|
|
|
|
invoiceNumByHash = invoiceIndex.Get(payHash[:])
|
|
|
|
invoiceNumByAddr []byte
|
|
|
|
)
|
|
|
|
if payAddr != nil {
|
|
|
|
invoiceNumByAddr = payAddrIndex.Get(payAddr[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
|
|
|
|
// If payment address and payment hash both reference an existing
|
|
|
|
// invoice, ensure they reference the _same_ invoice.
|
|
|
|
case invoiceNumByAddr != nil && invoiceNumByHash != nil:
|
|
|
|
if !bytes.Equal(invoiceNumByAddr, invoiceNumByHash) {
|
|
|
|
return nil, ErrInvRefEquivocation
|
|
|
|
}
|
|
|
|
|
|
|
|
return invoiceNumByAddr, nil
|
2020-05-22 01:37:10 +03:00
|
|
|
|
2020-05-22 01:37:39 +03:00
|
|
|
// If we were only able to reference the invoice by hash, return the
|
|
|
|
// corresponding invoice number. This can happen when no payment address
|
|
|
|
// was provided, or if it didn't match anything in our records.
|
|
|
|
case invoiceNumByHash != nil:
|
|
|
|
return invoiceNumByHash, nil
|
|
|
|
|
|
|
|
// Otherwise we don't know of the target invoice.
|
|
|
|
default:
|
2020-05-22 01:37:10 +03:00
|
|
|
return nil, ErrInvoiceNotFound
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-09 19:50:11 +03:00
|
|
|
// InvoiceWithPaymentHash is used to store an invoice and its corresponding
|
|
|
|
// payment hash. This struct is only used to store results of
|
|
|
|
// ChannelDB.FetchAllInvoicesWithPaymentHash() call.
|
|
|
|
type InvoiceWithPaymentHash struct {
|
|
|
|
// Invoice holds the invoice as selected from the invoices bucket.
|
|
|
|
Invoice Invoice
|
|
|
|
|
|
|
|
// PaymentHash is the payment hash for the Invoice.
|
|
|
|
PaymentHash lntypes.Hash
|
|
|
|
}
|
|
|
|
|
|
|
|
// FetchAllInvoicesWithPaymentHash returns all invoices and their payment hashes
|
|
|
|
// currently stored within the database. If the pendingOnly param is true, then
|
2019-12-02 15:24:26 +03:00
|
|
|
// only open or accepted invoices and their payment hashes will be returned,
|
|
|
|
// skipping all invoices that are fully settled or canceled. Note that the
|
|
|
|
// returned array is not ordered by add index.
|
2019-12-09 19:50:11 +03:00
|
|
|
func (d *DB) FetchAllInvoicesWithPaymentHash(pendingOnly bool) (
|
|
|
|
[]InvoiceWithPaymentHash, error) {
|
|
|
|
|
|
|
|
var result []InvoiceWithPaymentHash
|
|
|
|
|
2020-05-07 01:45:50 +03:00
|
|
|
err := kvdb.View(d, func(tx kvdb.RTx) error {
|
2019-12-13 05:22:19 +03:00
|
|
|
invoices := tx.ReadBucket(invoiceBucket)
|
2019-12-09 19:50:11 +03:00
|
|
|
if invoices == nil {
|
|
|
|
return ErrNoInvoicesCreated
|
|
|
|
}
|
|
|
|
|
2019-12-13 05:22:19 +03:00
|
|
|
invoiceIndex := invoices.NestedReadBucket(invoiceIndexBucket)
|
2019-12-09 19:50:11 +03:00
|
|
|
if invoiceIndex == nil {
|
|
|
|
// Mask the error if there's no invoice
|
|
|
|
// index as that simply means there are no
|
|
|
|
// invoices added yet to the DB. In this case
|
|
|
|
// we simply return an empty list.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return invoiceIndex.ForEach(func(k, v []byte) error {
|
|
|
|
// Skip the special numInvoicesKey as that does not
|
|
|
|
// point to a valid invoice.
|
|
|
|
if bytes.Equal(k, numInvoicesKey) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
invoice, err := fetchInvoice(v, invoices)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:24:26 +03:00
|
|
|
if pendingOnly && !invoice.IsPending() {
|
2019-12-09 19:50:11 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
invoiceWithPaymentHash := InvoiceWithPaymentHash{
|
|
|
|
Invoice: invoice,
|
|
|
|
}
|
|
|
|
|
|
|
|
copy(invoiceWithPaymentHash.PaymentHash[:], k)
|
|
|
|
result = append(result, invoiceWithPaymentHash)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2018-08-11 06:24:04 +03:00
|
|
|
// InvoiceQuery represents a query to the invoice database. The query allows a
|
|
|
|
// caller to retrieve all invoices starting from a particular add index and
|
|
|
|
// limit the number of results returned.
|
|
|
|
type InvoiceQuery struct {
|
|
|
|
// IndexOffset is the offset within the add indices to start at. This
|
|
|
|
// can be used to start the response at a particular invoice.
|
2018-09-11 04:19:55 +03:00
|
|
|
IndexOffset uint64
|
2018-08-11 06:24:04 +03:00
|
|
|
|
|
|
|
// NumMaxInvoices is the maximum number of invoices that should be
|
|
|
|
// starting from the add index.
|
2018-09-11 04:19:55 +03:00
|
|
|
NumMaxInvoices uint64
|
2018-08-11 06:24:04 +03:00
|
|
|
|
|
|
|
// PendingOnly, if set, returns unsettled invoices starting from the
|
|
|
|
// add index.
|
|
|
|
PendingOnly bool
|
2018-09-11 04:19:55 +03:00
|
|
|
|
|
|
|
// Reversed, if set, indicates that the invoices returned should start
|
|
|
|
// from the IndexOffset and go backwards.
|
|
|
|
Reversed bool
|
2018-08-11 06:24:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// InvoiceSlice is the response to a invoice query. It includes the original
|
|
|
|
// query, the set of invoices that match the query, and an integer which
|
|
|
|
// represents the offset index of the last item in the set of returned invoices.
|
|
|
|
// This integer allows callers to resume their query using this offset in the
|
|
|
|
// event that the query's response exceeds the maximum number of returnable
|
|
|
|
// invoices.
|
|
|
|
type InvoiceSlice struct {
|
|
|
|
InvoiceQuery
|
|
|
|
|
|
|
|
// Invoices is the set of invoices that matched the query above.
|
2018-09-11 04:19:55 +03:00
|
|
|
Invoices []Invoice
|
|
|
|
|
|
|
|
// FirstIndexOffset is the index of the first element in the set of
|
|
|
|
// returned Invoices above. Callers can use this to resume their query
|
|
|
|
// in the event that the slice has too many events to fit into a single
|
|
|
|
// response.
|
|
|
|
FirstIndexOffset uint64
|
2018-08-11 06:24:04 +03:00
|
|
|
|
|
|
|
// LastIndexOffset is the index of the last element in the set of
|
|
|
|
// returned Invoices above. Callers can use this to resume their query
|
2018-09-11 04:19:55 +03:00
|
|
|
// in the event that the slice has too many events to fit into a single
|
|
|
|
// response.
|
|
|
|
LastIndexOffset uint64
|
2018-08-11 06:24:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// QueryInvoices allows a caller to query the invoice database for invoices
|
|
|
|
// within the specified add index range.
|
|
|
|
func (d *DB) QueryInvoices(q InvoiceQuery) (InvoiceSlice, error) {
|
|
|
|
resp := InvoiceSlice{
|
|
|
|
InvoiceQuery: q,
|
|
|
|
}
|
|
|
|
|
2020-05-07 01:45:50 +03:00
|
|
|
err := kvdb.View(d, func(tx kvdb.RTx) error {
|
2018-08-11 06:24:04 +03:00
|
|
|
// If the bucket wasn't found, then there aren't any invoices
|
|
|
|
// within the database yet, so we can simply exit.
|
2019-12-13 05:22:19 +03:00
|
|
|
invoices := tx.ReadBucket(invoiceBucket)
|
2018-08-11 06:24:04 +03:00
|
|
|
if invoices == nil {
|
|
|
|
return ErrNoInvoicesCreated
|
|
|
|
}
|
2020-06-10 13:34:27 +03:00
|
|
|
|
|
|
|
// Get the add index bucket which we will use to iterate through
|
|
|
|
// our indexed invoices.
|
2019-12-13 05:22:19 +03:00
|
|
|
invoiceAddIndex := invoices.NestedReadBucket(addIndexBucket)
|
2018-09-11 04:19:55 +03:00
|
|
|
if invoiceAddIndex == nil {
|
2018-08-11 06:24:04 +03:00
|
|
|
return ErrNoInvoicesCreated
|
|
|
|
}
|
|
|
|
|
2020-06-10 13:34:27 +03:00
|
|
|
// Create a paginator which reads from our add index bucket with
|
|
|
|
// the parameters provided by the invoice query.
|
|
|
|
paginator := newPaginator(
|
|
|
|
invoiceAddIndex.ReadCursor(), q.Reversed, q.IndexOffset,
|
|
|
|
q.NumMaxInvoices,
|
|
|
|
)
|
2018-08-11 06:24:04 +03:00
|
|
|
|
2020-06-10 13:34:27 +03:00
|
|
|
// accumulateInvoices looks up an invoice based on the index we
|
|
|
|
// are given, adds it to our set of invoices if it has the right
|
|
|
|
// characteristics for our query and returns the number of items
|
|
|
|
// we have added to our set of invoices.
|
|
|
|
accumulateInvoices := func(_, indexValue []byte) (bool, error) {
|
|
|
|
invoice, err := fetchInvoice(indexValue, invoices)
|
2018-08-11 06:24:04 +03:00
|
|
|
if err != nil {
|
2020-06-10 13:34:27 +03:00
|
|
|
return false, err
|
2018-08-11 06:24:04 +03:00
|
|
|
}
|
|
|
|
|
2020-06-10 13:34:27 +03:00
|
|
|
// Skip any settled or canceled invoices if the caller
|
|
|
|
// is only interested in pending ones.
|
2019-12-02 15:24:26 +03:00
|
|
|
if q.PendingOnly && !invoice.IsPending() {
|
2020-06-10 13:34:27 +03:00
|
|
|
return false, nil
|
2018-08-11 06:24:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, we've exhausted the offset, so we'll
|
|
|
|
// begin collecting invoices found within the range.
|
2018-09-11 04:19:55 +03:00
|
|
|
resp.Invoices = append(resp.Invoices, invoice)
|
2020-06-10 13:34:27 +03:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query our paginator using accumulateInvoices to build up a
|
|
|
|
// set of invoices.
|
|
|
|
if err := paginator.query(accumulateInvoices); err != nil {
|
|
|
|
return err
|
2018-09-11 04:19:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we iterated through the add index in reverse order, then
|
|
|
|
// we'll need to reverse the slice of invoices to return them in
|
|
|
|
// forward order.
|
|
|
|
if q.Reversed {
|
|
|
|
numInvoices := len(resp.Invoices)
|
|
|
|
for i := 0; i < numInvoices/2; i++ {
|
|
|
|
opposite := numInvoices - i - 1
|
|
|
|
resp.Invoices[i], resp.Invoices[opposite] =
|
|
|
|
resp.Invoices[opposite], resp.Invoices[i]
|
|
|
|
}
|
2018-08-11 06:24:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil && err != ErrNoInvoicesCreated {
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
2018-09-11 04:19:55 +03:00
|
|
|
// Finally, record the indexes of the first and last invoices returned
|
|
|
|
// so that the caller can resume from this point later on.
|
|
|
|
if len(resp.Invoices) > 0 {
|
|
|
|
resp.FirstIndexOffset = resp.Invoices[0].AddIndex
|
|
|
|
resp.LastIndexOffset = resp.Invoices[len(resp.Invoices)-1].AddIndex
|
|
|
|
}
|
2018-08-11 06:24:04 +03:00
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2019-08-09 14:40:34 +03:00
|
|
|
// UpdateInvoice attempts to update an invoice corresponding to the passed
|
|
|
|
// payment hash. If an invoice matching the passed payment hash doesn't exist
|
|
|
|
// within the database, then the action will fail with a "not found" error.
|
2019-02-11 14:01:05 +03:00
|
|
|
//
|
2019-08-09 14:40:34 +03:00
|
|
|
// The update is performed inside the same database transaction that fetches the
|
|
|
|
// invoice and is therefore atomic. The fields to update are controlled by the
|
|
|
|
// supplied callback.
|
2020-05-22 01:37:10 +03:00
|
|
|
func (d *DB) UpdateInvoice(ref InvoiceRef,
|
2019-08-09 14:40:34 +03:00
|
|
|
callback InvoiceUpdateCallback) (*Invoice, error) {
|
2018-04-25 06:50:56 +03:00
|
|
|
|
2019-08-09 14:40:34 +03:00
|
|
|
var updatedInvoice *Invoice
|
2019-12-13 05:22:19 +03:00
|
|
|
err := kvdb.Update(d, func(tx kvdb.RwTx) error {
|
|
|
|
invoices, err := tx.CreateTopLevelBucket(invoiceBucket)
|
2016-09-17 03:23:37 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-25 06:57:30 +03:00
|
|
|
invoiceIndex, err := invoices.CreateBucketIfNotExists(
|
|
|
|
invoiceIndexBucket,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
settleIndex, err := invoices.CreateBucketIfNotExists(
|
|
|
|
settleIndexBucket,
|
|
|
|
)
|
2016-09-17 03:23:37 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-22 01:37:39 +03:00
|
|
|
payAddrIndex := tx.ReadBucket(payAddrIndexBucket)
|
2016-09-17 03:23:37 +03:00
|
|
|
|
2020-05-22 01:37:10 +03:00
|
|
|
// Retrieve the invoice number for this invoice using the
|
|
|
|
// provided invoice reference.
|
|
|
|
invoiceNum, err := fetchInvoiceNumByRef(
|
2020-05-22 01:37:39 +03:00
|
|
|
invoiceIndex, payAddrIndex, ref,
|
2020-05-22 01:37:10 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-09-17 03:23:37 +03:00
|
|
|
|
2020-05-22 01:37:10 +03:00
|
|
|
}
|
|
|
|
payHash := ref.PayHash()
|
2019-08-30 14:10:29 +03:00
|
|
|
updatedInvoice, err = d.updateInvoice(
|
2020-05-22 01:37:10 +03:00
|
|
|
payHash, invoices, settleIndex, invoiceNum,
|
2019-08-21 16:24:21 +03:00
|
|
|
callback,
|
2019-02-11 14:01:05 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return updatedInvoice, err
|
|
|
|
}
|
|
|
|
|
2018-04-25 06:59:53 +03:00
|
|
|
// InvoicesSettledSince can be used by callers to catch up any settled invoices
|
|
|
|
// they missed within the settled invoice time series. We'll return all known
|
|
|
|
// settled invoice that have a settle index higher than the passed
|
|
|
|
// sinceSettleIndex.
|
|
|
|
//
|
|
|
|
// NOTE: The index starts from 1, as a result. We enforce that specifying a
|
|
|
|
// value below the starting index value is a noop.
|
|
|
|
func (d *DB) InvoicesSettledSince(sinceSettleIndex uint64) ([]Invoice, error) {
|
|
|
|
var settledInvoices []Invoice
|
|
|
|
|
|
|
|
// If an index of zero was specified, then in order to maintain
|
|
|
|
// backwards compat, we won't send out any new invoices.
|
|
|
|
if sinceSettleIndex == 0 {
|
|
|
|
return settledInvoices, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var startIndex [8]byte
|
|
|
|
byteOrder.PutUint64(startIndex[:], sinceSettleIndex)
|
|
|
|
|
2020-05-07 01:45:50 +03:00
|
|
|
err := kvdb.View(d, func(tx kvdb.RTx) error {
|
2019-12-13 05:22:19 +03:00
|
|
|
invoices := tx.ReadBucket(invoiceBucket)
|
2018-04-25 06:59:53 +03:00
|
|
|
if invoices == nil {
|
2020-05-20 06:31:26 +03:00
|
|
|
return nil
|
2018-04-25 06:59:53 +03:00
|
|
|
}
|
|
|
|
|
2019-12-13 05:22:19 +03:00
|
|
|
settleIndex := invoices.NestedReadBucket(settleIndexBucket)
|
2018-04-25 06:59:53 +03:00
|
|
|
if settleIndex == nil {
|
2020-05-20 06:31:26 +03:00
|
|
|
return nil
|
2018-04-25 06:59:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We'll now run through each entry in the add index starting
|
|
|
|
// at our starting index. We'll continue until we reach the
|
|
|
|
// very end of the current key space.
|
2019-12-13 05:22:19 +03:00
|
|
|
invoiceCursor := settleIndex.ReadCursor()
|
2018-04-25 06:59:53 +03:00
|
|
|
|
|
|
|
// We'll seek to the starting index, then manually advance the
|
|
|
|
// cursor in order to skip the entry with the since add index.
|
|
|
|
invoiceCursor.Seek(startIndex[:])
|
|
|
|
seqNo, invoiceKey := invoiceCursor.Next()
|
|
|
|
|
|
|
|
for ; seqNo != nil && bytes.Compare(seqNo, startIndex[:]) > 0; seqNo, invoiceKey = invoiceCursor.Next() {
|
|
|
|
|
|
|
|
// For each key found, we'll look up the actual
|
|
|
|
// invoice, then accumulate it into our return value.
|
|
|
|
invoice, err := fetchInvoice(invoiceKey, invoices)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
settledInvoices = append(settledInvoices, invoice)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return settledInvoices, nil
|
|
|
|
}
|
|
|
|
|
2020-05-22 01:37:39 +03:00
|
|
|
func putInvoice(invoices, invoiceIndex, payAddrIndex, addIndex kvdb.RwBucket,
|
2018-10-05 11:14:56 +03:00
|
|
|
i *Invoice, invoiceNum uint32, paymentHash lntypes.Hash) (
|
|
|
|
uint64, error) {
|
2016-09-17 03:23:37 +03:00
|
|
|
|
|
|
|
// Create the invoice key which is just the big-endian representation
|
|
|
|
// of the invoice number.
|
|
|
|
var invoiceKey [4]byte
|
|
|
|
byteOrder.PutUint32(invoiceKey[:], invoiceNum)
|
|
|
|
|
|
|
|
// Increment the num invoice counter index so the next invoice bares
|
|
|
|
// the proper ID.
|
|
|
|
var scratch [4]byte
|
|
|
|
invoiceCounter := invoiceNum + 1
|
|
|
|
byteOrder.PutUint32(scratch[:], invoiceCounter)
|
2016-09-19 21:46:48 +03:00
|
|
|
if err := invoiceIndex.Put(numInvoicesKey, scratch[:]); err != nil {
|
2018-06-30 04:05:51 +03:00
|
|
|
return 0, err
|
2016-09-17 03:23:37 +03:00
|
|
|
}
|
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
// Add the payment hash to the invoice index. This will let us quickly
|
2016-09-17 03:23:37 +03:00
|
|
|
// identify if we can settle an incoming payment, and also to possibly
|
|
|
|
// allow a single invoice to have multiple payment installations.
|
2018-04-25 06:57:30 +03:00
|
|
|
err := invoiceIndex.Put(paymentHash[:], invoiceKey[:])
|
|
|
|
if err != nil {
|
2018-06-30 04:05:51 +03:00
|
|
|
return 0, err
|
2018-04-25 06:57:30 +03:00
|
|
|
}
|
2020-05-22 01:37:39 +03:00
|
|
|
err = payAddrIndex.Put(i.Terms.PaymentAddr[:], invoiceKey[:])
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2018-04-25 06:57:30 +03:00
|
|
|
|
|
|
|
// Next, we'll obtain the next add invoice index (sequence
|
|
|
|
// number), so we can properly place this invoice within this
|
|
|
|
// event stream.
|
|
|
|
nextAddSeqNo, err := addIndex.NextSequence()
|
|
|
|
if err != nil {
|
2018-06-30 04:05:51 +03:00
|
|
|
return 0, err
|
2018-04-25 06:57:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// With the next sequence obtained, we'll updating the event series in
|
|
|
|
// the add index bucket to map this current add counter to the index of
|
|
|
|
// this new invoice.
|
|
|
|
var seqNoBytes [8]byte
|
|
|
|
byteOrder.PutUint64(seqNoBytes[:], nextAddSeqNo)
|
|
|
|
if err := addIndex.Put(seqNoBytes[:], invoiceKey[:]); err != nil {
|
2018-06-30 04:05:51 +03:00
|
|
|
return 0, err
|
2016-09-17 03:23:37 +03:00
|
|
|
}
|
|
|
|
|
2018-04-25 06:57:30 +03:00
|
|
|
i.AddIndex = nextAddSeqNo
|
|
|
|
|
2016-09-17 03:23:37 +03:00
|
|
|
// Finally, serialize the invoice itself to be written to the disk.
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := serializeInvoice(&buf, i); err != nil {
|
2019-08-14 18:48:34 +03:00
|
|
|
return 0, err
|
2018-06-30 04:05:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := invoices.Put(invoiceKey[:], buf.Bytes()); err != nil {
|
|
|
|
return 0, err
|
2016-09-17 03:23:37 +03:00
|
|
|
}
|
|
|
|
|
2018-06-30 04:05:51 +03:00
|
|
|
return nextAddSeqNo, nil
|
2016-09-17 03:23:37 +03:00
|
|
|
}
|
|
|
|
|
2019-08-08 17:58:28 +03:00
|
|
|
// serializeInvoice serializes an invoice to a writer.
|
|
|
|
//
|
|
|
|
// Note: this function is in use for a migration. Before making changes that
|
|
|
|
// would modify the on disk format, make a copy of the original code and store
|
|
|
|
// it with the migration.
|
2016-09-17 03:23:37 +03:00
|
|
|
func serializeInvoice(w io.Writer, i *Invoice) error {
|
2019-11-22 13:24:28 +03:00
|
|
|
creationDateBytes, err := i.CreationDate.MarshalBinary()
|
2016-09-17 03:23:37 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-12-05 09:07:21 +03:00
|
|
|
|
2019-11-22 13:24:28 +03:00
|
|
|
settleDateBytes, err := i.SettleDate.MarshalBinary()
|
2017-12-05 09:07:21 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-22 13:24:28 +03:00
|
|
|
var fb bytes.Buffer
|
|
|
|
err = i.Terms.Features.EncodeBase256(&fb)
|
|
|
|
if err != nil {
|
2016-09-17 03:23:37 +03:00
|
|
|
return err
|
|
|
|
}
|
2019-11-22 13:24:28 +03:00
|
|
|
featureBytes := fb.Bytes()
|
|
|
|
|
2020-04-08 14:47:10 +03:00
|
|
|
preimage := [32]byte(unknownPreimage)
|
|
|
|
if i.Terms.PaymentPreimage != nil {
|
|
|
|
preimage = *i.Terms.PaymentPreimage
|
|
|
|
if preimage == unknownPreimage {
|
|
|
|
return errors.New("cannot use all-zeroes preimage")
|
|
|
|
}
|
|
|
|
}
|
2019-11-22 13:24:28 +03:00
|
|
|
value := uint64(i.Terms.Value)
|
2019-11-22 13:25:02 +03:00
|
|
|
cltvDelta := uint32(i.Terms.FinalCltvDelta)
|
|
|
|
expiry := uint64(i.Terms.Expiry)
|
2019-11-22 13:24:28 +03:00
|
|
|
|
|
|
|
amtPaid := uint64(i.AmtPaid)
|
2019-11-22 13:25:02 +03:00
|
|
|
state := uint8(i.State)
|
2019-11-22 13:24:28 +03:00
|
|
|
|
2020-04-08 14:47:10 +03:00
|
|
|
var hodlInvoice uint8
|
|
|
|
if i.HodlInvoice {
|
|
|
|
hodlInvoice = 1
|
|
|
|
}
|
|
|
|
|
2019-11-22 13:24:28 +03:00
|
|
|
tlvStream, err := tlv.NewStream(
|
|
|
|
// Memo and payreq.
|
|
|
|
tlv.MakePrimitiveRecord(memoType, &i.Memo),
|
|
|
|
tlv.MakePrimitiveRecord(payReqType, &i.PaymentRequest),
|
|
|
|
|
|
|
|
// Add/settle metadata.
|
|
|
|
tlv.MakePrimitiveRecord(createTimeType, &creationDateBytes),
|
|
|
|
tlv.MakePrimitiveRecord(settleTimeType, &settleDateBytes),
|
|
|
|
tlv.MakePrimitiveRecord(addIndexType, &i.AddIndex),
|
|
|
|
tlv.MakePrimitiveRecord(settleIndexType, &i.SettleIndex),
|
|
|
|
|
|
|
|
// Terms.
|
|
|
|
tlv.MakePrimitiveRecord(preimageType, &preimage),
|
|
|
|
tlv.MakePrimitiveRecord(valueType, &value),
|
|
|
|
tlv.MakePrimitiveRecord(cltvDeltaType, &cltvDelta),
|
|
|
|
tlv.MakePrimitiveRecord(expiryType, &expiry),
|
|
|
|
tlv.MakePrimitiveRecord(paymentAddrType, &i.Terms.PaymentAddr),
|
|
|
|
tlv.MakePrimitiveRecord(featuresType, &featureBytes),
|
|
|
|
|
|
|
|
// Invoice state.
|
|
|
|
tlv.MakePrimitiveRecord(invStateType, &state),
|
|
|
|
tlv.MakePrimitiveRecord(amtPaidType, &amtPaid),
|
2020-04-08 14:47:10 +03:00
|
|
|
|
|
|
|
tlv.MakePrimitiveRecord(hodlInvoiceType, &hodlInvoice),
|
2019-11-22 13:24:28 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
2016-09-17 03:23:37 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-22 13:24:28 +03:00
|
|
|
var b bytes.Buffer
|
|
|
|
if err = tlvStream.Encode(&b); err != nil {
|
2016-09-17 03:23:37 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-22 13:24:28 +03:00
|
|
|
err = binary.Write(w, byteOrder, uint64(b.Len()))
|
|
|
|
if err != nil {
|
2018-04-25 06:50:56 +03:00
|
|
|
return err
|
|
|
|
}
|
2018-04-25 06:57:30 +03:00
|
|
|
|
2019-11-22 13:24:28 +03:00
|
|
|
if _, err = w.Write(b.Bytes()); err != nil {
|
2019-08-08 17:58:28 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-22 13:24:28 +03:00
|
|
|
return serializeHtlcs(w, i.Htlcs)
|
2019-08-08 17:58:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// serializeHtlcs serializes a map containing circuit keys and invoice htlcs to
|
|
|
|
// a writer.
|
|
|
|
func serializeHtlcs(w io.Writer, htlcs map[CircuitKey]*InvoiceHTLC) error {
|
|
|
|
for key, htlc := range htlcs {
|
|
|
|
// Encode the htlc in a tlv stream.
|
|
|
|
chanID := key.ChanID.ToUint64()
|
|
|
|
amt := uint64(htlc.Amt)
|
2019-09-03 13:23:39 +03:00
|
|
|
mppTotalAmt := uint64(htlc.MppTotalAmt)
|
2019-08-08 17:58:28 +03:00
|
|
|
acceptTime := uint64(htlc.AcceptTime.UnixNano())
|
|
|
|
resolveTime := uint64(htlc.ResolveTime.UnixNano())
|
|
|
|
state := uint8(htlc.State)
|
|
|
|
|
2019-11-19 15:33:05 +03:00
|
|
|
var records []tlv.Record
|
|
|
|
records = append(records,
|
2019-08-08 17:58:28 +03:00
|
|
|
tlv.MakePrimitiveRecord(chanIDType, &chanID),
|
|
|
|
tlv.MakePrimitiveRecord(htlcIDType, &key.HtlcID),
|
|
|
|
tlv.MakePrimitiveRecord(amtType, &amt),
|
|
|
|
tlv.MakePrimitiveRecord(
|
|
|
|
acceptHeightType, &htlc.AcceptHeight,
|
|
|
|
),
|
|
|
|
tlv.MakePrimitiveRecord(acceptTimeType, &acceptTime),
|
|
|
|
tlv.MakePrimitiveRecord(resolveTimeType, &resolveTime),
|
|
|
|
tlv.MakePrimitiveRecord(expiryHeightType, &htlc.Expiry),
|
2019-11-22 13:24:28 +03:00
|
|
|
tlv.MakePrimitiveRecord(htlcStateType, &state),
|
2019-09-03 13:23:39 +03:00
|
|
|
tlv.MakePrimitiveRecord(mppTotalAmtType, &mppTotalAmt),
|
2019-08-08 17:58:28 +03:00
|
|
|
)
|
2019-11-19 15:33:05 +03:00
|
|
|
|
|
|
|
// Convert the custom records to tlv.Record types that are ready
|
|
|
|
// for serialization.
|
|
|
|
customRecords := tlv.MapToRecords(htlc.CustomRecords)
|
|
|
|
|
|
|
|
// Append the custom records. Their ids are in the experimental
|
|
|
|
// range and sorted, so there is no need to sort again.
|
|
|
|
records = append(records, customRecords...)
|
|
|
|
|
|
|
|
tlvStream, err := tlv.NewStream(records...)
|
2019-08-08 17:58:28 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
if err := tlvStream.Encode(&b); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the length of the tlv stream followed by the stream
|
|
|
|
// bytes.
|
|
|
|
err = binary.Write(w, byteOrder, uint64(b.Len()))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := w.Write(b.Bytes()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-29 22:40:42 +03:00
|
|
|
return nil
|
2016-09-17 03:23:37 +03:00
|
|
|
}
|
|
|
|
|
2020-05-07 01:48:00 +03:00
|
|
|
func fetchInvoice(invoiceNum []byte, invoices kvdb.RBucket) (Invoice, error) {
|
2016-09-17 03:23:37 +03:00
|
|
|
invoiceBytes := invoices.Get(invoiceNum)
|
|
|
|
if invoiceBytes == nil {
|
2018-04-25 06:57:30 +03:00
|
|
|
return Invoice{}, ErrInvoiceNotFound
|
2016-09-17 03:23:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
invoiceReader := bytes.NewReader(invoiceBytes)
|
|
|
|
|
|
|
|
return deserializeInvoice(invoiceReader)
|
|
|
|
}
|
|
|
|
|
2018-04-25 06:57:30 +03:00
|
|
|
func deserializeInvoice(r io.Reader) (Invoice, error) {
|
2019-11-22 13:24:28 +03:00
|
|
|
var (
|
2020-04-08 14:47:10 +03:00
|
|
|
preimageBytes [32]byte
|
|
|
|
value uint64
|
|
|
|
cltvDelta uint32
|
|
|
|
expiry uint64
|
|
|
|
amtPaid uint64
|
|
|
|
state uint8
|
|
|
|
hodlInvoice uint8
|
2019-11-22 13:24:28 +03:00
|
|
|
|
|
|
|
creationDateBytes []byte
|
|
|
|
settleDateBytes []byte
|
|
|
|
featureBytes []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
var i Invoice
|
|
|
|
tlvStream, err := tlv.NewStream(
|
|
|
|
// Memo and payreq.
|
|
|
|
tlv.MakePrimitiveRecord(memoType, &i.Memo),
|
|
|
|
tlv.MakePrimitiveRecord(payReqType, &i.PaymentRequest),
|
|
|
|
|
|
|
|
// Add/settle metadata.
|
|
|
|
tlv.MakePrimitiveRecord(createTimeType, &creationDateBytes),
|
|
|
|
tlv.MakePrimitiveRecord(settleTimeType, &settleDateBytes),
|
|
|
|
tlv.MakePrimitiveRecord(addIndexType, &i.AddIndex),
|
|
|
|
tlv.MakePrimitiveRecord(settleIndexType, &i.SettleIndex),
|
|
|
|
|
|
|
|
// Terms.
|
2020-04-08 14:47:10 +03:00
|
|
|
tlv.MakePrimitiveRecord(preimageType, &preimageBytes),
|
2019-11-22 13:24:28 +03:00
|
|
|
tlv.MakePrimitiveRecord(valueType, &value),
|
|
|
|
tlv.MakePrimitiveRecord(cltvDeltaType, &cltvDelta),
|
|
|
|
tlv.MakePrimitiveRecord(expiryType, &expiry),
|
|
|
|
tlv.MakePrimitiveRecord(paymentAddrType, &i.Terms.PaymentAddr),
|
|
|
|
tlv.MakePrimitiveRecord(featuresType, &featureBytes),
|
|
|
|
|
|
|
|
// Invoice state.
|
|
|
|
tlv.MakePrimitiveRecord(invStateType, &state),
|
|
|
|
tlv.MakePrimitiveRecord(amtPaidType, &amtPaid),
|
2020-04-08 14:47:10 +03:00
|
|
|
|
|
|
|
tlv.MakePrimitiveRecord(hodlInvoiceType, &hodlInvoice),
|
2019-11-22 13:24:28 +03:00
|
|
|
)
|
2016-09-24 01:15:22 +03:00
|
|
|
if err != nil {
|
2019-11-22 13:24:28 +03:00
|
|
|
return i, err
|
2017-09-05 18:59:52 +03:00
|
|
|
}
|
|
|
|
|
2019-11-22 13:24:28 +03:00
|
|
|
var bodyLen int64
|
|
|
|
err = binary.Read(r, byteOrder, &bodyLen)
|
2017-09-05 18:59:52 +03:00
|
|
|
if err != nil {
|
2019-11-22 13:24:28 +03:00
|
|
|
return i, err
|
2016-09-17 03:23:37 +03:00
|
|
|
}
|
|
|
|
|
2019-11-22 13:24:28 +03:00
|
|
|
lr := io.LimitReader(r, bodyLen)
|
|
|
|
if err = tlvStream.Decode(lr); err != nil {
|
|
|
|
return i, err
|
2019-08-08 17:58:28 +03:00
|
|
|
}
|
|
|
|
|
2020-04-08 14:47:10 +03:00
|
|
|
preimage := lntypes.Preimage(preimageBytes)
|
|
|
|
if preimage != unknownPreimage {
|
|
|
|
i.Terms.PaymentPreimage = &preimage
|
|
|
|
}
|
|
|
|
|
2019-11-22 13:24:28 +03:00
|
|
|
i.Terms.Value = lnwire.MilliSatoshi(value)
|
2019-11-22 13:25:02 +03:00
|
|
|
i.Terms.FinalCltvDelta = int32(cltvDelta)
|
|
|
|
i.Terms.Expiry = time.Duration(expiry)
|
2019-11-22 13:24:28 +03:00
|
|
|
i.AmtPaid = lnwire.MilliSatoshi(amtPaid)
|
2019-11-22 13:25:02 +03:00
|
|
|
i.State = ContractState(state)
|
2019-08-08 17:58:28 +03:00
|
|
|
|
2020-04-08 14:47:10 +03:00
|
|
|
if hodlInvoice != 0 {
|
|
|
|
i.HodlInvoice = true
|
|
|
|
}
|
|
|
|
|
2019-11-22 13:24:28 +03:00
|
|
|
err = i.CreationDate.UnmarshalBinary(creationDateBytes)
|
2016-09-17 03:23:37 +03:00
|
|
|
if err != nil {
|
2019-11-22 13:24:28 +03:00
|
|
|
return i, err
|
2016-09-17 03:23:37 +03:00
|
|
|
}
|
|
|
|
|
2019-11-22 13:24:28 +03:00
|
|
|
err = i.SettleDate.UnmarshalBinary(settleDateBytes)
|
2017-12-05 09:07:21 +03:00
|
|
|
if err != nil {
|
2019-11-22 13:24:28 +03:00
|
|
|
return i, err
|
2017-12-05 09:07:21 +03:00
|
|
|
}
|
|
|
|
|
2019-11-22 13:24:28 +03:00
|
|
|
rawFeatures := lnwire.NewRawFeatureVector()
|
|
|
|
err = rawFeatures.DecodeBase256(
|
|
|
|
bytes.NewReader(featureBytes), len(featureBytes),
|
|
|
|
)
|
2019-08-08 17:58:28 +03:00
|
|
|
if err != nil {
|
2019-11-22 13:24:28 +03:00
|
|
|
return i, err
|
2019-08-08 17:58:28 +03:00
|
|
|
}
|
|
|
|
|
2019-11-22 13:24:28 +03:00
|
|
|
i.Terms.Features = lnwire.NewFeatureVector(
|
|
|
|
rawFeatures, lnwire.Features,
|
|
|
|
)
|
|
|
|
|
|
|
|
i.Htlcs, err = deserializeHtlcs(r)
|
|
|
|
return i, err
|
2016-09-17 03:23:37 +03:00
|
|
|
}
|
|
|
|
|
2019-08-08 17:58:28 +03:00
|
|
|
// deserializeHtlcs reads a list of invoice htlcs from a reader and returns it
|
|
|
|
// as a map.
|
|
|
|
func deserializeHtlcs(r io.Reader) (map[CircuitKey]*InvoiceHTLC, error) {
|
2020-04-09 20:03:31 +03:00
|
|
|
htlcs := make(map[CircuitKey]*InvoiceHTLC)
|
2019-08-08 17:58:28 +03:00
|
|
|
|
|
|
|
for {
|
|
|
|
// Read the length of the tlv stream for this htlc.
|
2019-11-22 13:24:44 +03:00
|
|
|
var streamLen int64
|
2019-08-08 17:58:28 +03:00
|
|
|
if err := binary.Read(r, byteOrder, &streamLen); err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-11-22 13:24:44 +03:00
|
|
|
// Limit the reader so that it stops at the end of this htlc's
|
|
|
|
// stream.
|
|
|
|
htlcReader := io.LimitReader(r, streamLen)
|
2019-08-08 17:58:28 +03:00
|
|
|
|
|
|
|
// Decode the contents into the htlc fields.
|
|
|
|
var (
|
|
|
|
htlc InvoiceHTLC
|
|
|
|
key CircuitKey
|
|
|
|
chanID uint64
|
|
|
|
state uint8
|
|
|
|
acceptTime, resolveTime uint64
|
2019-09-03 13:23:39 +03:00
|
|
|
amt, mppTotalAmt uint64
|
2019-08-08 17:58:28 +03:00
|
|
|
)
|
|
|
|
tlvStream, err := tlv.NewStream(
|
|
|
|
tlv.MakePrimitiveRecord(chanIDType, &chanID),
|
|
|
|
tlv.MakePrimitiveRecord(htlcIDType, &key.HtlcID),
|
|
|
|
tlv.MakePrimitiveRecord(amtType, &amt),
|
|
|
|
tlv.MakePrimitiveRecord(
|
|
|
|
acceptHeightType, &htlc.AcceptHeight,
|
|
|
|
),
|
|
|
|
tlv.MakePrimitiveRecord(acceptTimeType, &acceptTime),
|
|
|
|
tlv.MakePrimitiveRecord(resolveTimeType, &resolveTime),
|
|
|
|
tlv.MakePrimitiveRecord(expiryHeightType, &htlc.Expiry),
|
2019-11-22 13:24:28 +03:00
|
|
|
tlv.MakePrimitiveRecord(htlcStateType, &state),
|
2019-09-03 13:23:39 +03:00
|
|
|
tlv.MakePrimitiveRecord(mppTotalAmtType, &mppTotalAmt),
|
2019-08-08 17:58:28 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-11-19 15:33:05 +03:00
|
|
|
parsedTypes, err := tlvStream.DecodeWithParsedTypes(htlcReader)
|
|
|
|
if err != nil {
|
2019-08-08 17:58:28 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
key.ChanID = lnwire.NewShortChanIDFromInt(chanID)
|
|
|
|
htlc.AcceptTime = time.Unix(0, int64(acceptTime))
|
|
|
|
htlc.ResolveTime = time.Unix(0, int64(resolveTime))
|
|
|
|
htlc.State = HtlcState(state)
|
|
|
|
htlc.Amt = lnwire.MilliSatoshi(amt)
|
2019-09-03 13:23:39 +03:00
|
|
|
htlc.MppTotalAmt = lnwire.MilliSatoshi(mppTotalAmt)
|
2019-08-08 17:58:28 +03:00
|
|
|
|
2019-11-19 15:33:05 +03:00
|
|
|
// Reconstruct the custom records fields from the parsed types
|
|
|
|
// map return from the tlv parser.
|
|
|
|
htlc.CustomRecords = hop.NewCustomRecords(parsedTypes)
|
|
|
|
|
2019-08-08 17:58:28 +03:00
|
|
|
htlcs[key] = &htlc
|
|
|
|
}
|
|
|
|
|
|
|
|
return htlcs, nil
|
|
|
|
}
|
|
|
|
|
2019-08-09 14:40:34 +03:00
|
|
|
// copySlice allocates a new slice and copies the source into it.
|
|
|
|
func copySlice(src []byte) []byte {
|
|
|
|
dest := make([]byte, len(src))
|
|
|
|
copy(dest, src)
|
|
|
|
return dest
|
|
|
|
}
|
|
|
|
|
2020-01-03 17:53:24 +03:00
|
|
|
// copyInvoiceHTLC makes a deep copy of the supplied invoice HTLC.
|
|
|
|
func copyInvoiceHTLC(src *InvoiceHTLC) *InvoiceHTLC {
|
|
|
|
result := *src
|
|
|
|
|
|
|
|
// Make a copy of the CustomSet map.
|
|
|
|
result.CustomRecords = make(record.CustomSet)
|
|
|
|
for k, v := range src.CustomRecords {
|
|
|
|
result.CustomRecords[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
2019-08-09 14:40:34 +03:00
|
|
|
// copyInvoice makes a deep copy of the supplied invoice.
|
|
|
|
func copyInvoice(src *Invoice) *Invoice {
|
|
|
|
dest := Invoice{
|
|
|
|
Memo: copySlice(src.Memo),
|
|
|
|
PaymentRequest: copySlice(src.PaymentRequest),
|
|
|
|
CreationDate: src.CreationDate,
|
|
|
|
SettleDate: src.SettleDate,
|
|
|
|
Terms: src.Terms,
|
|
|
|
AddIndex: src.AddIndex,
|
|
|
|
SettleIndex: src.SettleIndex,
|
2019-11-22 13:25:02 +03:00
|
|
|
State: src.State,
|
2019-08-09 14:40:34 +03:00
|
|
|
AmtPaid: src.AmtPaid,
|
|
|
|
Htlcs: make(
|
|
|
|
map[CircuitKey]*InvoiceHTLC, len(src.Htlcs),
|
|
|
|
),
|
2020-04-08 14:47:10 +03:00
|
|
|
HodlInvoice: src.HodlInvoice,
|
2019-08-09 14:40:34 +03:00
|
|
|
}
|
|
|
|
|
2019-11-22 13:24:28 +03:00
|
|
|
dest.Terms.Features = src.Terms.Features.Clone()
|
|
|
|
|
2020-04-08 14:47:10 +03:00
|
|
|
if src.Terms.PaymentPreimage != nil {
|
|
|
|
preimage := *src.Terms.PaymentPreimage
|
|
|
|
dest.Terms.PaymentPreimage = &preimage
|
|
|
|
}
|
|
|
|
|
2019-08-09 14:40:34 +03:00
|
|
|
for k, v := range src.Htlcs {
|
2020-01-03 17:53:24 +03:00
|
|
|
dest.Htlcs[k] = copyInvoiceHTLC(v)
|
2019-08-09 14:40:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return &dest
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateInvoice fetches the invoice, obtains the update descriptor from the
|
|
|
|
// callback and applies the updates in a single db transaction.
|
2019-12-13 05:22:19 +03:00
|
|
|
func (d *DB) updateInvoice(hash lntypes.Hash, invoices, settleIndex kvdb.RwBucket,
|
2019-08-21 16:24:21 +03:00
|
|
|
invoiceNum []byte, callback InvoiceUpdateCallback) (*Invoice, error) {
|
2018-04-25 06:50:56 +03:00
|
|
|
|
2016-09-17 03:23:37 +03:00
|
|
|
invoice, err := fetchInvoice(invoiceNum, invoices)
|
|
|
|
if err != nil {
|
2018-06-29 07:43:18 +03:00
|
|
|
return nil, err
|
2016-09-17 03:23:37 +03:00
|
|
|
}
|
|
|
|
|
2019-08-09 14:40:34 +03:00
|
|
|
// Create deep copy to prevent any accidental modification in the
|
|
|
|
// callback.
|
2019-11-22 13:24:28 +03:00
|
|
|
invoiceCopy := copyInvoice(&invoice)
|
2019-08-09 14:40:34 +03:00
|
|
|
|
|
|
|
// Call the callback and obtain the update descriptor.
|
2019-11-22 13:24:28 +03:00
|
|
|
update, err := callback(invoiceCopy)
|
2019-08-09 14:40:34 +03:00
|
|
|
if err != nil {
|
2019-04-16 13:11:20 +03:00
|
|
|
return &invoice, err
|
|
|
|
}
|
|
|
|
|
2019-11-27 12:05:14 +03:00
|
|
|
// If there is nothing to update, return early.
|
|
|
|
if update == nil {
|
|
|
|
return &invoice, nil
|
|
|
|
}
|
|
|
|
|
2020-01-20 12:57:34 +03:00
|
|
|
now := d.clock.Now()
|
2019-08-09 16:09:57 +03:00
|
|
|
|
2019-11-27 15:20:14 +03:00
|
|
|
// Update invoice state if the update descriptor indicates an invoice
|
|
|
|
// state change.
|
|
|
|
if update.State != nil {
|
|
|
|
err := updateInvoiceState(&invoice, hash, *update.State)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-11-27 16:19:15 +03:00
|
|
|
}
|
2019-08-09 16:09:57 +03:00
|
|
|
|
2019-11-27 15:20:14 +03:00
|
|
|
if update.State.NewState == ContractSettled {
|
|
|
|
err := setSettleMetaFields(
|
|
|
|
settleIndex, invoiceNum, &invoice, now,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2019-11-27 16:19:15 +03:00
|
|
|
}
|
2019-08-09 16:09:57 +03:00
|
|
|
|
2019-11-27 16:19:15 +03:00
|
|
|
// Process add actions from update descriptor.
|
|
|
|
for key, htlcUpdate := range update.AddHtlcs {
|
2019-11-27 15:20:14 +03:00
|
|
|
if _, exists := invoice.Htlcs[key]; exists {
|
|
|
|
return nil, fmt.Errorf("duplicate add of htlc %v", key)
|
2019-08-09 16:09:57 +03:00
|
|
|
}
|
2019-11-19 15:33:05 +03:00
|
|
|
|
|
|
|
// Force caller to supply htlc without custom records in a
|
|
|
|
// consistent way.
|
|
|
|
if htlcUpdate.CustomRecords == nil {
|
|
|
|
return nil, errors.New("nil custom records map")
|
|
|
|
}
|
|
|
|
|
2019-11-27 15:20:14 +03:00
|
|
|
htlc := &InvoiceHTLC{
|
2019-11-19 15:33:05 +03:00
|
|
|
Amt: htlcUpdate.Amt,
|
2019-09-03 13:23:39 +03:00
|
|
|
MppTotalAmt: htlcUpdate.MppTotalAmt,
|
2019-11-19 15:33:05 +03:00
|
|
|
Expiry: htlcUpdate.Expiry,
|
|
|
|
AcceptHeight: uint32(htlcUpdate.AcceptHeight),
|
|
|
|
AcceptTime: now,
|
|
|
|
State: HtlcStateAccepted,
|
|
|
|
CustomRecords: htlcUpdate.CustomRecords,
|
2019-08-09 16:09:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
invoice.Htlcs[key] = htlc
|
|
|
|
}
|
2019-08-09 14:40:34 +03:00
|
|
|
|
2019-11-27 15:20:14 +03:00
|
|
|
// Align htlc states with invoice state and recalculate amount paid.
|
|
|
|
var (
|
|
|
|
amtPaid lnwire.MilliSatoshi
|
|
|
|
cancelHtlcs = update.CancelHtlcs
|
|
|
|
)
|
|
|
|
for key, htlc := range invoice.Htlcs {
|
|
|
|
// Check whether this htlc needs to be canceled. If it does,
|
|
|
|
// update the htlc state to Canceled.
|
|
|
|
_, cancel := cancelHtlcs[key]
|
|
|
|
if cancel {
|
|
|
|
// Consistency check to verify that there is no overlap
|
|
|
|
// between the add and cancel sets.
|
|
|
|
if _, added := update.AddHtlcs[key]; added {
|
|
|
|
return nil, fmt.Errorf("added htlc %v canceled",
|
|
|
|
key)
|
|
|
|
}
|
2019-08-21 16:24:21 +03:00
|
|
|
|
2019-11-27 15:20:14 +03:00
|
|
|
err := cancelSingleHtlc(now, htlc, invoice.State)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-08-09 16:09:57 +03:00
|
|
|
}
|
|
|
|
|
2019-11-27 15:20:14 +03:00
|
|
|
// Delete processed cancel action, so that we can check
|
|
|
|
// later that there are no actions left.
|
|
|
|
delete(cancelHtlcs, key)
|
|
|
|
|
|
|
|
continue
|
2019-08-09 16:09:57 +03:00
|
|
|
}
|
|
|
|
|
2019-11-27 15:20:14 +03:00
|
|
|
// The invoice state may have changed and this could have
|
|
|
|
// implications for the states of the individual htlcs. Align
|
|
|
|
// the htlc state with the current invoice state.
|
|
|
|
err := updateHtlc(now, htlc, invoice.State)
|
2019-02-11 14:01:05 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-27 15:20:14 +03:00
|
|
|
|
|
|
|
// Update the running amount paid to this invoice. We don't
|
|
|
|
// include accepted htlcs when the invoice is still open.
|
|
|
|
if invoice.State != ContractOpen &&
|
|
|
|
(htlc.State == HtlcStateAccepted ||
|
|
|
|
htlc.State == HtlcStateSettled) {
|
|
|
|
|
|
|
|
amtPaid += htlc.Amt
|
|
|
|
}
|
2019-02-11 14:01:05 +03:00
|
|
|
}
|
2019-11-27 15:20:14 +03:00
|
|
|
invoice.AmtPaid = amtPaid
|
2019-02-11 14:01:05 +03:00
|
|
|
|
2019-11-27 15:20:14 +03:00
|
|
|
// Verify that we didn't get an action for htlcs that are not present on
|
|
|
|
// the invoice.
|
|
|
|
if len(cancelHtlcs) > 0 {
|
|
|
|
return nil, errors.New("cancel action on non-existent htlc(s)")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reserialize and update invoice.
|
2019-02-11 14:01:05 +03:00
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := serializeInvoice(&buf, &invoice); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := invoices.Put(invoiceNum[:], buf.Bytes()); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &invoice, nil
|
|
|
|
}
|
|
|
|
|
2019-11-27 15:20:14 +03:00
|
|
|
// updateInvoiceState validates and processes an invoice state update.
|
|
|
|
func updateInvoiceState(invoice *Invoice, hash lntypes.Hash,
|
|
|
|
update InvoiceStateUpdateDesc) error {
|
|
|
|
|
|
|
|
// Returning to open is never allowed from any state.
|
|
|
|
if update.NewState == ContractOpen {
|
|
|
|
return ErrInvoiceCannotOpen
|
|
|
|
}
|
|
|
|
|
|
|
|
switch invoice.State {
|
|
|
|
|
|
|
|
// Once a contract is accepted, we can only transition to settled or
|
|
|
|
// canceled. Forbid transitioning back into this state. Otherwise this
|
|
|
|
// state is identical to ContractOpen, so we fallthrough to apply the
|
|
|
|
// same checks that we apply to open invoices.
|
|
|
|
case ContractAccepted:
|
|
|
|
if update.NewState == ContractAccepted {
|
|
|
|
return ErrInvoiceCannotAccept
|
|
|
|
}
|
|
|
|
|
|
|
|
fallthrough
|
|
|
|
|
|
|
|
// If a contract is open, permit a state transition to accepted, settled
|
|
|
|
// or canceled. The only restriction is on transitioning to settled
|
|
|
|
// where we ensure the preimage is valid.
|
|
|
|
case ContractOpen:
|
|
|
|
if update.NewState == ContractSettled {
|
|
|
|
// Validate preimage.
|
2020-04-08 14:47:10 +03:00
|
|
|
switch {
|
|
|
|
case update.Preimage != nil:
|
|
|
|
if update.Preimage.Hash() != hash {
|
|
|
|
return ErrInvoicePreimageMismatch
|
|
|
|
}
|
|
|
|
invoice.Terms.PaymentPreimage = update.Preimage
|
|
|
|
|
|
|
|
case invoice.Terms.PaymentPreimage == nil:
|
|
|
|
return errors.New("unknown preimage")
|
2019-11-27 15:20:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Once settled, we are in a terminal state.
|
|
|
|
case ContractSettled:
|
|
|
|
return ErrInvoiceAlreadySettled
|
|
|
|
|
|
|
|
// Once canceled, we are in a terminal state.
|
|
|
|
case ContractCanceled:
|
|
|
|
return ErrInvoiceAlreadyCanceled
|
|
|
|
|
|
|
|
default:
|
|
|
|
return errors.New("unknown state transition")
|
|
|
|
}
|
|
|
|
|
|
|
|
invoice.State = update.NewState
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// cancelSingleHtlc validates cancelation of a single htlc and update its state.
|
|
|
|
func cancelSingleHtlc(resolveTime time.Time, htlc *InvoiceHTLC,
|
|
|
|
invState ContractState) error {
|
|
|
|
|
|
|
|
// It is only possible to cancel individual htlcs on an open invoice.
|
|
|
|
if invState != ContractOpen {
|
|
|
|
return fmt.Errorf("htlc canceled on invoice in "+
|
|
|
|
"state %v", invState)
|
|
|
|
}
|
|
|
|
|
|
|
|
// It is only possible if the htlc is still pending.
|
|
|
|
if htlc.State != HtlcStateAccepted {
|
|
|
|
return fmt.Errorf("htlc canceled in state %v",
|
|
|
|
htlc.State)
|
|
|
|
}
|
|
|
|
|
|
|
|
htlc.State = HtlcStateCanceled
|
|
|
|
htlc.ResolveTime = resolveTime
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateHtlc aligns the state of an htlc with the given invoice state.
|
|
|
|
func updateHtlc(resolveTime time.Time, htlc *InvoiceHTLC,
|
|
|
|
invState ContractState) error {
|
|
|
|
|
|
|
|
switch invState {
|
|
|
|
|
|
|
|
case ContractSettled:
|
|
|
|
if htlc.State == HtlcStateAccepted {
|
|
|
|
htlc.State = HtlcStateSettled
|
|
|
|
htlc.ResolveTime = resolveTime
|
|
|
|
}
|
|
|
|
|
|
|
|
case ContractCanceled:
|
|
|
|
switch htlc.State {
|
|
|
|
|
|
|
|
case HtlcStateAccepted:
|
|
|
|
htlc.State = HtlcStateCanceled
|
|
|
|
htlc.ResolveTime = resolveTime
|
|
|
|
|
|
|
|
case HtlcStateSettled:
|
|
|
|
return fmt.Errorf("cannot have a settled htlc with " +
|
|
|
|
"invoice in state canceled")
|
|
|
|
}
|
|
|
|
|
|
|
|
case ContractOpen, ContractAccepted:
|
|
|
|
if htlc.State == HtlcStateSettled {
|
|
|
|
return fmt.Errorf("cannot have a settled htlc with "+
|
|
|
|
"invoice in state %v", invState)
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return errors.New("unknown state transition")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// setSettleMetaFields updates the metadata associated with settlement of an
|
|
|
|
// invoice.
|
2019-12-13 05:22:19 +03:00
|
|
|
func setSettleMetaFields(settleIndex kvdb.RwBucket, invoiceNum []byte,
|
2019-08-30 14:10:29 +03:00
|
|
|
invoice *Invoice, now time.Time) error {
|
2019-02-11 14:01:05 +03:00
|
|
|
|
2018-04-25 06:57:30 +03:00
|
|
|
// Now that we know the invoice hasn't already been settled, we'll
|
|
|
|
// update the settle index so we can place this settle event in the
|
|
|
|
// proper location within our time series.
|
|
|
|
nextSettleSeqNo, err := settleIndex.NextSequence()
|
|
|
|
if err != nil {
|
2019-02-11 14:01:05 +03:00
|
|
|
return err
|
2018-04-25 06:57:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var seqNoBytes [8]byte
|
|
|
|
byteOrder.PutUint64(seqNoBytes[:], nextSettleSeqNo)
|
|
|
|
if err := settleIndex.Put(seqNoBytes[:], invoiceNum); err != nil {
|
2019-02-11 14:01:05 +03:00
|
|
|
return err
|
2018-04-25 06:57:30 +03:00
|
|
|
}
|
|
|
|
|
2019-08-30 14:10:29 +03:00
|
|
|
invoice.SettleDate = now
|
2018-04-25 06:57:30 +03:00
|
|
|
invoice.SettleIndex = nextSettleSeqNo
|
2016-09-17 03:23:37 +03:00
|
|
|
|
2019-02-11 14:01:05 +03:00
|
|
|
return nil
|
|
|
|
}
|