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. */
| 1889 | * called only once, and it is guaranteed to be called in post-order |
| 1890 | * traversal, i.e., all children are finished before the parent is called. */ |
| 1891 | static void payment_finished(struct payment *p) |
| 1892 | { |
| 1893 | struct payment_tree_result result = payment_collect_result(p); |
| 1894 | struct json_stream *ret; |
| 1895 | struct command *cmd = p->cmd; |
| 1896 | const char *msg; |
| 1897 | struct json_stream *n; |
| 1898 | struct payment *root = payment_root(p); |
| 1899 | |
| 1900 | /* Either none of the leaf attempts succeeded yet, or we have a |
| 1901 | * preimage. */ |
| 1902 | assert((result.leafstates & PAYMENT_STEP_SUCCESS) == 0 || |
| 1903 | result.preimage != NULL); |
| 1904 | |
| 1905 | if (p->parent == NULL) { |
| 1906 | /* We are about to reply, unset the pointer to the cmd so we |
| 1907 | * don't attempt to return a response twice. */ |
| 1908 | p->cmd = NULL; |
| 1909 | if (cmd == NULL) { |
| 1910 | /* This is the tree root, but we already reported |
| 1911 | * success or failure, so noop. */ |
| 1912 | return; |
| 1913 | } else if (payment_is_success(p)) { |
| 1914 | assert(result.treestates & PAYMENT_STEP_SUCCESS); |
| 1915 | assert(result.leafstates & PAYMENT_STEP_SUCCESS); |
| 1916 | assert(result.preimage != NULL); |
| 1917 | |
| 1918 | /* Call any callback we might have registered. */ |
| 1919 | if (p->on_payment_success != NULL) |
| 1920 | p->on_payment_success(p); |
| 1921 | |
| 1922 | ret = jsonrpc_stream_success(cmd); |
| 1923 | json_add_node_id(ret, "destination", p->destination); |
| 1924 | json_add_sha256(ret, "payment_hash", p->payment_hash); |
| 1925 | json_add_timeabs(ret, "created_at", p->start_time); |
| 1926 | json_add_num(ret, "parts", result.attempts); |
| 1927 | |
| 1928 | json_add_amount_msat_compat(ret, p->amount, "msatoshi", |
| 1929 | "amount_msat"); |
| 1930 | json_add_amount_msat_compat(ret, result.sent, |
| 1931 | "msatoshi_sent", |
| 1932 | "amount_sent_msat"); |
| 1933 | |
| 1934 | if (result.leafstates != PAYMENT_STEP_SUCCESS) |
| 1935 | json_add_string( |
| 1936 | ret, "warning_partial_completion", |
| 1937 | "Some parts of the payment are not yet " |
| 1938 | "completed, but we have the confirmation " |
| 1939 | "from the recipient."); |
| 1940 | json_add_preimage(ret, "payment_preimage", result.preimage); |
| 1941 | |
| 1942 | json_add_string(ret, "status", "complete"); |
| 1943 | |
| 1944 | n = plugin_notification_start(p->plugin, "pay_success"); |
| 1945 | json_add_sha256(n, "payment_hash", p->payment_hash); |
| 1946 | if (root->invstring != NULL) |
| 1947 | json_add_string(n, "bolt11", root->invstring); |
| 1948 | plugin_notification_end(p->plugin, n); |
no test coverage detected