routing: add subscriber event constructors

This commit is contained in:
Joost Jager 2019-06-04 17:07:45 +02:00
parent ae46fb00cb
commit 7bc56f5a7b
No known key found for this signature in database
GPG Key ID: A61B9D4C393C59C7

@ -114,16 +114,31 @@ func (p *controlTower) Success(paymentHash lntypes.Hash,
// Notify subscribers of success event. // Notify subscribers of success event.
p.notifyFinalEvent( p.notifyFinalEvent(
paymentHash, PaymentResult{ paymentHash, createSuccessResult(route, preimage),
Success: true,
Preimage: preimage,
Route: route,
},
) )
return nil return nil
} }
// createSuccessResult creates a success result to send to subscribers.
func createSuccessResult(rt *route.Route,
preimage lntypes.Preimage) *PaymentResult {
return &PaymentResult{
Success: true,
Preimage: preimage,
Route: rt,
}
}
// createFailResult creates a failed result to send to subscribers.
func createFailedResult(reason channeldb.FailureReason) *PaymentResult {
return &PaymentResult{
Success: false,
FailureReason: reason,
}
}
// Fail transitions a payment into the Failed state, and records the reason the // Fail transitions a payment into the Failed state, and records the reason the
// payment failed. After invoking this method, InitPayment should return nil on // payment failed. After invoking this method, InitPayment should return nil on
// its next call for this payment hash, allowing the switch to make a // its next call for this payment hash, allowing the switch to make a
@ -138,10 +153,7 @@ func (p *controlTower) Fail(paymentHash lntypes.Hash,
// Notify subscribers of fail event. // Notify subscribers of fail event.
p.notifyFinalEvent( p.notifyFinalEvent(
paymentHash, PaymentResult{ paymentHash, createFailedResult(reason),
Success: false,
FailureReason: reason,
},
) )
return nil return nil
@ -191,16 +203,15 @@ func (p *controlTower) SubscribePayment(paymentHash lntypes.Hash) (
// a subscriber, because we can send the result on the channel // a subscriber, because we can send the result on the channel
// immediately. // immediately.
case channeldb.StatusSucceeded: case channeldb.StatusSucceeded:
event.Success = true event = *createSuccessResult(
event.Preimage = *payment.PaymentPreimage &payment.Attempt.Route, *payment.PaymentPreimage,
event.Route = &payment.Attempt.Route )
// Payment already failed. It is not necessary to register as a // Payment already failed. It is not necessary to register as a
// subscriber, because we can send the result on the channel // subscriber, because we can send the result on the channel
// immediately. // immediately.
case channeldb.StatusFailed: case channeldb.StatusFailed:
event.Success = false event = *createFailedResult(*payment.Failure)
event.FailureReason = *payment.Failure
default: default:
return false, nil, errors.New("unknown payment status") return false, nil, errors.New("unknown payment status")
@ -216,7 +227,7 @@ func (p *controlTower) SubscribePayment(paymentHash lntypes.Hash) (
// notifyFinalEvent sends a final payment event to all subscribers of this // notifyFinalEvent sends a final payment event to all subscribers of this
// payment. The channel will be closed after this. // payment. The channel will be closed after this.
func (p *controlTower) notifyFinalEvent(paymentHash lntypes.Hash, func (p *controlTower) notifyFinalEvent(paymentHash lntypes.Hash,
event PaymentResult) { event *PaymentResult) {
// Get all subscribers for this hash. As there is only a single outcome, // Get all subscribers for this hash. As there is only a single outcome,
// the subscriber list can be cleared. // the subscriber list can be cleared.
@ -232,7 +243,7 @@ func (p *controlTower) notifyFinalEvent(paymentHash lntypes.Hash,
// Notify all subscribers of the event. The subscriber channel is // Notify all subscribers of the event. The subscriber channel is
// buffered, so it cannot block here. // buffered, so it cannot block here.
for _, subscriber := range list { for _, subscriber := range list {
subscriber <- event subscriber <- *event
close(subscriber) close(subscriber)
} }
} }