2016-12-05 14:59:36 +03:00
|
|
|
package channeldb
|
|
|
|
|
|
|
|
import (
|
2016-12-21 12:19:01 +03:00
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"github.com/boltdb/bolt"
|
|
|
|
"github.com/roasbeef/btcd/wire"
|
2016-12-05 14:59:36 +03:00
|
|
|
"github.com/roasbeef/btcutil"
|
|
|
|
"io"
|
2016-12-21 12:19:01 +03:00
|
|
|
"time"
|
2016-12-05 14:59:36 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-12-21 12:19:01 +03:00
|
|
|
// invoiceBucket 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
|
2016-12-05 14:59:36 +03:00
|
|
|
// which is a monotonically increasing uint64.
|
2016-12-21 12:19:01 +03:00
|
|
|
// BoltDB sequence feature is used for generating
|
|
|
|
// monotonically increasing id.
|
2016-12-05 14:59:36 +03:00
|
|
|
paymentBucket = []byte("payments")
|
|
|
|
)
|
|
|
|
|
2016-12-21 12:19:01 +03:00
|
|
|
// OutgoingPayment represents payment from given node.
|
2016-12-05 14:59:36 +03:00
|
|
|
type OutgoingPayment struct {
|
|
|
|
Invoice
|
2016-12-21 12:19:01 +03:00
|
|
|
|
|
|
|
// Total fee paid.
|
2016-12-05 14:59:36 +03:00
|
|
|
Fee btcutil.Amount
|
2016-12-21 12:19:01 +03:00
|
|
|
|
|
|
|
// Path including starting and ending nodes.
|
|
|
|
Path [][33]byte
|
|
|
|
|
|
|
|
// Timelock length.
|
|
|
|
TimeLockLength uint32
|
|
|
|
|
|
|
|
// RHash value used for payment.
|
|
|
|
// We need RHash because we start payment knowing only RHash
|
2016-12-05 14:59:36 +03:00
|
|
|
RHash [32]byte
|
|
|
|
|
2016-12-21 12:19:01 +03:00
|
|
|
// Timestamp is time when payment was created.
|
|
|
|
Timestamp time.Time
|
2016-12-05 14:59:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddPayment adds payment to DB.
|
|
|
|
// There is no checking that payment with the same hash already exist.
|
|
|
|
func (db *DB) AddPayment(p *OutgoingPayment) error {
|
2016-12-21 12:19:01 +03:00
|
|
|
err := validateInvoice(&p.Invoice)
|
2016-12-05 14:59:36 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
// We serialize before writing to database
|
|
|
|
// so no db access in the case of serialization errors
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
err = serializeOutgoingPayment(b, p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
paymentBytes := b.Bytes()
|
2016-12-21 12:19:01 +03:00
|
|
|
return db.Update(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-05 14:59:36 +03:00
|
|
|
paymentId, err := payments.NextSequence()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
// We use BigEndian for keys because
|
|
|
|
// it orders keys in ascending order
|
|
|
|
paymentIdBytes := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint64(paymentIdBytes, paymentId)
|
|
|
|
err = payments.Put(paymentIdBytes, paymentBytes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// FetchAllPayments returns all outgoing payments in DB.
|
|
|
|
func (db *DB) FetchAllPayments() ([]*OutgoingPayment, error) {
|
|
|
|
var payments []*OutgoingPayment
|
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-21 12:19:01 +03:00
|
|
|
err := bucket.ForEach(func(k, v []byte) error {
|
2016-12-05 14:59:36 +03:00
|
|
|
// Value can be nil if it is a sub-backet
|
|
|
|
// so simply ignore it.
|
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
r := bytes.NewReader(v)
|
|
|
|
payment, err := deserializeOutgoingPayment(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
payments = append(payments, payment)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return payments, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteAllPayments deletes all payments from DB.
|
|
|
|
// If payments bucket does not exist it will create
|
|
|
|
// new bucket without error.
|
|
|
|
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)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func serializeOutgoingPayment(w io.Writer, p *OutgoingPayment) error {
|
|
|
|
err := serializeInvoice(w, &p.Invoice)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
// Serialize fee.
|
|
|
|
feeBytes := make([]byte, 8)
|
|
|
|
byteOrder.PutUint64(feeBytes, uint64(p.Fee))
|
|
|
|
_, err = w.Write(feeBytes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
// Serialize path.
|
|
|
|
pathLen := uint32(len(p.Path))
|
|
|
|
pathLenBytes := make([]byte, 4)
|
2016-12-21 12:19:01 +03:00
|
|
|
// Write length of the path
|
2016-12-05 14:59:36 +03:00
|
|
|
byteOrder.PutUint32(pathLenBytes, pathLen)
|
|
|
|
_, err = w.Write(pathLenBytes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-21 12:19:01 +03:00
|
|
|
// Serialize each element of the path
|
2016-12-05 14:59:36 +03:00
|
|
|
for i := uint32(0); i < pathLen; i++ {
|
2016-12-21 12:19:01 +03:00
|
|
|
_, err := w.Write(p.Path[i][:])
|
2016-12-05 14:59:36 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
// Serialize TimeLockLength
|
2016-12-21 12:19:01 +03:00
|
|
|
timeLockLengthBytes := make([]byte, 4)
|
|
|
|
byteOrder.PutUint32(timeLockLengthBytes, p.TimeLockLength)
|
2016-12-05 14:59:36 +03:00
|
|
|
_, err = w.Write(timeLockLengthBytes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
// Serialize RHash
|
|
|
|
_, err = w.Write(p.RHash[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-21 12:19:01 +03:00
|
|
|
|
|
|
|
// Serialize Timestamp.
|
|
|
|
tBytes, err := p.Timestamp.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = wire.WriteVarBytes(w, 0, tBytes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-05 14:59:36 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func deserializeOutgoingPayment(r io.Reader) (*OutgoingPayment, error) {
|
|
|
|
p := &OutgoingPayment{}
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
// Deserialize invoice
|
|
|
|
inv, err := deserializeInvoice(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p.Invoice = *inv
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
// Deserialize fee
|
|
|
|
feeBytes := make([]byte, 8)
|
|
|
|
_, err = r.Read(feeBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p.Fee = btcutil.Amount(byteOrder.Uint64(feeBytes))
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
// Deserialize path
|
|
|
|
pathLenBytes := make([]byte, 4)
|
|
|
|
_, err = r.Read(pathLenBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pathLen := byteOrder.Uint32(pathLenBytes)
|
2016-12-21 12:19:01 +03:00
|
|
|
path := make([][33]byte, pathLen)
|
|
|
|
for i := uint32(0); i < pathLen; i++ {
|
|
|
|
_, err := r.Read(path[i][:])
|
2016-12-05 14:59:36 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.Path = path
|
2016-12-21 12:19:01 +03:00
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
// Deserialize TimeLockLength
|
2016-12-21 12:19:01 +03:00
|
|
|
timeLockLengthBytes := make([]byte, 4)
|
2016-12-05 14:59:36 +03:00
|
|
|
_, err = r.Read(timeLockLengthBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-21 12:19:01 +03:00
|
|
|
p.TimeLockLength = byteOrder.Uint32(timeLockLengthBytes)
|
|
|
|
|
2016-12-05 14:59:36 +03:00
|
|
|
// Deserialize RHash
|
|
|
|
_, err = r.Read(p.RHash[:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-21 12:19:01 +03:00
|
|
|
|
|
|
|
// Deserialize Timestamp
|
|
|
|
tBytes, err := wire.ReadVarBytes(r, 0, 100,
|
|
|
|
"OutgoingPayment.Timestamp")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = p.Timestamp.UnmarshalBinary(tBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-05 14:59:36 +03:00
|
|
|
return p, nil
|
2016-12-21 12:19:01 +03:00
|
|
|
}
|