channeldb+multi: rename Grounded->Unknown, Completed->Succeeded

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

@ -22,9 +22,9 @@ var (
// switch. // switch.
ErrPaymentNotInitiated = errors.New("payment isn't initiated") ErrPaymentNotInitiated = errors.New("payment isn't initiated")
// ErrPaymentAlreadyCompleted is returned in the event we attempt to // ErrPaymentAlreadySucceeded is returned in the event we attempt to
// recomplete a completed payment. // change the status of a payment already succeeded.
ErrPaymentAlreadyCompleted = errors.New("payment is already completed") ErrPaymentAlreadySucceeded = errors.New("payment is already succeeded")
// ErrPaymentAlreadyFailed is returned in the event we attempt to // ErrPaymentAlreadyFailed is returned in the event we attempt to
// re-fail a failed payment. // re-fail a failed payment.
@ -42,14 +42,14 @@ var (
// ControlTower interface provides access to driving the state transitions. // ControlTower interface provides access to driving the state transitions.
type ControlTower interface { type ControlTower interface {
// InitPayment atomically moves the payment into the InFlight state. // InitPayment atomically moves the payment into the InFlight state.
// This method checks that no completed payment exist for this payment // This method checks that no suceeded payment exist for this payment
// hash. // hash.
InitPayment(lntypes.Hash, *PaymentCreationInfo) error InitPayment(lntypes.Hash, *PaymentCreationInfo) error
// RegisterAttempt atomically records the provided PaymentAttemptInfo. // RegisterAttempt atomically records the provided PaymentAttemptInfo.
RegisterAttempt(lntypes.Hash, *PaymentAttemptInfo) error RegisterAttempt(lntypes.Hash, *PaymentAttemptInfo) error
// Success transitions a payment into the Completed state. After // Success transitions a payment into the Succeeded state. After
// invoking this method, InitPayment should always return an error to // invoking this method, InitPayment should always return an error to
// prevent us from making duplicate payments to the same payment hash. // prevent us from making duplicate payments to the same payment hash.
// The provided preimage is atomically saved to the DB for record // The provided preimage is atomically saved to the DB for record
@ -113,7 +113,7 @@ func (p *paymentControl) InitPayment(paymentHash lntypes.Hash,
// This is a new payment that is being initialized for the // This is a new payment that is being initialized for the
// first time. // first time.
case StatusGrounded: case StatusUnknown:
// We already have an InFlight payment on the network. We will // We already have an InFlight payment on the network. We will
// disallow any new payments. // disallow any new payments.
@ -121,9 +121,9 @@ func (p *paymentControl) InitPayment(paymentHash lntypes.Hash,
takeoffErr = ErrPaymentInFlight takeoffErr = ErrPaymentInFlight
return nil return nil
// We've already completed a payment to this payment hash, // We've already succeeded a payment to this payment hash,
// forbid the switch from sending another. // forbid the switch from sending another.
case StatusCompleted: case StatusSucceeded:
takeoffErr = ErrAlreadyPaid takeoffErr = ErrAlreadyPaid
return nil return nil
@ -217,7 +217,7 @@ func (p *paymentControl) RegisterAttempt(paymentHash lntypes.Hash,
return updateErr return updateErr
} }
// Success transitions a payment into the Completed state. After invoking this // Success transitions a payment into the Succeeded state. After invoking this
// method, InitPayment should always return an error to prevent us from making // method, InitPayment should always return an error to prevent us from making
// duplicate payments to the same payment hash. The provided preimage is // duplicate payments to the same payment hash. The provided preimage is
// atomically saved to the DB for record keeping. // atomically saved to the DB for record keeping.
@ -248,7 +248,7 @@ func (p *paymentControl) Success(paymentHash lntypes.Hash,
return err return err
} }
return bucket.Put(paymentStatusKey, StatusCompleted.Bytes()) return bucket.Put(paymentStatusKey, StatusSucceeded.Bytes())
}) })
if err != nil { if err != nil {
return err return err
@ -332,11 +332,11 @@ func nextPaymentSequence(tx *bbolt.Tx) ([]byte, error) {
} }
// fetchPaymentStatus fetches the payment status from the bucket. If the // fetchPaymentStatus fetches the payment status from the bucket. If the
// status isn't found, it will default to "StatusGrounded". // status 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 // The default status for all payments that aren't recorded in
// database. // database.
var paymentStatus = StatusGrounded var paymentStatus = StatusUnknown
paymentStatusBytes := bucket.Get(paymentStatusKey) paymentStatusBytes := bucket.Get(paymentStatusKey)
if paymentStatusBytes != nil { if paymentStatusBytes != nil {
@ -360,12 +360,12 @@ func ensureInFlight(bucket *bbolt.Bucket) error {
// Our records show the payment as unknown, meaning it never // Our records show the payment as unknown, meaning it never
// should have left the switch. // should have left the switch.
case paymentStatus == StatusGrounded: case paymentStatus == StatusUnknown:
return ErrPaymentNotInitiated return ErrPaymentNotInitiated
// The payment succeeded previously. // The payment succeeded previously.
case paymentStatus == StatusCompleted: case paymentStatus == StatusSucceeded:
return ErrPaymentAlreadyCompleted return ErrPaymentAlreadySucceeded
// The payment was already failed. // The payment was already failed.
case paymentStatus == StatusFailed: case paymentStatus == StatusFailed:

@ -130,12 +130,12 @@ func TestPaymentControlSwitchFail(t *testing.T) {
nil, nil,
) )
// Verifies that status was changed to StatusCompleted. // Verifies that status was changed to StatusSucceeded.
if err := pControl.Success(info.PaymentHash, preimg); err != nil { if err := pControl.Success(info.PaymentHash, preimg); err != nil {
t.Fatalf("error shouldn't have been received, got: %v", err) t.Fatalf("error shouldn't have been received, got: %v", err)
} }
assertPaymentStatus(t, db, info.PaymentHash, StatusCompleted) assertPaymentStatus(t, db, info.PaymentHash, StatusSucceeded)
assertPaymentInfo(t, db, info.PaymentHash, info, attempt, preimg, nil) assertPaymentInfo(t, db, info.PaymentHash, info, attempt, preimg, nil)
// Attempt a final payment, which should now fail since the prior // Attempt a final payment, which should now fail since the prior
@ -207,7 +207,7 @@ func TestPaymentControlSwitchDoubleSend(t *testing.T) {
if err := pControl.Success(info.PaymentHash, preimg); err != nil { if err := pControl.Success(info.PaymentHash, preimg); err != nil {
t.Fatalf("error shouldn't have been received, got: %v", err) t.Fatalf("error shouldn't have been received, got: %v", err)
} }
assertPaymentStatus(t, db, info.PaymentHash, StatusCompleted) assertPaymentStatus(t, db, info.PaymentHash, StatusSucceeded)
assertPaymentInfo(t, db, info.PaymentHash, info, attempt, preimg, nil) assertPaymentInfo(t, db, info.PaymentHash, info, attempt, preimg, nil)
err = pControl.InitPayment(info.PaymentHash, info) err = pControl.InitPayment(info.PaymentHash, info)
@ -239,7 +239,7 @@ func TestPaymentControlSuccessesWithoutInFlight(t *testing.T) {
t.Fatalf("expected ErrPaymentNotInitiated, got %v", err) t.Fatalf("expected ErrPaymentNotInitiated, got %v", err)
} }
assertPaymentStatus(t, db, info.PaymentHash, StatusGrounded) assertPaymentStatus(t, db, info.PaymentHash, StatusUnknown)
assertPaymentInfo( assertPaymentInfo(
t, db, info.PaymentHash, nil, nil, lntypes.Preimage{}, t, db, info.PaymentHash, nil, nil, lntypes.Preimage{},
nil, nil,
@ -269,7 +269,7 @@ func TestPaymentControlFailsWithoutInFlight(t *testing.T) {
t.Fatalf("expected ErrPaymentNotInitiated, got %v", err) t.Fatalf("expected ErrPaymentNotInitiated, got %v", err)
} }
assertPaymentStatus(t, db, info.PaymentHash, StatusGrounded) assertPaymentStatus(t, db, info.PaymentHash, StatusUnknown)
assertPaymentInfo( assertPaymentInfo(
t, db, info.PaymentHash, nil, nil, lntypes.Preimage{}, nil, t, db, info.PaymentHash, nil, nil, lntypes.Preimage{}, nil,
) )
@ -280,7 +280,7 @@ func assertPaymentStatus(t *testing.T, db *DB,
t.Helper() t.Helper()
var paymentStatus = StatusGrounded var paymentStatus = StatusUnknown
err := db.View(func(tx *bbolt.Tx) error { err := db.View(func(tx *bbolt.Tx) error {
payments := tx.Bucket(paymentsRootBucket) payments := tx.Bucket(paymentsRootBucket)
if payments == nil { if payments == nil {

@ -132,18 +132,18 @@ func (db *DB) fetchAllPayments() ([]*outgoingPayment, error) {
} }
// 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 "StatusUnknown".
// //
// NOTE: Deprecated. Kept around for migration purposes. // NOTE: Deprecated. Kept around for migration purposes.
func (db *DB) fetchPaymentStatus(paymentHash [32]byte) (PaymentStatus, error) { func (db *DB) fetchPaymentStatus(paymentHash [32]byte) (PaymentStatus, error) {
var paymentStatus = StatusGrounded var paymentStatus = StatusUnknown
err := db.View(func(tx *bbolt.Tx) error { err := db.View(func(tx *bbolt.Tx) error {
var err error var err error
paymentStatus, err = fetchPaymentStatusTx(tx, paymentHash) paymentStatus, err = fetchPaymentStatusTx(tx, paymentHash)
return err return err
}) })
if err != nil { if err != nil {
return StatusGrounded, err return StatusUnknown, err
} }
return paymentStatus, nil return paymentStatus, nil
@ -151,13 +151,13 @@ func (db *DB) fetchPaymentStatus(paymentHash [32]byte) (PaymentStatus, error) {
// fetchPaymentStatusTx is a helper method that returns the payment status for // 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 // outgoing payment. If status of the payment isn't found, it will default to
// "StatusGrounded". It accepts the boltdb transactions such that this method // "StatusUnknown". It accepts the boltdb transactions such that this method
// can be composed into other atomic operations. // can be composed into other atomic operations.
// //
// NOTE: Deprecated. Kept around for migration purposes. // NOTE: Deprecated. Kept around for migration purposes.
func fetchPaymentStatusTx(tx *bbolt.Tx, paymentHash [32]byte) (PaymentStatus, error) { func fetchPaymentStatusTx(tx *bbolt.Tx, paymentHash [32]byte) (PaymentStatus, error) {
// The default status for all payments that aren't recorded in database. // The default status for all payments that aren't recorded in database.
var paymentStatus = StatusGrounded var paymentStatus = StatusUnknown
bucket := tx.Bucket(paymentStatusBucket) bucket := tx.Bucket(paymentStatusBucket)
if bucket == nil { if bucket == nil {

@ -451,7 +451,7 @@ func paymentStatusesMigration(tx *bbolt.Tx) error {
// Update status for current payment to completed. If it fails, // Update status for current payment to completed. If it fails,
// the migration is aborted and the payment bucket is returned // the migration is aborted and the payment bucket is returned
// to its previous state. // to its previous state.
return paymentStatuses.Put(paymentHash[:], StatusCompleted.Bytes()) return paymentStatuses.Put(paymentHash[:], StatusSucceeded.Bytes())
}) })
if err != nil { if err != nil {
return err return err
@ -845,7 +845,7 @@ func migrateOutgoingPayments(tx *bbolt.Tx) error {
// Since only completed payments were previously stored as // Since only completed payments were previously stored as
// OutgoingPayments, set the status as Completed. // OutgoingPayments, set the status as Completed.
err = bucket.Put(paymentStatusKey, StatusCompleted.Bytes()) err = bucket.Put(paymentStatusKey, StatusSucceeded.Bytes())
if err != nil { if err != nil {
return err return err
} }

@ -46,9 +46,9 @@ func TestPaymentStatusesMigration(t *testing.T) {
} }
// We should receive default status if we have any in database. // We should receive default status if we have any in database.
if paymentStatus != StatusGrounded { if paymentStatus != StatusUnknown {
t.Fatalf("wrong payment status: expected %v, got %v", t.Fatalf("wrong payment status: expected %v, got %v",
StatusGrounded.String(), paymentStatus.String()) StatusUnknown.String(), paymentStatus.String())
} }
// Lastly, we'll add a locally-sourced circuit and // Lastly, we'll add a locally-sourced circuit and
@ -146,9 +146,9 @@ func TestPaymentStatusesMigration(t *testing.T) {
t.Fatalf("unable to fetch payment status: %v", err) t.Fatalf("unable to fetch payment status: %v", err)
} }
if paymentStatus != StatusCompleted { if paymentStatus != StatusSucceeded {
t.Fatalf("wrong payment status: expected %v, got %v", t.Fatalf("wrong payment status: expected %v, got %v",
StatusCompleted.String(), paymentStatus.String()) StatusSucceeded.String(), paymentStatus.String())
} }
inFlightHash := [32]byte{ inFlightHash := [32]byte{
@ -184,9 +184,9 @@ func TestPaymentStatusesMigration(t *testing.T) {
t.Fatalf("unable to fetch payment status: %v", err) t.Fatalf("unable to fetch payment status: %v", err)
} }
if paymentStatus != StatusGrounded { if paymentStatus != StatusUnknown {
t.Fatalf("wrong payment status: expected %v, got %v", t.Fatalf("wrong payment status: expected %v, got %v",
StatusGrounded.String(), paymentStatus.String()) StatusUnknown.String(), paymentStatus.String())
} }
} }
@ -640,7 +640,7 @@ func TestOutgoingPaymentsMigration(t *testing.T) {
for i, p := range sentPayments { for i, p := range sentPayments {
// The payment status should be Completed. // The payment status should be Completed.
if p.Status != StatusCompleted { if p.Status != StatusSucceeded {
t.Fatalf("expected Completed, got %v", p.Status) t.Fatalf("expected Completed, got %v", p.Status)
} }

@ -105,17 +105,17 @@ const (
type PaymentStatus byte type PaymentStatus byte
const ( const (
// StatusGrounded is the status where a payment has never been // StatusUnknown is the status where a payment has never been initiated
// initiated. // and hence is unknown.
StatusGrounded PaymentStatus = 0 StatusUnknown PaymentStatus = 0
// StatusInFlight is the status where a payment has been initiated, but // StatusInFlight is the status where a payment has been initiated, but
// a response has not been received. // a response has not been received.
StatusInFlight PaymentStatus = 1 StatusInFlight PaymentStatus = 1
// StatusCompleted is the status where a payment has been initiated and // StatusSucceeded is the status where a payment has been initiated and
// the payment was completed successfully. // the payment was completed successfully.
StatusCompleted PaymentStatus = 2 StatusSucceeded PaymentStatus = 2
// StatusFailed is the status where a payment has been initiated and a // StatusFailed is the status where a payment has been initiated and a
// failure result has come back. // failure result has come back.
@ -134,7 +134,7 @@ func (ps *PaymentStatus) FromBytes(status []byte) error {
} }
switch PaymentStatus(status[0]) { switch PaymentStatus(status[0]) {
case StatusGrounded, StatusInFlight, StatusCompleted, StatusFailed: case StatusUnknown, StatusInFlight, StatusSucceeded, StatusFailed:
*ps = PaymentStatus(status[0]) *ps = PaymentStatus(status[0])
default: default:
return errors.New("unknown payment status") return errors.New("unknown payment status")
@ -146,12 +146,12 @@ func (ps *PaymentStatus) FromBytes(status []byte) error {
// String returns readable representation of payment status. // String returns readable representation of payment status.
func (ps PaymentStatus) String() string { func (ps PaymentStatus) String() string {
switch ps { switch ps {
case StatusGrounded: case StatusUnknown:
return "Grounded" return "Unknown"
case StatusInFlight: case StatusInFlight:
return "In Flight" return "In Flight"
case StatusCompleted: case StatusSucceeded:
return "Completed" return "Succeeded"
case StatusFailed: case StatusFailed:
return "Failed" return "Failed"
default: default: