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.
p.notifyFinalEvent(
paymentHash, PaymentResult{
Success: true,
Preimage: preimage,
Route: route,
},
paymentHash, createSuccessResult(route, preimage),
)
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
// payment failed. After invoking this method, InitPayment should return nil on
// 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.
p.notifyFinalEvent(
paymentHash, PaymentResult{
Success: false,
FailureReason: reason,
},
paymentHash, createFailedResult(reason),
)
return nil
@ -191,16 +203,15 @@ func (p *controlTower) SubscribePayment(paymentHash lntypes.Hash) (
// a subscriber, because we can send the result on the channel
// immediately.
case channeldb.StatusSucceeded:
event.Success = true
event.Preimage = *payment.PaymentPreimage
event.Route = &payment.Attempt.Route
event = *createSuccessResult(
&payment.Attempt.Route, *payment.PaymentPreimage,
)
// Payment already failed. It is not necessary to register as a
// subscriber, because we can send the result on the channel
// immediately.
case channeldb.StatusFailed:
event.Success = false
event.FailureReason = *payment.Failure
event = *createFailedResult(*payment.Failure)
default:
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
// payment. The channel will be closed after this.
func (p *controlTower) notifyFinalEvent(paymentHash lntypes.Hash,
event PaymentResult) {
event *PaymentResult) {
// Get all subscribers for this hash. As there is only a single outcome,
// 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
// buffered, so it cannot block here.
for _, subscriber := range list {
subscriber <- event
subscriber <- *event
close(subscriber)
}
}