From 86b347c996f75f36b263a595509e649a3bfdf26a Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Mon, 20 Aug 2018 21:13:06 -0700 Subject: [PATCH] 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. --- channeldb/payments.go | 63 +++++++++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 20 deletions(-) diff --git a/channeldb/payments.go b/channeldb/payments.go index d8d1a856..7d32f20c 100644 --- a/channeldb/payments.go +++ b/channeldb/payments.go @@ -191,33 +191,33 @@ func (db *DB) DeleteAllPayments() error { // local database. func (db *DB) UpdatePaymentStatus(paymentHash [32]byte, status PaymentStatus) error { return db.Batch(func(tx *bolt.Tx) error { - paymentStatuses, err := tx.CreateBucketIfNotExists(paymentStatusBucket) - if err != nil { - return err - } - - return paymentStatuses.Put(paymentHash[:], status.Bytes()) + return UpdatePaymentStatusTx(tx, paymentHash, status) }) } +// 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. // If status of the payment isn't found, it will default to "StatusGrounded". func (db *DB) FetchPaymentStatus(paymentHash [32]byte) (PaymentStatus, error) { - // The default status for all payments that aren't recorded in database. - paymentStatus := StatusGrounded - + var paymentStatus = StatusGrounded err := db.View(func(tx *bolt.Tx) error { - bucket := tx.Bucket(paymentStatusBucket) - if bucket == nil { - return nil - } - - paymentStatusBytes := bucket.Get(paymentHash[:]) - if paymentStatusBytes == nil { - return nil - } - - return paymentStatus.FromBytes(paymentStatusBytes) + var err error + paymentStatus, err = FetchPaymentStatusTx(tx, paymentHash) + return err }) if err != nil { return StatusGrounded, err @@ -226,6 +226,29 @@ func (db *DB) FetchPaymentStatus(paymentHash [32]byte) (PaymentStatus, error) { 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 { var scratch [8]byte