@@ -121,6 +121,13 @@ const (
121
121
)
122
122
123
123
// decideNextStep is used to determine the next step in the payment lifecycle.
124
+ // It first checks whether the current state of the payment allows more HTLC
125
+ // attempts to be made. If allowed, it will return so the lifecycle can continue
126
+ // making new attempts. Otherwise, it checks whether we need to wait for the
127
+ // results of already sent attempts. If needed, it will block until one of the
128
+ // results is sent back. then process its result here. When there's no need to
129
+ // wait for results, the method will exit with `stepExit` such that the payment
130
+ // lifecycle loop will terminate.
124
131
func (p * paymentLifecycle ) decideNextStep (
125
132
payment DBMPPayment ) (stateStep , error ) {
126
133
@@ -130,53 +137,53 @@ func (p *paymentLifecycle) decideNextStep(
130
137
return stepExit , err
131
138
}
132
139
133
- if ! allow {
134
- // Check whether we need to wait for results.
135
- wait , err := payment .NeedWaitAttempts ()
136
- if err != nil {
137
- return stepExit , err
138
- }
139
-
140
- // If we are not allowed to make new HTLC attempts and there's
141
- // no need to wait, the lifecycle is done and we can exit.
142
- if ! wait {
143
- return stepExit , nil
144
- }
140
+ // Exit early we need to make more attempts.
141
+ if allow {
142
+ return stepProceed , nil
143
+ }
145
144
146
- log .Tracef ("Waiting for attempt results for payment %v" ,
147
- p .identifier )
145
+ // We cannot make more attempts, we now check whether we need to wait
146
+ // for results.
147
+ wait , err := payment .NeedWaitAttempts ()
148
+ if err != nil {
149
+ return stepExit , err
150
+ }
148
151
149
- // Otherwise we wait for the result for one HTLC attempt then
150
- // continue the lifecycle.
151
- select {
152
- case r := <- p .resultCollected :
153
- log .Tracef ("Received attempt result for payment %v" ,
154
- p .identifier )
152
+ // If we are not allowed to make new HTLC attempts and there's no need
153
+ // to wait, the lifecycle is done and we can exit.
154
+ if ! wait {
155
+ return stepExit , nil
156
+ }
155
157
156
- // Handle the result here. If there's no error, we will
157
- // return stepSkip and move to the next lifecycle
158
- // iteration, which will refresh the payment and wait
159
- // for the next attempt result, if any.
160
- _ , err := p .handleAttemptResult (r .attempt , r .result )
158
+ log .Tracef ("Waiting for attempt results for payment %v" , p .identifier )
161
159
162
- // We would only get a DB-related error here, which will
163
- // cause us to abort the payment flow.
164
- if err != nil {
165
- return stepExit , err
166
- }
160
+ // Otherwise we wait for the result for one HTLC attempt then continue
161
+ // the lifecycle.
162
+ select {
163
+ case r := <- p .resultCollected :
164
+ log .Tracef ("Received attempt result for payment %v" ,
165
+ p .identifier )
167
166
168
- case <- p .quit :
169
- return stepExit , ErrPaymentLifecycleExiting
167
+ // Handle the result here. If there's no error, we will return
168
+ // stepSkip and move to the next lifecycle iteration, which will
169
+ // refresh the payment and wait for the next attempt result, if
170
+ // any.
171
+ _ , err := p .handleAttemptResult (r .attempt , r .result )
170
172
171
- case <- p .router .quit :
172
- return stepExit , ErrRouterShuttingDown
173
+ // We would only get a DB-related error here, which will cause
174
+ // us to abort the payment flow.
175
+ if err != nil {
176
+ return stepExit , err
173
177
}
174
178
175
- return stepSkip , nil
179
+ case <- p .quit :
180
+ return stepExit , ErrPaymentLifecycleExiting
181
+
182
+ case <- p .router .quit :
183
+ return stepExit , ErrRouterShuttingDown
176
184
}
177
185
178
- // Otherwise we need to make more attempts.
179
- return stepProceed , nil
186
+ return stepSkip , nil
180
187
}
181
188
182
189
// resumePayment resumes the paymentLifecycle from the current state.
0 commit comments