2017-04-01 15:05:29 +02:00
|
|
|
#include "pay.h"
|
|
|
|
#include <bitcoin/preimage.h>
|
|
|
|
#include <ccan/str/hex/hex.h>
|
|
|
|
#include <ccan/structeq/structeq.h>
|
2017-10-26 05:07:19 +02:00
|
|
|
#include <ccan/tal/str/str.h>
|
2017-08-29 06:12:04 +02:00
|
|
|
#include <channeld/gen_channel_wire.h>
|
2017-11-22 01:25:39 +01:00
|
|
|
#include <common/bolt11.h>
|
2017-10-26 05:07:19 +02:00
|
|
|
#include <gossipd/gen_gossip_wire.h>
|
2017-10-26 04:56:19 +02:00
|
|
|
#include <gossipd/routing.h>
|
2017-04-01 15:05:29 +02:00
|
|
|
#include <inttypes.h>
|
2017-08-28 18:04:01 +02:00
|
|
|
#include <lightningd/chaintopology.h>
|
|
|
|
#include <lightningd/jsonrpc.h>
|
2018-02-03 12:46:12 +01:00
|
|
|
#include <lightningd/jsonrpc_errors.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
|
|
|
|
-----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
struct routing_failure {
|
|
|
|
unsigned int erring_index;
|
|
|
|
enum onion_type failcode;
|
|
|
|
struct pubkey erring_node;
|
|
|
|
struct short_channel_id erring_channel;
|
|
|
|
u8 *channel_update;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Result of sendpay */
|
|
|
|
struct sendpay_result {
|
|
|
|
/* Did the payment succeed? */
|
|
|
|
bool succeeded;
|
|
|
|
/* Preimage. Only loaded if payment succeeded. */
|
|
|
|
struct preimage preimage;
|
|
|
|
/* Error code, one of the PAY_* macro in jsonrpc_errors.h.
|
|
|
|
* Only loaded if payment failed. */
|
|
|
|
int errorcode;
|
|
|
|
/* Unparseable onion reply. Only loaded if payment failed,
|
|
|
|
* and errorcode == PAY_UNPARSEABLE_ONION. */
|
|
|
|
const u8* onionreply;
|
|
|
|
/* Routing failure object. Only loaded if payment failed,
|
|
|
|
* and errorcode == PAY_DESTINATION_PERM_FAIL or
|
|
|
|
* errorcode == PAY_TRY_OTHER_ROUTE */
|
|
|
|
struct routing_failure* routing_failure;
|
|
|
|
/* Error message. Only loaded if payment failed. */
|
|
|
|
const char *details;
|
|
|
|
};
|
|
|
|
|
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-02-14 01:09:23 +01:00
|
|
|
/* Owned by cxt; if cxt is deleted, then cb will
|
|
|
|
* no longer be called. */
|
2018-02-11 02:40:43 +01:00
|
|
|
static struct sendpay_command *
|
2018-02-14 01:09:23 +01:00
|
|
|
new_sendpay_command(const tal_t *cxt,
|
2018-02-11 02:40:43 +01:00
|
|
|
const struct sha256 *payment_hash,
|
2018-02-14 01:09:23 +01:00
|
|
|
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
|
|
|
|
|
|
|
pc->payment_hash = *payment_hash;
|
2018-02-14 01:09:23 +01:00
|
|
|
pc->cb = cb;
|
|
|
|
pc->cbarg = cbarg;
|
2018-02-11 02:40:43 +01:00
|
|
|
list_add(&ld->sendpay_commands, &pc->list);
|
|
|
|
tal_add_destructor(pc, destroy_sendpay_command);
|
2018-02-02 01:55:06 +01:00
|
|
|
return pc;
|
|
|
|
}
|
|
|
|
|
2018-02-14 01:09:23 +01:00
|
|
|
/* Caller responsible for freeing ctx. */
|
|
|
|
static void sendpay_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-02-11 02:40:43 +01:00
|
|
|
list_for_each_safe(&ld->sendpay_commands, pc, next, list) {
|
2018-02-02 01:55:06 +01:00
|
|
|
if (!structeq(payment_hash, &pc->payment_hash))
|
|
|
|
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-02-14 01:09:23 +01:00
|
|
|
static void sendpay_success(struct lightningd *ld,
|
|
|
|
const struct sha256 *payment_hash,
|
|
|
|
const struct preimage *payment_preimage)
|
|
|
|
{
|
|
|
|
const tal_t *tmpctx = tal_tmpctx(ld);
|
|
|
|
struct sendpay_result *result;
|
2018-02-03 12:46:12 +01:00
|
|
|
|
2018-02-14 01:09:23 +01:00
|
|
|
result = tal(tmpctx, struct sendpay_result);
|
|
|
|
result->succeeded = true;
|
|
|
|
result->preimage = *payment_preimage;
|
|
|
|
|
|
|
|
sendpay_resolve(tmpctx, ld, payment_hash, result);
|
|
|
|
|
|
|
|
tal_free(tmpctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sendpay_route_failure(struct lightningd *ld,
|
|
|
|
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
|
|
|
const tal_t *tmpctx = tal_tmpctx(ld);
|
|
|
|
struct sendpay_result *result;
|
|
|
|
|
|
|
|
result = tal(tmpctx, struct sendpay_result);
|
|
|
|
result->succeeded = false;
|
|
|
|
result->errorcode =
|
2018-02-03 12:46:12 +01:00
|
|
|
(!fail) ? PAY_UNPARSEABLE_ONION :
|
|
|
|
(!retry_plausible) ? PAY_DESTINATION_PERM_FAIL :
|
|
|
|
/*otherwise*/ PAY_TRY_OTHER_ROUTE ;
|
2018-02-14 01:09:23 +01:00
|
|
|
result->onionreply = onionreply;
|
|
|
|
result->routing_failure = fail;
|
|
|
|
result->details = details;
|
|
|
|
|
|
|
|
sendpay_resolve(tmpctx, ld, payment_hash, result);
|
2018-02-03 12:46:12 +01:00
|
|
|
|
2018-02-14 01:09:23 +01:00
|
|
|
tal_free(tmpctx);
|
2018-02-03 12:46:12 +01:00
|
|
|
}
|
|
|
|
|
2018-02-14 01:09:23 +01:00
|
|
|
/* Immediately fail during send_payment call. */
|
|
|
|
static void sendpay_fail_now(void (*cb)(const struct sendpay_result *, void*),
|
|
|
|
void *cbarg,
|
|
|
|
int errorcode,
|
|
|
|
char const *details)
|
2017-04-01 15:05:29 +02:00
|
|
|
{
|
2018-02-14 01:09:23 +01:00
|
|
|
const tal_t *tmpctx = tal_tmpctx(NULL);
|
|
|
|
struct sendpay_result *result;
|
2018-02-02 01:55:06 +01:00
|
|
|
|
2018-02-14 01:09:23 +01:00
|
|
|
result = tal(tmpctx, struct sendpay_result);
|
|
|
|
result->succeeded = false;
|
|
|
|
result->errorcode = errorcode;
|
|
|
|
result->details = details;
|
2018-02-02 01:55:06 +01:00
|
|
|
|
2018-02-14 01:09:23 +01:00
|
|
|
cb(result, cbarg);
|
|
|
|
|
|
|
|
tal_free(tmpctx);
|
|
|
|
}
|
|
|
|
/* Immediately fail during send_payment call. */
|
|
|
|
static void
|
|
|
|
sendpay_succeed_now(void (*cb)(const struct sendpay_result*, void*),
|
|
|
|
void *cbarg,
|
|
|
|
const struct preimage *payment_preimage)
|
|
|
|
{
|
|
|
|
const tal_t *tmpctx = tal_tmpctx(NULL);
|
|
|
|
struct sendpay_result *result;
|
|
|
|
|
|
|
|
result = tal(tmpctx, struct sendpay_result);
|
|
|
|
result->succeeded = true;
|
|
|
|
result->preimage = *payment_preimage;
|
|
|
|
|
|
|
|
cb(result, cbarg);
|
|
|
|
|
|
|
|
tal_free(tmpctx);
|
2017-04-01 15:05:29 +02: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)
|
|
|
|
{
|
2018-01-17 21:29:49 +01:00
|
|
|
wallet_payment_set_status(ld->wallet, &hout->payment_hash,
|
|
|
|
PAYMENT_COMPLETE, rval);
|
2018-02-14 01:09:23 +01:00
|
|
|
sendpay_success(ld, &hout->payment_hash, rval);
|
2017-04-01 15:05:29 +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,
|
|
|
|
onion_message, NULL,
|
|
|
|
&channel_update) &&
|
|
|
|
!fromwire_amount_below_minimum(ctx,
|
|
|
|
onion_message, NULL, &unused64,
|
|
|
|
&channel_update) &&
|
|
|
|
!fromwire_fee_insufficient(ctx,
|
|
|
|
onion_message, NULL, &unused64,
|
|
|
|
&channel_update) &&
|
|
|
|
!fromwire_incorrect_cltv_expiry(ctx,
|
|
|
|
onion_message, NULL, &unused32,
|
|
|
|
&channel_update) &&
|
|
|
|
!fromwire_expiry_too_soon(ctx,
|
|
|
|
onion_message, NULL,
|
|
|
|
&channel_update))
|
|
|
|
/* No channel update. */
|
|
|
|
channel_update = NULL;
|
|
|
|
|
|
|
|
return channel_update;
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
const struct short_channel_id *channel0)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
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];
|
|
|
|
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,
|
|
|
|
const struct onionreply *failure)
|
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;
|
|
|
|
static const struct short_channel_id dummy_channel = { 0, 0, 0 };
|
|
|
|
int origin_index;
|
|
|
|
bool retry_plausible;
|
|
|
|
bool report_to_gossipd;
|
|
|
|
|
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-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) {
|
2018-02-03 11:44:31 +01:00
|
|
|
erring_channel = &dummy_channel;
|
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;
|
|
|
|
} else
|
|
|
|
/* Report the *next* channel as failing. */
|
|
|
|
erring_channel = &route_channels[origin_index + 1];
|
|
|
|
|
2018-01-29 16:04:33 +01:00
|
|
|
erring_node = &route_nodes[origin_index];
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
*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)
|
|
|
|
{
|
|
|
|
const tal_t *tmpctx = tal_tmpctx(gossip);
|
|
|
|
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);
|
|
|
|
|
|
|
|
tal_free(tmpctx);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
const tal_t *tmpctx = tal_tmpctx(gossip);
|
|
|
|
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));
|
|
|
|
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-02-03 00:56:40 +01:00
|
|
|
tal_free(tmpctx);
|
2018-01-29 16:04:33 +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
|
|
|
{
|
2017-06-20 07:53:03 +02:00
|
|
|
struct onionreply *reply;
|
2017-11-28 06:03:04 +01:00
|
|
|
enum onion_type failcode;
|
2018-01-17 21:29:49 +01:00
|
|
|
struct secret *path_secrets;
|
2018-01-21 05:15:07 +01:00
|
|
|
struct wallet_payment *payment;
|
2018-01-17 21:29:49 +01:00
|
|
|
const tal_t *tmpctx = tal_tmpctx(ld);
|
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
|
|
|
|
|
|
|
/* This gives more details than a generic failure message */
|
|
|
|
if (localfail) {
|
|
|
|
fail = local_routing_failure(tmpctx, ld, hout, payment);
|
|
|
|
failcode = fail->failcode;
|
|
|
|
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. */
|
|
|
|
path_secrets = payment->path_secrets;
|
|
|
|
reply = unwrap_onionreply(tmpctx, path_secrets,
|
|
|
|
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,
|
|
|
|
tal_hex(ltmp, hout->failuremsg));
|
|
|
|
/* Cannot report failure. */
|
|
|
|
fail = NULL;
|
|
|
|
failcode = WIRE_PERMANENT_NODE_FAILURE;
|
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 {
|
|
|
|
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,
|
|
|
|
payment, reply);
|
2018-01-29 16:04:33 +01:00
|
|
|
}
|
2017-06-20 07:53:03 +02:00
|
|
|
}
|
|
|
|
|
2018-01-29 18:34:59 +01:00
|
|
|
/* This may invalidated the payment structure returned, so
|
|
|
|
* access to payment object should not be done after the
|
|
|
|
* below call. */
|
|
|
|
wallet_payment_set_status(ld->wallet, &hout->payment_hash,
|
|
|
|
PAYMENT_FAILED, NULL);
|
|
|
|
|
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-01-29 16:04:33 +01:00
|
|
|
/* FIXME(ZmnSCPxj): if retrying is plausible, and we are
|
|
|
|
* using pay command rather than sendpay, retry routing
|
|
|
|
* and payment again. */
|
|
|
|
(void) retry_plausible;
|
2017-06-20 07:53:03 +02:00
|
|
|
|
2018-02-14 01:09:23 +01:00
|
|
|
/* Report to client. */
|
|
|
|
sendpay_route_failure(ld, &hout->payment_hash,
|
|
|
|
retry_plausible, fail, hout->failuremsg,
|
|
|
|
failmsg);
|
2018-01-17 21:29:49 +01:00
|
|
|
tal_free(tmpctx);
|
2017-04-01 15:05:29 +02:00
|
|
|
}
|
|
|
|
|
2018-02-14 01:09:23 +01:00
|
|
|
/* Returns false if we called callback directly, true if
|
|
|
|
* callback is scheduled for later.
|
|
|
|
*
|
|
|
|
* This call expects that if it calls the callback, then
|
|
|
|
* the given context should have been freed. */
|
|
|
|
static bool send_payment(const tal_t *ctx,
|
|
|
|
struct lightningd* ld,
|
2017-10-26 04:56:19 +02:00
|
|
|
const struct sha256 *rhash,
|
2018-02-14 01:09:23 +01:00
|
|
|
const struct route_hop *route,
|
|
|
|
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;
|
2018-02-14 01:09:23 +01:00
|
|
|
const tal_t *tmpctx = tal_tmpctx(ctx);
|
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;
|
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");
|
|
|
|
sendpay_fail_now(cb, cbarg, PAY_IN_PROGRESS,
|
|
|
|
"Payment is still in progress");
|
2017-12-15 11:15:54 +01:00
|
|
|
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-01-17 21:29:49 +01:00
|
|
|
if (payment->msatoshi != hop_data[n_hops-1].amt_forward) {
|
2018-02-14 01:09:23 +01:00
|
|
|
char *msg = tal_fmt(tmpctx,
|
|
|
|
"Already succeeded "
|
|
|
|
"with amount %"PRIu64,
|
|
|
|
payment->msatoshi);
|
|
|
|
sendpay_fail_now(cb, cbarg,
|
|
|
|
PAY_RHASH_ALREADY_USED,
|
|
|
|
msg);
|
2017-12-15 11:15:54 +01:00
|
|
|
return false;
|
2017-04-01 15:05:29 +02:00
|
|
|
}
|
2018-01-17 21:29:49 +01:00
|
|
|
if (!structeq(&payment->destination, &ids[n_hops-1])) {
|
2018-02-14 01:09:23 +01:00
|
|
|
char *msg = tal_fmt(tmpctx,
|
|
|
|
"Already succeeded to %s",
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct pubkey,
|
|
|
|
&payment->destination));
|
|
|
|
sendpay_fail_now(cb, cbarg,
|
|
|
|
PAY_RHASH_ALREADY_USED,
|
|
|
|
msg);
|
2017-12-15 11:15:54 +01:00
|
|
|
return false;
|
2017-04-01 15:05:29 +02:00
|
|
|
}
|
2018-02-14 01:09:23 +01:00
|
|
|
sendpay_succeed_now(cb, cbarg,
|
|
|
|
payment->payment_preimage);
|
2017-12-15 11:15:54 +01:00
|
|
|
return false;
|
2017-04-01 15:05:29 +02:00
|
|
|
}
|
2018-02-14 01:09:23 +01:00
|
|
|
wallet_payment_delete(ld->wallet, rhash);
|
|
|
|
log_add(ld->log, "... retrying");
|
2017-04-01 15:05:29 +02:00
|
|
|
}
|
|
|
|
|
2018-02-14 01:09:23 +01:00
|
|
|
/* At this point we know there is no duplicate payment.
|
|
|
|
* Register it to the lightningd. Use the caller
|
|
|
|
* context, not our temporary context. */
|
|
|
|
new_sendpay_command(ctx, rhash, ld, cb, cbarg);
|
|
|
|
|
|
|
|
channel = active_channel_by_id(ld, &ids[0]);
|
2018-02-12 11:13:04 +01:00
|
|
|
if (!channel) {
|
2018-02-03 12:20:24 +01:00
|
|
|
/* Report routing failure to gossipd */
|
2018-02-14 01:09:23 +01:00
|
|
|
fail = immediate_routing_failure(tmpctx, ld,
|
2018-02-03 12:20:24 +01:00
|
|
|
WIRE_UNKNOWN_NEXT_PEER,
|
|
|
|
&route[0].channel_id);
|
2018-02-14 01:09:23 +01:00
|
|
|
report_routing_failure(ld->log, ld->gossip, fail);
|
2018-02-03 12:20:24 +01:00
|
|
|
|
|
|
|
/* Report routing failure to user */
|
2018-02-14 01:09:23 +01:00
|
|
|
sendpay_route_failure(ld, rhash, true, fail, NULL,
|
|
|
|
"No connection to first "
|
|
|
|
"peer found");
|
2017-12-15 11:15:54 +01:00
|
|
|
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-02-14 01:09:23 +01:00
|
|
|
log_info(ld->log, "Sending %u over %zu hops to deliver %u",
|
2018-01-17 21:29:49 +01:00
|
|
|
route[0].amount, n_hops, route[n_hops-1].amount);
|
|
|
|
|
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-02-14 01:09:23 +01:00
|
|
|
fail = immediate_routing_failure(tmpctx, ld,
|
2018-02-03 01:05:17 +01:00
|
|
|
failcode,
|
|
|
|
&route[0].channel_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-02-03 12:20:24 +01:00
|
|
|
/* Report routing failure to user */
|
2018-02-14 01:09:23 +01:00
|
|
|
sendpay_route_failure(ld, rhash, true, fail, NULL,
|
|
|
|
"First peer not ready");
|
2017-12-15 11:15:54 +01:00
|
|
|
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-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;
|
|
|
|
payment->msatoshi = route[n_hops-1].amount;
|
|
|
|
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-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
|
|
|
|
2017-12-15 11:29:41 +01:00
|
|
|
tal_free(tmpctx);
|
2017-12-15 11:15:54 +01:00
|
|
|
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,
|
|
|
|
const struct preimage *payment_preimage)
|
|
|
|
{
|
|
|
|
struct json_result *response;
|
|
|
|
|
|
|
|
response = new_json_result(cmd);
|
|
|
|
json_object_start(response, NULL);
|
|
|
|
json_add_hex(response, "preimage",
|
|
|
|
payment_preimage, sizeof(*payment_preimage));
|
|
|
|
json_object_end(response);
|
|
|
|
command_success(cmd, response);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void json_sendpay_on_resolve(const struct sendpay_result *r,
|
|
|
|
void *vcmd)
|
|
|
|
{
|
|
|
|
struct command *cmd = (struct command*) vcmd;
|
|
|
|
|
|
|
|
struct json_result *data;
|
|
|
|
const char *msg;
|
|
|
|
struct routing_failure *fail;
|
|
|
|
|
|
|
|
if (r->succeeded)
|
|
|
|
json_sendpay_success(cmd, &r->preimage);
|
|
|
|
else {
|
|
|
|
switch (r->errorcode) {
|
|
|
|
case PAY_IN_PROGRESS:
|
|
|
|
case PAY_RHASH_ALREADY_USED:
|
|
|
|
data = NULL;
|
|
|
|
msg = r->details;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PAY_UNPARSEABLE_ONION:
|
|
|
|
data = new_json_result(cmd);
|
|
|
|
json_object_start(data, NULL);
|
|
|
|
json_add_hex(data, "onionreply",
|
|
|
|
r->onionreply, tal_len(r->onionreply));
|
|
|
|
json_object_end(data);
|
|
|
|
|
|
|
|
msg = tal_fmt(cmd,
|
|
|
|
"failed: WIRE_PERMANENT_NODE_FAILURE "
|
|
|
|
"(%s)",
|
|
|
|
r->details);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PAY_DESTINATION_PERM_FAIL:
|
|
|
|
case PAY_TRY_OTHER_ROUTE:
|
|
|
|
fail = r->routing_failure;
|
|
|
|
data = new_json_result(cmd);
|
|
|
|
|
|
|
|
json_object_start(data, NULL);
|
|
|
|
json_add_num(data, "erring_index",
|
|
|
|
fail->erring_index);
|
|
|
|
json_add_num(data, "failcode",
|
|
|
|
(unsigned) fail->failcode);
|
|
|
|
json_add_hex(data, "erring_node",
|
|
|
|
&fail->erring_node,
|
|
|
|
sizeof(fail->erring_node));
|
|
|
|
json_add_short_channel_id(data, "erring_channel",
|
|
|
|
&fail->erring_channel);
|
|
|
|
if (fail->channel_update)
|
|
|
|
json_add_hex(data, "channel_update",
|
|
|
|
fail->channel_update,
|
|
|
|
tal_len(fail->channel_update));
|
|
|
|
json_object_end(data);
|
|
|
|
|
|
|
|
msg = tal_fmt(cmd,
|
|
|
|
"failed: %s (%s)",
|
|
|
|
onion_type_name(fail->failcode),
|
|
|
|
r->details);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
command_fail_detailed(cmd, r->errorcode, data, "%s", msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-26 04:56:19 +02:00
|
|
|
static void json_sendpay(struct command *cmd,
|
|
|
|
const char *buffer, const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
jsmntok_t *routetok, *rhashtok;
|
|
|
|
const jsmntok_t *t, *end;
|
|
|
|
size_t n_hops;
|
|
|
|
struct sha256 rhash;
|
|
|
|
struct route_hop *route;
|
|
|
|
|
2018-01-29 01:34:28 +01:00
|
|
|
if (!json_get_params(cmd, buffer, params,
|
2017-10-26 04:56:19 +02:00
|
|
|
"route", &routetok,
|
|
|
|
"rhash", &rhashtok,
|
|
|
|
NULL)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hex_decode(buffer + rhashtok->start,
|
|
|
|
rhashtok->end - rhashtok->start,
|
|
|
|
&rhash, sizeof(rhash))) {
|
|
|
|
command_fail(cmd, "'%.*s' is not a valid sha256 hash",
|
|
|
|
(int)(rhashtok->end - rhashtok->start),
|
|
|
|
buffer + rhashtok->start);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (routetok->type != JSMN_ARRAY) {
|
|
|
|
command_fail(cmd, "'%.*s' is not an array",
|
|
|
|
(int)(routetok->end - routetok->start),
|
|
|
|
buffer + routetok->start);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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)) {
|
|
|
|
const jsmntok_t *amttok, *idtok, *delaytok, *chantok;
|
|
|
|
|
|
|
|
if (t->type != JSMN_OBJECT) {
|
2018-01-26 02:03:48 +01:00
|
|
|
command_fail(cmd, "Route %zu '%.*s' is not an object",
|
2017-10-26 04:56:19 +02:00
|
|
|
n_hops,
|
|
|
|
(int)(t->end - t->start),
|
|
|
|
buffer + t->start);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
amttok = json_get_member(buffer, t, "msatoshi");
|
|
|
|
idtok = json_get_member(buffer, t, "id");
|
|
|
|
delaytok = json_get_member(buffer, t, "delay");
|
|
|
|
chantok = json_get_member(buffer, t, "channel");
|
|
|
|
if (!amttok || !idtok || !delaytok || !chantok) {
|
2018-01-26 02:03:48 +01:00
|
|
|
command_fail(cmd, "Route %zu needs msatoshi/id/channel/delay",
|
2017-10-26 04:56:19 +02:00
|
|
|
n_hops);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tal_resize(&route, n_hops + 1);
|
|
|
|
|
|
|
|
/* What that hop will forward */
|
|
|
|
if (!json_tok_number(buffer, amttok, &route[n_hops].amount)) {
|
2018-01-26 02:03:48 +01:00
|
|
|
command_fail(cmd, "Route %zu invalid msatoshi",
|
2017-10-26 04:56:19 +02:00
|
|
|
n_hops);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-16 20:44:32 +01:00
|
|
|
if (!json_tok_short_channel_id(buffer, chantok,
|
2017-10-26 04:56:19 +02:00
|
|
|
&route[n_hops].channel_id)) {
|
2018-01-26 02:03:48 +01:00
|
|
|
command_fail(cmd, "Route %zu invalid channel_id", n_hops);
|
2017-10-26 04:56:19 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!json_tok_pubkey(buffer, idtok, &route[n_hops].nodeid)) {
|
2018-01-26 02:03:48 +01:00
|
|
|
command_fail(cmd, "Route %zu invalid id", n_hops);
|
2017-10-26 04:56:19 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!json_tok_number(buffer, delaytok, &route[n_hops].delay)) {
|
2018-01-26 02:03:48 +01:00
|
|
|
command_fail(cmd, "Route %zu invalid delay", n_hops);
|
2017-10-26 04:56:19 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
n_hops++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n_hops == 0) {
|
|
|
|
command_fail(cmd, "Empty route");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-14 01:09:23 +01:00
|
|
|
if (send_payment(cmd, cmd->ld, &rhash, route,
|
|
|
|
&json_sendpay_on_resolve, cmd))
|
2017-12-15 11:15:54 +01:00
|
|
|
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",
|
|
|
|
json_sendpay,
|
2018-01-22 09:55:07 +01:00
|
|
|
"Send along {route} in return for preimage of {rhash}"
|
2017-04-01 15:05:29 +02:00
|
|
|
};
|
|
|
|
AUTODATA(json_command, &sendpay_command);
|
2017-10-26 05:07:19 +02:00
|
|
|
|
|
|
|
struct pay {
|
|
|
|
struct sha256 payment_hash;
|
|
|
|
struct command *cmd;
|
2018-02-07 14:17:58 +01:00
|
|
|
u64 msatoshi;
|
|
|
|
double maxfeepercent;
|
2017-10-26 05:07:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static void json_pay_getroute_reply(struct subd *gossip,
|
|
|
|
const u8 *reply, const int *fds,
|
|
|
|
struct pay *pay)
|
|
|
|
{
|
|
|
|
struct route_hop *route;
|
2018-02-07 14:17:58 +01:00
|
|
|
u64 msatoshi_sent;
|
|
|
|
u64 fee;
|
|
|
|
double feepercent;
|
|
|
|
struct json_result *data;
|
2017-10-26 05:07:19 +02:00
|
|
|
|
|
|
|
fromwire_gossip_getroute_reply(reply, reply, NULL, &route);
|
|
|
|
|
|
|
|
if (tal_count(route) == 0) {
|
2018-02-03 12:46:12 +01:00
|
|
|
command_fail_detailed(pay->cmd, PAY_ROUTE_NOT_FOUND, NULL,
|
|
|
|
"Could not find a route");
|
2017-10-26 05:07:19 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-07 14:17:58 +01:00
|
|
|
msatoshi_sent = route[0].amount;
|
|
|
|
fee = msatoshi_sent - pay->msatoshi;
|
|
|
|
/* FIXME: IEEE Double-precision floating point has only 53 bits
|
|
|
|
* of precision. Total satoshis that can ever be created is
|
|
|
|
* slightly less than 2100000000000000. Total msatoshis that
|
|
|
|
* can ever be created is 1000 times that or
|
|
|
|
* 2100000000000000000, requiring 60.865 bits of precision,
|
|
|
|
* and thus losing precision in the below. Currently, OK, as,
|
|
|
|
* payments are limited to 4294967295 msatoshi. */
|
|
|
|
feepercent = ((double) fee) * 100.0 / ((double) pay->msatoshi);
|
|
|
|
if (feepercent > pay->maxfeepercent) {
|
|
|
|
data = new_json_result(pay);
|
|
|
|
json_object_start(data, NULL);
|
|
|
|
json_add_u64(data, "fee", fee);
|
|
|
|
json_add_double(data, "feepercent", feepercent);
|
|
|
|
json_add_u64(data, "msatoshi", pay->msatoshi);
|
|
|
|
json_add_double(data, "maxfeepercent", pay->maxfeepercent);
|
|
|
|
json_object_end(data);
|
|
|
|
|
|
|
|
command_fail_detailed(pay->cmd, PAY_ROUTE_TOO_EXPENSIVE,
|
|
|
|
data,
|
|
|
|
"Fee %"PRIu64" is %f%% "
|
|
|
|
"of payment %"PRIu64"; "
|
|
|
|
"max fee requested is %f%%",
|
|
|
|
fee, feepercent,
|
|
|
|
pay->msatoshi,
|
|
|
|
pay->maxfeepercent);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-14 01:09:23 +01:00
|
|
|
send_payment(pay->cmd, pay->cmd->ld, &pay->payment_hash, route,
|
|
|
|
&json_sendpay_on_resolve, pay->cmd);
|
2017-10-26 05:07:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void json_pay(struct command *cmd,
|
|
|
|
const char *buffer, const jsmntok_t *params)
|
|
|
|
{
|
2018-02-07 14:17:58 +01:00
|
|
|
jsmntok_t *bolt11tok, *msatoshitok, *desctok, *riskfactortok, *maxfeetok;
|
2017-10-26 05:07:19 +02:00
|
|
|
double riskfactor = 1.0;
|
2018-02-07 14:17:58 +01:00
|
|
|
double maxfeepercent = 0.5;
|
2017-10-26 05:07:19 +02:00
|
|
|
u64 msatoshi;
|
|
|
|
struct pay *pay = tal(cmd, struct pay);
|
|
|
|
struct bolt11 *b11;
|
|
|
|
char *fail, *b11str, *desc;
|
|
|
|
u8 *req;
|
|
|
|
|
2018-01-29 01:34:28 +01:00
|
|
|
if (!json_get_params(cmd, buffer, params,
|
2017-10-26 05:07:19 +02:00
|
|
|
"bolt11", &bolt11tok,
|
|
|
|
"?msatoshi", &msatoshitok,
|
|
|
|
"?description", &desctok,
|
|
|
|
"?riskfactor", &riskfactortok,
|
2018-02-07 14:17:58 +01:00
|
|
|
"?maxfeepercent", &maxfeetok,
|
2017-10-26 05:07:19 +02:00
|
|
|
NULL)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
b11str = tal_strndup(cmd, buffer + bolt11tok->start,
|
|
|
|
bolt11tok->end - bolt11tok->start);
|
|
|
|
if (desctok)
|
|
|
|
desc = tal_strndup(cmd, buffer + desctok->start,
|
|
|
|
desctok->end - desctok->start);
|
|
|
|
else
|
|
|
|
desc = NULL;
|
|
|
|
|
|
|
|
b11 = bolt11_decode(pay, b11str, desc, &fail);
|
|
|
|
if (!b11) {
|
|
|
|
command_fail(cmd, "Invalid bolt11: %s", fail);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pay->cmd = cmd;
|
|
|
|
pay->payment_hash = b11->payment_hash;
|
|
|
|
|
|
|
|
if (b11->msatoshi) {
|
|
|
|
msatoshi = *b11->msatoshi;
|
2018-01-15 00:11:41 +01:00
|
|
|
if (msatoshitok) {
|
2017-10-26 05:07:19 +02:00
|
|
|
command_fail(cmd, "msatoshi parameter unnecessary");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!msatoshitok) {
|
|
|
|
command_fail(cmd, "msatoshi parameter required");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!json_tok_u64(buffer, msatoshitok, &msatoshi)) {
|
|
|
|
command_fail(cmd,
|
|
|
|
"msatoshi '%.*s' is not a valid number",
|
|
|
|
(int)(msatoshitok->end-msatoshitok->start),
|
|
|
|
buffer + msatoshitok->start);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-02-07 14:17:58 +01:00
|
|
|
pay->msatoshi = msatoshi;
|
2017-10-26 05:07:19 +02:00
|
|
|
|
|
|
|
if (riskfactortok
|
|
|
|
&& !json_tok_double(buffer, riskfactortok, &riskfactor)) {
|
|
|
|
command_fail(cmd, "'%.*s' is not a valid double",
|
|
|
|
(int)(riskfactortok->end - riskfactortok->start),
|
|
|
|
buffer + riskfactortok->start);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-07 14:17:58 +01:00
|
|
|
if (maxfeetok
|
|
|
|
&& !json_tok_double(buffer, maxfeetok, &maxfeepercent)) {
|
|
|
|
command_fail(cmd, "'%.*s' is not a valid double",
|
|
|
|
(int)(maxfeetok->end - maxfeetok->start),
|
|
|
|
buffer + maxfeetok->start);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Ensure it is in range 0.0 <= maxfeepercent <= 100.0 */
|
|
|
|
if (!(0.0 <= maxfeepercent)) {
|
|
|
|
command_fail(cmd, "%f maxfeepercent must be non-negative",
|
|
|
|
maxfeepercent);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!(maxfeepercent <= 100.0)) {
|
|
|
|
command_fail(cmd, "%f maxfeepercent must be <= 100.0",
|
|
|
|
maxfeepercent);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pay->maxfeepercent = maxfeepercent;
|
|
|
|
|
2017-10-26 05:07:19 +02:00
|
|
|
/* FIXME: use b11->routes */
|
|
|
|
req = towire_gossip_getroute_request(cmd, &cmd->ld->id,
|
|
|
|
&b11->receiver_id,
|
2017-10-26 05:08:19 +02:00
|
|
|
msatoshi, riskfactor*1000,
|
|
|
|
b11->min_final_cltv_expiry);
|
2017-10-26 05:07:19 +02:00
|
|
|
subd_req(pay, cmd->ld->gossip, req, -1, 0, json_pay_getroute_reply, pay);
|
2017-12-15 11:15:54 +01:00
|
|
|
command_still_pending(cmd);
|
2017-10-26 05:07:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct json_command pay_command = {
|
|
|
|
"pay",
|
|
|
|
json_pay,
|
2018-02-07 14:17:58 +01:00
|
|
|
"Send payment specified by {bolt11} with optional {msatoshi} "
|
|
|
|
"(if and only if {bolt11} does not have amount), "
|
|
|
|
"{description} (required if {bolt11} uses description hash), "
|
|
|
|
"{riskfactor} (default 1.0), and "
|
|
|
|
"{maxfeepercent} (default 0.5) the maximum acceptable fee as a percentage (e.g. 0.5 => 0.5%)"
|
2017-10-26 05:07:19 +02:00
|
|
|
};
|
|
|
|
AUTODATA(json_command, &pay_command);
|
2017-11-16 19:09:09 +01:00
|
|
|
|
|
|
|
static void json_listpayments(struct command *cmd, const char *buffer,
|
|
|
|
const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
const struct wallet_payment **payments;
|
|
|
|
struct json_result *response = new_json_result(cmd);
|
2018-01-16 20:44:32 +01:00
|
|
|
jsmntok_t *bolt11tok, *rhashtok;
|
|
|
|
struct sha256 *rhash = NULL;
|
2017-11-16 19:09:09 +01:00
|
|
|
|
2018-01-29 01:34:28 +01:00
|
|
|
if (!json_get_params(cmd, buffer, params,
|
2018-01-16 20:44:32 +01:00
|
|
|
"?bolt11", &bolt11tok,
|
|
|
|
"?payment_hash", &rhashtok,
|
|
|
|
NULL)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bolt11tok) {
|
|
|
|
struct bolt11 *b11;
|
|
|
|
char *b11str, *fail;
|
|
|
|
|
|
|
|
if (rhashtok) {
|
|
|
|
command_fail(cmd, "Can only specify one of"
|
|
|
|
" {bolt11} or {payment_hash}");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
b11str = tal_strndup(cmd, buffer + bolt11tok->start,
|
|
|
|
bolt11tok->end - bolt11tok->start);
|
|
|
|
|
|
|
|
b11 = bolt11_decode(cmd, b11str, NULL, &fail);
|
|
|
|
if (!b11) {
|
|
|
|
command_fail(cmd, "Invalid bolt11: %s", fail);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rhash = &b11->payment_hash;
|
|
|
|
} else if (rhashtok) {
|
|
|
|
rhash = tal(cmd, struct sha256);
|
|
|
|
if (!hex_decode(buffer + rhashtok->start,
|
|
|
|
rhashtok->end - rhashtok->start,
|
|
|
|
rhash, sizeof(*rhash))) {
|
|
|
|
command_fail(cmd, "'%.*s' is not a valid sha256 hash",
|
|
|
|
(int)(rhashtok->end - rhashtok->start),
|
|
|
|
buffer + rhashtok->start);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
payments = wallet_payment_list(cmd, cmd->ld->wallet, rhash);
|
2017-11-16 19:09:09 +01:00
|
|
|
|
2018-01-17 00:53:18 +01:00
|
|
|
json_object_start(response, NULL);
|
|
|
|
json_array_start(response, "payments");
|
2017-11-16 19:09:09 +01:00
|
|
|
for (int i=0; i<tal_count(payments); i++) {
|
|
|
|
const struct wallet_payment *t = payments[i];
|
|
|
|
json_object_start(response, NULL);
|
|
|
|
json_add_u64(response, "id", t->id);
|
|
|
|
json_add_hex(response, "payment_hash", &t->payment_hash, sizeof(t->payment_hash));
|
2018-01-17 21:22:22 +01:00
|
|
|
json_add_pubkey(response, "destination", &t->destination);
|
|
|
|
json_add_u64(response, "msatoshi", t->msatoshi);
|
2018-01-19 10:53:24 +01:00
|
|
|
if (deprecated_apis)
|
|
|
|
json_add_u64(response, "timestamp", t->timestamp);
|
|
|
|
json_add_u64(response, "created_at", t->timestamp);
|
2017-11-16 19:09:09 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2018-01-17 21:29:49 +01:00
|
|
|
if (t->payment_preimage)
|
|
|
|
json_add_hex(response, "payment_preimage",
|
|
|
|
t->payment_preimage,
|
|
|
|
sizeof(*t->payment_preimage));
|
2017-11-16 19:09:09 +01:00
|
|
|
|
|
|
|
json_object_end(response);
|
|
|
|
}
|
|
|
|
json_array_end(response);
|
2018-01-17 00:53:18 +01:00
|
|
|
json_object_end(response);
|
2017-11-16 19:09:09 +01:00
|
|
|
command_success(cmd, response);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|