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