2022-11-01 09:23:51 +08:00
|
|
|
package channeldb
|
|
|
|
|
2022-11-24 18:16:21 +08:00
|
|
|
import "fmt"
|
|
|
|
|
2022-11-01 09:23:51 +08:00
|
|
|
// PaymentStatus represent current status of payment.
|
|
|
|
type PaymentStatus byte
|
|
|
|
|
|
|
|
const (
|
2022-11-24 12:26:17 +08:00
|
|
|
// NOTE: PaymentStatus = 0 was previously used for status unknown and
|
|
|
|
// is now deprecated.
|
2022-11-01 09:23:51 +08:00
|
|
|
|
2022-11-24 12:05:48 +08:00
|
|
|
// StatusInitiated is the status where a payment has just been
|
|
|
|
// initiated.
|
|
|
|
StatusInitiated PaymentStatus = 1
|
|
|
|
|
2022-11-01 09:23:51 +08:00
|
|
|
// StatusInFlight is the status where a payment has been initiated, but
|
|
|
|
// a response has not been received.
|
2022-11-24 12:05:48 +08:00
|
|
|
StatusInFlight PaymentStatus = 2
|
2022-11-01 09:23:51 +08:00
|
|
|
|
|
|
|
// StatusSucceeded is the status where a payment has been initiated and
|
|
|
|
// the payment was completed successfully.
|
2022-11-24 12:05:48 +08:00
|
|
|
StatusSucceeded PaymentStatus = 3
|
2022-11-01 09:23:51 +08:00
|
|
|
|
|
|
|
// StatusFailed is the status where a payment has been initiated and a
|
|
|
|
// failure result has come back.
|
2022-11-24 12:05:48 +08:00
|
|
|
StatusFailed PaymentStatus = 4
|
2022-11-01 09:23:51 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// String returns readable representation of payment status.
|
|
|
|
func (ps PaymentStatus) String() string {
|
|
|
|
switch ps {
|
2022-11-24 12:05:48 +08:00
|
|
|
case StatusInitiated:
|
|
|
|
return "Initiated"
|
|
|
|
|
2022-11-01 09:23:51 +08:00
|
|
|
case StatusInFlight:
|
|
|
|
return "In Flight"
|
|
|
|
|
|
|
|
case StatusSucceeded:
|
|
|
|
return "Succeeded"
|
|
|
|
|
|
|
|
case StatusFailed:
|
|
|
|
return "Failed"
|
|
|
|
|
|
|
|
default:
|
|
|
|
return "Unknown"
|
|
|
|
}
|
|
|
|
}
|
2022-11-24 18:16:21 +08:00
|
|
|
|
|
|
|
// initializable returns an error to specify whether initiating the payment
|
|
|
|
// with its current status is allowed. A payment can only be initialized if it
|
|
|
|
// hasn't been created yet or already failed.
|
|
|
|
func (ps PaymentStatus) initializable() error {
|
|
|
|
switch ps {
|
|
|
|
// The payment has been created already. We will disallow creating it
|
|
|
|
// again in case other goroutines have already been creating HTLCs for
|
|
|
|
// it.
|
|
|
|
case StatusInitiated:
|
|
|
|
return ErrPaymentExists
|
|
|
|
|
|
|
|
// We already have an InFlight payment on the network. We will disallow
|
|
|
|
// any new payments.
|
|
|
|
case StatusInFlight:
|
|
|
|
return ErrPaymentInFlight
|
|
|
|
|
|
|
|
// The payment has been attempted and is succeeded so we won't allow
|
|
|
|
// creating it again.
|
|
|
|
case StatusSucceeded:
|
|
|
|
return ErrAlreadyPaid
|
|
|
|
|
|
|
|
// We allow retrying failed payments.
|
|
|
|
case StatusFailed:
|
|
|
|
return nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("%w: %v", ErrUnknownPaymentStatus, ps)
|
|
|
|
}
|
|
|
|
}
|
2022-11-24 17:52:04 +08:00
|
|
|
|
|
|
|
// removable returns an error to specify whether deleting the payment with its
|
|
|
|
// current status is allowed. A payment cannot be safely deleted if it has
|
|
|
|
// inflight HTLCs.
|
|
|
|
func (ps PaymentStatus) removable() error {
|
|
|
|
switch ps {
|
|
|
|
// The payment has been created but has no HTLCs and can be removed.
|
|
|
|
case StatusInitiated:
|
|
|
|
return nil
|
|
|
|
|
|
|
|
// There are still inflight HTLCs and the payment needs to wait for the
|
|
|
|
// final outcomes.
|
|
|
|
case StatusInFlight:
|
|
|
|
return ErrPaymentInFlight
|
|
|
|
|
|
|
|
// The payment has been attempted and is succeeded and is allowed to be
|
|
|
|
// removed.
|
|
|
|
case StatusSucceeded:
|
|
|
|
return nil
|
|
|
|
|
|
|
|
// Failed payments are allowed to be removed.
|
|
|
|
case StatusFailed:
|
|
|
|
return nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("%w: %v", ErrUnknownPaymentStatus, ps)
|
|
|
|
}
|
|
|
|
}
|