db: add partid, total_msat fields to payment entries.

This is in preparation for partial payments.  For existing payments,
partid is 0 (arbitrarity) and total_msat is msatoshi.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2019-12-12 09:46:23 +10:30
committed by Christian Decker
parent 727d6a0415
commit 2d18c3a209
7 changed files with 211 additions and 54 deletions

View File

@@ -81,6 +81,8 @@ void json_add_payment_fields(struct json_stream *response,
{
json_add_u64(response, "id", t->id);
json_add_sha256(response, "payment_hash", &t->payment_hash);
if (t->partid)
json_add_u64(response, "partid", t->partid);
if (t->destination != NULL)
json_add_node_id(response, "destination", t->destination);
@@ -278,9 +280,11 @@ void payment_succeeded(struct lightningd *ld, struct htlc_out *hout,
struct wallet_payment *payment;
wallet_payment_set_status(ld->wallet, &hout->payment_hash,
/* FIXME: Set partid! */ 0,
PAYMENT_COMPLETE, rval);
payment = wallet_payment_by_hash(tmpctx, ld->wallet,
&hout->payment_hash);
&hout->payment_hash,
/* FIXME: Set partid! */ 0);
assert(payment);
tell_waiters_success(ld, &hout->payment_hash, payment);
@@ -467,14 +471,16 @@ remote_routing_failure(const tal_t *ctx,
return routing_failure;
}
void payment_store(struct lightningd *ld, const struct sha256 *payment_hash)
void payment_store(struct lightningd *ld,
const struct sha256 *payment_hash, u64 partid)
{
struct sendpay_command *pc;
struct sendpay_command *next;
const struct wallet_payment *payment;
wallet_payment_store(ld->wallet, payment_hash);
payment = wallet_payment_by_hash(tmpctx, ld->wallet, payment_hash);
wallet_payment_store(ld->wallet, payment_hash, partid);
payment = wallet_payment_by_hash(tmpctx, ld->wallet,
payment_hash, partid);
assert(payment);
/* Trigger any sendpay commands waiting for the store to occur. */
@@ -496,7 +502,8 @@ void payment_failed(struct lightningd *ld, const struct htlc_out *hout,
int pay_errcode;
payment = wallet_payment_by_hash(tmpctx, ld->wallet,
&hout->payment_hash);
&hout->payment_hash,
/* FIXME: Set partid! */0);
#ifdef COMPAT_V052
/* Prior to "pay: delete HTLC when we delete payment." we would
@@ -566,11 +573,11 @@ void payment_failed(struct lightningd *ld, const struct htlc_out *hout,
}
/* Save to DB */
payment_store(ld, &hout->payment_hash);
wallet_payment_set_status(ld->wallet, &hout->payment_hash,
payment_store(ld, &hout->payment_hash, /* FIXME: Set partid! */ 0);
wallet_payment_set_status(ld->wallet, &hout->payment_hash, /* FIXME: Set partid! */ 0,
PAYMENT_FAILED, NULL);
wallet_payment_set_failinfo(ld->wallet,
&hout->payment_hash,
&hout->payment_hash, /* FIXME: Set partid! */ 0,
fail ? NULL : hout->failuremsg,
pay_errcode == PAY_DESTINATION_PERM_FAIL,
fail ? fail->erring_index : -1,
@@ -590,7 +597,8 @@ void payment_failed(struct lightningd *ld, const struct htlc_out *hout,
* Return callback if we called already, otherwise NULL. */
static struct command_result *wait_payment(struct lightningd *ld,
struct command *cmd,
const struct sha256 *payment_hash)
const struct sha256 *payment_hash,
u64 partid)
{
struct wallet_payment *payment;
u8 *failonionreply;
@@ -604,7 +612,8 @@ static struct command_result *wait_payment(struct lightningd *ld,
struct routing_failure *fail;
int faildirection;
payment = wallet_payment_by_hash(tmpctx, ld->wallet, payment_hash);
payment = wallet_payment_by_hash(tmpctx, ld->wallet,
payment_hash, partid);
if (!payment) {
return command_fail(cmd, PAY_NO_SUCH_PAYMENT,
"Never attempted payment for '%s'",
@@ -622,7 +631,9 @@ static struct command_result *wait_payment(struct lightningd *ld,
case PAYMENT_FAILED:
/* Get error from DB */
wallet_payment_get_failinfo(tmpctx, ld->wallet, payment_hash,
wallet_payment_get_failinfo(tmpctx, ld->wallet,
payment_hash,
partid,
&failonionreply,
&faildestperm,
&failindex,
@@ -706,8 +717,10 @@ static struct command_result *
send_payment(struct lightningd *ld,
struct command *cmd,
const struct sha256 *rhash,
u64 partid,
const struct route_hop *route,
struct amount_msat msat,
struct amount_msat total_msat,
const char *label TAKES,
const char *b11str TAKES,
const struct secret *payment_secret)
@@ -779,7 +792,7 @@ send_payment(struct lightningd *ld,
sphinx_add_hop(path, &pubkey, onion);
/* Now, do we already have a payment? */
payment = wallet_payment_by_hash(tmpctx, ld->wallet, rhash);
payment = wallet_payment_by_hash(tmpctx, ld->wallet, rhash, partid);
if (payment) {
/* FIXME: We should really do something smarter here! */
if (payment->status == PAYMENT_PENDING) {
@@ -853,18 +866,21 @@ send_payment(struct lightningd *ld,
* onchain_failed_our_htlc->payment_failed with no payment.
*/
if (payment) {
wallet_payment_delete(ld->wallet, rhash);
wallet_local_htlc_out_delete(ld->wallet, channel, rhash);
wallet_payment_delete(ld->wallet, rhash, payment->partid);
wallet_local_htlc_out_delete(ld->wallet, channel, rhash,
payment->partid);
}
/* If hout fails, payment should be freed too. */
payment = tal(hout, struct wallet_payment);
payment->id = 0;
payment->payment_hash = *rhash;
payment->partid = partid;
payment->destination = tal_dup(payment, struct node_id, &ids[n_hops - 1]);
payment->status = PAYMENT_PENDING;
payment->msatoshi = msat;
payment->msatoshi_sent = route[0].amount;
payment->total_msat = total_msat;
payment->timestamp = time_now().ts.tv_sec;
payment->payment_preimage = NULL;
payment->path_secrets = tal_steal(payment, path_secrets);
@@ -998,7 +1014,7 @@ static struct command_result *json_sendonion(struct command *cmd,
failcode);
/* Now, do we already have a payment? */
payment = wallet_payment_by_hash(tmpctx, ld->wallet, payment_hash);
payment = wallet_payment_by_hash(tmpctx, ld->wallet, payment_hash, /* FIXME: Set partid! */0);
if (payment) {
if (payment->status == PAYMENT_PENDING) {
log_debug(ld->log, "send_payment: previous still in progress");
@@ -1028,8 +1044,8 @@ static struct command_result *json_sendonion(struct command *cmd,
/* Cleanup any prior payment. We're about to retry. */
if (payment) {
wallet_payment_delete(ld->wallet, payment_hash);
wallet_local_htlc_out_delete(ld->wallet, channel, payment_hash);
wallet_payment_delete(ld->wallet, payment_hash, /* FIXME: Set partid! */0);
wallet_local_htlc_out_delete(ld->wallet, channel, payment_hash, /* FIXME: Set partid! */0);
}
failcode = send_onion(cmd->ld, &packet, first_hop, payment_hash, channel,
@@ -1247,7 +1263,10 @@ static struct command_result *json_sendpay(struct command *cmd,
}
#endif
res = send_payment(cmd->ld, cmd, rhash, route,
res = send_payment(cmd->ld, cmd, rhash, /* FIXME: Set partid! */ 0,
route,
msat ? *msat : route[routetok->size-1].amount,
/* FIXME: Set total_msat! */
msat ? *msat : route[routetok->size-1].amount,
label, b11str, payment_secret);
if (res)
@@ -1284,7 +1303,7 @@ static struct command_result *json_waitsendpay(struct command *cmd,
NULL))
return command_param_failed();
res = wait_payment(cmd->ld, cmd, rhash);
res = wait_payment(cmd->ld, cmd, rhash, /* FIXME: Set partid! */0);
if (res)
return res;

View File

@@ -18,7 +18,8 @@ void payment_failed(struct lightningd *ld, const struct htlc_out *hout,
const char *localfail);
/* Inform payment system to save the payment. */
void payment_store(struct lightningd *ld, const struct sha256 *payment_hash);
void payment_store(struct lightningd *ld,
const struct sha256 *payment_hash, u64 partid);
/* This json will be also used in 'sendpay_success' notifictaion. */
void json_add_payment_fields(struct json_stream *response,

View File

@@ -1274,7 +1274,8 @@ static bool update_out_htlc(struct channel *channel,
/* For our own HTLCs, we commit payment to db lazily */
if (hout->origin_htlc_id == 0)
payment_store(ld,
&hout->payment_hash);
&hout->payment_hash,
/* FIXME: Set partid! */ 0);
}
if (!htlc_out_update_state(channel, hout, newstate))