2016-12-05 14:59:36 +03:00
|
|
|
package channeldb
|
|
|
|
|
|
|
|
import (
|
2016-12-21 12:19:01 +03:00
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
2016-12-31 03:32:20 +03:00
|
|
|
"io"
|
|
|
|
|
2016-12-21 12:19:01 +03:00
|
|
|
"github.com/boltdb/bolt"
|
2016-12-05 14:59:36 +03:00
|
|
|
"github.com/roasbeef/btcutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
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")
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2016-12-31 03:32:20 +03:00
|
|
|
// Fee is the total fee paid for the payment in satoshis.
|
2016-12-05 14:59:36 +03:00
|
|
|
Fee btcutil.Amount
|
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
|
|
|
|
|
2016-12-31 03:32:20 +03:00
|
|
|
// Path encodes the path the payment took throuhg the network. The path
|
|
|
|
// 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
|
|
|
|
2016-12-31 03:32:20 +03:00
|
|
|
// PaymentHash is the payment hash (r-hash) used to send the payment.
|
|
|
|
//
|
|
|
|
// TODO(roasbeef): weave through preimage on payment success to can
|
|
|
|
// store only supplemental info the embedded Invoice
|
|
|
|
PaymentHash [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
|
|
|
|
2017-04-12 07:18:22 +03:00
|
|
|
return db.Batch(func(tx *bolt.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
|
|
|
|
2016-12-21 12:19:01 +03:00
|
|
|
err := db.View(func(tx *bolt.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 {
|
2016-12-21 12:19:01 +03:00
|
|
|
return db.Update(func(tx *bolt.Tx) error {
|
2016-12-05 14:59:36 +03:00
|
|
|
err := tx.DeleteBucket(paymentBucket)
|
|
|
|
if err != nil && err != bolt.ErrBucketNotFound {
|
|
|
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-12-31 03:32:20 +03:00
|
|
|
if _, err := w.Write(p.PaymentHash[:]); 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
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
2016-12-31 03:32:20 +03:00
|
|
|
p.Fee = btcutil.Amount(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
|
|
|
|
2016-12-31 03:32:20 +03:00
|
|
|
if _, err := r.Read(p.PaymentHash[:]); 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
|
|
|
}
|