Skip to content

Commit eb2f5f3

Browse files
committed
xpay: more accurately reflect pay when xpay-handle-pay is set.
Note that the slight code reorder changes the JSON order, which is generally undefined, but our doc checker is very strict! Changelog-Changed: `xpay` now gives the same JSON success return as documented by `pay` when `xpay-handle-pay` is set. Signed-off-by: Rusty Russell <[email protected]> Fixes: #7923
1 parent 2c12f67 commit eb2f5f3

File tree

5 files changed

+54
-25
lines changed

5 files changed

+54
-25
lines changed

contrib/msggen/msggen/schema.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36428,10 +36428,10 @@
3642836428
},
3642936429
"response": {
3643036430
"payment_preimage": "paymentpreimgxp1010101010101010101010101010101010101010101010101",
36431-
"failed_parts": 0,
36432-
"successful_parts": 1,
3643336431
"amount_msat": 10000,
36434-
"amount_sent_msat": 10002
36432+
"amount_sent_msat": 10002,
36433+
"failed_parts": 0,
36434+
"successful_parts": 1
3643536435
}
3643636436
},
3643736437
{
@@ -36444,10 +36444,10 @@
3644436444
},
3644536445
"response": {
3644636446
"payment_preimage": "paymentpreimgxp2020202020202020202020202020202020202020202020202",
36447-
"failed_parts": 0,
36448-
"successful_parts": 1,
3644936447
"amount_msat": 1000,
36450-
"amount_sent_msat": 1000
36448+
"amount_sent_msat": 1000,
36449+
"failed_parts": 0,
36450+
"successful_parts": 1
3645136451
}
3645236452
}
3645336453
]

doc/lightningd-config.5.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,8 +540,7 @@ command, so they invoices can also be paid onchain.
540540

541541
* **xpay-handle-pay**=*BOOL* [plugin `xpay`, *dynamic*]
542542

543-
Setting this makes `xpay` intercept simply `pay` commands (default `false`). Note that the
544-
response will be different from the normal pay command, however.
543+
Setting this makes `xpay` intercept simply `pay` commands (default `false`).
545544

546545
### Networking options
547546

doc/schemas/lightning-xpay.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,10 @@
132132
},
133133
"response": {
134134
"payment_preimage": "paymentpreimgxp1010101010101010101010101010101010101010101010101",
135-
"failed_parts": 0,
136-
"successful_parts": 1,
137135
"amount_msat": 10000,
138-
"amount_sent_msat": 10002
136+
"amount_sent_msat": 10002,
137+
"failed_parts": 0,
138+
"successful_parts": 1
139139
}
140140
},
141141
{
@@ -148,10 +148,10 @@
148148
},
149149
"response": {
150150
"payment_preimage": "paymentpreimgxp2020202020202020202020202020202020202020202020202",
151-
"failed_parts": 0,
152-
"successful_parts": 1,
153151
"amount_msat": 1000,
154-
"amount_sent_msat": 1000
152+
"amount_sent_msat": 1000,
153+
"failed_parts": 0,
154+
"successful_parts": 1
155155
}
156156
}
157157
]

plugins/xpay/xpay.c

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ struct payment {
111111

112112
/* Requests currently outstanding */
113113
struct out_req **requests;
114+
115+
/* Are we pretending to be "pay"? */
116+
bool pay_compat;
117+
/* When did we start? */
118+
struct timeabs start_time;
114119
};
115120

116121
/* One step in a path. */
@@ -374,11 +379,19 @@ static void payment_succeeded(struct payment *payment,
374379
if (payment->cmd) {
375380
js = jsonrpc_stream_success(payment->cmd);
376381
json_add_preimage(js, "payment_preimage", preimage);
377-
json_add_u64(js, "failed_parts", payment->num_failures);
378-
json_add_u64(js, "successful_parts",
379-
payment->total_num_attempts - payment->num_failures);
380382
json_add_amount_msat(js, "amount_msat", payment->amount);
381383
json_add_amount_msat(js, "amount_sent_msat", total_sent(payment, attempt));
384+
/* Pay's schema expects these fields */
385+
if (payment->pay_compat) {
386+
json_add_u64(js, "parts", payment->total_num_attempts);
387+
json_add_sha256(js, "payment_hash", &payment->payment_hash);
388+
json_add_string(js, "status", "complete");
389+
json_add_u64(js, "created_at", (u64)payment->start_time.ts.tv_sec);
390+
} else {
391+
json_add_u64(js, "failed_parts", payment->num_failures);
392+
json_add_u64(js, "successful_parts",
393+
payment->total_num_attempts - payment->num_failures);
394+
}
382395
was_pending(command_finished(payment->cmd, js));
383396
payment->cmd = NULL;
384397
}
@@ -1436,9 +1449,10 @@ preapproveinvoice_succeed(struct command *cmd,
14361449
return populate_private_layer(cmd, payment);
14371450
}
14381451

1439-
static struct command_result *json_xpay(struct command *cmd,
1440-
const char *buffer,
1441-
const jsmntok_t *params)
1452+
static struct command_result *json_xpay_core(struct command *cmd,
1453+
const char *buffer,
1454+
const jsmntok_t *params,
1455+
bool as_pay)
14421456
{
14431457
struct xpay *xpay = xpay_of(cmd->plugin);
14441458
struct amount_msat *msat, *maxfee, *partial;
@@ -1468,6 +1482,8 @@ static struct command_result *json_xpay(struct command *cmd,
14681482
payment->requests = tal_arr(payment, struct out_req *, 0);
14691483
payment->prior_results = tal_strdup(payment, "");
14701484
payment->deadline = timemono_add(time_mono(), time_from_sec(*retryfor));
1485+
payment->start_time = time_now();
1486+
payment->pay_compat = as_pay;
14711487

14721488
if (bolt12_has_prefix(payment->invstring)) {
14731489
struct gossmap *gossmap = get_gossmap(xpay);
@@ -1595,6 +1611,20 @@ static struct command_result *json_xpay(struct command *cmd,
15951611
return send_outreq(req);
15961612
}
15971613

1614+
static struct command_result *json_xpay(struct command *cmd,
1615+
const char *buffer,
1616+
const jsmntok_t *params)
1617+
{
1618+
return json_xpay_core(cmd, buffer, params, false);
1619+
}
1620+
1621+
static struct command_result *json_xpay_as_pay(struct command *cmd,
1622+
const char *buffer,
1623+
const jsmntok_t *params)
1624+
{
1625+
return json_xpay_core(cmd, buffer, params, true);
1626+
}
1627+
15981628
static struct command_result *getchaininfo_done(struct command *aux_cmd,
15991629
const char *method,
16001630
const char *buf,
@@ -1727,6 +1757,10 @@ static const struct plugin_command commands[] = {
17271757
"xpay",
17281758
json_xpay,
17291759
},
1760+
{
1761+
"xpay-as-pay",
1762+
json_xpay_as_pay,
1763+
},
17301764
};
17311765

17321766
static struct command_result *handle_block_added(struct command *cmd,
@@ -1930,7 +1964,7 @@ static struct command_result *handle_rpc_command(struct command *cmd,
19301964
json_object_start(response, "replace");
19311965
json_add_string(response, "jsonrpc", "2.0");
19321966
json_add_tok(response, "id", id_tok, buf);
1933-
json_add_string(response, "method", "xpay");
1967+
json_add_string(response, "method", "xpay-as-pay");
19341968
json_object_start(response, "params");
19351969
json_add_tok(response, "invstring", bolt11, buf);
19361970
if (amount_msat)

tests/test_xpay.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,6 @@ def test_xpay_takeover(node_factory, executor):
368368
l1, l2, l3 = node_factory.line_graph(3, wait_for_announce=True,
369369
opts={'xpay-handle-pay': True})
370370

371-
# xpay does NOT look like pay!
372-
l1.rpc.jsonschemas = {}
373-
l2.rpc.jsonschemas = {}
374-
375371
# Simple bolt11/bolt12 payment.
376372
inv = l3.rpc.invoice(100000, "test_xpay_takeover1", "test_xpay_takeover1")['bolt11']
377373
l1.rpc.pay(inv)

0 commit comments

Comments
 (0)