From 95b4828780d73153f89d7bc1eb73fd09b39a34bd Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Thu, 23 May 2019 20:05:31 +0200 Subject: [PATCH] channeldb: derive PaymentStatus implicitly from stored info --- channeldb/control_tower.go | 45 +++++++++++++------------------------- channeldb/migrations.go | 7 ------ channeldb/payments.go | 4 ---- 3 files changed, 15 insertions(+), 41 deletions(-) diff --git a/channeldb/control_tower.go b/channeldb/control_tower.go index 8d94ae0c..7cbce262 100644 --- a/channeldb/control_tower.go +++ b/channeldb/control_tower.go @@ -145,12 +145,6 @@ func (p *paymentControl) InitPayment(paymentHash lntypes.Hash, return err } - // We'll move it into the InFlight state. - err = bucket.Put(paymentStatusKey, StatusInFlight.Bytes()) - if err != nil { - return err - } - // Add the payment info to the bucket, which contains the // static information for this payment err = bucket.Put(paymentCreationInfoKey, infoBytes) @@ -243,12 +237,7 @@ func (p *paymentControl) Success(paymentHash lntypes.Hash, // Record the successful payment info atomically to the // payments record. - err = bucket.Put(paymentSettleInfoKey, preimage[:]) - if err != nil { - return err - } - - return bucket.Put(paymentStatusKey, StatusSucceeded.Bytes()) + return bucket.Put(paymentSettleInfoKey, preimage[:]) }) if err != nil { return err @@ -284,14 +273,7 @@ func (p *paymentControl) Fail(paymentHash lntypes.Hash, // Put the failure reason in the bucket for record keeping. v := []byte{byte(reason)} - err = bucket.Put(paymentFailInfoKey, v) - if err != nil { - return err - } - - // A failed response was received for an InFlight payment, mark - // it as Failed to allow subsequent attempts. - return bucket.Put(paymentStatusKey, StatusFailed.Bytes()) + return bucket.Put(paymentFailInfoKey, v) }) if err != nil { return err @@ -331,19 +313,22 @@ func nextPaymentSequence(tx *bbolt.Tx) ([]byte, error) { return b, nil } -// fetchPaymentStatus fetches the payment status from the bucket. If the -// status isn't found, it will default to "StatusUnknown". +// fetchPaymentStatus fetches the payment status of the payment. If the payment +// isn't found, it will default to "StatusUnknown". func fetchPaymentStatus(bucket *bbolt.Bucket) PaymentStatus { - // The default status for all payments that aren't recorded in - // database. - var paymentStatus = StatusUnknown - - paymentStatusBytes := bucket.Get(paymentStatusKey) - if paymentStatusBytes != nil { - paymentStatus.FromBytes(paymentStatusBytes) + if bucket.Get(paymentSettleInfoKey) != nil { + return StatusSucceeded } - return paymentStatus + if bucket.Get(paymentFailInfoKey) != nil { + return StatusFailed + } + + if bucket.Get(paymentCreationInfoKey) != nil { + return StatusInFlight + } + + return StatusUnknown } // ensureInFlight checks whether the payment found in the given bucket has diff --git a/channeldb/migrations.go b/channeldb/migrations.go index c7263958..0a7098c0 100644 --- a/channeldb/migrations.go +++ b/channeldb/migrations.go @@ -843,13 +843,6 @@ func migrateOutgoingPayments(tx *bbolt.Tx) error { return err } - // Since only completed payments were previously stored as - // OutgoingPayments, set the status as Completed. - err = bucket.Put(paymentStatusKey, StatusSucceeded.Bytes()) - if err != nil { - return err - } - return nil }) if err != nil { diff --git a/channeldb/payments.go b/channeldb/payments.go index 2e236771..9eabe37d 100644 --- a/channeldb/payments.go +++ b/channeldb/payments.go @@ -53,10 +53,6 @@ var ( // paymentsRootBucket = []byte("payments-root-bucket") - // paymentStatusKey is a key used in the payment's sub-bucket to store - // the status of the payment. - paymentStatusKey = []byte("payment-status-key") - // paymentDublicateBucket is the name of a optional sub-bucket within // the payment hash bucket, that is used to hold duplicate payments to // a payment hash. This is needed to support information from earlier