Skip to content

Commit 7b08aff

Browse files
committed
xpay: add xpay_attempt_start and xpay_attempt_end notifications.
Requested-by: Michael at Boltz Signed-off-by: Rusty Russell <[email protected]> Changelog-Added: Plugins: `xpay` now publishes `xpay_attempt_start` and `xpay_attempt_end` notifications on every payment send attempt.
1 parent faf08f8 commit 7b08aff

File tree

2 files changed

+157
-1
lines changed

2 files changed

+157
-1
lines changed

doc/developers-guide/plugin-development/event-notifications.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,3 +567,81 @@ Where:
567567
- `plugin_name`: The short name of the plugin.
568568
- `plugin_path`: The full file path to the plugin executable.
569569
- `methods`: An array of RPC method names that the plugin registered.
570+
571+
572+
### `xpay_attempt_start` (v25.09 onward)
573+
574+
Emitted by `xpay` when part of a payment begins. `payment_hash` and
575+
`groupid` uniquely identify this xpay invocation, and `partid` then identifes
576+
this particular attempt to pay part of that.
577+
578+
`total_payment_msat` is the total amount (usually the invoice amount),
579+
which will be the same across all parts, adn `attempt_msat` is the
580+
amount being delivered to the destination by this part.
581+
582+
Each element in `hops` shows the amount going into the node (i.e. with
583+
fees, `amount_with_fees_msat`) and the amount we're telling it to send
584+
to the other end (`amount_reaching_next_node_msat`). The final
585+
`amount_reaching_next_node_msat` will be equal to the `attempt_msat`.
586+
587+
```json
588+
{
589+
"xpay_attempt_start": {
590+
"payment_hash": "f5a6a059a25d1e329d9b094aeeec8c2191ca037d3f5b0662e21ae850debe8ea2",
591+
"groupid": 1,
592+
"partid": 1,
593+
"total_payment_msat": 100000,
594+
"attempt_msat": 5000,
595+
"hops": [
596+
{
597+
"next_node": "035d2b1192dfba134e10e540875d366ebc8bc353d5aa766b80c090b39c3a5d885d",
598+
"short_channel_id": "1x2x3",
599+
"direction": 1,
600+
"amount_with_fees_msat": 100030,
601+
"amount_reaching_next_node_msat": 100030
602+
},
603+
{
604+
"next_node": "022d223620a359a47ff7f7ac447c85c46c923da53389221a0054c11c1e3ca31d59",
605+
"short_channel_id": "2x3x4",
606+
"direction": 0,
607+
"amount_with_fees_msat": 100030,
608+
"amount_reaching_next_node_msat": 100000
609+
}
610+
]
611+
}
612+
}
613+
```
614+
615+
### `xpay_attempt_end` (v25.09 onward)
616+
617+
Emitted by `xpay` when part of a payment ends. `payment_hash`, `groupid` and `partid`
618+
will match a previous `xpay_attempt_start`.
619+
620+
`status` will be "success" or "failure". `duration` will be an integer (with 9 decimal places)
621+
in seconds, between the time `xpay` tells lightningd to send the onion, to when `xpay` processes
622+
the response.
623+
624+
If `status` is "failure", there will always be an `error_message`: the other fields below
625+
will be missing in the unusual case where the error onion is corrupted.
626+
627+
`failed_node_id`: If it's a non-local error, the source of the error.
628+
`failed_short_channel_id`: if it's not the final node, the channel it's complaining about.
629+
`failed_direction`: if it's not the final node, the channel direction.
630+
`error_code`: the error code returned (present unless onion was corrupted).
631+
`error_message`: always present: if `failed_node_id` is present it's just the name of the `error_code`, but otherwise it can be a more informative error from our own node.
632+
633+
```json
634+
{
635+
"xpay_attempt_end": {
636+
"payment_hash": "f5a6a059a25d1e329d9b094aeeec8c2191ca037d3f5b0662e21ae850debe8ea2",
637+
"groupid": 12345677890,
638+
"partid": 1,
639+
"status": "failure",
640+
"failed_node_id": "035d2b1192dfba134e10e540875d366ebc8bc353d5aa766b80c090b39c3a5d885d",
641+
"failed_short_channel_id": "1x2x3",
642+
"failed_direction": 1,
643+
"error_code": 4103,
644+
"error_message": "temporary_channel_failure"
645+
}
646+
}
647+
```

plugins/xpay/xpay.c

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ struct attempt {
149149

150150
struct payment *payment;
151151
struct amount_msat delivers;
152+
struct timemono start_time;
152153

153154
/* Path we tried, so we can unreserve, and tell askrene the results */
154155
const struct hop *hops;
@@ -536,6 +537,70 @@ static struct amount_msat total_delivered(const struct payment *payment)
536537
return sum;
537538
}
538539

540+
/* We can notify others of what the details are, so they can do their own
541+
* layer heuristics. */
542+
static void json_add_attempt_fields(struct json_stream *js,
543+
const struct attempt *attempt)
544+
{
545+
/* These three uniquely identify this attempt */
546+
json_add_sha256(js, "payment_hash", &attempt->payment->payment_hash);
547+
json_add_u64(js, "groupid", attempt->payment->group_id);
548+
json_add_u64(js, "partid", attempt->partid);
549+
}
550+
551+
static void outgoing_notify_start(const struct attempt *attempt)
552+
{
553+
struct json_stream *js = plugin_notification_start(NULL, "xpay_attempt_start");
554+
json_add_attempt_fields(js, attempt);
555+
json_add_amount_msat(js, "total_payment_msat", attempt->payment->amount);
556+
json_add_amount_msat(js, "attempt_msat", attempt->delivers);
557+
json_array_start(js, "hops");
558+
for (size_t i = 0; i < tal_count(attempt->hops); i++) {
559+
const struct hop *hop = &attempt->hops[i];
560+
json_object_start(js, NULL);
561+
json_add_pubkey(js, "next_node", &hop->next_node);
562+
json_add_short_channel_id(js, "short_channel_id", hop->scidd.scid);
563+
json_add_u32(js, "direction", hop->scidd.dir);
564+
json_add_amount_msat(js, "amount_with_fees_msat", hop->amount_in);
565+
json_add_amount_msat(js, "amount_reaching_next_node_msat", hop->amount_out);
566+
json_object_end(js);
567+
}
568+
json_array_end(js);
569+
plugin_notification_end(attempt->payment->plugin, js);
570+
}
571+
572+
static void outgoing_notify_success(const struct attempt *attempt)
573+
{
574+
struct json_stream *js = plugin_notification_start(NULL, "xpay_attempt_end");
575+
json_add_string(js, "status", "success");
576+
json_add_timerel(js, "duration", timemono_between(time_mono(), attempt->start_time));
577+
json_add_attempt_fields(js, attempt);
578+
plugin_notification_end(attempt->payment->plugin, js);
579+
}
580+
581+
static void outgoing_notify_failure(const struct attempt *attempt,
582+
int failindex, int errcode,
583+
const char *errstr)
584+
{
585+
struct json_stream *js = plugin_notification_start(NULL, "xpay_attempt_end");
586+
json_add_string(js, "status", "failure");
587+
json_add_attempt_fields(js, attempt);
588+
json_add_timerel(js, "duration", timemono_between(time_mono(), attempt->start_time));
589+
if (failindex != -1) {
590+
if (failindex != 0)
591+
json_add_pubkey(js, "failed_node_id", &attempt->hops[failindex-1].next_node);
592+
if (failindex != tal_count(attempt->hops)) {
593+
const struct hop *hop = &attempt->hops[failindex];
594+
json_add_short_channel_id(js, "failed_short_channel_id", hop->scidd.scid);
595+
json_add_u32(js, "failed_direction", hop->scidd.dir);
596+
}
597+
}
598+
if (errcode != -1)
599+
json_add_u32(js, "error_code", errcode);
600+
json_add_string(js, "error_message", errstr);
601+
plugin_notification_end(attempt->payment->plugin, js);
602+
}
603+
539604
static void update_knowledge_from_error(struct command *aux_cmd,
540605
const char *buf,
541606
const jsmntok_t *error,
@@ -590,6 +655,7 @@ static void update_knowledge_from_error(struct command *aux_cmd,
590655

591656
/* Garbled? Blame random hop. */
592657
if (!replymsg) {
658+
outgoing_notify_failure(attempt, -1, -1, "Garbled error message");
593659
index = pseudorand(tal_count(attempt->hops));
594660
description = "Garbled error message";
595661
add_result_summary(attempt, LOG_UNUSUAL,
@@ -627,6 +693,7 @@ static void update_knowledge_from_error(struct command *aux_cmd,
627693
} else
628694
errmsg = failcode_name;
629695

696+
outgoing_notify_failure(attempt, index, failcode, errmsg);
630697
description = tal_fmt(tmpctx,
631698
"Error %s for path %s, from %s",
632699
errmsg,
@@ -881,6 +948,8 @@ static struct command_result *injectpaymentonion_succeeded(struct command *aux_c
881948
plugin_err(aux_cmd->plugin, "Invalid injectpaymentonion result '%.*s'",
882949
json_tok_full_len(result), json_tok_full(buf, result));
883950

951+
outgoing_notify_success(attempt);
952+
884953
/* Move from current_attempts to past_attempts */
885954
list_del_from(&payment->current_attempts, &attempt->list);
886955
list_add(&payment->past_attempts, &attempt->list);
@@ -1008,6 +1077,9 @@ static struct command_result *do_inject(struct command *aux_cmd,
10081077
return command_still_pending(aux_cmd);
10091078
}
10101079

1080+
outgoing_notify_start(attempt);
1081+
attempt->start_time = time_mono();
1082+
10111083
req = jsonrpc_request_start(aux_cmd,
10121084
"injectpaymentonion",
10131085
injectpaymentonion_succeeded,
@@ -2118,6 +2190,12 @@ static const struct plugin_hook hooks[] = {
21182190
},
21192191
};
21202192

2193+
/* Notifications for each payment part we attempt */
2194+
static const char *outgoing_notifications[] = {
2195+
"xpay_attempt_start",
2196+
"xpay_attempt_end",
2197+
};
2198+
21212199
int main(int argc, char *argv[])
21222200
{
21232201
struct xpay *xpay;
@@ -2131,7 +2209,7 @@ int main(int argc, char *argv[])
21312209
commands, ARRAY_SIZE(commands),
21322210
notifications, ARRAY_SIZE(notifications),
21332211
hooks, ARRAY_SIZE(hooks),
2134-
NULL, 0,
2212+
outgoing_notifications, ARRAY_SIZE(outgoing_notifications),
21352213
plugin_option_dynamic("xpay-handle-pay", "bool",
21362214
"Make xpay take over pay commands it can handle.",
21372215
bool_option, bool_jsonfmt, &xpay->take_over_pay),

0 commit comments

Comments
 (0)