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>
|
|
|
|
|
2018-02-14 01:09:23 +01:00
|
|
|
/*-----------------------------------------------------------------------------
|
|
|
|
Internal sendpay interface
|
|
|
|
-----------------------------------------------------------------------------*/
|
|
|
|
|
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;
|
2018-02-14 01:09:23 +01:00
|
|
|
|
|
|
|
void (*cb)(const struct sendpay_result *, void*);
|
|
|
|
void *cbarg;
|
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
|
|
|
|
2018-03-10 08:44:28 +01:00
|
|
|
/* Owned by cxt, if cxt is deleted, then cb will
|
2018-02-14 01:09:23 +01:00
|
|
|
* no longer be called. */
|
2018-03-04 06:35:37 +01:00
|
|
|
static void
|
2018-03-10 08:44:28 +01:00
|
|
|
add_sendpay_waiter(const tal_t *cxt,
|
2018-03-04 06:35:37 +01:00
|
|
|
const struct sha256 *payment_hash,
|
|
|
|
struct lightningd *ld,
|
|
|
|
void (*cb)(const struct sendpay_result *, void*),
|
|
|
|
void *cbarg)
|
2018-02-02 01:55:06 +01:00
|
|
|
{
|
2018-02-14 01:09:23 +01:00
|
|
|
struct sendpay_command *pc = tal(cxt, struct sendpay_command);
|
2018-02-02 01:55:06 +01:00
|
|
|
|
2018-03-10 08:44:28 +01:00
|
|
|
pc->payment_hash = *payment_hash;
|
|
|
|
pc->cb = cb;
|
|
|
|
pc->cbarg = cbarg;
|
|
|
|
list_add(&ld->sendpay_commands, &pc->list);
|
|
|
|
tal_add_destructor(pc, destroy_sendpay_command);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Owned by cxt; if cxt is deleted, then cb will
|
|
|
|
* no longer be called. */
|
|
|
|
static void
|
|
|
|
add_waitsendpay_waiter(const tal_t *cxt,
|
|
|
|
const struct sha256 *payment_hash,
|
|
|
|
struct lightningd *ld,
|
|
|
|
void (*cb)(const struct sendpay_result *, void*),
|
|
|
|
void *cbarg)
|
|
|
|
{
|
|
|
|
struct sendpay_command *pc = tal(cxt, struct sendpay_command);
|
|
|
|
|
2018-02-02 01:55:06 +01:00
|
|
|
pc->payment_hash = *payment_hash;
|
2018-02-14 01:09:23 +01:00
|
|
|
pc->cb = cb;
|
|
|
|
pc->cbarg = cbarg;
|
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
|
|
|
}
|
|
|
|
|
2018-02-14 01:09:23 +01:00
|
|
|
/* Caller responsible for freeing ctx. */
|
2018-03-10 07:41:45 +01:00
|
|
|
static void waitsendpay_resolve(const tal_t *ctx,
|
|
|
|
struct lightningd *ld,
|
|
|
|
const struct sha256 *payment_hash,
|
|
|
|
const struct sendpay_result *result)
|
2018-02-02 01:55:06 +01:00
|
|
|
{
|
2018-02-14 01:09:23 +01:00
|
|
|
struct sendpay_command *pc;
|
|
|
|
struct sendpay_command *next;
|
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;
|
|
|
|
|
2018-02-14 01:09:23 +01:00
|
|
|
/* Delete later (in our own caller) if callback did
|
|
|
|
* not delete. */
|
|
|
|
tal_steal(ctx, pc);
|
|
|
|
pc->cb(result, pc->cbarg);
|
2018-02-02 01:55:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-03 01:11:39 +01:00
|
|
|
static struct sendpay_result*
|
|
|
|
sendpay_result_success(const tal_t *ctx,
|
2018-03-13 14:35:43 +01:00
|
|
|
const struct preimage *payment_preimage,
|
|
|
|
const struct wallet_payment *payment)
|
2018-03-03 01:11:39 +01:00
|
|
|
{
|
|
|
|
struct sendpay_result *result = tal(ctx, struct sendpay_result);
|
|
|
|
result->succeeded = true;
|
|
|
|
result->preimage = *payment_preimage;
|
2018-03-13 14:35:43 +01:00
|
|
|
result->payment = payment;
|
2018-03-03 01:11:39 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-03-10 07:41:45 +01:00
|
|
|
static void payment_trigger_success(struct lightningd *ld,
|
2018-03-13 14:35:43 +01:00
|
|
|
const struct sha256 *payment_hash)
|
2018-02-14 01:09:23 +01:00
|
|
|
{
|
|
|
|
struct sendpay_result *result;
|
2018-03-13 14:35:43 +01:00
|
|
|
struct wallet_payment *payment;
|
|
|
|
|
|
|
|
payment = wallet_payment_by_hash(tmpctx, ld->wallet, payment_hash);
|
|
|
|
assert(payment);
|
2018-02-03 12:46:12 +01:00
|
|
|
|
2018-03-13 14:35:43 +01:00
|
|
|
result = sendpay_result_success(tmpctx, payment->payment_preimage, payment);
|
2018-02-14 01:09:23 +01:00
|
|
|
|
2018-03-10 07:41:45 +01:00
|
|
|
waitsendpay_resolve(tmpctx, ld, payment_hash, result);
|
2018-02-14 01:09:23 +01:00
|
|
|
}
|
|
|
|
|
2018-03-03 01:11:39 +01:00
|
|
|
static struct sendpay_result*
|
|
|
|
sendpay_result_route_failure(const tal_t *ctx,
|
|
|
|
bool retry_plausible,
|
|
|
|
struct routing_failure *fail,
|
|
|
|
const u8 *onionreply,
|
|
|
|
const char *details)
|
|
|
|
{
|
|
|
|
struct sendpay_result *result = tal(ctx, struct sendpay_result);
|
|
|
|
result->succeeded = false;
|
|
|
|
result->errorcode =
|
|
|
|
(!fail) ? PAY_UNPARSEABLE_ONION :
|
|
|
|
(!retry_plausible) ? PAY_DESTINATION_PERM_FAIL :
|
|
|
|
/*otherwise*/ PAY_TRY_OTHER_ROUTE ;
|
|
|
|
result->onionreply = onionreply;
|
|
|
|
result->routing_failure = fail;
|
|
|
|
result->details = details;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-03-10 07:41:45 +01:00
|
|
|
static void payment_route_failure(struct lightningd *ld,
|
2018-02-14 01:09:23 +01:00
|
|
|
const struct sha256 *payment_hash,
|
|
|
|
bool retry_plausible,
|
|
|
|
struct routing_failure *fail,
|
|
|
|
const u8 *onionreply,
|
|
|
|
const char *details)
|
2018-02-03 12:46:12 +01:00
|
|
|
{
|
2018-02-14 01:09:23 +01:00
|
|
|
struct sendpay_result *result;
|
|
|
|
|
2018-03-03 01:11:39 +01:00
|
|
|
result = sendpay_result_route_failure(tmpctx,
|
|
|
|
retry_plausible,
|
|
|
|
fail,
|
|
|
|
onionreply,
|
|
|
|
details);
|
2018-02-14 01:09:23 +01:00
|
|
|
|
2018-03-10 07:41:45 +01:00
|
|
|
waitsendpay_resolve(tmpctx, ld, payment_hash, result);
|
2018-02-03 12:46:12 +01:00
|
|
|
}
|
|
|
|
|
2018-03-03 01:11:39 +01:00
|
|
|
static struct sendpay_result *
|
|
|
|
sendpay_result_simple_fail(const tal_t *ctx,
|
|
|
|
int errorcode,
|
|
|
|
char const *details)
|
|
|
|
{
|
|
|
|
struct sendpay_result *result = tal(ctx, struct sendpay_result);
|
|
|
|
result->succeeded = false;
|
|
|
|
result->errorcode = errorcode;
|
|
|
|
result->details = details;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-03-13 14:35:43 +01:00
|
|
|
static struct sendpay_result *
|
|
|
|
sendpay_result_in_progress(const tal_t *ctx,
|
|
|
|
const struct wallet_payment* payment,
|
|
|
|
char const *details)
|
|
|
|
{
|
|
|
|
struct sendpay_result *result = tal(ctx, struct sendpay_result);
|
|
|
|
result->succeeded = false;
|
|
|
|
result->errorcode = PAY_IN_PROGRESS;
|
|
|
|
result->payment = payment;
|
|
|
|
result->details = details;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2018-01-17 21:29:49 +01:00
|
|
|
wallet_payment_set_status(ld->wallet, &hout->payment_hash,
|
|
|
|
PAYMENT_COMPLETE, rval);
|
2018-03-13 14:35:43 +01:00
|
|
|
payment_trigger_success(ld, &hout->payment_hash);
|
2017-04-01 15:05:29 +02:00
|
|
|
}
|
|
|
|
|
2018-07-30 11:51:28 +02:00
|
|
|
/* Fix up the channel_update to include the type if it doesn't currently have
|
|
|
|
* one. See ElementsProject/lightning#1730 and lightningnetwork/lnd#1599 for the
|
|
|
|
* in-depth discussion on why we break message parsing here... */
|
|
|
|
static u8 *patch_channel_update(const tal_t *ctx, u8 *channel_update TAKES)
|
|
|
|
{
|
|
|
|
u8 *fixed;
|
|
|
|
if (channel_update != NULL &&
|
|
|
|
fromwire_peektype(channel_update) != WIRE_CHANNEL_UPDATE) {
|
|
|
|
/* This should be a channel_update, prefix with the
|
|
|
|
* WIRE_CHANNEL_UPDATE type, but isn't. Let's prefix it. */
|
|
|
|
fixed = tal_arr(ctx, u8, 0);
|
|
|
|
towire_u16(&fixed, WIRE_CHANNEL_UPDATE);
|
|
|
|
towire(&fixed, channel_update, tal_bytelen(channel_update));
|
|
|
|
if (taken(channel_update))
|
|
|
|
tal_free(channel_update);
|
|
|
|
return fixed;
|
|
|
|
} else {
|
2018-10-09 10:43:52 +02:00
|
|
|
return tal_dup_arr(ctx, u8,
|
|
|
|
channel_update, tal_count(channel_update), 0);
|
2018-07-30 11:51:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-21 05:15:07 +01:00
|
|
|
/* Return NULL if the wrapped onion error message has no
|
|
|
|
* channel_update field, or return the embedded
|
|
|
|
* channel_update message otherwise. */
|
|
|
|
static u8 *channel_update_from_onion_error(const tal_t *ctx,
|
|
|
|
const u8 *onion_message)
|
|
|
|
{
|
|
|
|
u8 *channel_update = NULL;
|
|
|
|
u64 unused64;
|
|
|
|
u32 unused32;
|
|
|
|
|
|
|
|
/* Identify failcodes that have some channel_update.
|
|
|
|
*
|
|
|
|
* TODO > BOLT 1.0: Add new failcodes when updating to a
|
|
|
|
* new BOLT version. */
|
|
|
|
if (!fromwire_temporary_channel_failure(ctx,
|
2018-02-20 21:59:09 +01:00
|
|
|
onion_message,
|
2018-01-21 05:15:07 +01:00
|
|
|
&channel_update) &&
|
|
|
|
!fromwire_amount_below_minimum(ctx,
|
2018-02-20 21:59:09 +01:00
|
|
|
onion_message, &unused64,
|
2018-01-21 05:15:07 +01:00
|
|
|
&channel_update) &&
|
|
|
|
!fromwire_fee_insufficient(ctx,
|
2018-02-20 21:59:09 +01:00
|
|
|
onion_message, &unused64,
|
2018-01-21 05:15:07 +01:00
|
|
|
&channel_update) &&
|
|
|
|
!fromwire_incorrect_cltv_expiry(ctx,
|
2018-02-20 21:59:09 +01:00
|
|
|
onion_message, &unused32,
|
2018-01-21 05:15:07 +01:00
|
|
|
&channel_update) &&
|
|
|
|
!fromwire_expiry_too_soon(ctx,
|
2018-02-20 21:59:09 +01:00
|
|
|
onion_message,
|
2018-01-21 05:15:07 +01:00
|
|
|
&channel_update))
|
|
|
|
/* No channel update. */
|
2018-07-30 11:51:28 +02:00
|
|
|
return NULL;
|
2018-01-21 05:15:07 +01:00
|
|
|
|
2018-07-30 11:51:28 +02:00
|
|
|
return patch_channel_update(ctx, take(channel_update));
|
2018-01-21 05:15:07 +01: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,
|
|
|
|
const struct pubkey *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-01-15 05:02:27 +01:00
|
|
|
if (dstid)
|
|
|
|
routing_failure->channel_dir = pubkey_idx(&ld->id, dstid);
|
|
|
|
/* FIXME: Don't set at all unless we know. */
|
|
|
|
else
|
|
|
|
routing_failure->channel_dir = 0;
|
2018-02-03 01:05:17 +01:00
|
|
|
routing_failure->channel_update = NULL;
|
|
|
|
|
|
|
|
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-01-15 05:02:27 +01:00
|
|
|
routing_failure->channel_dir = pubkey_idx(&ld->id,
|
|
|
|
&payment->route_nodes[0]);
|
2018-01-29 16:04:33 +01:00
|
|
|
routing_failure->channel_update = NULL;
|
|
|
|
|
|
|
|
return routing_failure;
|
|
|
|
}
|
|
|
|
|
2018-01-21 05:15:07 +01:00
|
|
|
/* Return false if permanent failure at the destination, true if
|
2018-01-29 16:04:33 +01:00
|
|
|
* retrying is plausible. Fill *routing_failure with NULL if
|
|
|
|
* we cannot report the remote failure, or with the routing
|
|
|
|
* failure to report (allocated from ctx) otherwise. */
|
2018-02-03 11:44:31 +01:00
|
|
|
static struct routing_failure*
|
|
|
|
remote_routing_failure(const tal_t *ctx,
|
|
|
|
bool *p_retry_plausible,
|
|
|
|
bool *p_report_to_gossipd,
|
|
|
|
const struct wallet_payment *payment,
|
2018-07-30 18:01:34 +02:00
|
|
|
const struct onionreply *failure,
|
|
|
|
struct log *log)
|
2018-01-21 05:15:07 +01:00
|
|
|
{
|
|
|
|
enum onion_type failcode = fromwire_peektype(failure->msg);
|
|
|
|
u8 *channel_update;
|
2018-02-03 11:44:31 +01:00
|
|
|
struct routing_failure *routing_failure;
|
2018-01-21 05:15:07 +01:00
|
|
|
const struct pubkey *route_nodes;
|
2018-01-29 16:04:33 +01:00
|
|
|
const struct pubkey *erring_node;
|
2018-01-21 05:15:07 +01:00
|
|
|
const struct short_channel_id *route_channels;
|
|
|
|
const struct short_channel_id *erring_channel;
|
2018-03-01 10:22:24 +01:00
|
|
|
static const struct short_channel_id dummy_channel = { 0 };
|
2018-01-21 05:15:07 +01:00
|
|
|
int origin_index;
|
|
|
|
bool retry_plausible;
|
|
|
|
bool report_to_gossipd;
|
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;
|
|
|
|
channel_update
|
2018-02-03 11:44:31 +01:00
|
|
|
= channel_update_from_onion_error(routing_failure,
|
2018-01-29 16:04:33 +01:00
|
|
|
failure->msg);
|
2018-07-30 18:01:34 +02:00
|
|
|
if (channel_update)
|
|
|
|
log_debug(log, "Extracted channel_update %s from onionreply %s",
|
|
|
|
tal_hex(tmpctx, channel_update),
|
|
|
|
tal_hex(tmpctx, failure->msg));
|
|
|
|
|
2018-01-21 05:15:07 +01:00
|
|
|
retry_plausible = true;
|
|
|
|
report_to_gossipd = true;
|
|
|
|
|
|
|
|
assert(origin_index < tal_count(route_nodes));
|
|
|
|
|
|
|
|
/* Check if at destination. */
|
|
|
|
if (origin_index == tal_count(route_nodes) - 1) {
|
2019-01-15 05:02:27 +01:00
|
|
|
/* FIXME: Don't set erring_channel or dir in this case! */
|
2018-02-03 11:44:31 +01:00
|
|
|
erring_channel = &dummy_channel;
|
2019-01-15 05:02:27 +01:00
|
|
|
dir = 0;
|
|
|
|
|
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)
|
|
|
|
retry_plausible = false;
|
|
|
|
else
|
|
|
|
retry_plausible = true;
|
|
|
|
/* Only send message to gossipd if NODE error;
|
|
|
|
* there is no "next" channel to report as
|
|
|
|
* failing if this is the last node. */
|
2018-02-03 11:44:31 +01:00
|
|
|
if (failcode & NODE)
|
2018-01-21 05:15:07 +01:00
|
|
|
report_to_gossipd = true;
|
2018-02-03 11:44:31 +01:00
|
|
|
else
|
2018-01-21 05:15:07 +01:00
|
|
|
report_to_gossipd = false;
|
2019-01-08 01:53:25 +01:00
|
|
|
erring_node = &route_nodes[origin_index];
|
|
|
|
} else {
|
2018-01-21 05:15:07 +01:00
|
|
|
/* Report the *next* channel as failing. */
|
|
|
|
erring_channel = &route_channels[origin_index + 1];
|
|
|
|
|
2019-01-15 05:02:27 +01:00
|
|
|
dir = pubkey_idx(&route_nodes[origin_index],
|
|
|
|
&route_nodes[origin_index+1]);
|
|
|
|
|
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];
|
|
|
|
}
|
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;
|
|
|
|
routing_failure->channel_update = channel_update;
|
2019-01-15 05:02:27 +01:00
|
|
|
routing_failure->channel_dir = dir;
|
2018-02-03 11:44:31 +01:00
|
|
|
|
|
|
|
*p_retry_plausible = retry_plausible;
|
|
|
|
*p_report_to_gossipd = report_to_gossipd;
|
2018-01-21 05:15:07 +01:00
|
|
|
|
2018-02-03 11:44:31 +01:00
|
|
|
return routing_failure;
|
2018-01-21 05:15:07 +01:00
|
|
|
}
|
|
|
|
|
2018-02-07 13:35:52 +01:00
|
|
|
static void random_mark_channel_unroutable(struct log *log,
|
|
|
|
struct subd *gossip,
|
|
|
|
struct short_channel_id *route_channels)
|
|
|
|
{
|
|
|
|
size_t num_channels = tal_count(route_channels);
|
|
|
|
size_t i;
|
|
|
|
const struct short_channel_id *channel;
|
|
|
|
u8 *msg;
|
2018-02-08 09:09:17 +01:00
|
|
|
assert(num_channels != 0);
|
2018-02-07 13:35:52 +01:00
|
|
|
|
|
|
|
/* Select one channel by random. */
|
|
|
|
randombytes_buf(&i, sizeof(i));
|
|
|
|
i = i % num_channels;
|
|
|
|
channel = &route_channels[i];
|
|
|
|
|
|
|
|
log_debug(log,
|
|
|
|
"Disable randomly %dth channel (%s) along route "
|
|
|
|
"(guessing due to bad reply)",
|
|
|
|
(int) i,
|
|
|
|
type_to_string(tmpctx, struct short_channel_id,
|
|
|
|
channel));
|
|
|
|
msg = towire_gossip_mark_channel_unroutable(tmpctx, channel);
|
|
|
|
subd_send_msg(gossip, msg);
|
|
|
|
}
|
|
|
|
|
2018-02-03 00:56:40 +01:00
|
|
|
static void report_routing_failure(struct log *log,
|
|
|
|
struct subd *gossip,
|
2018-01-29 16:04:33 +01:00
|
|
|
struct routing_failure *fail)
|
|
|
|
{
|
2018-02-03 00:56:40 +01:00
|
|
|
u8 *gossip_msg;
|
|
|
|
assert(fail);
|
|
|
|
|
|
|
|
log_debug(log,
|
|
|
|
"Reporting route failure to gossipd: 0x%04x (%s) "
|
|
|
|
"node %s channel %s update %s",
|
|
|
|
fail->failcode, onion_type_name(fail->failcode),
|
|
|
|
type_to_string(tmpctx, struct pubkey,
|
|
|
|
&fail->erring_node),
|
|
|
|
type_to_string(tmpctx, struct short_channel_id,
|
|
|
|
&fail->erring_channel),
|
|
|
|
tal_hex(tmpctx, fail->channel_update));
|
2018-07-30 13:16:20 +02:00
|
|
|
|
2018-02-03 00:56:40 +01:00
|
|
|
gossip_msg = towire_gossip_routing_failure(tmpctx,
|
|
|
|
&fail->erring_node,
|
|
|
|
&fail->erring_channel,
|
|
|
|
(u16) fail->failcode,
|
|
|
|
fail->channel_update);
|
2018-01-29 16:04:33 +01:00
|
|
|
subd_send_msg(gossip, gossip_msg);
|
|
|
|
}
|
|
|
|
|
2018-03-10 07:39:11 +01:00
|
|
|
void payment_store(struct lightningd *ld,
|
|
|
|
const struct sha256 *payment_hash)
|
|
|
|
{
|
2018-03-10 08:44:28 +01:00
|
|
|
struct sendpay_command *pc;
|
|
|
|
struct sendpay_command *next;
|
|
|
|
struct sendpay_result *result;
|
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
|
|
|
|
|
|
|
/* Invent a sendpay result with PAY_IN_PROGRESS. */
|
2018-03-13 14:35:43 +01:00
|
|
|
result = sendpay_result_in_progress(tmpctx, payment,
|
2018-03-10 08:44:28 +01:00
|
|
|
"Payment is still in progress");
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
|
|
|
/* Delete later if callback did not delete. */
|
|
|
|
tal_steal(tmpctx, pc);
|
|
|
|
pc->cb(result, pc->cbarg);
|
|
|
|
}
|
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;
|
|
|
|
bool retry_plausible;
|
2018-02-03 11:44:31 +01:00
|
|
|
bool report_to_gossipd;
|
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;
|
|
|
|
retry_plausible = true;
|
2018-02-03 11:44:31 +01:00
|
|
|
report_to_gossipd = true;
|
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;
|
|
|
|
struct onionreply *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));
|
2018-01-29 16:04:33 +01:00
|
|
|
/* Cannot report failure. */
|
|
|
|
fail = NULL;
|
2018-02-07 13:35:52 +01:00
|
|
|
/* Select a channel to mark unroutable by random */
|
2018-02-12 11:13:04 +01:00
|
|
|
random_mark_channel_unroutable(hout->key.channel->log,
|
2018-02-07 13:35:52 +01:00
|
|
|
ld->gossip,
|
|
|
|
payment->route_channels);
|
|
|
|
/* Can now retry; we selected a channel to mark
|
|
|
|
* unroutable by random */
|
|
|
|
retry_plausible = true;
|
|
|
|
/* Already reported something to gossipd, do not
|
|
|
|
* report anything else */
|
2018-02-03 11:44:31 +01:00
|
|
|
report_to_gossipd = false;
|
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));
|
2018-02-03 11:44:31 +01:00
|
|
|
fail = remote_routing_failure(tmpctx,
|
|
|
|
&retry_plausible,
|
|
|
|
&report_to_gossipd,
|
2018-07-30 18:01:34 +02:00
|
|
|
payment, reply,
|
|
|
|
hout->key.channel->log);
|
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,
|
|
|
|
(fail && !retry_plausible),
|
|
|
|
fail ? fail->erring_index : -1,
|
|
|
|
fail ? fail->failcode : 0,
|
|
|
|
fail ? &fail->erring_node : NULL,
|
|
|
|
fail ? &fail->erring_channel : NULL,
|
2018-04-16 15:29:40 +02:00
|
|
|
fail ? fail->channel_update : NULL,
|
2019-01-15 05:02:27 +01:00
|
|
|
failmsg,
|
|
|
|
fail ? fail->channel_dir : 0);
|
2018-01-29 18:34:59 +01:00
|
|
|
|
2018-02-03 11:44:31 +01:00
|
|
|
/* Report to gossipd if we decided we should. */
|
|
|
|
if (report_to_gossipd)
|
2018-02-03 00:56:40 +01:00
|
|
|
report_routing_failure(ld->log, ld->gossip, fail);
|
2017-06-20 07:53:03 +02:00
|
|
|
|
2018-03-07 17:10:08 +01:00
|
|
|
|
2018-02-14 01:09:23 +01:00
|
|
|
/* Report to client. */
|
2018-03-10 07:41:45 +01:00
|
|
|
payment_route_failure(ld, &hout->payment_hash,
|
2018-02-14 01:09:23 +01:00
|
|
|
retry_plausible, fail, hout->failuremsg,
|
|
|
|
failmsg);
|
2017-04-01 15:05:29 +02:00
|
|
|
}
|
|
|
|
|
2018-03-04 06:35:37 +01:00
|
|
|
/* Wait for a payment. If cxt is deleted, then cb will
|
|
|
|
* no longer be called.
|
|
|
|
* Return false if we called callback already, true if
|
|
|
|
* callback is scheduled for later. */
|
|
|
|
bool wait_payment(const tal_t *cxt,
|
|
|
|
struct lightningd *ld,
|
|
|
|
const struct sha256 *payment_hash,
|
|
|
|
void (*cb)(const struct sendpay_result *, void*),
|
2018-02-15 04:32:03 +01:00
|
|
|
void *cbarg)
|
2018-03-04 06:35:37 +01:00
|
|
|
{
|
|
|
|
struct wallet_payment *payment;
|
|
|
|
struct sendpay_result *result;
|
|
|
|
char const *details;
|
|
|
|
bool cb_not_called;
|
2018-03-07 17:10:08 +01:00
|
|
|
u8 *failonionreply;
|
|
|
|
bool faildestperm;
|
|
|
|
int failindex;
|
|
|
|
enum onion_type failcode;
|
|
|
|
struct pubkey *failnode;
|
|
|
|
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) {
|
|
|
|
details = tal_fmt(tmpctx,
|
|
|
|
"Never attempted payment for '%s'",
|
|
|
|
type_to_string(tmpctx, struct sha256,
|
|
|
|
payment_hash));
|
|
|
|
result = sendpay_result_simple_fail(tmpctx,
|
|
|
|
PAY_NO_SUCH_PAYMENT,
|
|
|
|
details);
|
|
|
|
cb(result, cbarg);
|
|
|
|
cb_not_called = false;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (payment->status) {
|
|
|
|
case PAYMENT_PENDING:
|
2018-03-10 08:44:28 +01:00
|
|
|
add_waitsendpay_waiter(cxt, payment_hash, ld, cb, cbarg);
|
2018-03-04 06:35:37 +01:00
|
|
|
cb_not_called = true;
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
case PAYMENT_COMPLETE:
|
|
|
|
result = sendpay_result_success(tmpctx,
|
2018-03-13 14:35:43 +01:00
|
|
|
payment->payment_preimage,
|
|
|
|
payment);
|
2018-03-04 06:35:37 +01:00
|
|
|
cb(result, cbarg);
|
|
|
|
cb_not_called = false;
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
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 */
|
|
|
|
if (!failonionreply && !failnode)
|
|
|
|
result = sendpay_result_simple_fail(tmpctx,
|
|
|
|
PAY_UNSPECIFIED_ERROR,
|
|
|
|
"Payment failure reason unknown");
|
|
|
|
else if (failonionreply) {
|
|
|
|
/* failed to parse returned onion error */
|
2018-04-16 15:29:40 +02:00
|
|
|
result = sendpay_result_route_failure(tmpctx, true, NULL, failonionreply, 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;
|
|
|
|
fail->channel_update = failupdate;
|
2019-01-15 05:02:27 +01:00
|
|
|
fail->channel_dir = faildirection;
|
2018-04-16 15:29:40 +02:00
|
|
|
result = sendpay_result_route_failure(tmpctx, !faildestperm, fail, NULL, faildetail);
|
2018-03-07 17:10:08 +01:00
|
|
|
}
|
|
|
|
|
2018-03-04 06:35:37 +01:00
|
|
|
cb(result, cbarg);
|
|
|
|
cb_not_called = false;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Impossible. */
|
|
|
|
abort();
|
|
|
|
|
|
|
|
end:
|
|
|
|
return cb_not_called;
|
|
|
|
}
|
|
|
|
|
2018-03-10 08:44:28 +01:00
|
|
|
/* Returns false if cb was called, true if cb not yet called. */
|
|
|
|
bool
|
2018-03-04 06:35:37 +01:00
|
|
|
send_payment(const tal_t *ctx,
|
|
|
|
struct lightningd* ld,
|
|
|
|
const struct sha256 *rhash,
|
2018-03-10 08:44:28 +01:00
|
|
|
const struct route_hop *route,
|
2018-03-22 13:49:09 +01:00
|
|
|
u64 msatoshi,
|
2018-07-23 13:59:54 +02:00
|
|
|
const char *description TAKES,
|
2018-03-10 08:44:28 +01:00
|
|
|
void (*cb)(const struct sendpay_result *, void*),
|
|
|
|
void *cbarg)
|
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);
|
|
|
|
struct pubkey *ids = tal_arr(tmpctx, struct pubkey, 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;
|
2018-03-04 06:35:37 +01:00
|
|
|
struct sendpay_result *result;
|
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;
|
2017-04-01 15:05:29 +02:00
|
|
|
|
2017-10-26 04:56:19 +02:00
|
|
|
/* Extract IDs for each hop: create_onionpacket wants array. */
|
|
|
|
for (i = 0; i < n_hops; i++)
|
|
|
|
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++) {
|
|
|
|
hop_data[i].realm = 0;
|
|
|
|
hop_data[i].channel_id = route[i+1].channel_id;
|
|
|
|
hop_data[i].amt_forward = route[i+1].amount;
|
|
|
|
hop_data[i].outgoing_cltv = 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 */
|
2017-10-26 04:56:19 +02:00
|
|
|
hop_data[i].realm = 0;
|
|
|
|
hop_data[i].outgoing_cltv = base_expiry + route[i].delay;
|
|
|
|
memset(&hop_data[i].channel_id, 0, sizeof(struct short_channel_id));
|
|
|
|
hop_data[i].amt_forward = route[i].amount;
|
2017-04-01 15:05:29 +02:00
|
|
|
|
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");
|
2018-03-13 14:35:43 +01:00
|
|
|
result = sendpay_result_in_progress(tmpctx,
|
|
|
|
payment,
|
2018-03-10 08:44:28 +01:00
|
|
|
"Payment is still in progress");
|
|
|
|
cb(result, cbarg);
|
|
|
|
return false;
|
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. */
|
2018-03-22 13:49:09 +01:00
|
|
|
if (payment->msatoshi != msatoshi) {
|
2018-03-10 08:44:28 +01:00
|
|
|
char *msg = tal_fmt(tmpctx,
|
2018-02-14 01:09:23 +01:00
|
|
|
"Already succeeded "
|
|
|
|
"with amount %"PRIu64,
|
|
|
|
payment->msatoshi);
|
2018-03-10 08:44:28 +01:00
|
|
|
result = sendpay_result_simple_fail(tmpctx,
|
|
|
|
PAY_RHASH_ALREADY_USED,
|
|
|
|
msg);
|
|
|
|
cb(result, cbarg);
|
|
|
|
return false;
|
2017-04-01 15:05:29 +02:00
|
|
|
}
|
2018-07-04 07:30:02 +02:00
|
|
|
if (!pubkey_eq(&payment->destination, &ids[n_hops-1])) {
|
2018-03-10 08:44:28 +01:00
|
|
|
char *msg = tal_fmt(tmpctx,
|
2018-02-14 01:09:23 +01:00
|
|
|
"Already succeeded to %s",
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct pubkey,
|
|
|
|
&payment->destination));
|
2018-03-10 08:44:28 +01:00
|
|
|
result = sendpay_result_simple_fail(tmpctx,
|
|
|
|
PAY_RHASH_ALREADY_USED,
|
|
|
|
msg);
|
|
|
|
cb(result, cbarg);
|
|
|
|
return false;
|
2017-04-01 15:05:29 +02:00
|
|
|
}
|
2018-03-10 08:44:28 +01:00
|
|
|
result = sendpay_result_success(tmpctx,
|
2018-03-13 14:35:43 +01:00
|
|
|
payment->payment_preimage,
|
|
|
|
payment);
|
2018-03-10 08:44:28 +01:00
|
|
|
cb(result, cbarg);
|
|
|
|
return false;
|
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) {
|
2018-02-03 12:20:24 +01:00
|
|
|
/* Report routing failure to gossipd */
|
2018-03-04 06:35:37 +01:00
|
|
|
fail = immediate_routing_failure(ctx, ld,
|
2018-02-03 12:20:24 +01:00
|
|
|
WIRE_UNKNOWN_NEXT_PEER,
|
2019-01-15 05:02:27 +01:00
|
|
|
&route[0].channel_id,
|
|
|
|
0);
|
2018-02-14 01:09:23 +01:00
|
|
|
report_routing_failure(ld->log, ld->gossip, fail);
|
2018-02-03 12:20:24 +01:00
|
|
|
|
2018-03-04 06:35:37 +01:00
|
|
|
/* Report routing failure to caller */
|
2018-03-10 08:44:28 +01:00
|
|
|
result = sendpay_result_route_failure(tmpctx, true, fail, NULL,
|
|
|
|
"No connection to first "
|
|
|
|
"peer found");
|
|
|
|
cb(result, cbarg);
|
|
|
|
return false;
|
2017-04-01 15:05:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
randombytes_buf(&sessionkey, sizeof(sessionkey));
|
|
|
|
|
|
|
|
/* Onion will carry us from first peer onwards. */
|
2018-02-14 01:09:23 +01:00
|
|
|
packet = create_onionpacket(tmpctx, ids, hop_data, sessionkey, rhash->u.u8,
|
2017-05-03 11:01:58 +02:00
|
|
|
sizeof(struct sha256), &path_secrets);
|
2018-02-14 01:09:23 +01:00
|
|
|
onion = serialize_onionpacket(tmpctx, packet);
|
2017-04-01 15:05:29 +02:00
|
|
|
|
2018-04-09 15:31:23 +02:00
|
|
|
log_info(ld->log, "Sending %"PRIu64" over %zu hops to deliver %"PRIu64"",
|
2018-03-22 13:49:09 +01:00
|
|
|
route[0].amount, n_hops, msatoshi);
|
2018-01-17 21:29:49 +01:00
|
|
|
|
2018-02-12 11:13:04 +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) {
|
2018-02-03 01:05:17 +01:00
|
|
|
/* Report routing failure to gossipd */
|
2018-03-04 06:35:37 +01:00
|
|
|
fail = immediate_routing_failure(ctx, 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-14 01:09:23 +01:00
|
|
|
report_routing_failure(ld->log, ld->gossip, fail);
|
2018-02-03 01:05:17 +01:00
|
|
|
|
2018-03-04 06:35:37 +01:00
|
|
|
/* Report routing failure to caller */
|
2018-03-10 08:44:28 +01:00
|
|
|
result = sendpay_result_route_failure(tmpctx, true, fail, NULL,
|
|
|
|
"First peer not ready");
|
|
|
|
cb(result, cbarg);
|
|
|
|
return false;
|
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;
|
2018-03-22 13:49:09 +01:00
|
|
|
payment->msatoshi = msatoshi;
|
2018-03-22 14:21:18 +01:00
|
|
|
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);
|
2018-07-23 13:59:54 +02:00
|
|
|
if (description != NULL)
|
|
|
|
payment->description = tal_strdup(payment, description);
|
|
|
|
else
|
|
|
|
payment->description = 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
|
|
|
|
2018-03-10 08:44:28 +01:00
|
|
|
add_sendpay_waiter(ctx, rhash, ld, cb, cbarg);
|
|
|
|
|
|
|
|
return true;
|
2017-04-01 15:05:29 +02:00
|
|
|
}
|
|
|
|
|
2018-02-14 01:09:23 +01:00
|
|
|
/*-----------------------------------------------------------------------------
|
|
|
|
JSON-RPC sendpay interface
|
|
|
|
-----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
json_sendpay_success(struct command *cmd,
|
2018-03-14 01:30:56 +01:00
|
|
|
const struct sendpay_result *r)
|
2018-02-14 01:09:23 +01:00
|
|
|
{
|
2018-10-19 03:17:49 +02:00
|
|
|
struct json_stream *response;
|
2018-02-14 01:09:23 +01:00
|
|
|
|
2018-03-14 01:30:56 +01:00
|
|
|
assert(r->payment->status == PAYMENT_COMPLETE);
|
|
|
|
|
2018-10-19 03:17:48 +02:00
|
|
|
response = json_stream_success(cmd);
|
2018-02-14 01:09:23 +01:00
|
|
|
json_object_start(response, NULL);
|
2018-03-14 01:30:56 +01:00
|
|
|
json_add_payment_fields(response, r->payment);
|
2018-02-14 01:09:23 +01:00
|
|
|
json_object_end(response);
|
2018-12-16 05:53:06 +01:00
|
|
|
was_pending(command_success(cmd, response));
|
2018-02-14 01:09:23 +01:00
|
|
|
}
|
|
|
|
|
2018-03-10 07:41:45 +01:00
|
|
|
static void json_waitsendpay_on_resolve(const struct sendpay_result *r,
|
2018-02-14 01:09:23 +01:00
|
|
|
void *vcmd)
|
|
|
|
{
|
|
|
|
struct command *cmd = (struct command*) vcmd;
|
|
|
|
|
2018-02-19 10:59:31 +01:00
|
|
|
const char *msg = NULL;
|
2018-02-14 01:09:23 +01:00
|
|
|
struct routing_failure *fail;
|
|
|
|
|
|
|
|
if (r->succeeded)
|
2018-03-14 01:30:56 +01:00
|
|
|
json_sendpay_success(cmd, r);
|
2018-02-14 01:09:23 +01:00
|
|
|
else {
|
2018-10-19 03:17:49 +02:00
|
|
|
struct json_stream *data;
|
2018-02-14 01:09:23 +01:00
|
|
|
switch (r->errorcode) {
|
2018-03-14 01:30:56 +01:00
|
|
|
/* We will never handle this case */
|
2018-02-14 01:09:23 +01:00
|
|
|
case PAY_IN_PROGRESS:
|
2018-03-14 01:30:56 +01:00
|
|
|
abort();
|
|
|
|
|
2018-02-14 01:09:23 +01:00
|
|
|
case PAY_RHASH_ALREADY_USED:
|
2018-03-04 06:35:37 +01:00
|
|
|
case PAY_UNSPECIFIED_ERROR:
|
|
|
|
case PAY_NO_SUCH_PAYMENT:
|
2018-12-16 05:53:06 +01:00
|
|
|
was_pending(command_fail(cmd, r->errorcode, "%s",
|
|
|
|
r->details));
|
2018-10-19 03:17:48 +02:00
|
|
|
return;
|
2018-02-14 01:09:23 +01:00
|
|
|
|
|
|
|
case PAY_UNPARSEABLE_ONION:
|
2018-10-19 03:17:48 +02:00
|
|
|
msg = tal_fmt(tmpctx,
|
2018-02-14 01:09:23 +01:00
|
|
|
"failed: WIRE_PERMANENT_NODE_FAILURE "
|
|
|
|
"(%s)",
|
|
|
|
r->details);
|
|
|
|
|
2018-10-19 03:17:48 +02:00
|
|
|
data = json_stream_fail(cmd, r->errorcode, msg);
|
|
|
|
json_object_start(data, NULL);
|
|
|
|
json_add_hex_talarr(data, "onionreply", r->onionreply);
|
|
|
|
json_object_end(data);
|
2018-12-16 05:53:06 +01:00
|
|
|
was_pending(command_failed(cmd, data));
|
2018-10-19 03:17:48 +02:00
|
|
|
return;
|
2018-02-14 01:09:23 +01:00
|
|
|
|
|
|
|
case PAY_DESTINATION_PERM_FAIL:
|
|
|
|
case PAY_TRY_OTHER_ROUTE:
|
|
|
|
fail = r->routing_failure;
|
2018-10-19 03:17:48 +02:00
|
|
|
msg = tal_fmt(cmd,
|
|
|
|
"failed: %s (%s)",
|
|
|
|
onion_type_name(fail->failcode),
|
|
|
|
r->details);
|
|
|
|
data = json_stream_fail(cmd, r->errorcode, msg);
|
2018-02-14 01:09:23 +01:00
|
|
|
|
|
|
|
json_object_start(data, NULL);
|
|
|
|
json_add_num(data, "erring_index",
|
|
|
|
fail->erring_index);
|
|
|
|
json_add_num(data, "failcode",
|
|
|
|
(unsigned) fail->failcode);
|
2018-04-04 02:19:42 +02:00
|
|
|
json_add_pubkey(data, "erring_node", &fail->erring_node);
|
2018-02-14 01:09:23 +01:00
|
|
|
json_add_short_channel_id(data, "erring_channel",
|
|
|
|
&fail->erring_channel);
|
2019-01-15 05:12:27 +01:00
|
|
|
json_add_num(data, "erring_direction",
|
2019-01-15 05:02:27 +01:00
|
|
|
fail->channel_dir);
|
2018-02-14 01:09:23 +01:00
|
|
|
if (fail->channel_update)
|
2018-07-28 07:53:33 +02:00
|
|
|
json_add_hex_talarr(data, "channel_update",
|
|
|
|
fail->channel_update);
|
2018-02-14 01:09:23 +01:00
|
|
|
json_object_end(data);
|
2018-12-16 05:53:06 +01:00
|
|
|
was_pending(command_failed(cmd, data));
|
2018-10-19 03:17:48 +02:00
|
|
|
return;
|
2018-02-14 01:09:23 +01:00
|
|
|
}
|
2018-10-19 03:17:48 +02:00
|
|
|
abort();
|
2018-02-14 01:09:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-10 07:41:45 +01:00
|
|
|
static void json_sendpay_on_resolve(const struct sendpay_result* r,
|
|
|
|
void *vcmd)
|
|
|
|
{
|
|
|
|
struct command *cmd = (struct command*) vcmd;
|
2018-03-10 08:44:28 +01:00
|
|
|
|
|
|
|
if (!r->succeeded && r->errorcode == PAY_IN_PROGRESS) {
|
|
|
|
/* This is normal for sendpay. Succeed. */
|
2018-10-19 03:17:49 +02:00
|
|
|
struct json_stream *response = json_stream_success(cmd);
|
2018-03-10 08:44:28 +01:00
|
|
|
json_object_start(response, NULL);
|
|
|
|
json_add_string(response, "message",
|
|
|
|
"Monitor status with listpayments or waitsendpay");
|
2018-03-14 01:30:56 +01:00
|
|
|
json_add_payment_fields(response, r->payment);
|
2018-03-10 08:44:28 +01:00
|
|
|
json_object_end(response);
|
2018-12-16 05:53:06 +01:00
|
|
|
was_pending(command_success(cmd, response));
|
2018-03-10 08:44:28 +01:00
|
|
|
} else
|
|
|
|
json_waitsendpay_on_resolve(r, cmd);
|
2018-03-10 07:41:45 +01:00
|
|
|
}
|
|
|
|
|
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;
|
2017-10-26 04:56:19 +02:00
|
|
|
const jsmntok_t *t, *end;
|
|
|
|
size_t n_hops;
|
2018-08-14 22:16:49 +02:00
|
|
|
struct sha256 *rhash;
|
2017-10-26 04:56:19 +02:00
|
|
|
struct route_hop *route;
|
2018-07-20 03:14:02 +02:00
|
|
|
u64 *msatoshi;
|
2018-07-23 14:37:05 +02:00
|
|
|
const char *description;
|
2017-10-26 04:56:19 +02:00
|
|
|
|
2018-07-20 03:14:02 +02:00
|
|
|
if (!param(cmd, buffer, params,
|
2018-12-16 05:50:06 +01:00
|
|
|
p_req("route", param_array, &routetok),
|
|
|
|
p_req("payment_hash", param_sha256, &rhash),
|
|
|
|
p_opt("description", param_escaped_string, &description),
|
|
|
|
p_opt("msatoshi", param_u64, &msatoshi),
|
2018-07-20 03:14:02 +02:00
|
|
|
NULL))
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_param_failed();
|
2017-10-26 04:56:19 +02:00
|
|
|
|
|
|
|
end = json_next(routetok);
|
|
|
|
n_hops = 0;
|
|
|
|
route = tal_arr(cmd, struct route_hop, n_hops);
|
|
|
|
|
|
|
|
for (t = routetok + 1; t < end; t = json_next(t)) {
|
2018-08-23 18:08:32 +02:00
|
|
|
u64 *amount;
|
|
|
|
struct pubkey *id;
|
|
|
|
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,
|
2018-12-16 05:50:06 +01:00
|
|
|
p_req("msatoshi", param_u64, &amount),
|
|
|
|
p_req("id", param_pubkey, &id),
|
|
|
|
p_req("delay", param_number, &delay),
|
|
|
|
p_req("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
|
|
|
|
|
|
|
tal_resize(&route, n_hops + 1);
|
|
|
|
|
2018-08-23 18:08:32 +02:00
|
|
|
route[n_hops].amount = *amount;
|
|
|
|
route[n_hops].nodeid = *id;
|
|
|
|
route[n_hops].delay = *delay;
|
|
|
|
route[n_hops].channel_id = *channel;
|
2019-01-15 11:04:07 +01:00
|
|
|
/* FIXME: Actually ignored by sending code! */
|
|
|
|
route[n_hops].direction = direction ? *direction : 0;
|
2017-10-26 04:56:19 +02:00
|
|
|
n_hops++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n_hops == 0) {
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS, "Empty route");
|
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. */
|
|
|
|
if (msatoshi) {
|
|
|
|
if (!(*msatoshi <= route[n_hops-1].amount &&
|
|
|
|
route[n_hops-1].amount <= 2 * *msatoshi)) {
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"msatoshi %"PRIu64" out of range",
|
|
|
|
*msatoshi);
|
2018-03-22 13:49:09 +01:00
|
|
|
}
|
2018-07-20 03:14:02 +02:00
|
|
|
}
|
2018-03-22 13:49:09 +01:00
|
|
|
|
2018-08-14 22:16:49 +02:00
|
|
|
if (send_payment(cmd, cmd->ld, rhash, route,
|
2018-07-20 03:14:02 +02:00
|
|
|
msatoshi ? *msatoshi : route[n_hops-1].amount,
|
2018-07-23 14:37:05 +02:00
|
|
|
description,
|
2018-07-20 03:14:02 +02:00
|
|
|
&json_sendpay_on_resolve, cmd))
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_still_pending(cmd);
|
2018-12-17 04:52:08 +01:00
|
|
|
return command_its_complicated("send_payment is called in multiple paths,"
|
|
|
|
" patching return value through is hard");
|
2017-10-26 04:56:19 +02:00
|
|
|
}
|
|
|
|
|
2017-04-01 15:05:29 +02:00
|
|
|
static const struct json_command sendpay_command = {
|
|
|
|
"sendpay",
|
|
|
|
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;
|
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
|
|
|
|
2018-08-14 22:16:49 +02:00
|
|
|
if (!wait_payment(cmd, cmd->ld, rhash, &json_waitsendpay_on_resolve, cmd))
|
2018-12-17 04:52:08 +01:00
|
|
|
return command_its_complicated("wait_payment called in multiple"
|
|
|
|
" paths, patching return value"
|
|
|
|
" through is hard");
|
2018-03-04 06:35:37 +01:00
|
|
|
|
2018-07-20 03:14:02 +02:00
|
|
|
if (timeout)
|
|
|
|
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",
|
|
|
|
json_waitsendpay,
|
|
|
|
"Wait for payment attempt on {payment_hash} to succeed or fail, "
|
|
|
|
"but only up to {timeout} seconds."
|
|
|
|
};
|
|
|
|
AUTODATA(json_command, &waitsendpay_command);
|
|
|
|
|
2018-12-16 05:52:06 +01:00
|
|
|
static struct command_result *json_listpayments(struct command *cmd,
|
|
|
|
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-01-17 00:53:18 +01:00
|
|
|
json_object_start(response, NULL);
|
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-01-17 00:53:18 +01:00
|
|
|
json_object_end(response);
|
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",
|
|
|
|
json_listpayments,
|
2018-01-22 09:55:07 +01:00
|
|
|
"Show outgoing payments"
|
2017-11-16 19:09:09 +01:00
|
|
|
};
|
|
|
|
AUTODATA(json_command, &listpayments_command);
|