channeldb/payments: make payment status helper methods

This commit splits FetchPaymentStatus and
UpdatePaymentStatus, such that they each invoke
helper methods that can be composed into different
db txns. This enables us to improve performance on
send/receive, as we can remove the exclusive lock
from the control tower, and allow concurrent calls
to utilize Batch more effectively.
This commit is contained in:
Conner Fromknecht 2018-08-20 21:13:06 -07:00
parent 0865ac7cf6
commit 86b347c996
No known key found for this signature in database
GPG Key ID: E7D737B67FA592C7

@ -191,33 +191,33 @@ func (db *DB) DeleteAllPayments() error {
// local database. // local database.
func (db *DB) UpdatePaymentStatus(paymentHash [32]byte, status PaymentStatus) error { func (db *DB) UpdatePaymentStatus(paymentHash [32]byte, status PaymentStatus) error {
return db.Batch(func(tx *bolt.Tx) error { return db.Batch(func(tx *bolt.Tx) error {
paymentStatuses, err := tx.CreateBucketIfNotExists(paymentStatusBucket) return UpdatePaymentStatusTx(tx, paymentHash, status)
if err != nil {
return err
}
return paymentStatuses.Put(paymentHash[:], status.Bytes())
}) })
} }
// 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.
func UpdatePaymentStatusTx(tx *bolt.Tx,
paymentHash [32]byte, status PaymentStatus) error {
paymentStatuses, err := tx.CreateBucketIfNotExists(paymentStatusBucket)
if err != nil {
return err
}
return paymentStatuses.Put(paymentHash[:], status.Bytes())
}
// FetchPaymentStatus returns the payment status for outgoing payment. // FetchPaymentStatus returns the payment status for outgoing payment.
// If status of the payment isn't found, it will default to "StatusGrounded". // If status of the payment isn't found, it will default to "StatusGrounded".
func (db *DB) FetchPaymentStatus(paymentHash [32]byte) (PaymentStatus, error) { func (db *DB) FetchPaymentStatus(paymentHash [32]byte) (PaymentStatus, error) {
// The default status for all payments that aren't recorded in database. var paymentStatus = StatusGrounded
paymentStatus := StatusGrounded
err := db.View(func(tx *bolt.Tx) error { err := db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket(paymentStatusBucket) var err error
if bucket == nil { paymentStatus, err = FetchPaymentStatusTx(tx, paymentHash)
return nil return err
}
paymentStatusBytes := bucket.Get(paymentHash[:])
if paymentStatusBytes == nil {
return nil
}
return paymentStatus.FromBytes(paymentStatusBytes)
}) })
if err != nil { if err != nil {
return StatusGrounded, err return StatusGrounded, err
@ -226,6 +226,29 @@ func (db *DB) FetchPaymentStatus(paymentHash [32]byte) (PaymentStatus, error) {
return paymentStatus, nil return paymentStatus, nil
} }
// 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.
func FetchPaymentStatusTx(tx *bolt.Tx, paymentHash [32]byte) (PaymentStatus, error) {
// 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
}
func serializeOutgoingPayment(w io.Writer, p *OutgoingPayment) error { func serializeOutgoingPayment(w io.Writer, p *OutgoingPayment) error {
var scratch [8]byte var scratch [8]byte