mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-11-10 14:17:56 +01:00
routing: make sure StatusInitiated is notified when creating payments
This commit fixes `InitPayment` method to make sure the subscribers get notified when new payments are created.
This commit is contained in:
@@ -181,12 +181,29 @@ func NewControlTower(db *channeldb.PaymentControl) ControlTower {
|
|||||||
|
|
||||||
// InitPayment checks or records the given PaymentCreationInfo with the DB,
|
// InitPayment checks or records the given PaymentCreationInfo with the DB,
|
||||||
// making sure it does not already exist as an in-flight payment. Then this
|
// making sure it does not already exist as an in-flight payment. Then this
|
||||||
// method returns successfully, the payment is guaranteed to be in the InFlight
|
// method returns successfully, the payment is guaranteed to be in the
|
||||||
// state.
|
// Initiated state.
|
||||||
func (p *controlTower) InitPayment(paymentHash lntypes.Hash,
|
func (p *controlTower) InitPayment(paymentHash lntypes.Hash,
|
||||||
info *channeldb.PaymentCreationInfo) error {
|
info *channeldb.PaymentCreationInfo) error {
|
||||||
|
|
||||||
return p.db.InitPayment(paymentHash, info)
|
err := p.db.InitPayment(paymentHash, info)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Take lock before querying the db to prevent missing or duplicating
|
||||||
|
// an update.
|
||||||
|
p.paymentsMtx.Lock(paymentHash)
|
||||||
|
defer p.paymentsMtx.Unlock(paymentHash)
|
||||||
|
|
||||||
|
payment, err := p.db.FetchPayment(paymentHash)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
p.notifySubscribers(paymentHash, payment)
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteFailedAttempts deletes all failed htlcs if the payment was
|
// DeleteFailedAttempts deletes all failed htlcs if the payment was
|
||||||
|
|||||||
@@ -196,7 +196,7 @@ func TestPaymentControlSubscribeAllSuccess(t *testing.T) {
|
|||||||
err = pControl.InitPayment(info1.PaymentIdentifier, info1)
|
err = pControl.InitPayment(info1.PaymentIdentifier, info1)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Subscription should succeed and immediately report the InFlight
|
// Subscription should succeed and immediately report the Initiated
|
||||||
// status.
|
// status.
|
||||||
subscription, err := pControl.SubscribeAllPayments()
|
subscription, err := pControl.SubscribeAllPayments()
|
||||||
require.NoError(t, err, "expected subscribe to succeed, but got: %v")
|
require.NoError(t, err, "expected subscribe to succeed, but got: %v")
|
||||||
@@ -246,8 +246,8 @@ func TestPaymentControlSubscribeAllSuccess(t *testing.T) {
|
|||||||
// for each payment.
|
// for each payment.
|
||||||
results := make(map[lntypes.Hash]*channeldb.MPPayment)
|
results := make(map[lntypes.Hash]*channeldb.MPPayment)
|
||||||
|
|
||||||
// After exactly 5 updates both payments will/should have completed.
|
// After exactly 6 updates both payments will/should have completed.
|
||||||
for i := 0; i < 5; i++ {
|
for i := 0; i < 6; i++ {
|
||||||
select {
|
select {
|
||||||
case item := <-subscription.Updates():
|
case item := <-subscription.Updates():
|
||||||
id := item.(*channeldb.MPPayment).Info.PaymentIdentifier
|
id := item.(*channeldb.MPPayment).Info.PaymentIdentifier
|
||||||
@@ -354,10 +354,6 @@ func TestPaymentControlUnsubscribeSuccess(t *testing.T) {
|
|||||||
err = pControl.InitPayment(info.PaymentIdentifier, info)
|
err = pControl.InitPayment(info.PaymentIdentifier, info)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Register a payment update.
|
|
||||||
err = pControl.RegisterAttempt(info.PaymentIdentifier, attempt)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
// Assert all subscriptions receive the update.
|
// Assert all subscriptions receive the update.
|
||||||
select {
|
select {
|
||||||
case update1 := <-subscription1.Updates():
|
case update1 := <-subscription1.Updates():
|
||||||
@@ -376,14 +372,9 @@ func TestPaymentControlUnsubscribeSuccess(t *testing.T) {
|
|||||||
// Close the first subscription.
|
// Close the first subscription.
|
||||||
subscription1.Close()
|
subscription1.Close()
|
||||||
|
|
||||||
// Register another update.
|
// Register a payment update.
|
||||||
failInfo := channeldb.HTLCFailInfo{
|
err = pControl.RegisterAttempt(info.PaymentIdentifier, attempt)
|
||||||
Reason: channeldb.HTLCFailInternal,
|
require.NoError(t, err)
|
||||||
}
|
|
||||||
_, err = pControl.FailAttempt(
|
|
||||||
info.PaymentIdentifier, attempt.AttemptID, &failInfo,
|
|
||||||
)
|
|
||||||
require.NoError(t, err, "unable to fail htlc")
|
|
||||||
|
|
||||||
// Assert only subscription 2 receives the update.
|
// Assert only subscription 2 receives the update.
|
||||||
select {
|
select {
|
||||||
@@ -398,9 +389,14 @@ func TestPaymentControlUnsubscribeSuccess(t *testing.T) {
|
|||||||
// Close the second subscription.
|
// Close the second subscription.
|
||||||
subscription2.Close()
|
subscription2.Close()
|
||||||
|
|
||||||
// Register a last update.
|
// Register another update.
|
||||||
err = pControl.RegisterAttempt(info.PaymentIdentifier, attempt)
|
failInfo := channeldb.HTLCFailInfo{
|
||||||
require.NoError(t, err)
|
Reason: channeldb.HTLCFailInternal,
|
||||||
|
}
|
||||||
|
_, err = pControl.FailAttempt(
|
||||||
|
info.PaymentIdentifier, attempt.AttemptID, &failInfo,
|
||||||
|
)
|
||||||
|
require.NoError(t, err, "unable to fail htlc")
|
||||||
|
|
||||||
// Assert no subscriptions receive the update.
|
// Assert no subscriptions receive the update.
|
||||||
require.Len(t, subscription1.Updates(), 0)
|
require.Len(t, subscription1.Updates(), 0)
|
||||||
|
|||||||
Reference in New Issue
Block a user