channeldb: derive PaymentStatus implicitly from stored info

This commit is contained in:
Johan T. Halseth 2019-05-23 20:05:31 +02:00
parent d705b8a013
commit 95b4828780
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26
3 changed files with 15 additions and 41 deletions

@ -145,12 +145,6 @@ func (p *paymentControl) InitPayment(paymentHash lntypes.Hash,
return err 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 // Add the payment info to the bucket, which contains the
// static information for this payment // static information for this payment
err = bucket.Put(paymentCreationInfoKey, infoBytes) err = bucket.Put(paymentCreationInfoKey, infoBytes)
@ -243,12 +237,7 @@ func (p *paymentControl) Success(paymentHash lntypes.Hash,
// Record the successful payment info atomically to the // Record the successful payment info atomically to the
// payments record. // payments record.
err = bucket.Put(paymentSettleInfoKey, preimage[:]) return bucket.Put(paymentSettleInfoKey, preimage[:])
if err != nil {
return err
}
return bucket.Put(paymentStatusKey, StatusSucceeded.Bytes())
}) })
if err != nil { if err != nil {
return err return err
@ -284,14 +273,7 @@ func (p *paymentControl) Fail(paymentHash lntypes.Hash,
// Put the failure reason in the bucket for record keeping. // Put the failure reason in the bucket for record keeping.
v := []byte{byte(reason)} v := []byte{byte(reason)}
err = bucket.Put(paymentFailInfoKey, v) return 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())
}) })
if err != nil { if err != nil {
return err return err
@ -331,19 +313,22 @@ func nextPaymentSequence(tx *bbolt.Tx) ([]byte, error) {
return b, nil return b, nil
} }
// fetchPaymentStatus fetches the payment status from the bucket. If the // fetchPaymentStatus fetches the payment status of the payment. If the payment
// status isn't found, it will default to "StatusUnknown". // isn't found, it will default to "StatusUnknown".
func fetchPaymentStatus(bucket *bbolt.Bucket) PaymentStatus { func fetchPaymentStatus(bucket *bbolt.Bucket) PaymentStatus {
// The default status for all payments that aren't recorded in if bucket.Get(paymentSettleInfoKey) != nil {
// database. return StatusSucceeded
var paymentStatus = StatusUnknown
paymentStatusBytes := bucket.Get(paymentStatusKey)
if paymentStatusBytes != nil {
paymentStatus.FromBytes(paymentStatusBytes)
} }
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 // ensureInFlight checks whether the payment found in the given bucket has

@ -843,13 +843,6 @@ func migrateOutgoingPayments(tx *bbolt.Tx) error {
return err 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 return nil
}) })
if err != nil { if err != nil {

@ -53,10 +53,6 @@ var (
// //
paymentsRootBucket = []byte("payments-root-bucket") 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 // paymentDublicateBucket is the name of a optional sub-bucket within
// the payment hash bucket, that is used to hold duplicate payments to // the payment hash bucket, that is used to hold duplicate payments to
// a payment hash. This is needed to support information from earlier // a payment hash. This is needed to support information from earlier