2017-04-01 15:05:29 +02:00
|
|
|
#include "pay.h"
|
|
|
|
#include <ccan/str/hex/hex.h>
|
2017-10-26 05:07:19 +02:00
|
|
|
#include <ccan/tal/str/str.h>
|
2017-11-22 01:25:39 +01:00
|
|
|
#include <common/bolt11.h>
|
2018-12-08 01:39:28 +01:00
|
|
|
#include <common/json_command.h>
|
|
|
|
#include <common/jsonrpc_errors.h>
|
|
|
|
#include <common/param.h>
|
2018-03-04 06:35:37 +01:00
|
|
|
#include <common/timeout.h>
|
2017-10-26 05:07:19 +02:00
|
|
|
#include <gossipd/gen_gossip_wire.h>
|
2017-08-28 18:04:01 +02:00
|
|
|
#include <lightningd/chaintopology.h>
|
2018-03-16 04:45:08 +01:00
|
|
|
#include <lightningd/json.h>
|
2017-08-28 18:04:01 +02:00
|
|
|
#include <lightningd/jsonrpc.h>
|
2017-04-01 15:05:29 +02:00
|
|
|
#include <lightningd/lightningd.h>
|
2017-08-28 18:04:01 +02:00
|
|
|
#include <lightningd/log.h>
|
2018-01-19 10:53:24 +01:00
|
|
|
#include <lightningd/options.h>
|
2017-04-01 15:05:29 +02:00
|
|
|
#include <lightningd/peer_control.h>
|
2017-06-20 08:12:03 +02:00
|
|
|
#include <lightningd/peer_htlcs.h>
|
2017-04-01 15:05:29 +02:00
|
|
|
#include <lightningd/subd.h>
|
|
|
|
#include <sodium/randombytes.h>
|
|
|
|
|
2019-01-17 16:25:32 +01:00
|
|
|
/* Routing failure object */
|
|
|
|
struct routing_failure {
|
|
|
|
unsigned int erring_index;
|
|
|
|
enum onion_type failcode;
|
2019-04-08 11:58:32 +02:00
|
|
|
struct node_id erring_node;
|
2019-01-17 16:25:32 +01:00
|
|
|
struct short_channel_id erring_channel;
|
|
|
|
int channel_dir;
|
|
|
|
};
|
|
|
|
|
2018-02-11 02:40:43 +01:00
|
|
|
/* sendpay command */
|
|
|
|
struct sendpay_command {
|
2018-02-02 01:55:06 +01:00
|
|
|
struct list_node list;
|
|
|
|
|
|
|
|
struct sha256 payment_hash;
|
2019-01-18 07:35:58 +01:00
|
|
|
struct command *cmd;
|
2018-02-02 01:55:06 +01:00
|
|
|
};
|
|
|
|
|
2018-02-11 02:40:43 +01:00
|
|
|
static void destroy_sendpay_command(struct sendpay_command *pc)
|
2017-04-01 15:05:29 +02:00
|
|
|
{
|
2018-02-02 01:55:06 +01:00
|
|
|
list_del(&pc->list);
|
|
|
|
}
|
2017-04-01 15:05:29 +02:00
|
|
|
|
2019-01-18 07:37:06 +01:00
|
|
|
/* Owned by cmd, if cmd is deleted, then sendpay_success/sendpay_fail will
|
2018-02-14 01:09:23 +01:00
|
|
|
* no longer be called. */
|
2018-03-04 06:35:37 +01:00
|
|
|
static void
|
2019-01-18 07:35:58 +01:00
|
|
|
add_sendpay_waiter(struct lightningd *ld,
|
|
|
|
struct command *cmd,
|
|
|
|
const struct sha256 *payment_hash)
|
2018-02-02 01:55:06 +01:00
|
|
|
{
|
2019-01-18 07:35:58 +01:00
|
|
|
struct sendpay_command *pc = tal(cmd, struct sendpay_command);
|
2018-02-02 01:55:06 +01:00
|
|
|
|
2018-03-10 08:44:28 +01:00
|
|
|
pc->payment_hash = *payment_hash;
|
2019-01-18 07:35:58 +01:00
|
|
|
pc->cmd = cmd;
|
2018-03-10 08:44:28 +01:00
|
|
|
list_add(&ld->sendpay_commands, &pc->list);
|
|
|
|
tal_add_destructor(pc, destroy_sendpay_command);
|
|
|
|
}
|
|
|
|
|
2019-01-18 07:37:06 +01:00
|
|
|
/* Owned by cmd, if cmd is deleted, then sendpay_success/sendpay_fail will
|
2018-03-10 08:44:28 +01:00
|
|
|
* no longer be called. */
|
|
|
|
static void
|
2019-01-18 07:35:58 +01:00
|
|
|
add_waitsendpay_waiter(struct lightningd *ld,
|
|
|
|
struct command *cmd,
|
|
|
|
const struct sha256 *payment_hash)
|
2018-03-10 08:44:28 +01:00
|
|
|
{
|
2019-01-18 07:35:58 +01:00
|
|
|
struct sendpay_command *pc = tal(cmd, struct sendpay_command);
|
2018-03-10 08:44:28 +01:00
|
|
|
|
2018-02-02 01:55:06 +01:00
|
|
|
pc->payment_hash = *payment_hash;
|
2019-01-18 07:35:58 +01:00
|
|
|
pc->cmd = cmd;
|
2018-03-10 07:41:45 +01:00
|
|
|
list_add(&ld->waitsendpay_commands, &pc->list);
|
2018-02-11 02:40:43 +01:00
|
|
|
tal_add_destructor(pc, destroy_sendpay_command);
|
2018-02-02 01:55:06 +01:00
|
|
|
}
|
|
|
|
|
2019-01-18 07:35:58 +01:00
|
|
|
/* Outputs fields, not a separate object*/
|
|
|
|
static void
|
|
|
|
json_add_payment_fields(struct json_stream *response,
|
|
|
|
const struct wallet_payment *t)
|
|
|
|
{
|
|
|
|
json_add_u64(response, "id", t->id);
|
|
|
|
json_add_hex(response, "payment_hash", &t->payment_hash, sizeof(t->payment_hash));
|
2019-04-08 11:58:32 +02:00
|
|
|
json_add_node_id(response, "destination", &t->destination);
|
2019-05-20 07:07:40 +02:00
|
|
|
json_add_amount_msat_compat(response, t->msatoshi,
|
|
|
|
"msatoshi", "amount_msat");
|
|
|
|
json_add_amount_msat_compat(response, t->msatoshi_sent,
|
|
|
|
"msatoshi_sent", "amount_sent_msat");
|
2019-01-18 07:35:58 +01:00
|
|
|
json_add_u64(response, "created_at", t->timestamp);
|
|
|
|
|
|
|
|
switch (t->status) {
|
|
|
|
case PAYMENT_PENDING:
|
|
|
|
json_add_string(response, "status", "pending");
|
|
|
|
break;
|
|
|
|
case PAYMENT_COMPLETE:
|
|
|
|
json_add_string(response, "status", "complete");
|
|
|
|
break;
|
|
|
|
case PAYMENT_FAILED:
|
|
|
|
json_add_string(response, "status", "failed");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (t->payment_preimage)
|
|
|
|
json_add_hex(response, "payment_preimage",
|
|
|
|
t->payment_preimage,
|
|
|
|
sizeof(*t->payment_preimage));
|
2019-02-23 04:11:14 +01:00
|
|
|
if (t->label) {
|
|
|
|
if (deprecated_apis)
|
|
|
|
json_add_string(response, "description", t->label);
|
|
|
|
json_add_string(response, "label", t->label);
|
|
|
|
}
|
2019-02-23 04:11:12 +01:00
|
|
|
if (t->bolt11)
|
|
|
|
json_add_string(response, "bolt11", t->bolt11);
|
2019-01-18 07:35:58 +01:00
|
|
|
}
|
|
|
|
|
2019-01-18 07:37:06 +01:00
|
|
|
static struct command_result *sendpay_success(struct command *cmd,
|
|
|
|
const struct wallet_payment *payment)
|
2019-01-18 07:35:58 +01:00
|
|
|
{
|
|
|
|
struct json_stream *response;
|
|
|
|
|
2019-01-18 07:37:06 +01:00
|
|
|
assert(payment->status == PAYMENT_COMPLETE);
|
2019-01-18 07:35:58 +01:00
|
|
|
|
|
|
|
response = json_stream_success(cmd);
|
2019-01-18 07:37:06 +01:00
|
|
|
json_add_payment_fields(response, payment);
|
|
|
|
return command_success(cmd, response);
|
2019-01-18 07:35:58 +01:00
|
|
|
}
|
|
|
|
|
2019-01-18 07:37:06 +01:00
|
|
|
static void
|
|
|
|
json_add_routefail_info(struct json_stream *js,
|
|
|
|
unsigned int erring_index,
|
|
|
|
enum onion_type failcode,
|
2019-04-08 11:58:32 +02:00
|
|
|
const struct node_id *erring_node,
|
2019-01-18 07:37:06 +01:00
|
|
|
const struct short_channel_id *erring_channel,
|
|
|
|
int channel_dir)
|
2019-01-18 07:35:58 +01:00
|
|
|
{
|
2019-01-18 07:37:06 +01:00
|
|
|
const char *failcodename = onion_type_name(failcode);
|
|
|
|
|
|
|
|
json_add_num(js, "erring_index", erring_index);
|
|
|
|
json_add_num(js, "failcode", failcode);
|
|
|
|
/* FIXME: Better way to detect this? */
|
|
|
|
if (!strstarts(failcodename, "INVALID "))
|
|
|
|
json_add_string(js, "failcodename", failcodename);
|
2019-04-08 11:58:32 +02:00
|
|
|
json_add_node_id(js, "erring_node", erring_node);
|
2019-01-18 07:37:06 +01:00
|
|
|
json_add_short_channel_id(js, "erring_channel", erring_channel);
|
|
|
|
json_add_num(js, "erring_direction", channel_dir);
|
|
|
|
}
|
2019-01-18 07:35:58 +01:00
|
|
|
|
2019-01-18 07:37:06 +01:00
|
|
|
/* onionreply used if pay_errcode == PAY_UNPARSEABLE_ONION */
|
|
|
|
static struct command_result *
|
|
|
|
sendpay_fail(struct command *cmd,
|
|
|
|
int pay_errcode,
|
|
|
|
const u8 *onionreply,
|
|
|
|
const struct routing_failure *fail,
|
|
|
|
const char *details)
|
|
|
|
{
|
|
|
|
struct json_stream *data;
|
|
|
|
|
|
|
|
if (pay_errcode == PAY_UNPARSEABLE_ONION) {
|
|
|
|
data = json_stream_fail(cmd, PAY_UNPARSEABLE_ONION,
|
|
|
|
"Malformed error reply");
|
|
|
|
json_add_hex_talarr(data, "onionreply", onionreply);
|
|
|
|
json_object_end(data);
|
|
|
|
return command_failed(cmd, data);
|
2019-01-18 07:35:58 +01:00
|
|
|
}
|
2019-01-18 07:37:06 +01:00
|
|
|
|
|
|
|
assert(fail);
|
|
|
|
data = json_stream_fail(cmd, pay_errcode,
|
|
|
|
tal_fmt(tmpctx, "failed: %s (%s)",
|
|
|
|
onion_type_name(fail->failcode),
|
|
|
|
details));
|
|
|
|
json_add_routefail_info(data,
|
|
|
|
fail->erring_index,
|
|
|
|
fail->failcode,
|
|
|
|
&fail->erring_node,
|
|
|
|
&fail->erring_channel,
|
|
|
|
fail->channel_dir);
|
2019-06-12 02:38:54 +02:00
|
|
|
json_object_end(data);
|
2019-01-18 07:37:06 +01:00
|
|
|
return command_failed(cmd, data);
|
2019-01-18 07:35:58 +01:00
|
|
|
}
|
|
|
|
|
2019-01-18 07:37:06 +01:00
|
|
|
/* We defer sendpay "success" until we know it's pending; consumes cmd */
|
|
|
|
static struct command_result *
|
|
|
|
json_sendpay_in_progress(struct command *cmd,
|
|
|
|
const struct wallet_payment *payment)
|
2019-01-18 07:35:58 +01:00
|
|
|
{
|
2019-01-18 07:37:06 +01:00
|
|
|
struct json_stream *response = json_stream_success(cmd);
|
|
|
|
json_add_string(response, "message",
|
|
|
|
"Monitor status with listpayments or waitsendpay");
|
|
|
|
json_add_payment_fields(response, payment);
|
|
|
|
return command_success(cmd, response);
|
2019-01-18 07:35:58 +01:00
|
|
|
}
|
|
|
|
|
2019-01-18 07:37:06 +01:00
|
|
|
static void tell_waiters_failed(struct lightningd *ld,
|
2018-03-10 07:41:45 +01:00
|
|
|
const struct sha256 *payment_hash,
|
2019-01-18 07:37:06 +01:00
|
|
|
int pay_errcode,
|
|
|
|
const u8 *onionreply,
|
|
|
|
const struct routing_failure *fail,
|
|
|
|
const char *details)
|
2018-02-02 01:55:06 +01:00
|
|
|
{
|
2018-02-14 01:09:23 +01:00
|
|
|
struct sendpay_command *pc;
|
|
|
|
struct sendpay_command *next;
|
2019-01-18 07:35:58 +01:00
|
|
|
|
2019-01-18 07:37:06 +01:00
|
|
|
/* Careful: sendpay_fail deletes cmd */
|
2018-03-10 07:41:45 +01:00
|
|
|
list_for_each_safe(&ld->waitsendpay_commands, pc, next, list) {
|
2018-07-04 07:30:02 +02:00
|
|
|
if (!sha256_eq(payment_hash, &pc->payment_hash))
|
2018-02-02 01:55:06 +01:00
|
|
|
continue;
|
|
|
|
|
2019-01-18 07:37:06 +01:00
|
|
|
sendpay_fail(pc->cmd, pay_errcode, onionreply, fail, details);
|
2018-02-02 01:55:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-18 07:37:06 +01:00
|
|
|
static void tell_waiters_success(struct lightningd *ld,
|
|
|
|
const struct sha256 *payment_hash,
|
|
|
|
struct wallet_payment *payment)
|
2018-03-03 01:11:39 +01:00
|
|
|
{
|
2019-01-18 07:37:06 +01:00
|
|
|
struct sendpay_command *pc;
|
|
|
|
struct sendpay_command *next;
|
2018-02-03 12:46:12 +01:00
|
|
|
|
2019-01-18 07:37:06 +01:00
|
|
|
/* Careful: sendpay_success deletes cmd */
|
|
|
|
list_for_each_safe(&ld->waitsendpay_commands, pc, next, list) {
|
|
|
|
if (!sha256_eq(payment_hash, &pc->payment_hash))
|
|
|
|
continue;
|
2018-03-03 01:11:39 +01:00
|
|
|
|
2019-01-18 07:37:06 +01:00
|
|
|
sendpay_success(pc->cmd, payment);
|
|
|
|
}
|
2018-03-13 14:35:43 +01:00
|
|
|
}
|
|
|
|
|
2017-06-20 07:53:03 +02:00
|
|
|
void payment_succeeded(struct lightningd *ld, struct htlc_out *hout,
|
2017-04-01 15:05:29 +02:00
|
|
|
const struct preimage *rval)
|
|
|
|
{
|
2019-01-18 07:37:06 +01:00
|
|
|
struct wallet_payment *payment;
|
|
|
|
|
2018-01-17 21:29:49 +01:00
|
|
|
wallet_payment_set_status(ld->wallet, &hout->payment_hash,
|
|
|
|
PAYMENT_COMPLETE, rval);
|
2019-01-18 07:37:06 +01:00
|
|
|
payment = wallet_payment_by_hash(tmpctx, ld->wallet,
|
|
|
|
&hout->payment_hash);
|
|
|
|
assert(payment);
|
|
|
|
|
|
|
|
tell_waiters_success(ld, &hout->payment_hash, payment);
|
2017-04-01 15:05:29 +02:00
|
|
|
}
|
|
|
|
|
2018-02-03 01:05:17 +01:00
|
|
|
/* Return a struct routing_failure for an immediate failure
|
|
|
|
* (returned directly from send_htlc_out). The returned
|
|
|
|
* failure is allocated from the given context. */
|
|
|
|
static struct routing_failure*
|
|
|
|
immediate_routing_failure(const tal_t *ctx,
|
|
|
|
const struct lightningd *ld,
|
|
|
|
enum onion_type failcode,
|
2019-01-15 05:02:27 +01:00
|
|
|
const struct short_channel_id *channel0,
|
2019-04-08 11:58:32 +02:00
|
|
|
const struct node_id *dstid)
|
2018-02-03 01:05:17 +01:00
|
|
|
{
|
|
|
|
struct routing_failure *routing_failure;
|
|
|
|
|
|
|
|
assert(failcode);
|
|
|
|
|
|
|
|
routing_failure = tal(ctx, struct routing_failure);
|
2018-02-06 14:41:15 +01:00
|
|
|
routing_failure->erring_index = 0;
|
2018-02-03 01:05:17 +01:00
|
|
|
routing_failure->failcode = failcode;
|
|
|
|
routing_failure->erring_node = ld->id;
|
|
|
|
routing_failure->erring_channel = *channel0;
|
2019-04-08 11:58:32 +02:00
|
|
|
routing_failure->channel_dir = node_id_idx(&ld->id, dstid);
|
2018-02-03 01:05:17 +01:00
|
|
|
|
|
|
|
return routing_failure;
|
|
|
|
}
|
|
|
|
|
2018-01-29 16:04:33 +01:00
|
|
|
/* Return a struct routing_failure for a local failure allocated
|
|
|
|
* from the given context. */
|
|
|
|
static struct routing_failure*
|
|
|
|
local_routing_failure(const tal_t *ctx,
|
|
|
|
const struct lightningd *ld,
|
|
|
|
const struct htlc_out *hout,
|
|
|
|
const struct wallet_payment *payment)
|
|
|
|
{
|
|
|
|
struct routing_failure *routing_failure;
|
|
|
|
|
|
|
|
assert(hout->failcode);
|
|
|
|
|
|
|
|
routing_failure = tal(ctx, struct routing_failure);
|
2018-02-06 14:41:15 +01:00
|
|
|
routing_failure->erring_index = 0;
|
2018-01-29 16:04:33 +01:00
|
|
|
routing_failure->failcode = hout->failcode;
|
|
|
|
routing_failure->erring_node = ld->id;
|
|
|
|
routing_failure->erring_channel = payment->route_channels[0];
|
2019-04-08 11:58:32 +02:00
|
|
|
routing_failure->channel_dir = node_id_idx(&ld->id,
|
|
|
|
&payment->route_nodes[0]);
|
2018-01-29 16:04:33 +01:00
|
|
|
|
2019-01-17 16:24:32 +01:00
|
|
|
log_debug(hout->key.channel->log, "local_routing_failure: %u (%s)",
|
|
|
|
hout->failcode, onion_type_name(hout->failcode));
|
2018-01-29 16:04:33 +01:00
|
|
|
return routing_failure;
|
|
|
|
}
|
|
|
|
|
2019-01-18 07:37:06 +01:00
|
|
|
/* Fills in *pay_errcode with PAY_TRY_OTHER_ROUTE or PAY_DESTINATION_PERM_FAIL */
|
2018-02-03 11:44:31 +01:00
|
|
|
static struct routing_failure*
|
|
|
|
remote_routing_failure(const tal_t *ctx,
|
2019-01-17 16:24:32 +01:00
|
|
|
struct lightningd *ld,
|
2018-02-03 11:44:31 +01:00
|
|
|
const struct wallet_payment *payment,
|
2018-07-30 18:01:34 +02:00
|
|
|
const struct onionreply *failure,
|
2019-01-18 07:37:06 +01:00
|
|
|
struct log *log,
|
|
|
|
int *pay_errcode)
|
2018-01-21 05:15:07 +01:00
|
|
|
{
|
|
|
|
enum onion_type failcode = fromwire_peektype(failure->msg);
|
2018-02-03 11:44:31 +01:00
|
|
|
struct routing_failure *routing_failure;
|
2019-04-08 11:58:32 +02:00
|
|
|
const struct node_id *route_nodes;
|
|
|
|
const struct node_id *erring_node;
|
2018-01-21 05:15:07 +01:00
|
|
|
const struct short_channel_id *route_channels;
|
|
|
|
const struct short_channel_id *erring_channel;
|
|
|
|
int origin_index;
|
2019-01-15 05:02:27 +01:00
|
|
|
int dir;
|
2018-01-21 05:15:07 +01:00
|
|
|
|
2018-02-03 11:44:31 +01:00
|
|
|
routing_failure = tal(ctx, struct routing_failure);
|
2018-01-21 05:15:07 +01:00
|
|
|
route_nodes = payment->route_nodes;
|
|
|
|
route_channels = payment->route_channels;
|
|
|
|
origin_index = failure->origin_index;
|
|
|
|
|
|
|
|
assert(origin_index < tal_count(route_nodes));
|
|
|
|
|
|
|
|
/* Check if at destination. */
|
|
|
|
if (origin_index == tal_count(route_nodes) - 1) {
|
2019-01-18 07:37:08 +01:00
|
|
|
/* If any channel is to blame, it's the last one. */
|
|
|
|
erring_channel = &route_channels[origin_index];
|
|
|
|
/* Single hop? */
|
|
|
|
if (origin_index == 0)
|
2019-04-08 11:58:32 +02:00
|
|
|
dir = node_id_idx(&ld->id,
|
|
|
|
&route_nodes[origin_index]);
|
2019-01-18 07:37:08 +01:00
|
|
|
else
|
2019-04-08 11:58:32 +02:00
|
|
|
dir = node_id_idx(&route_nodes[origin_index - 1],
|
|
|
|
&route_nodes[origin_index]);
|
2019-01-15 05:02:27 +01:00
|
|
|
|
2018-01-21 05:15:07 +01:00
|
|
|
/* BOLT #4:
|
|
|
|
*
|
|
|
|
* - if the _final node_ is returning the error:
|
|
|
|
* - if the PERM bit is set:
|
|
|
|
* - SHOULD fail the payment.
|
|
|
|
* */
|
|
|
|
if (failcode & PERM)
|
2019-01-18 07:37:06 +01:00
|
|
|
*pay_errcode = PAY_DESTINATION_PERM_FAIL;
|
2018-01-21 05:15:07 +01:00
|
|
|
else
|
2019-02-17 12:21:35 +01:00
|
|
|
/* FIXME: not right for WIRE_FINAL_EXPIRY_TOO_SOON */
|
2019-01-18 07:37:06 +01:00
|
|
|
*pay_errcode = PAY_TRY_OTHER_ROUTE;
|
2019-01-08 01:53:25 +01:00
|
|
|
erring_node = &route_nodes[origin_index];
|
|
|
|
} else {
|
2019-01-17 16:24:32 +01:00
|
|
|
u8 *gossip_msg;
|
|
|
|
|
2019-01-18 07:37:06 +01:00
|
|
|
*pay_errcode = PAY_TRY_OTHER_ROUTE;
|
|
|
|
|
2018-01-21 05:15:07 +01:00
|
|
|
/* Report the *next* channel as failing. */
|
|
|
|
erring_channel = &route_channels[origin_index + 1];
|
|
|
|
|
2019-04-08 11:58:32 +02:00
|
|
|
dir = node_id_idx(&route_nodes[origin_index],
|
|
|
|
&route_nodes[origin_index+1]);
|
2019-01-15 05:02:27 +01:00
|
|
|
|
2019-01-08 01:53:25 +01:00
|
|
|
/* If the error is a BADONION, then it's on behalf of the
|
|
|
|
* following node. */
|
|
|
|
if (failcode & BADONION) {
|
2019-01-08 01:53:25 +01:00
|
|
|
log_debug(log, "failcode %u from onionreply %s",
|
|
|
|
failcode, tal_hex(tmpctx, failure->msg));
|
2019-01-08 01:53:25 +01:00
|
|
|
erring_node = &route_nodes[origin_index + 1];
|
|
|
|
} else
|
|
|
|
erring_node = &route_nodes[origin_index];
|
2019-01-17 16:24:32 +01:00
|
|
|
|
|
|
|
/* Tell gossipd: it may want to remove channels or even nodes
|
|
|
|
* in response to this, and there may be a channel_update
|
|
|
|
* embedded too */
|
|
|
|
gossip_msg = towire_gossip_payment_failure(NULL,
|
|
|
|
erring_node,
|
|
|
|
erring_channel,
|
|
|
|
dir,
|
|
|
|
failure->msg);
|
|
|
|
subd_send_msg(ld->gossip, take(gossip_msg));
|
2019-01-08 01:53:25 +01:00
|
|
|
}
|
2018-01-29 16:04:33 +01:00
|
|
|
|
2018-02-06 14:41:15 +01:00
|
|
|
routing_failure->erring_index = (unsigned int) (origin_index + 1);
|
2018-02-03 11:44:31 +01:00
|
|
|
routing_failure->failcode = failcode;
|
|
|
|
routing_failure->erring_node = *erring_node;
|
|
|
|
routing_failure->erring_channel = *erring_channel;
|
2019-01-15 05:02:27 +01:00
|
|
|
routing_failure->channel_dir = dir;
|
2018-02-03 11:44:31 +01:00
|
|
|
|
|
|
|
return routing_failure;
|
2018-01-21 05:15:07 +01:00
|
|
|
}
|
|
|
|
|
2019-01-18 07:37:06 +01:00
|
|
|
void payment_store(struct lightningd *ld, const struct sha256 *payment_hash)
|
2018-03-10 07:39:11 +01:00
|
|
|
{
|
2018-03-10 08:44:28 +01:00
|
|
|
struct sendpay_command *pc;
|
|
|
|
struct sendpay_command *next;
|
2018-03-13 14:35:43 +01:00
|
|
|
const struct wallet_payment *payment;
|
2018-03-10 08:44:28 +01:00
|
|
|
|
2018-03-10 07:39:11 +01:00
|
|
|
wallet_payment_store(ld->wallet, payment_hash);
|
2018-03-13 14:35:43 +01:00
|
|
|
payment = wallet_payment_by_hash(tmpctx, ld->wallet, payment_hash);
|
|
|
|
assert(payment);
|
2018-03-10 08:44:28 +01:00
|
|
|
|
|
|
|
/* Trigger any sendpay commands waiting for the store to occur. */
|
|
|
|
list_for_each_safe(&ld->sendpay_commands, pc, next, list) {
|
2018-07-04 07:30:02 +02:00
|
|
|
if (!sha256_eq(payment_hash, &pc->payment_hash))
|
2018-03-10 08:44:28 +01:00
|
|
|
continue;
|
|
|
|
|
2019-01-18 07:37:06 +01:00
|
|
|
/* Deletes from list, frees pc */
|
|
|
|
json_sendpay_in_progress(pc->cmd, payment);
|
2018-03-10 08:44:28 +01:00
|
|
|
}
|
2018-03-10 07:39:11 +01:00
|
|
|
}
|
|
|
|
|
2017-06-20 08:12:03 +02:00
|
|
|
void payment_failed(struct lightningd *ld, const struct htlc_out *hout,
|
|
|
|
const char *localfail)
|
2017-04-01 15:05:29 +02:00
|
|
|
{
|
2018-01-21 05:15:07 +01:00
|
|
|
struct wallet_payment *payment;
|
2018-01-29 16:04:33 +01:00
|
|
|
struct routing_failure* fail = NULL;
|
|
|
|
const char *failmsg;
|
2019-01-18 07:37:06 +01:00
|
|
|
int pay_errcode;
|
2017-06-20 07:53:03 +02:00
|
|
|
|
2018-01-21 05:15:07 +01:00
|
|
|
payment = wallet_payment_by_hash(tmpctx, ld->wallet,
|
|
|
|
&hout->payment_hash);
|
2018-01-29 16:04:33 +01:00
|
|
|
|
2018-03-06 19:31:21 +01:00
|
|
|
#ifdef COMPAT_V052
|
2018-03-16 01:14:44 +01:00
|
|
|
/* Prior to "pay: delete HTLC when we delete payment." we would
|
|
|
|
* delete a payment on retry, but leave the HTLC. */
|
|
|
|
if (!payment) {
|
|
|
|
log_unusual(hout->key.channel->log,
|
|
|
|
"No payment for %s:"
|
|
|
|
" was this an old database?",
|
|
|
|
type_to_string(tmpctx, struct sha256,
|
|
|
|
&hout->payment_hash));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-17 05:42:35 +01:00
|
|
|
/* FIXME: Prior to 299b280f7, we didn't put route_nodes and
|
|
|
|
* route_channels in db. If this happens, it's an old payment,
|
|
|
|
* so we can simply mark it failed in db and return. */
|
|
|
|
if (!payment->route_channels) {
|
|
|
|
log_unusual(hout->key.channel->log,
|
|
|
|
"No route_channels for htlc %s:"
|
|
|
|
" was this an old database?",
|
2018-03-15 05:30:39 +01:00
|
|
|
type_to_string(tmpctx, struct sha256,
|
2018-02-17 05:42:35 +01:00
|
|
|
&hout->payment_hash));
|
|
|
|
wallet_payment_set_status(ld->wallet, &hout->payment_hash,
|
|
|
|
PAYMENT_FAILED, NULL);
|
|
|
|
return;
|
|
|
|
}
|
2018-10-09 10:58:52 +02:00
|
|
|
#else
|
|
|
|
assert(payment);
|
|
|
|
assert(payment->route_channels);
|
2018-03-06 19:31:21 +01:00
|
|
|
#endif
|
2018-02-17 05:42:35 +01:00
|
|
|
|
2018-01-29 16:04:33 +01:00
|
|
|
/* This gives more details than a generic failure message */
|
|
|
|
if (localfail) {
|
|
|
|
fail = local_routing_failure(tmpctx, ld, hout, payment);
|
|
|
|
failmsg = localfail;
|
2019-01-18 07:37:06 +01:00
|
|
|
pay_errcode = PAY_TRY_OTHER_ROUTE;
|
2017-11-28 06:03:04 +01:00
|
|
|
} else {
|
2018-01-29 16:04:33 +01:00
|
|
|
/* Must be remote fail. */
|
|
|
|
assert(!hout->failcode);
|
|
|
|
failmsg = "reply from remote";
|
|
|
|
/* Try to parse reply. */
|
2018-03-25 21:51:11 +02:00
|
|
|
struct secret *path_secrets = payment->path_secrets;
|
2019-01-17 16:24:32 +01:00
|
|
|
struct onionreply *reply;
|
|
|
|
|
|
|
|
reply = unwrap_onionreply(tmpctx, path_secrets,
|
2018-01-29 16:04:33 +01:00
|
|
|
tal_count(path_secrets),
|
|
|
|
hout->failuremsg);
|
|
|
|
if (!reply) {
|
2018-02-12 11:13:04 +01:00
|
|
|
log_info(hout->key.channel->log,
|
2018-01-29 16:04:33 +01:00
|
|
|
"htlc %"PRIu64" failed with bad reply (%s)",
|
|
|
|
hout->key.id,
|
2018-03-15 05:30:39 +01:00
|
|
|
tal_hex(tmpctx, hout->failuremsg));
|
2019-01-17 16:24:32 +01:00
|
|
|
/* Cannot record failure. */
|
2018-01-29 16:04:33 +01:00
|
|
|
fail = NULL;
|
2019-01-18 07:37:06 +01:00
|
|
|
pay_errcode = PAY_UNPARSEABLE_ONION;
|
2018-01-29 16:04:33 +01:00
|
|
|
} else {
|
2018-03-05 17:43:40 +01:00
|
|
|
enum onion_type failcode = fromwire_peektype(reply->msg);
|
2018-02-12 11:13:04 +01:00
|
|
|
log_info(hout->key.channel->log,
|
2018-01-29 16:04:33 +01:00
|
|
|
"htlc %"PRIu64" "
|
|
|
|
"failed from %ith node "
|
|
|
|
"with code 0x%04x (%s)",
|
|
|
|
hout->key.id,
|
|
|
|
reply->origin_index,
|
|
|
|
failcode, onion_type_name(failcode));
|
2019-01-17 16:24:32 +01:00
|
|
|
fail = remote_routing_failure(tmpctx, ld,
|
2018-07-30 18:01:34 +02:00
|
|
|
payment, reply,
|
2019-01-18 07:37:06 +01:00
|
|
|
hout->key.channel->log,
|
|
|
|
&pay_errcode);
|
2018-01-29 16:04:33 +01:00
|
|
|
}
|
2017-06-20 07:53:03 +02:00
|
|
|
}
|
|
|
|
|
2018-03-07 17:10:08 +01:00
|
|
|
/* Save to DB */
|
2018-03-10 07:39:11 +01:00
|
|
|
payment_store(ld, &hout->payment_hash);
|
2018-01-29 18:34:59 +01:00
|
|
|
wallet_payment_set_status(ld->wallet, &hout->payment_hash,
|
|
|
|
PAYMENT_FAILED, NULL);
|
2018-03-07 17:10:08 +01:00
|
|
|
wallet_payment_set_failinfo(ld->wallet,
|
|
|
|
&hout->payment_hash,
|
|
|
|
fail ? NULL : hout->failuremsg,
|
2019-01-18 07:37:06 +01:00
|
|
|
pay_errcode == PAY_DESTINATION_PERM_FAIL,
|
2018-03-07 17:10:08 +01:00
|
|
|
fail ? fail->erring_index : -1,
|
|
|
|
fail ? fail->failcode : 0,
|
|
|
|
fail ? &fail->erring_node : NULL,
|
|
|
|
fail ? &fail->erring_channel : NULL,
|
2019-01-17 16:24:32 +01:00
|
|
|
NULL,
|
2019-01-15 05:02:27 +01:00
|
|
|
failmsg,
|
|
|
|
fail ? fail->channel_dir : 0);
|
2018-01-29 18:34:59 +01:00
|
|
|
|
2019-01-18 07:37:06 +01:00
|
|
|
tell_waiters_failed(ld, &hout->payment_hash, pay_errcode,
|
|
|
|
hout->failuremsg, fail, failmsg);
|
2017-04-01 15:05:29 +02:00
|
|
|
}
|
|
|
|
|
2019-01-18 07:35:58 +01:00
|
|
|
/* Wait for a payment. If cmd is deleted, then json_waitsendpay_on_resolve
|
2018-03-04 06:35:37 +01:00
|
|
|
* no longer be called.
|
2019-01-18 07:37:06 +01:00
|
|
|
* 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)
|
2018-03-04 06:35:37 +01:00
|
|
|
{
|
|
|
|
struct wallet_payment *payment;
|
2018-03-07 17:10:08 +01:00
|
|
|
u8 *failonionreply;
|
|
|
|
bool faildestperm;
|
|
|
|
int failindex;
|
|
|
|
enum onion_type failcode;
|
2019-04-08 11:58:32 +02:00
|
|
|
struct node_id *failnode;
|
2018-03-07 17:10:08 +01:00
|
|
|
struct short_channel_id *failchannel;
|
|
|
|
u8 *failupdate;
|
2018-04-16 15:29:40 +02:00
|
|
|
char *faildetail;
|
2018-03-07 17:10:08 +01:00
|
|
|
struct routing_failure *fail;
|
2019-01-15 05:02:27 +01:00
|
|
|
int faildirection;
|
2018-03-04 06:35:37 +01:00
|
|
|
|
|
|
|
payment = wallet_payment_by_hash(tmpctx, ld->wallet, payment_hash);
|
|
|
|
if (!payment) {
|
2019-01-18 07:37:06 +01:00
|
|
|
return command_fail(cmd, PAY_NO_SUCH_PAYMENT,
|
|
|
|
"Never attempted payment for '%s'",
|
|
|
|
type_to_string(tmpctx, struct sha256,
|
|
|
|
payment_hash));
|
2018-03-04 06:35:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (payment->status) {
|
|
|
|
case PAYMENT_PENDING:
|
2019-01-18 07:35:58 +01:00
|
|
|
add_waitsendpay_waiter(ld, cmd, payment_hash);
|
2019-01-18 07:37:06 +01:00
|
|
|
return NULL;
|
2018-03-04 06:35:37 +01:00
|
|
|
|
|
|
|
case PAYMENT_COMPLETE:
|
2019-01-18 07:37:06 +01:00
|
|
|
return sendpay_success(cmd, payment);
|
2018-03-04 06:35:37 +01:00
|
|
|
|
|
|
|
case PAYMENT_FAILED:
|
2018-03-07 17:10:08 +01:00
|
|
|
/* Get error from DB */
|
|
|
|
wallet_payment_get_failinfo(tmpctx, ld->wallet, payment_hash,
|
|
|
|
&failonionreply,
|
|
|
|
&faildestperm,
|
|
|
|
&failindex,
|
|
|
|
&failcode,
|
|
|
|
&failnode,
|
|
|
|
&failchannel,
|
2018-04-16 15:29:40 +02:00
|
|
|
&failupdate,
|
2019-01-15 05:02:27 +01:00
|
|
|
&faildetail,
|
|
|
|
&faildirection);
|
2018-03-07 17:10:08 +01:00
|
|
|
/* Old DB might not save failure information */
|
2019-01-18 07:37:06 +01:00
|
|
|
if (!failonionreply && !failnode) {
|
|
|
|
return command_fail(cmd, PAY_UNSPECIFIED_ERROR,
|
|
|
|
"Payment failure reason unknown");
|
|
|
|
} else if (failonionreply) {
|
2018-03-07 17:10:08 +01:00
|
|
|
/* failed to parse returned onion error */
|
2019-01-18 07:37:06 +01:00
|
|
|
return sendpay_fail(cmd, PAY_UNPARSEABLE_ONION,
|
|
|
|
failonionreply,
|
|
|
|
NULL, faildetail);
|
2018-03-07 17:10:08 +01:00
|
|
|
} else {
|
|
|
|
/* Parsed onion error, get its details */
|
|
|
|
assert(failnode);
|
|
|
|
assert(failchannel);
|
|
|
|
fail = tal(tmpctx, struct routing_failure);
|
|
|
|
fail->erring_index = failindex;
|
|
|
|
fail->failcode = failcode;
|
|
|
|
fail->erring_node = *failnode;
|
|
|
|
fail->erring_channel = *failchannel;
|
2019-01-15 05:02:27 +01:00
|
|
|
fail->channel_dir = faildirection;
|
2019-01-18 07:37:06 +01:00
|
|
|
return sendpay_fail(cmd,
|
|
|
|
faildestperm
|
|
|
|
? PAY_DESTINATION_PERM_FAIL
|
|
|
|
: PAY_TRY_OTHER_ROUTE,
|
|
|
|
NULL,
|
|
|
|
fail, faildetail);
|
2018-03-07 17:10:08 +01:00
|
|
|
}
|
2018-03-04 06:35:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Impossible. */
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2019-01-18 07:37:06 +01:00
|
|
|
/* Returns command_result if cmd was resolved, NULL if not yet called. */
|
|
|
|
static struct command_result *
|
2019-01-18 07:35:58 +01:00
|
|
|
send_payment(struct lightningd *ld,
|
|
|
|
struct command *cmd,
|
2018-03-04 06:35:37 +01:00
|
|
|
const struct sha256 *rhash,
|
2018-03-10 08:44:28 +01:00
|
|
|
const struct route_hop *route,
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat msat,
|
2019-02-23 04:11:14 +01:00
|
|
|
const char *label TAKES,
|
2019-02-23 04:11:12 +01:00
|
|
|
const char *b11str TAKES)
|
2017-04-01 15:05:29 +02:00
|
|
|
{
|
|
|
|
const u8 *onion;
|
|
|
|
u8 sessionkey[32];
|
2017-10-26 04:56:19 +02:00
|
|
|
unsigned int base_expiry;
|
2017-04-01 15:05:29 +02:00
|
|
|
struct onionpacket *packet;
|
2017-05-06 04:19:44 +02:00
|
|
|
struct secret *path_secrets;
|
2017-06-20 08:14:03 +02:00
|
|
|
enum onion_type failcode;
|
2017-10-26 04:56:19 +02:00
|
|
|
size_t i, n_hops = tal_count(route);
|
2017-12-15 11:29:41 +01:00
|
|
|
struct hop_data *hop_data = tal_arr(tmpctx, struct hop_data, n_hops);
|
2019-04-08 11:58:32 +02:00
|
|
|
struct node_id *ids = tal_arr(tmpctx, struct node_id, n_hops);
|
2017-11-23 21:54:19 +01:00
|
|
|
struct wallet_payment *payment = NULL;
|
2018-01-17 21:29:49 +01:00
|
|
|
struct htlc_out *hout;
|
2018-01-20 15:00:35 +01:00
|
|
|
struct short_channel_id *channels;
|
2018-02-03 01:05:17 +01:00
|
|
|
struct routing_failure *fail;
|
2018-02-12 11:13:04 +01:00
|
|
|
struct channel *channel;
|
2019-02-14 16:59:17 +01:00
|
|
|
struct sphinx_path *path;
|
|
|
|
struct short_channel_id finalscid;
|
|
|
|
struct pubkey pubkey;
|
|
|
|
bool ret;
|
2017-04-01 15:05:29 +02:00
|
|
|
|
|
|
|
/* Expiry for HTLCs is absolute. And add one to give some margin. */
|
2018-02-14 01:09:23 +01:00
|
|
|
base_expiry = get_block_height(ld->topology) + 1;
|
2019-02-16 18:41:04 +01:00
|
|
|
memset(&finalscid, 0, sizeof(struct short_channel_id));
|
2017-04-01 15:05:29 +02:00
|
|
|
|
2019-02-14 16:59:17 +01:00
|
|
|
path = sphinx_path_new(tmpctx, rhash->u.u8);
|
|
|
|
/* Extract IDs for each hop: create_onionpacket wants array. */
|
|
|
|
for (i = 0; i < n_hops; i++)
|
2017-10-26 04:56:19 +02:00
|
|
|
ids[i] = route[i].nodeid;
|
2017-04-01 15:05:29 +02:00
|
|
|
|
2017-10-26 04:56:19 +02:00
|
|
|
/* Copy hop_data[n] from route[n+1] (ie. where it goes next) */
|
|
|
|
for (i = 0; i < n_hops - 1; i++) {
|
2019-02-14 16:59:17 +01:00
|
|
|
ret = pubkey_from_node_id(&pubkey, &ids[i]);
|
|
|
|
assert(ret);
|
2017-10-26 04:56:19 +02:00
|
|
|
hop_data[i].realm = 0;
|
|
|
|
hop_data[i].channel_id = route[i+1].channel_id;
|
2019-02-21 04:45:55 +01:00
|
|
|
hop_data[i].amt_forward = route[i+1].amount;
|
2017-10-26 04:56:19 +02:00
|
|
|
hop_data[i].outgoing_cltv = base_expiry + route[i+1].delay;
|
2019-02-14 16:59:17 +01:00
|
|
|
sphinx_add_v0_hop(path, &pubkey, &route[i + 1].channel_id,
|
|
|
|
route[i + 1].amount,
|
|
|
|
base_expiry + route[i + 1].delay);
|
2017-04-01 15:05:29 +02:00
|
|
|
}
|
|
|
|
|
2017-04-30 16:29:31 +02:00
|
|
|
/* And finally set the final hop to the special values in
|
|
|
|
* BOLT04 */
|
2019-02-14 16:59:17 +01:00
|
|
|
memset(&finalscid, 0, sizeof(struct short_channel_id));
|
|
|
|
ret = pubkey_from_node_id(&pubkey, &ids[i]);
|
|
|
|
assert(ret);
|
|
|
|
sphinx_add_v0_hop(path, &pubkey, &finalscid,
|
|
|
|
route[i].amount,
|
|
|
|
base_expiry + route[i].delay);
|
|
|
|
|
2018-01-17 21:29:49 +01:00
|
|
|
/* Now, do we already have a payment? */
|
2018-02-14 01:09:23 +01:00
|
|
|
payment = wallet_payment_by_hash(tmpctx, ld->wallet, rhash);
|
2018-01-17 21:29:49 +01:00
|
|
|
if (payment) {
|
2018-01-17 21:29:49 +01:00
|
|
|
/* FIXME: We should really do something smarter here! */
|
2018-02-14 01:09:23 +01:00
|
|
|
log_debug(ld->log, "send_payment: found previous");
|
2018-01-17 21:29:49 +01:00
|
|
|
if (payment->status == PAYMENT_PENDING) {
|
2018-02-14 01:09:23 +01:00
|
|
|
log_add(ld->log, "Payment is still in progress");
|
2019-01-18 07:37:06 +01:00
|
|
|
return json_sendpay_in_progress(cmd, payment);
|
2017-04-01 15:05:29 +02:00
|
|
|
}
|
2018-01-17 21:29:49 +01:00
|
|
|
if (payment->status == PAYMENT_COMPLETE) {
|
2018-02-14 01:09:23 +01:00
|
|
|
log_add(ld->log, "... succeeded");
|
2017-04-01 15:05:29 +02:00
|
|
|
/* Must match successful payment parameters. */
|
2019-02-21 04:45:55 +01:00
|
|
|
if (!amount_msat_eq(payment->msatoshi, msat)) {
|
2019-01-18 07:37:06 +01:00
|
|
|
return command_fail(cmd, PAY_RHASH_ALREADY_USED,
|
2018-02-14 01:09:23 +01:00
|
|
|
"Already succeeded "
|
2019-02-21 04:45:55 +01:00
|
|
|
"with amount %s",
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct amount_msat,
|
|
|
|
&payment->msatoshi));
|
2017-04-01 15:05:29 +02:00
|
|
|
}
|
2019-04-08 11:58:32 +02:00
|
|
|
if (!node_id_eq(&payment->destination, &ids[n_hops-1])) {
|
2019-01-18 07:37:06 +01:00
|
|
|
return command_fail(cmd, PAY_RHASH_ALREADY_USED,
|
2018-02-14 01:09:23 +01:00
|
|
|
"Already succeeded to %s",
|
|
|
|
type_to_string(tmpctx,
|
2019-04-08 11:58:32 +02:00
|
|
|
struct node_id,
|
2018-02-14 01:09:23 +01:00
|
|
|
&payment->destination));
|
2017-04-01 15:05:29 +02:00
|
|
|
}
|
2019-01-18 07:37:06 +01:00
|
|
|
return sendpay_success(cmd, payment);
|
2017-04-01 15:05:29 +02:00
|
|
|
}
|
2018-02-14 01:09:23 +01:00
|
|
|
log_add(ld->log, "... retrying");
|
2017-04-01 15:05:29 +02:00
|
|
|
}
|
|
|
|
|
2018-02-19 02:06:02 +01:00
|
|
|
channel = active_channel_by_id(ld, &ids[0], NULL);
|
2018-02-12 11:13:04 +01:00
|
|
|
if (!channel) {
|
2019-01-18 07:37:06 +01:00
|
|
|
struct json_stream *data
|
|
|
|
= json_stream_fail(cmd, PAY_TRY_OTHER_ROUTE,
|
|
|
|
"No connection to first "
|
|
|
|
"peer found");
|
|
|
|
|
|
|
|
json_add_routefail_info(data, 0, WIRE_UNKNOWN_NEXT_PEER,
|
|
|
|
&ld->id, &route[0].channel_id,
|
2019-04-08 11:58:32 +02:00
|
|
|
node_id_idx(&ld->id, &route[0].nodeid));
|
2019-06-12 02:38:54 +02:00
|
|
|
json_object_end(data);
|
2019-01-18 07:37:06 +01:00
|
|
|
return command_failed(cmd, data);
|
2017-04-01 15:05:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
randombytes_buf(&sessionkey, sizeof(sessionkey));
|
|
|
|
|
|
|
|
/* Onion will carry us from first peer onwards. */
|
2019-02-14 16:59:17 +01:00
|
|
|
packet = create_onionpacket(tmpctx, path, &path_secrets);
|
2018-02-14 01:09:23 +01:00
|
|
|
onion = serialize_onionpacket(tmpctx, packet);
|
2017-04-01 15:05:29 +02:00
|
|
|
|
2019-02-21 04:45:55 +01:00
|
|
|
log_info(ld->log, "Sending %s over %zu hops to deliver %s",
|
2019-02-21 03:39:21 +01:00
|
|
|
type_to_string(tmpctx, struct amount_msat, &route[0].amount),
|
2019-02-21 04:45:55 +01:00
|
|
|
n_hops, type_to_string(tmpctx, struct amount_msat, &msat));
|
2018-01-17 21:29:49 +01:00
|
|
|
|
2019-02-21 04:45:55 +01:00
|
|
|
failcode = send_htlc_out(channel, route[0].amount,
|
2017-10-26 04:56:19 +02:00
|
|
|
base_expiry + route[0].delay,
|
2018-02-02 01:55:06 +01:00
|
|
|
rhash, onion, NULL, &hout);
|
2017-06-20 08:14:03 +02:00
|
|
|
if (failcode) {
|
2019-01-18 07:35:58 +01:00
|
|
|
fail = immediate_routing_failure(cmd, ld,
|
2018-02-03 01:05:17 +01:00
|
|
|
failcode,
|
2019-01-15 05:02:27 +01:00
|
|
|
&route[0].channel_id,
|
|
|
|
&channel->peer->id);
|
2018-02-03 01:05:17 +01:00
|
|
|
|
2019-01-18 07:37:06 +01:00
|
|
|
return sendpay_fail(cmd, PAY_TRY_OTHER_ROUTE, NULL,
|
|
|
|
fail, "First peer not ready");
|
2017-06-20 08:14:03 +02:00
|
|
|
}
|
2018-01-17 21:29:49 +01:00
|
|
|
|
2018-01-20 15:00:35 +01:00
|
|
|
/* Copy channels used along the route. */
|
|
|
|
channels = tal_arr(tmpctx, struct short_channel_id, n_hops);
|
|
|
|
for (i = 0; i < n_hops; ++i)
|
|
|
|
channels[i] = route[i].channel_id;
|
|
|
|
|
2018-03-16 01:09:37 +01:00
|
|
|
/* If we're retrying, delete all trace of previous one. We delete
|
|
|
|
* outgoing HTLC, too, otherwise it gets reported to onchaind as
|
|
|
|
* a possibility, and we end up in handle_missing_htlc_output->
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2018-01-17 21:29:49 +01:00
|
|
|
/* If hout fails, payment should be freed too. */
|
|
|
|
payment = tal(hout, struct wallet_payment);
|
|
|
|
payment->id = 0;
|
|
|
|
payment->payment_hash = *rhash;
|
|
|
|
payment->destination = ids[n_hops - 1];
|
|
|
|
payment->status = PAYMENT_PENDING;
|
2019-02-21 04:45:55 +01:00
|
|
|
payment->msatoshi = msat;
|
|
|
|
payment->msatoshi_sent = route[0].amount;
|
2018-01-17 21:29:49 +01:00
|
|
|
payment->timestamp = time_now().ts.tv_sec;
|
|
|
|
payment->payment_preimage = NULL;
|
|
|
|
payment->path_secrets = tal_steal(payment, path_secrets);
|
2018-01-20 15:00:35 +01:00
|
|
|
payment->route_nodes = tal_steal(payment, ids);
|
|
|
|
payment->route_channels = tal_steal(payment, channels);
|
2019-02-23 04:11:14 +01:00
|
|
|
if (label != NULL)
|
|
|
|
payment->label = tal_strdup(payment, label);
|
2018-07-23 13:59:54 +02:00
|
|
|
else
|
2019-02-23 04:11:14 +01:00
|
|
|
payment->label = NULL;
|
2019-02-23 04:11:12 +01:00
|
|
|
if (b11str != NULL)
|
|
|
|
payment->bolt11 = tal_strdup(payment, b11str);
|
|
|
|
else
|
|
|
|
payment->bolt11 = NULL;
|
2018-01-17 21:29:49 +01:00
|
|
|
|
|
|
|
/* We write this into db when HTLC is actually sent. */
|
2018-02-14 01:09:23 +01:00
|
|
|
wallet_payment_setup(ld->wallet, payment);
|
2018-01-17 21:29:49 +01:00
|
|
|
|
2019-01-18 07:35:58 +01:00
|
|
|
add_sendpay_waiter(ld, cmd, rhash);
|
2019-01-18 07:37:06 +01:00
|
|
|
return NULL;
|
2017-04-01 15:05:29 +02:00
|
|
|
}
|
|
|
|
|
2018-02-14 01:09:23 +01:00
|
|
|
/*-----------------------------------------------------------------------------
|
|
|
|
JSON-RPC sendpay interface
|
|
|
|
-----------------------------------------------------------------------------*/
|
|
|
|
|
2018-12-16 05:52:06 +01:00
|
|
|
static struct command_result *json_sendpay(struct command *cmd,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *obj UNNEEDED,
|
|
|
|
const jsmntok_t *params)
|
2017-10-26 04:56:19 +02:00
|
|
|
{
|
2018-08-29 22:44:04 +02:00
|
|
|
const jsmntok_t *routetok;
|
2019-01-17 03:12:13 +01:00
|
|
|
const jsmntok_t *t;
|
|
|
|
size_t i;
|
2018-08-14 22:16:49 +02:00
|
|
|
struct sha256 *rhash;
|
2017-10-26 04:56:19 +02:00
|
|
|
struct route_hop *route;
|
2019-02-21 03:39:21 +01:00
|
|
|
struct amount_msat *msat;
|
2019-02-23 04:11:14 +01:00
|
|
|
const char *b11str, *label;
|
2019-01-18 07:37:06 +01:00
|
|
|
struct command_result *res;
|
2017-10-26 04:56:19 +02:00
|
|
|
|
2019-02-23 04:11:14 +01:00
|
|
|
/* If by array, or 'check' command, use 'label' as param name */
|
|
|
|
if (!params || params->type == JSMN_ARRAY) {
|
|
|
|
if (!param(cmd, buffer, params,
|
|
|
|
p_req("route", param_array, &routetok),
|
|
|
|
p_req("payment_hash", param_sha256, &rhash),
|
|
|
|
p_opt("label", param_escaped_string, &label),
|
|
|
|
p_opt("msatoshi", param_msat, &msat),
|
|
|
|
p_opt("bolt11", param_string, &b11str),
|
|
|
|
NULL))
|
|
|
|
return command_param_failed();
|
|
|
|
} else {
|
|
|
|
const char *description_deprecated;
|
|
|
|
|
|
|
|
/* If by keyword, treat description and label as
|
|
|
|
* separate parameters. */
|
|
|
|
if (!param(cmd, buffer, params,
|
|
|
|
p_req("route", param_array, &routetok),
|
|
|
|
p_req("payment_hash", param_sha256, &rhash),
|
|
|
|
p_opt("label", param_escaped_string, &label),
|
|
|
|
p_opt("description", param_escaped_string,
|
|
|
|
&description_deprecated),
|
|
|
|
p_opt("msatoshi", param_msat, &msat),
|
|
|
|
p_opt("bolt11", param_string, &b11str),
|
|
|
|
NULL))
|
|
|
|
return command_param_failed();
|
|
|
|
|
|
|
|
if (description_deprecated) {
|
|
|
|
if (!deprecated_apis)
|
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"Deprecated parameter description, use label");
|
|
|
|
if (label)
|
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"Cannot specify both description and label");
|
|
|
|
label = description_deprecated;
|
|
|
|
}
|
|
|
|
}
|
2017-10-26 04:56:19 +02:00
|
|
|
|
2019-01-17 03:12:13 +01:00
|
|
|
if (routetok->size == 0)
|
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS, "Empty route");
|
2017-10-26 04:56:19 +02:00
|
|
|
|
2019-01-17 03:12:13 +01:00
|
|
|
route = tal_arr(cmd, struct route_hop, routetok->size);
|
|
|
|
json_for_each_arr(i, t, routetok) {
|
2019-02-21 03:38:51 +01:00
|
|
|
struct amount_msat *msat, *amount_msat;
|
2019-04-08 11:58:32 +02:00
|
|
|
struct node_id *id;
|
2018-08-23 18:08:32 +02:00
|
|
|
struct short_channel_id *channel;
|
2019-01-15 11:04:07 +01:00
|
|
|
unsigned *delay, *direction;
|
2018-08-23 18:08:32 +02:00
|
|
|
|
|
|
|
if (!param(cmd, buffer, t,
|
2019-02-21 03:38:51 +01:00
|
|
|
/* Only *one* of these is required */
|
|
|
|
p_opt("msatoshi", param_msat, &msat),
|
|
|
|
p_opt("amount_msat", param_msat, &amount_msat),
|
|
|
|
/* These three actually required */
|
2019-04-08 11:58:32 +02:00
|
|
|
p_opt("id", param_node_id, &id),
|
2019-02-21 03:38:51 +01:00
|
|
|
p_opt("delay", param_number, &delay),
|
|
|
|
p_opt("channel", param_short_channel_id, &channel),
|
2019-01-15 11:04:07 +01:00
|
|
|
p_opt("direction", param_number, &direction),
|
2018-08-23 18:08:32 +02:00
|
|
|
NULL))
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_param_failed();
|
2017-10-26 04:56:19 +02:00
|
|
|
|
2019-02-21 03:38:51 +01:00
|
|
|
if (!msat && !amount_msat)
|
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"route[%zi]: must have msatoshi"
|
|
|
|
" or amount_msat", i);
|
|
|
|
if (!id || !channel || !delay)
|
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"route[%zi]: must have id, channel"
|
|
|
|
" and delay", i);
|
|
|
|
if (msat && amount_msat && !amount_msat_eq(*msat, *amount_msat))
|
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"route[%zi]: msatoshi %s != amount_msat %s",
|
|
|
|
i,
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct amount_msat,
|
|
|
|
msat),
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct amount_msat,
|
|
|
|
amount_msat));
|
|
|
|
if (!msat)
|
|
|
|
msat = amount_msat;
|
|
|
|
|
2019-02-21 03:39:21 +01:00
|
|
|
route[i].amount = *msat;
|
2019-01-17 03:12:13 +01:00
|
|
|
route[i].nodeid = *id;
|
|
|
|
route[i].delay = *delay;
|
|
|
|
route[i].channel_id = *channel;
|
2019-01-15 11:04:07 +01:00
|
|
|
/* FIXME: Actually ignored by sending code! */
|
2019-01-17 03:12:13 +01:00
|
|
|
route[i].direction = direction ? *direction : 0;
|
2017-10-26 04:56:19 +02:00
|
|
|
}
|
|
|
|
|
2018-07-20 03:14:02 +02:00
|
|
|
/* The given msatoshi is the actual payment that the payee is
|
|
|
|
* requesting. The final hop amount is what we actually give, which can
|
|
|
|
* be from the msatoshi to twice msatoshi. */
|
|
|
|
|
|
|
|
/* if not: msatoshi <= finalhop.amount <= 2 * msatoshi, fail. */
|
2019-02-21 03:39:21 +01:00
|
|
|
if (msat) {
|
2019-02-21 04:45:56 +01:00
|
|
|
struct amount_msat limit = route[routetok->size-1].amount;
|
2019-02-21 03:39:21 +01:00
|
|
|
|
2019-02-21 04:45:56 +01:00
|
|
|
if (amount_msat_less(*msat, limit))
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
2019-02-21 03:39:21 +01:00
|
|
|
"msatoshi %s less than final %s",
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct amount_msat,
|
|
|
|
msat),
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct amount_msat,
|
|
|
|
&route[routetok->size-1].amount));
|
2019-02-21 04:45:56 +01:00
|
|
|
limit.millisatoshis *= 2; /* Raw: sanity check */
|
|
|
|
if (amount_msat_greater(*msat, limit))
|
2019-02-21 03:39:21 +01:00
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"msatoshi %s more than twice final %s",
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct amount_msat,
|
|
|
|
msat),
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct amount_msat,
|
|
|
|
&route[routetok->size-1].amount));
|
2018-07-20 03:14:02 +02:00
|
|
|
}
|
2018-03-22 13:49:09 +01:00
|
|
|
|
2019-01-18 07:37:06 +01:00
|
|
|
res = send_payment(cmd->ld, cmd, rhash, route,
|
2019-02-21 04:45:55 +01:00
|
|
|
msat ? *msat : route[routetok->size-1].amount,
|
2019-02-23 04:11:14 +01:00
|
|
|
label, b11str);
|
2019-01-18 07:37:06 +01:00
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
return command_still_pending(cmd);
|
2017-10-26 04:56:19 +02:00
|
|
|
}
|
|
|
|
|
2017-04-01 15:05:29 +02:00
|
|
|
static const struct json_command sendpay_command = {
|
|
|
|
"sendpay",
|
2019-05-22 16:08:16 +02:00
|
|
|
"payment",
|
2017-04-01 15:05:29 +02:00
|
|
|
json_sendpay,
|
2018-03-02 13:54:37 +01:00
|
|
|
"Send along {route} in return for preimage of {payment_hash}"
|
2017-04-01 15:05:29 +02:00
|
|
|
};
|
|
|
|
AUTODATA(json_command, &sendpay_command);
|
2017-10-26 05:07:19 +02:00
|
|
|
|
2018-03-04 06:35:37 +01:00
|
|
|
static void waitsendpay_timeout(struct command *cmd)
|
|
|
|
{
|
2018-12-16 05:53:06 +01:00
|
|
|
was_pending(command_fail(cmd, PAY_IN_PROGRESS,
|
|
|
|
"Timed out while waiting"));
|
2018-03-04 06:35:37 +01:00
|
|
|
}
|
|
|
|
|
2018-12-16 05:52:06 +01:00
|
|
|
static struct command_result *json_waitsendpay(struct command *cmd,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *obj UNNEEDED,
|
|
|
|
const jsmntok_t *params)
|
2018-03-04 06:35:37 +01:00
|
|
|
{
|
2018-08-14 22:16:49 +02:00
|
|
|
struct sha256 *rhash;
|
2018-07-20 03:14:02 +02:00
|
|
|
unsigned int *timeout;
|
2019-01-18 07:37:06 +01:00
|
|
|
struct command_result *res;
|
2018-03-04 06:35:37 +01:00
|
|
|
|
2018-07-20 03:14:02 +02:00
|
|
|
if (!param(cmd, buffer, params,
|
2018-12-16 05:50:06 +01:00
|
|
|
p_req("payment_hash", param_sha256, &rhash),
|
|
|
|
p_opt("timeout", param_number, &timeout),
|
2018-07-20 03:14:02 +02:00
|
|
|
NULL))
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_param_failed();
|
2018-03-04 06:35:37 +01:00
|
|
|
|
2019-01-18 07:37:06 +01:00
|
|
|
res = wait_payment(cmd->ld, cmd, rhash);
|
|
|
|
if (res)
|
|
|
|
return res;
|
2018-03-04 06:35:37 +01:00
|
|
|
|
2018-07-20 03:14:02 +02:00
|
|
|
if (timeout)
|
2019-06-14 05:49:23 +02:00
|
|
|
new_reltimer(cmd->ld->timers, cmd, time_from_sec(*timeout),
|
2018-03-04 06:35:37 +01:00
|
|
|
&waitsendpay_timeout, cmd);
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_still_pending(cmd);
|
2018-03-04 06:35:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct json_command waitsendpay_command = {
|
|
|
|
"waitsendpay",
|
2019-05-22 16:08:16 +02:00
|
|
|
"payment",
|
2018-03-04 06:35:37 +01:00
|
|
|
json_waitsendpay,
|
|
|
|
"Wait for payment attempt on {payment_hash} to succeed or fail, "
|
|
|
|
"but only up to {timeout} seconds."
|
|
|
|
};
|
|
|
|
AUTODATA(json_command, &waitsendpay_command);
|
|
|
|
|
2019-02-23 06:00:04 +01:00
|
|
|
static struct command_result *json_listsendpays(struct command *cmd,
|
2018-12-16 05:52:06 +01:00
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *obj UNNEEDED,
|
|
|
|
const jsmntok_t *params)
|
2017-11-16 19:09:09 +01:00
|
|
|
{
|
|
|
|
const struct wallet_payment **payments;
|
2018-10-19 03:17:49 +02:00
|
|
|
struct json_stream *response;
|
2018-08-30 15:48:36 +02:00
|
|
|
struct sha256 *rhash;
|
2018-08-30 00:53:00 +02:00
|
|
|
const char *b11str;
|
2017-11-16 19:09:09 +01:00
|
|
|
|
2018-07-20 03:14:02 +02:00
|
|
|
if (!param(cmd, buffer, params,
|
2018-12-16 05:50:06 +01:00
|
|
|
p_opt("bolt11", param_string, &b11str),
|
|
|
|
p_opt("payment_hash", param_sha256, &rhash),
|
2018-07-20 03:14:02 +02:00
|
|
|
NULL))
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_param_failed();
|
2018-01-16 20:44:32 +01:00
|
|
|
|
2018-08-30 15:48:36 +02:00
|
|
|
if (rhash && b11str) {
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"Can only specify one of"
|
|
|
|
" {bolt11} or {payment_hash}");
|
2018-07-23 14:46:03 +02:00
|
|
|
}
|
|
|
|
|
2018-08-30 00:53:00 +02:00
|
|
|
if (b11str) {
|
2018-01-16 20:44:32 +01:00
|
|
|
struct bolt11 *b11;
|
2018-08-30 00:53:00 +02:00
|
|
|
char *fail;
|
2018-01-16 20:44:32 +01:00
|
|
|
|
|
|
|
b11 = bolt11_decode(cmd, b11str, NULL, &fail);
|
|
|
|
if (!b11) {
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"Invalid bolt11: %s", fail);
|
2018-01-16 20:44:32 +01:00
|
|
|
}
|
|
|
|
rhash = &b11->payment_hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
payments = wallet_payment_list(cmd, cmd->ld->wallet, rhash);
|
2017-11-16 19:09:09 +01:00
|
|
|
|
2018-10-19 03:17:48 +02:00
|
|
|
response = json_stream_success(cmd);
|
2018-07-23 14:46:03 +02:00
|
|
|
|
2018-01-17 00:53:18 +01:00
|
|
|
json_array_start(response, "payments");
|
2018-07-23 14:46:03 +02:00
|
|
|
for (size_t i = 0; i < tal_count(payments); i++) {
|
2017-11-16 19:09:09 +01:00
|
|
|
json_object_start(response, NULL);
|
2018-03-14 01:30:56 +01:00
|
|
|
json_add_payment_fields(response, payments[i]);
|
2017-11-16 19:09:09 +01:00
|
|
|
json_object_end(response);
|
|
|
|
}
|
|
|
|
json_array_end(response);
|
2018-07-23 14:46:03 +02:00
|
|
|
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_success(cmd, response);
|
2017-11-16 19:09:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct json_command listpayments_command = {
|
|
|
|
"listpayments",
|
2019-05-22 16:08:16 +02:00
|
|
|
"payment",
|
2019-02-23 06:00:04 +01:00
|
|
|
json_listsendpays,
|
|
|
|
"Show outgoing payments",
|
|
|
|
true /* deprecated, use new name */
|
2017-11-16 19:09:09 +01:00
|
|
|
};
|
|
|
|
AUTODATA(json_command, &listpayments_command);
|
2019-02-23 06:00:04 +01:00
|
|
|
|
|
|
|
static const struct json_command listsendpays_command = {
|
|
|
|
"listsendpays",
|
2019-05-22 16:08:16 +02:00
|
|
|
"payment",
|
2019-02-23 06:00:04 +01:00
|
|
|
json_listsendpays,
|
|
|
|
"Show sendpay, old and current, optionally limiting to {bolt11} or {payment_hash}."
|
|
|
|
};
|
|
|
|
AUTODATA(json_command, &listsendpays_command);
|