mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-06-30 18:43:42 +02:00
channeldb: add method initializable
to decide initiating payment
This commit adds a new method, `initializable`, to help decide whether initiating a payment is allowed.
This commit is contained in:
committed by
Olaoluwa Osuntokun
parent
30fd29371c
commit
de33c3e009
@ -151,30 +151,8 @@ func (p *PaymentControl) InitPayment(paymentHash lntypes.Hash,
|
||||
// payment. We'll check the status to decide whether we allow
|
||||
// retrying the payment or return a specific error.
|
||||
case err == nil:
|
||||
switch paymentStatus {
|
||||
// We allow retrying failed payments.
|
||||
case StatusFailed:
|
||||
|
||||
// We already have payment creation info for this
|
||||
// payment so we won't allow creating a duplicate one.
|
||||
case StatusInitiated:
|
||||
updateErr = ErrPaymentExists
|
||||
return nil
|
||||
|
||||
// We already have an InFlight payment on the network.
|
||||
// We will disallow any new payments.
|
||||
case StatusInFlight:
|
||||
updateErr = ErrPaymentInFlight
|
||||
return nil
|
||||
|
||||
// We've already succeeded a payment to this payment
|
||||
// hash, forbid the switch from sending another.
|
||||
case StatusSucceeded:
|
||||
updateErr = ErrAlreadyPaid
|
||||
return nil
|
||||
|
||||
default:
|
||||
updateErr = ErrUnknownPaymentStatus
|
||||
if err := paymentStatus.initializable(); err != nil {
|
||||
updateErr = err
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,7 @@
|
||||
package channeldb
|
||||
|
||||
import "fmt"
|
||||
|
||||
// PaymentStatus represent current status of payment.
|
||||
type PaymentStatus byte
|
||||
|
||||
@ -43,3 +45,33 @@ func (ps PaymentStatus) String() string {
|
||||
return "Unknown"
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user