Skip to content

Commit

Permalink
waitsendpay: add partid arg.
Browse files Browse the repository at this point in the history
We need to be able to wait for a unique payment, now payment_hash is not
always unique.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell authored and cdecker committed Dec 12, 2019
1 parent 63fffd4 commit b670b51
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
24 changes: 19 additions & 5 deletions lightningd/pay.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ struct sendpay_command {
struct list_node list;

struct sha256 payment_hash;
u64 partid;
struct command *cmd;
};

Expand All @@ -50,11 +51,13 @@ static void destroy_sendpay_command(struct sendpay_command *pc)
static void
add_sendpay_waiter(struct lightningd *ld,
struct command *cmd,
const struct sha256 *payment_hash)
const struct sha256 *payment_hash,
u64 partid)
{
struct sendpay_command *pc = tal(cmd, struct sendpay_command);

pc->payment_hash = *payment_hash;
pc->partid = partid;
pc->cmd = cmd;
list_add(&ld->sendpay_commands, &pc->list);
tal_add_destructor(pc, destroy_sendpay_command);
Expand All @@ -65,11 +68,13 @@ add_sendpay_waiter(struct lightningd *ld,
static void
add_waitsendpay_waiter(struct lightningd *ld,
struct command *cmd,
const struct sha256 *payment_hash)
const struct sha256 *payment_hash,
u64 partid)
{
struct sendpay_command *pc = tal(cmd, struct sendpay_command);

pc->payment_hash = *payment_hash;
pc->partid = partid;
pc->cmd = cmd;
list_add(&ld->waitsendpay_commands, &pc->list);
tal_add_destructor(pc, destroy_sendpay_command);
Expand Down Expand Up @@ -252,6 +257,8 @@ static void tell_waiters_failed(struct lightningd *ld,
list_for_each_safe(&ld->waitsendpay_commands, pc, next, list) {
if (!sha256_eq(payment_hash, &pc->payment_hash))
continue;
if (payment->partid != pc->partid)
continue;

sendpay_fail(pc->cmd, payment,
pay_errcode, onionreply, fail, details);
Expand All @@ -269,6 +276,8 @@ static void tell_waiters_success(struct lightningd *ld,
list_for_each_safe(&ld->waitsendpay_commands, pc, next, list) {
if (!sha256_eq(payment_hash, &pc->payment_hash))
continue;
if (payment->partid != pc->partid)
continue;

sendpay_success(pc->cmd, payment);
}
Expand Down Expand Up @@ -623,9 +632,12 @@ static struct command_result *wait_payment(struct lightningd *ld,
payment_hash));
}

log_debug(cmd->ld->log, "Payment part %"PRIu64"/%"PRIu64" status %u",
partid, payment->partid, payment->status);

switch (payment->status) {
case PAYMENT_PENDING:
add_waitsendpay_waiter(ld, cmd, payment_hash);
add_waitsendpay_waiter(ld, cmd, payment_hash, partid);
return NULL;

case PAYMENT_COMPLETE:
Expand Down Expand Up @@ -913,7 +925,7 @@ send_payment_core(struct lightningd *ld,
/* We write this into db when HTLC is actually sent. */
wallet_payment_setup(ld->wallet, payment);

add_sendpay_waiter(ld, cmd, rhash);
add_sendpay_waiter(ld, cmd, rhash, partid);
return command_still_pending(cmd);
}

Expand Down Expand Up @@ -1344,14 +1356,16 @@ static struct command_result *json_waitsendpay(struct command *cmd,
struct sha256 *rhash;
unsigned int *timeout;
struct command_result *res;
u64 *partid;

if (!param(cmd, buffer, params,
p_req("payment_hash", param_sha256, &rhash),
p_opt("timeout", param_number, &timeout),
p_opt_def("partid", param_u64, &partid, 0),
NULL))
return command_param_failed();

res = wait_payment(cmd->ld, cmd, rhash, /* FIXME: Set partid! */0);
res = wait_payment(cmd->ld, cmd, rhash, *partid);
if (res)
return res;

Expand Down
15 changes: 12 additions & 3 deletions tests/test_pay.py
Original file line number Diff line number Diff line change
Expand Up @@ -2569,11 +2569,11 @@ def serialize_payload(n):
assert(e.error['data']['raw_message'] == "400f00000000000003e80000006c")


@unittest.skipIf(not DEVELOPER, "needs DEVELOPER=1")
@unittest.skipIf(not DEVELOPER, "needs dev-disconnect, dev-no-htlc-timeout")
@unittest.skipIf(not EXPERIMENTAL_FEATURES, "needs partid support")
def test_partial_payment(node_factory, bitcoind, executor):
# We want to test two payments at the same time, before we send commit
l1, l2, l3, l4 = node_factory.get_nodes(4, [{}] + [{'disconnect': ['=WIRE_UPDATE_ADD_HTLC-nocommit']}] * 2 + [{}])
l1, l2, l3, l4 = node_factory.get_nodes(4, [{}] + [{'disconnect': ['=WIRE_UPDATE_ADD_HTLC-nocommit'], 'dev-no-htlc-timeout': None}] * 2 + [{}])

# Two routes to l4: one via l2, and one via l3.
l1.rpc.connect(l2.info['id'], 'localhost', l2.port)
Expand Down Expand Up @@ -2621,8 +2621,17 @@ def test_partial_payment(node_factory, bitcoind, executor):
l1.rpc.call('sendpay', [r124, inv['payment_hash'], None, 1000, inv['bolt11'], paysecret, 1])
l1.rpc.call('sendpay', [r134, inv['payment_hash'], None, 1000, inv['bolt11'], paysecret, 2])

# Make sure they've done the suppress-commitment thing before we unsuppress
l2.daemon.wait_for_log(r'dev_disconnect')
l3.daemon.wait_for_log(r'dev_disconnect')

# Now continue, payments will fail because receiver doesn't do MPP.
l2.rpc.dev_reenable_commit(l4.info['id'])
l3.rpc.dev_reenable_commit(l4.info['id'])

# FIXME: waitsendpay needs a 'partid' field.
# See FIXME in peer_htlcs: WIRE_INVALID_ONION_PAYLOAD would be better.
with pytest.raises(RpcError, match=r'WIRE_FINAL_INCORRECT_HTLC_AMOUNT'):
l1.rpc.call('waitsendpay', [inv['payment_hash'], None, 1])
with pytest.raises(RpcError, match=r'WIRE_FINAL_INCORRECT_HTLC_AMOUNT'):
l1.rpc.call('waitsendpay', {'payment_hash': inv['payment_hash'],
'partid': 2})

0 comments on commit b670b51

Please sign in to comment.