2016-12-05 14:59:36 +03:00
|
|
|
package channeldb
|
|
|
|
|
|
|
|
import (
|
2016-12-21 12:19:01 +03:00
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
2018-08-12 16:12:37 +03:00
|
|
|
"errors"
|
2016-12-31 03:32:20 +03:00
|
|
|
"io"
|
|
|
|
|
2018-03-11 06:00:57 +03:00
|
|
|
"github.com/coreos/bbolt"
|
2017-08-22 08:49:56 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2016-12-05 14:59:36 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-12-31 03:32:20 +03:00
|
|
|
// paymentBucket is the name of the bucket within the database that
|
|
|
|
// stores all data related to payments.
|
|
|
|
//
|
|
|
|
// Within the payments bucket, each invoice is keyed by its invoice ID
|
|
|
|
// which is a monotonically increasing uint64. BoltDB's sequence
|
|
|
|
// feature is used for generating monotonically increasing id.
|
2016-12-05 14:59:36 +03:00
|
|
|
paymentBucket = []byte("payments")
|
2018-08-12 16:12:37 +03:00
|
|
|
|
|
|
|
// paymentStatusBucket is the name of the bucket within the database that
|
|
|
|
// stores the status of a payment indexed by the payment's preimage.
|
|
|
|
paymentStatusBucket = []byte("payment-status")
|
|
|
|
)
|
|
|
|
|
|
|
|
// PaymentStatus represent current status of payment
|
|
|
|
type PaymentStatus byte
|
|
|
|
|
|
|
|
const (
|
2018-08-11 00:31:47 +03:00
|
|
|
// StatusGrounded is the status where a payment has never been
|
|
|
|
// initiated, or has been initiated and received an intermittent
|
|
|
|
// failure.
|
2018-08-12 16:12:37 +03:00
|
|
|
StatusGrounded PaymentStatus = 0
|
|
|
|
|
2018-08-11 00:31:47 +03:00
|
|
|
// StatusInFlight is the status where a payment has been initiated, but
|
|
|
|
// a response has not been received.
|
2018-08-12 16:12:37 +03:00
|
|
|
StatusInFlight PaymentStatus = 1
|
|
|
|
|
2018-08-11 00:31:47 +03:00
|
|
|
// StatusCompleted is the status where a payment has been initiated and
|
|
|
|
// the payment was completed successfully.
|
2018-08-12 16:12:37 +03:00
|
|
|
StatusCompleted PaymentStatus = 2
|
2016-12-05 14:59:36 +03:00
|
|
|
)
|
|
|
|
|
2018-08-11 00:31:47 +03:00
|
|
|
// Bytes returns status as slice of bytes.
|
2018-08-12 16:12:37 +03:00
|
|
|
func (ps PaymentStatus) Bytes() []byte {
|
|
|
|
return []byte{byte(ps)}
|
|
|
|
}
|
|
|
|
|
2018-08-11 00:31:47 +03:00
|
|
|
// FromBytes sets status from slice of bytes.
|
2018-08-12 16:12:37 +03:00
|
|
|
func (ps *PaymentStatus) FromBytes(status []byte) error {
|
|
|
|
if len(status) != 1 {
|
|
|
|
return errors.New("payment status is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch PaymentStatus(status[0]) {
|
|
|
|
case StatusGrounded, StatusInFlight, StatusCompleted:
|
|
|
|
*ps = PaymentStatus(status[0])
|
|
|
|
default:
|
|
|
|
return errors.New("unknown payment status")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-11 00:31:47 +03:00
|
|
|
// String returns readable representation of payment status.
|
2018-08-12 16:12:37 +03:00
|
|
|
func (ps PaymentStatus) String() string {
|
|
|
|
switch ps {
|
|
|
|
case StatusGrounded:
|
|
|
|
return "Grounded"
|
|
|
|
case StatusInFlight:
|
|
|
|
return "In Flight"
|
|
|
|
case StatusCompleted:
|
|
|
|
return "Completed"
|
|
|
|
default:
|
|
|
|
return "Unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-31 03:32:20 +03:00
|
|
|
// OutgoingPayment represents a successful payment between the daemon and a
|
|
|
|
// remote node. Details such as the total fee paid, and the time of the payment
|
|
|
|
// are stored.
|
2016-12-05 14:59:36 +03:00
|
|
|
type OutgoingPayment struct {
|
|
|
|
Invoice
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2017-08-22 08:49:56 +03:00
|
|
|
// Fee is the total fee paid for the payment in milli-satoshis.
|
|
|
|
Fee lnwire.MilliSatoshi
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2016-12-31 03:32:20 +03:00
|
|
|
// TotalTimeLock is the total cumulative time-lock in the HTLC extended
|
|
|
|
// from the second-to-last hop to the destination.
|
2016-12-21 12:19:01 +03:00
|
|
|
TimeLockLength uint32
|
|
|
|
|
2017-12-18 05:40:05 +03:00
|
|
|
// Path encodes the path the payment took through the network. The path
|
2016-12-31 03:32:20 +03:00
|
|
|
// excludes the outgoing node and consists of the hex-encoded
|
|
|
|
// compressed public key of each of the nodes involved in the payment.
|
|
|
|
Path [][33]byte
|
2016-12-05 14:59:36 +03:00
|
|
|
|
2017-12-14 04:04:18 +03:00
|
|
|
// PaymentPreimage is the preImage of a successful payment. This is used
|
|
|
|
// to calculate the PaymentHash as well as serve as a proof of payment.
|
|
|
|
PaymentPreimage [32]byte
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
|
|
|
|
2016-12-31 03:32:20 +03:00
|
|
|
// AddPayment saves a successful payment to the database. It is assumed that
|
|
|
|
// all payment are sent using unique payment hashes.
|
|
|
|
func (db *DB) AddPayment(payment *OutgoingPayment) error {
|
|
|
|
// Validate the field of the inner voice within the outgoing payment,
|
|
|
|
// these must also adhere to the same constraints as regular invoices.
|
|
|
|
if err := validateInvoice(&payment.Invoice); err != nil {
|
2016-12-05 14:59:36 +03:00
|
|
|
return err
|
|
|
|
}
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2016-12-31 03:32:20 +03:00
|
|
|
// We first serialize the payment before starting the database
|
|
|
|
// transaction so we can avoid creating a DB payment in the case of a
|
|
|
|
// serialization error.
|
|
|
|
var b bytes.Buffer
|
|
|
|
if err := serializeOutgoingPayment(&b, payment); err != nil {
|
2016-12-05 14:59:36 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
paymentBytes := b.Bytes()
|
2016-12-31 03:32:20 +03:00
|
|
|
|
2018-11-30 07:04:21 +03:00
|
|
|
return db.Batch(func(tx *bbolt.Tx) error {
|
2016-12-05 14:59:36 +03:00
|
|
|
payments, err := tx.CreateBucketIfNotExists(paymentBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2016-12-31 03:32:20 +03:00
|
|
|
// Obtain the new unique sequence number for this payment.
|
2017-02-23 22:56:47 +03:00
|
|
|
paymentID, err := payments.NextSequence()
|
2016-12-05 14:59:36 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2016-12-31 03:32:20 +03:00
|
|
|
// We use BigEndian for keys as it orders keys in
|
|
|
|
// ascending order. This allows bucket scans to order payments
|
|
|
|
// in the order in which they were created.
|
2017-02-23 22:56:47 +03:00
|
|
|
paymentIDBytes := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint64(paymentIDBytes, paymentID)
|
2016-12-31 03:32:20 +03:00
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
return payments.Put(paymentIDBytes, paymentBytes)
|
2016-12-05 14:59:36 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// FetchAllPayments returns all outgoing payments in DB.
|
|
|
|
func (db *DB) FetchAllPayments() ([]*OutgoingPayment, error) {
|
|
|
|
var payments []*OutgoingPayment
|
2016-12-31 03:32:20 +03:00
|
|
|
|
2018-11-30 07:04:21 +03:00
|
|
|
err := db.View(func(tx *bbolt.Tx) error {
|
2016-12-05 14:59:36 +03:00
|
|
|
bucket := tx.Bucket(paymentBucket)
|
|
|
|
if bucket == nil {
|
2016-12-21 12:19:01 +03:00
|
|
|
return ErrNoPaymentsCreated
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
2016-12-31 03:32:20 +03:00
|
|
|
|
|
|
|
return bucket.ForEach(func(k, v []byte) error {
|
|
|
|
// If the value is nil, then we ignore it as it may be
|
|
|
|
// a sub-bucket.
|
2016-12-05 14:59:36 +03:00
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2016-12-31 03:32:20 +03:00
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
r := bytes.NewReader(v)
|
|
|
|
payment, err := deserializeOutgoingPayment(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-31 03:32:20 +03:00
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
payments = append(payments, payment)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-31 03:32:20 +03:00
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
return payments, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteAllPayments deletes all payments from DB.
|
|
|
|
func (db *DB) DeleteAllPayments() error {
|
2018-11-30 07:04:21 +03:00
|
|
|
return db.Update(func(tx *bbolt.Tx) error {
|
2016-12-05 14:59:36 +03:00
|
|
|
err := tx.DeleteBucket(paymentBucket)
|
2018-11-30 07:04:21 +03:00
|
|
|
if err != nil && err != bbolt.ErrBucketNotFound {
|
2016-12-05 14:59:36 +03:00
|
|
|
return err
|
|
|
|
}
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
_, err = tx.CreateBucket(paymentBucket)
|
2017-02-23 21:59:50 +03:00
|
|
|
return err
|
2016-12-05 14:59:36 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-08-11 00:31:47 +03:00
|
|
|
// UpdatePaymentStatus sets the payment status for outgoing/finished payments in
|
2018-08-12 16:12:37 +03:00
|
|
|
// local database.
|
|
|
|
func (db *DB) UpdatePaymentStatus(paymentHash [32]byte, status PaymentStatus) error {
|
2018-11-30 07:04:21 +03:00
|
|
|
return db.Batch(func(tx *bbolt.Tx) error {
|
2018-08-21 07:13:06 +03:00
|
|
|
return UpdatePaymentStatusTx(tx, paymentHash, status)
|
2018-08-12 16:12:37 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-08-21 07:13:06 +03:00
|
|
|
// UpdatePaymentStatusTx is a helper method that sets the payment status for
|
|
|
|
// outgoing/finished payments in the local database. This method accepts a
|
|
|
|
// boltdb transaction such that the operation can be composed into other
|
|
|
|
// database transactions.
|
2018-11-30 07:04:21 +03:00
|
|
|
func UpdatePaymentStatusTx(tx *bbolt.Tx,
|
2018-08-21 07:13:06 +03:00
|
|
|
paymentHash [32]byte, status PaymentStatus) error {
|
|
|
|
|
|
|
|
paymentStatuses, err := tx.CreateBucketIfNotExists(paymentStatusBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return paymentStatuses.Put(paymentHash[:], status.Bytes())
|
|
|
|
}
|
|
|
|
|
2018-08-11 00:31:47 +03:00
|
|
|
// FetchPaymentStatus returns the payment status for outgoing payment.
|
|
|
|
// If status of the payment isn't found, it will default to "StatusGrounded".
|
2018-08-12 16:12:37 +03:00
|
|
|
func (db *DB) FetchPaymentStatus(paymentHash [32]byte) (PaymentStatus, error) {
|
2018-08-21 07:13:06 +03:00
|
|
|
var paymentStatus = StatusGrounded
|
2018-11-30 07:04:21 +03:00
|
|
|
err := db.View(func(tx *bbolt.Tx) error {
|
2018-08-21 07:13:06 +03:00
|
|
|
var err error
|
|
|
|
paymentStatus, err = FetchPaymentStatusTx(tx, paymentHash)
|
|
|
|
return err
|
2018-08-12 16:12:37 +03:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return StatusGrounded, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return paymentStatus, nil
|
|
|
|
}
|
|
|
|
|
2018-08-21 07:13:06 +03:00
|
|
|
// FetchPaymentStatusTx is a helper method that returns the payment status for
|
|
|
|
// outgoing payment. If status of the payment isn't found, it will default to
|
|
|
|
// "StatusGrounded". It accepts the boltdb transactions such that this method
|
|
|
|
// can be composed into other atomic operations.
|
2018-11-30 07:04:21 +03:00
|
|
|
func FetchPaymentStatusTx(tx *bbolt.Tx, paymentHash [32]byte) (PaymentStatus, error) {
|
2018-08-21 07:13:06 +03:00
|
|
|
// The default status for all payments that aren't recorded in database.
|
|
|
|
var paymentStatus = StatusGrounded
|
|
|
|
|
|
|
|
bucket := tx.Bucket(paymentStatusBucket)
|
|
|
|
if bucket == nil {
|
|
|
|
return paymentStatus, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
paymentStatusBytes := bucket.Get(paymentHash[:])
|
|
|
|
if paymentStatusBytes == nil {
|
|
|
|
return paymentStatus, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
paymentStatus.FromBytes(paymentStatusBytes)
|
|
|
|
|
|
|
|
return paymentStatus, nil
|
|
|
|
}
|
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
func serializeOutgoingPayment(w io.Writer, p *OutgoingPayment) error {
|
2016-12-31 03:32:20 +03:00
|
|
|
var scratch [8]byte
|
|
|
|
|
|
|
|
if err := serializeInvoice(w, &p.Invoice); err != nil {
|
2016-12-05 14:59:36 +03:00
|
|
|
return err
|
|
|
|
}
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2016-12-31 03:32:20 +03:00
|
|
|
byteOrder.PutUint64(scratch[:], uint64(p.Fee))
|
|
|
|
if _, err := w.Write(scratch[:]); err != nil {
|
2016-12-05 14:59:36 +03:00
|
|
|
return err
|
|
|
|
}
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2016-12-31 03:32:20 +03:00
|
|
|
// First write out the length of the bytes to prefix the value.
|
2016-12-05 14:59:36 +03:00
|
|
|
pathLen := uint32(len(p.Path))
|
2016-12-31 03:32:20 +03:00
|
|
|
byteOrder.PutUint32(scratch[:4], pathLen)
|
|
|
|
if _, err := w.Write(scratch[:4]); err != nil {
|
2016-12-05 14:59:36 +03:00
|
|
|
return err
|
|
|
|
}
|
2016-12-31 03:32:20 +03:00
|
|
|
|
|
|
|
// Then with the path written, we write out the series of public keys
|
|
|
|
// involved in the path.
|
|
|
|
for _, hop := range p.Path {
|
|
|
|
if _, err := w.Write(hop[:]); err != nil {
|
2016-12-05 14:59:36 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2016-12-31 03:32:20 +03:00
|
|
|
byteOrder.PutUint32(scratch[:4], p.TimeLockLength)
|
|
|
|
if _, err := w.Write(scratch[:4]); err != nil {
|
2016-12-05 14:59:36 +03:00
|
|
|
return err
|
|
|
|
}
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2017-12-14 04:04:18 +03:00
|
|
|
if _, err := w.Write(p.PaymentPreimage[:]); err != nil {
|
2016-12-05 14:59:36 +03:00
|
|
|
return err
|
|
|
|
}
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func deserializeOutgoingPayment(r io.Reader) (*OutgoingPayment, error) {
|
2016-12-31 03:32:20 +03:00
|
|
|
var scratch [8]byte
|
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
p := &OutgoingPayment{}
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
inv, err := deserializeInvoice(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-25 06:34:30 +03:00
|
|
|
p.Invoice = inv
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2016-12-31 03:32:20 +03:00
|
|
|
if _, err := r.Read(scratch[:]); err != nil {
|
2016-12-05 14:59:36 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-08-22 08:49:56 +03:00
|
|
|
p.Fee = lnwire.MilliSatoshi(byteOrder.Uint64(scratch[:]))
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2016-12-31 03:32:20 +03:00
|
|
|
if _, err = r.Read(scratch[:4]); err != nil {
|
2016-12-05 14:59:36 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-31 03:32:20 +03:00
|
|
|
pathLen := byteOrder.Uint32(scratch[:4])
|
|
|
|
|
2016-12-21 12:19:01 +03:00
|
|
|
path := make([][33]byte, pathLen)
|
|
|
|
for i := uint32(0); i < pathLen; i++ {
|
2016-12-31 03:32:20 +03:00
|
|
|
if _, err := r.Read(path[i][:]); err != nil {
|
2016-12-05 14:59:36 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.Path = path
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2016-12-31 03:32:20 +03:00
|
|
|
if _, err = r.Read(scratch[:4]); err != nil {
|
2016-12-05 14:59:36 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-31 03:32:20 +03:00
|
|
|
p.TimeLockLength = byteOrder.Uint32(scratch[:4])
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2017-12-14 04:04:18 +03:00
|
|
|
if _, err := r.Read(p.PaymentPreimage[:]); err != nil {
|
2016-12-05 14:59:36 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
return p, nil
|
2016-12-21 12:19:01 +03:00
|
|
|
}
|