jsonrpc: Add groupid to waitsendpay

This commit is contained in:
Christian Decker
2021-09-29 19:10:12 +02:00
committed by Rusty Russell
parent 511cb14fbe
commit 5f260840ab
6 changed files with 42 additions and 25 deletions

View File

@@ -33,6 +33,7 @@ struct sendpay_command {
struct sha256 payment_hash; struct sha256 payment_hash;
u64 partid; u64 partid;
u64 groupid;
struct command *cmd; struct command *cmd;
}; };
@@ -77,12 +78,13 @@ static void
add_sendpay_waiter(struct lightningd *ld, add_sendpay_waiter(struct lightningd *ld,
struct command *cmd, struct command *cmd,
const struct sha256 *payment_hash, const struct sha256 *payment_hash,
u64 partid) u64 partid, u64 groupid)
{ {
struct sendpay_command *pc = tal(cmd, struct sendpay_command); struct sendpay_command *pc = tal(cmd, struct sendpay_command);
pc->payment_hash = *payment_hash; pc->payment_hash = *payment_hash;
pc->partid = partid; pc->partid = partid;
pc->groupid = groupid;
pc->cmd = cmd; pc->cmd = cmd;
list_add(&ld->sendpay_commands, &pc->list); list_add(&ld->sendpay_commands, &pc->list);
tal_add_destructor(pc, destroy_sendpay_command); tal_add_destructor(pc, destroy_sendpay_command);
@@ -94,12 +96,13 @@ static void
add_waitsendpay_waiter(struct lightningd *ld, add_waitsendpay_waiter(struct lightningd *ld,
struct command *cmd, struct command *cmd,
const struct sha256 *payment_hash, const struct sha256 *payment_hash,
u64 partid) u64 partid, u64 groupid)
{ {
struct sendpay_command *pc = tal(cmd, struct sendpay_command); struct sendpay_command *pc = tal(cmd, struct sendpay_command);
pc->payment_hash = *payment_hash; pc->payment_hash = *payment_hash;
pc->partid = partid; pc->partid = partid;
pc->groupid = groupid;
pc->cmd = cmd; pc->cmd = cmd;
list_add(&ld->waitsendpay_commands, &pc->list); list_add(&ld->waitsendpay_commands, &pc->list);
tal_add_destructor(pc, destroy_sendpay_command); tal_add_destructor(pc, destroy_sendpay_command);
@@ -285,6 +288,8 @@ static void tell_waiters_failed(struct lightningd *ld,
continue; continue;
if (payment->partid != pc->partid) if (payment->partid != pc->partid)
continue; continue;
if (payment->groupid != pc->groupid)
continue;
sendpay_fail(pc->cmd, payment, pay_errcode, onionreply, fail, sendpay_fail(pc->cmd, payment, pay_errcode, onionreply, fail,
errmsg); errmsg);
@@ -311,6 +316,8 @@ static void tell_waiters_success(struct lightningd *ld,
continue; continue;
if (payment->partid != pc->partid) if (payment->partid != pc->partid)
continue; continue;
if (payment->groupid != pc->groupid)
continue;
sendpay_success(pc->cmd, payment); sendpay_success(pc->cmd, payment);
} }
@@ -327,7 +334,7 @@ void payment_succeeded(struct lightningd *ld, struct htlc_out *hout,
PAYMENT_COMPLETE, rval); PAYMENT_COMPLETE, rval);
payment = wallet_payment_by_hash(tmpctx, ld->wallet, payment = wallet_payment_by_hash(tmpctx, ld->wallet,
&hout->payment_hash, &hout->payment_hash,
hout->partid); hout->partid, hout->groupid);
assert(payment); assert(payment);
if (payment->local_offer_id) if (payment->local_offer_id)
@@ -539,7 +546,7 @@ void payment_failed(struct lightningd *ld, const struct htlc_out *hout,
payment = wallet_payment_by_hash(tmpctx, ld->wallet, payment = wallet_payment_by_hash(tmpctx, ld->wallet,
&hout->payment_hash, &hout->payment_hash,
hout->partid); hout->partid, hout->groupid);
#ifdef COMPAT_V052 #ifdef COMPAT_V052
/* Prior to "pay: delete HTLC when we delete payment." we would /* Prior to "pay: delete HTLC when we delete payment." we would
@@ -652,7 +659,7 @@ void payment_failed(struct lightningd *ld, const struct htlc_out *hout,
static struct command_result *wait_payment(struct lightningd *ld, static struct command_result *wait_payment(struct lightningd *ld,
struct command *cmd, struct command *cmd,
const struct sha256 *payment_hash, const struct sha256 *payment_hash,
u64 partid) u64 partid, u64 groupid)
{ {
struct wallet_payment *payment; struct wallet_payment *payment;
struct onionreply *failonionreply; struct onionreply *failonionreply;
@@ -668,7 +675,7 @@ static struct command_result *wait_payment(struct lightningd *ld,
errcode_t rpcerrorcode; errcode_t rpcerrorcode;
payment = wallet_payment_by_hash(tmpctx, ld->wallet, payment = wallet_payment_by_hash(tmpctx, ld->wallet,
payment_hash, partid); payment_hash, partid, groupid);
if (!payment) { if (!payment) {
return command_fail(cmd, PAY_NO_SUCH_PAYMENT, return command_fail(cmd, PAY_NO_SUCH_PAYMENT,
"Never attempted payment part %"PRIu64 "Never attempted payment part %"PRIu64
@@ -678,12 +685,12 @@ static struct command_result *wait_payment(struct lightningd *ld,
payment_hash)); payment_hash));
} }
log_debug(cmd->ld->log, "Payment part %"PRIu64"/%"PRIu64" status %u", log_debug(cmd->ld->log, "Payment part %"PRIu64"/%"PRIu64"/%"PRIu64" status %u",
partid, payment->partid, payment->status); partid, payment->partid, payment->groupid, payment->status);
switch (payment->status) { switch (payment->status) {
case PAYMENT_PENDING: case PAYMENT_PENDING:
add_waitsendpay_waiter(ld, cmd, payment_hash, partid); add_waitsendpay_waiter(ld, cmd, payment_hash, partid, groupid);
return NULL; return NULL;
case PAYMENT_COMPLETE: case PAYMENT_COMPLETE:
@@ -694,6 +701,7 @@ static struct command_result *wait_payment(struct lightningd *ld,
wallet_payment_get_failinfo(tmpctx, ld->wallet, wallet_payment_get_failinfo(tmpctx, ld->wallet,
payment_hash, payment_hash,
partid, partid,
groupid,
&failonionreply, &failonionreply,
&faildestperm, &faildestperm,
&failindex, &failindex,
@@ -1078,7 +1086,7 @@ send_payment_core(struct lightningd *ld,
/* We write this into db when HTLC is actually sent. */ /* We write this into db when HTLC is actually sent. */
wallet_payment_setup(ld->wallet, payment); wallet_payment_setup(ld->wallet, payment);
add_sendpay_waiter(ld, cmd, rhash, partid); add_sendpay_waiter(ld, cmd, rhash, partid, group);
return command_still_pending(cmd); return command_still_pending(cmd);
} }
@@ -1487,16 +1495,21 @@ static struct command_result *json_waitsendpay(struct command *cmd,
struct sha256 *rhash; struct sha256 *rhash;
unsigned int *timeout; unsigned int *timeout;
struct command_result *res; struct command_result *res;
u64 *partid; u64 *partid, *groupid;
if (!param(cmd, buffer, params, if (!param(cmd, buffer, params,
p_req("payment_hash", param_sha256, &rhash), p_req("payment_hash", param_sha256, &rhash),
p_opt("timeout", param_number, &timeout), p_opt("timeout", param_number, &timeout),
p_opt_def("partid", param_u64, &partid, 0), p_opt_def("partid", param_u64, &partid, 0),
p_opt("groupid", param_u64, &groupid),
NULL)) NULL))
return command_param_failed(); return command_param_failed();
res = wait_payment(cmd->ld, cmd, rhash, *partid); if (groupid == NULL) {
groupid = tal(cmd, u64);
*groupid = wallet_payment_get_groupid(cmd->ld->wallet, rhash);
}
res = wait_payment(cmd->ld, cmd, rhash, *partid, *groupid);
if (res) if (res)
return res; return res;

View File

@@ -1622,7 +1622,8 @@ static bool update_out_htlc(struct channel *channel,
if (hout->am_origin) { if (hout->am_origin) {
payment = wallet_payment_by_hash(tmpctx, ld->wallet, payment = wallet_payment_by_hash(tmpctx, ld->wallet,
&hout->payment_hash, &hout->payment_hash,
hout->partid); hout->partid,
hout->groupid);
assert(payment); assert(payment);
payment_store(ld, take(payment)); payment_store(ld, take(payment));
} }

View File

@@ -1490,6 +1490,7 @@ static struct command_result *payment_sendonion_success(struct command *cmd,
payment_waitsendpay_finished, p); payment_waitsendpay_finished, p);
json_add_sha256(req->js, "payment_hash", p->payment_hash); json_add_sha256(req->js, "payment_hash", p->payment_hash);
json_add_num(req->js, "partid", p->partid); json_add_num(req->js, "partid", p->partid);
json_add_u64(req->js, "groupid", p->groupid);
send_outreq(p->plugin, req); send_outreq(p->plugin, req);
return command_still_pending(cmd); return command_still_pending(cmd);

View File

@@ -1820,13 +1820,13 @@ static bool test_payment_crud(struct lightningd *ld, const tal_t *ctx)
t->payment_preimage = NULL; t->payment_preimage = NULL;
memset(&t->payment_hash, 1, sizeof(t->payment_hash)); memset(&t->payment_hash, 1, sizeof(t->payment_hash));
t->partid = 0; t->partid = 0;
t->groupid = 0; t->groupid = 12345;
db_begin_transaction(w->db); db_begin_transaction(w->db);
t2 = tal_dup(NULL, struct wallet_payment, t); t2 = tal_dup(NULL, struct wallet_payment, t);
wallet_payment_setup(w, t2); wallet_payment_setup(w, t2);
wallet_payment_store(w, take(t2)); wallet_payment_store(w, take(t2));
t2 = wallet_payment_by_hash(ctx, w, &t->payment_hash, 0); t2 = wallet_payment_by_hash(ctx, w, &t->payment_hash, 0, t->groupid);
CHECK(t2 != NULL); CHECK(t2 != NULL);
CHECK(t2->status == t->status); CHECK(t2->status == t->status);
CHECK(sha256_eq(&t2->payment_hash, &t->payment_hash)); CHECK(sha256_eq(&t2->payment_hash, &t->payment_hash));
@@ -1842,7 +1842,7 @@ static bool test_payment_crud(struct lightningd *ld, const tal_t *ctx)
memset(t->payment_preimage, 2, sizeof(*t->payment_preimage)); memset(t->payment_preimage, 2, sizeof(*t->payment_preimage));
wallet_payment_set_status(w, &t->payment_hash, t->partid, t->groupid, wallet_payment_set_status(w, &t->payment_hash, t->partid, t->groupid,
t->status, t->payment_preimage); t->status, t->payment_preimage);
t2 = wallet_payment_by_hash(ctx, w, &t->payment_hash, t->partid); t2 = wallet_payment_by_hash(ctx, w, &t->payment_hash, t->partid, t->groupid);
CHECK(t2 != NULL); CHECK(t2 != NULL);
CHECK(t2->status == t->status); CHECK(t2->status == t->status);
CHECK(sha256_eq(&t2->payment_hash, &t->payment_hash)); CHECK(sha256_eq(&t2->payment_hash, &t->payment_hash));

View File

@@ -2910,9 +2910,10 @@ void wallet_payment_store(struct wallet *wallet,
stmt = stmt =
db_prepare_v2(wallet->db, SQL("SELECT status FROM payments" db_prepare_v2(wallet->db, SQL("SELECT status FROM payments"
" WHERE payment_hash=?" " WHERE payment_hash=?"
" AND partid = ?;")); " AND partid = ? AND groupid = ?;"));
db_bind_sha256(stmt, 0, &payment->payment_hash); db_bind_sha256(stmt, 0, &payment->payment_hash);
db_bind_u64(stmt, 1, payment->partid); db_bind_u64(stmt, 1, payment->partid);
db_bind_u64(stmt, 2, payment->groupid);
db_query_prepared(stmt); db_query_prepared(stmt);
res = db_step(stmt); res = db_step(stmt);
assert(res); assert(res);
@@ -3134,10 +3135,7 @@ static struct wallet_payment *wallet_stmt2payment(const tal_t *ctx,
} else } else
payment->local_offer_id = NULL; payment->local_offer_id = NULL;
if (!db_column_is_null(stmt, 17)) payment->groupid = db_column_u64(stmt, 17);
payment->groupid = db_column_u64(stmt, 17);
else
payment->groupid = 0;
return payment; return payment;
} }
@@ -3145,7 +3143,7 @@ static struct wallet_payment *wallet_stmt2payment(const tal_t *ctx,
struct wallet_payment * struct wallet_payment *
wallet_payment_by_hash(const tal_t *ctx, struct wallet *wallet, wallet_payment_by_hash(const tal_t *ctx, struct wallet *wallet,
const struct sha256 *payment_hash, const struct sha256 *payment_hash,
u64 partid) u64 partid, u64 groupid)
{ {
struct db_stmt *stmt; struct db_stmt *stmt;
struct wallet_payment *payment; struct wallet_payment *payment;
@@ -3176,10 +3174,11 @@ wallet_payment_by_hash(const tal_t *ctx, struct wallet *wallet,
", groupid" ", groupid"
" FROM payments" " FROM payments"
" WHERE payment_hash = ?" " WHERE payment_hash = ?"
" AND partid = ?")); " AND partid = ? AND groupid=?"));
db_bind_sha256(stmt, 0, payment_hash); db_bind_sha256(stmt, 0, payment_hash);
db_bind_u64(stmt, 1, partid); db_bind_u64(stmt, 1, partid);
db_bind_u64(stmt, 2, groupid);
db_query_prepared(stmt); db_query_prepared(stmt);
if (db_step(stmt)) { if (db_step(stmt)) {
payment = wallet_stmt2payment(ctx, stmt); payment = wallet_stmt2payment(ctx, stmt);
@@ -3245,6 +3244,7 @@ void wallet_payment_get_failinfo(const tal_t *ctx,
struct wallet *wallet, struct wallet *wallet,
const struct sha256 *payment_hash, const struct sha256 *payment_hash,
u64 partid, u64 partid,
u64 groupid,
/* outputs */ /* outputs */
struct onionreply **failonionreply, struct onionreply **failonionreply,
bool *faildestperm, bool *faildestperm,
@@ -3266,9 +3266,10 @@ void wallet_payment_get_failinfo(const tal_t *ctx,
", failnode, failchannel" ", failnode, failchannel"
", failupdate, faildetail, faildirection" ", failupdate, faildetail, faildirection"
" FROM payments" " FROM payments"
" WHERE payment_hash=? AND partid=?;")); " WHERE payment_hash=? AND partid=? AND groupid=?;"));
db_bind_sha256(stmt, 0, payment_hash); db_bind_sha256(stmt, 0, payment_hash);
db_bind_u64(stmt, 1, partid); db_bind_u64(stmt, 1, partid);
db_bind_u64(stmt, 2, groupid);
db_query_prepared(stmt); db_query_prepared(stmt);
resb = db_step(stmt); resb = db_step(stmt);
assert(resb); assert(resb);

View File

@@ -1048,7 +1048,7 @@ void wallet_local_htlc_out_delete(struct wallet *wallet,
struct wallet_payment * struct wallet_payment *
wallet_payment_by_hash(const tal_t *ctx, struct wallet *wallet, wallet_payment_by_hash(const tal_t *ctx, struct wallet *wallet,
const struct sha256 *payment_hash, const struct sha256 *payment_hash,
u64 partid); u64 partid, u64 groupid);
/** /**
* Retrieve maximum groupid for a given payment_hash. * Retrieve maximum groupid for a given payment_hash.
@@ -1082,6 +1082,7 @@ void wallet_payment_get_failinfo(const tal_t *ctx,
struct wallet *wallet, struct wallet *wallet,
const struct sha256 *payment_hash, const struct sha256 *payment_hash,
u64 partid, u64 partid,
u64 groupid,
/* outputs */ /* outputs */
struct onionreply **failonionreply, struct onionreply **failonionreply,
bool *faildestperm, bool *faildestperm,