This function is called whenever a payment ends up in a final state, or all * leafs in the subtree rooted in the payment are all in a final state. It is * called only once, and it is guaranteed to be called in post-order * traversal, i.e., all children are finished before the parent is called. */
| 2207 | * called only once, and it is guaranteed to be called in post-order |
| 2208 | * traversal, i.e., all children are finished before the parent is called. */ |
| 2209 | static struct command_result *payment_finished(struct payment *p) |
| 2210 | { |
| 2211 | struct payment_tree_result result = payment_collect_result(p); |
| 2212 | struct json_stream *ret; |
| 2213 | struct command *cmd = p->cmd; |
| 2214 | const char *msg; |
| 2215 | |
| 2216 | /* Either none of the leaf attempts succeeded yet, or we have a |
| 2217 | * preimage. */ |
| 2218 | assert((result.leafstates & PAYMENT_STEP_SUCCESS) == 0 || |
| 2219 | result.preimage != NULL); |
| 2220 | |
| 2221 | if (p->parent != NULL) |
| 2222 | return payment_child_finished(p->parent, p); |
| 2223 | |
| 2224 | if (p->finished) { |
| 2225 | /* cmd is the aux_command: real cmd has been freed. */ |
| 2226 | return command_still_pending(p->cmd); |
| 2227 | } |
| 2228 | |
| 2229 | /* on_payment_failure / on_payment_success looks at this! */ |
| 2230 | p->finished = true; |
| 2231 | if (payment_is_success(p)) { |
| 2232 | assert(result.treestates & PAYMENT_STEP_SUCCESS); |
| 2233 | assert(result.leafstates & PAYMENT_STEP_SUCCESS); |
| 2234 | assert(result.preimage != NULL); |
| 2235 | |
| 2236 | /* Call any callback we might have registered. */ |
| 2237 | if (p->on_payment_success != NULL) |
| 2238 | p->on_payment_success(p); |
| 2239 | |
| 2240 | ret = jsonrpc_stream_success(cmd); |
| 2241 | json_add_payment_success(ret, p, result.preimage, |
| 2242 | &result); |
| 2243 | } else if (p->aborterror != NULL) { |
| 2244 | /* We set an explicit toplevel error message, |
| 2245 | * so let's report that. */ |
| 2246 | ret = jsonrpc_stream_fail(cmd, p->errorcode, |
| 2247 | p->aborterror); |
| 2248 | payment_json_add_attempts(ret, "attempts", p); |
| 2249 | |
| 2250 | payment_notify_failure(p, p->aborterror); |
| 2251 | } else if (result.failure == NULL || result.failure->failcode < NODE) { |
| 2252 | if (p->on_payment_failure != NULL) |
| 2253 | p->on_payment_failure(p); |
| 2254 | |
| 2255 | /* This is failing because we have no more routes to try */ |
| 2256 | msg = tal_fmt(cmd, |
| 2257 | "Ran out of routes to try after " |
| 2258 | "%d attempt%s: see `paystatus`", |
| 2259 | result.attempts, |
| 2260 | result.attempts == 1 ? "" : "s"); |
| 2261 | ret = jsonrpc_stream_fail(cmd, PAY_STOPPED_RETRYING, |
| 2262 | msg); |
| 2263 | payment_json_add_attempts(ret, "attempts", p); |
| 2264 | |
| 2265 | payment_notify_failure(p, msg); |
| 2266 | } else { |
no test coverage detected