2019-01-09 16:12:35 +01:00
|
|
|
#include <bitcoin/preimage.h>
|
2017-08-18 06:43:52 +02:00
|
|
|
#include <bitcoin/tx.h>
|
2017-06-20 07:50:03 +02:00
|
|
|
#include <ccan/build_assert/build_assert.h>
|
2018-07-26 23:26:37 +02:00
|
|
|
#include <ccan/cast/cast.h>
|
2017-08-18 06:43:53 +02:00
|
|
|
#include <ccan/crypto/ripemd160/ripemd160.h>
|
2017-06-20 07:50:03 +02:00
|
|
|
#include <ccan/mem/mem.h>
|
2017-06-20 08:17:03 +02:00
|
|
|
#include <ccan/tal/str/str.h>
|
2017-08-29 06:12:04 +02:00
|
|
|
#include <channeld/gen_channel_wire.h>
|
2018-12-08 01:39:28 +01:00
|
|
|
#include <common/json_command.h>
|
|
|
|
#include <common/jsonrpc_errors.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/overflows.h>
|
2018-12-08 01:39:28 +01:00
|
|
|
#include <common/param.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/sphinx.h>
|
2018-08-09 04:53:04 +02:00
|
|
|
#include <common/timeout.h>
|
2017-08-29 06:12:04 +02:00
|
|
|
#include <gossipd/gen_gossip_wire.h>
|
2017-08-28 18:04:01 +02:00
|
|
|
#include <lightningd/chaintopology.h>
|
2017-06-20 07:45:03 +02:00
|
|
|
#include <lightningd/htlc_end.h>
|
2018-07-20 03:14:02 +02:00
|
|
|
#include <lightningd/json.h>
|
2018-04-03 09:19:42 +02:00
|
|
|
#include <lightningd/jsonrpc.h>
|
2017-06-20 07:45:03 +02:00
|
|
|
#include <lightningd/lightningd.h>
|
2017-08-28 18:04:01 +02:00
|
|
|
#include <lightningd/log.h>
|
2019-08-15 02:31:23 +02:00
|
|
|
#include <lightningd/options.h>
|
2017-06-20 07:45:03 +02:00
|
|
|
#include <lightningd/pay.h>
|
|
|
|
#include <lightningd/peer_control.h>
|
|
|
|
#include <lightningd/peer_htlcs.h>
|
2019-01-03 17:27:46 +01:00
|
|
|
#include <lightningd/plugin_hook.h>
|
2017-06-20 07:45:03 +02:00
|
|
|
#include <lightningd/subd.h>
|
2017-09-20 06:45:41 +02:00
|
|
|
#include <onchaind/gen_onchain_wire.h>
|
2017-08-29 06:12:04 +02:00
|
|
|
#include <onchaind/onchain_wire.h>
|
2018-01-14 15:15:30 +01:00
|
|
|
#include <wallet/wallet.h>
|
2017-06-20 07:45:03 +02:00
|
|
|
#include <wire/gen_onion_wire.h>
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
static bool state_update_ok(struct channel *channel,
|
2017-06-20 08:22:03 +02:00
|
|
|
enum htlc_state oldstate, enum htlc_state newstate,
|
|
|
|
u64 htlc_id, const char *dir)
|
|
|
|
{
|
|
|
|
enum htlc_state expected = oldstate + 1;
|
|
|
|
|
|
|
|
/* We never get told about RCVD_REMOVE_HTLC, so skip over that
|
|
|
|
* (we initialize in SENT_ADD_HTLC / RCVD_ADD_COMMIT, so those
|
|
|
|
* work). */
|
|
|
|
if (expected == RCVD_REMOVE_HTLC)
|
|
|
|
expected = RCVD_REMOVE_COMMIT;
|
|
|
|
|
|
|
|
if (newstate != expected) {
|
2018-02-12 11:13:04 +01:00
|
|
|
channel_internal_error(channel,
|
|
|
|
"HTLC %s %"PRIu64" invalid update %s->%s",
|
|
|
|
dir, htlc_id,
|
|
|
|
htlc_state_name(oldstate),
|
|
|
|
htlc_state_name(newstate));
|
2017-06-20 08:22:03 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
log_debug(channel->log, "HTLC %s %"PRIu64" %s->%s",
|
2017-06-20 08:22:03 +02:00
|
|
|
dir, htlc_id,
|
|
|
|
htlc_state_name(oldstate), htlc_state_name(newstate));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
static bool htlc_in_update_state(struct channel *channel,
|
2017-06-20 08:22:03 +02:00
|
|
|
struct htlc_in *hin,
|
|
|
|
enum htlc_state newstate)
|
|
|
|
{
|
2018-02-12 11:13:04 +01:00
|
|
|
if (!state_update_ok(channel, hin->hstate, newstate, hin->key.id, "in"))
|
2017-06-20 08:22:03 +02:00
|
|
|
return false;
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
wallet_htlc_update(channel->peer->ld->wallet,
|
2018-10-09 10:56:52 +02:00
|
|
|
hin->dbid, newstate, hin->preimage,
|
|
|
|
hin->failcode, hin->failuremsg);
|
2017-09-18 20:18:29 +02:00
|
|
|
|
2017-06-20 08:22:03 +02:00
|
|
|
hin->hstate = newstate;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
static bool htlc_out_update_state(struct channel *channel,
|
2017-06-20 08:22:03 +02:00
|
|
|
struct htlc_out *hout,
|
|
|
|
enum htlc_state newstate)
|
|
|
|
{
|
2018-02-12 11:13:04 +01:00
|
|
|
if (!state_update_ok(channel, hout->hstate, newstate, hout->key.id,
|
|
|
|
"out"))
|
2017-06-20 08:22:03 +02:00
|
|
|
return false;
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
wallet_htlc_update(channel->peer->ld->wallet, hout->dbid, newstate,
|
2018-10-09 10:56:52 +02:00
|
|
|
hout->preimage, hout->failcode, hout->failuremsg);
|
2017-09-18 20:18:29 +02:00
|
|
|
|
2017-06-20 08:22:03 +02:00
|
|
|
hout->hstate = newstate;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fail_in_htlc(struct htlc_in *hin,
|
2017-11-28 06:03:04 +01:00
|
|
|
enum onion_type failcode,
|
2017-11-28 06:04:20 +01:00
|
|
|
const u8 *failuremsg,
|
|
|
|
const struct short_channel_id *out_channelid)
|
2017-06-20 07:45:03 +02:00
|
|
|
{
|
2018-07-26 23:26:37 +02:00
|
|
|
struct failed_htlc failed_htlc;
|
2017-06-20 08:22:03 +02:00
|
|
|
assert(!hin->preimage);
|
|
|
|
|
2017-11-28 06:03:04 +01:00
|
|
|
assert(failcode || failuremsg);
|
|
|
|
hin->failcode = failcode;
|
2017-06-20 08:22:03 +02:00
|
|
|
if (failuremsg)
|
2018-07-28 08:00:16 +02:00
|
|
|
hin->failuremsg = tal_dup_arr(hin, u8, failuremsg, tal_count(failuremsg), 0);
|
2017-06-20 08:22:03 +02:00
|
|
|
|
2017-11-28 06:04:20 +01:00
|
|
|
/* We need this set, since we send it to channeld. */
|
|
|
|
if (hin->failcode & UPDATE)
|
|
|
|
hin->failoutchannel = *out_channelid;
|
|
|
|
|
2017-06-20 08:22:03 +02:00
|
|
|
/* We update state now to signal it's in progress, for persistence. */
|
2018-02-12 11:13:04 +01:00
|
|
|
htlc_in_update_state(hin->key.channel, hin, SENT_REMOVE_HTLC);
|
2018-10-09 10:45:52 +02:00
|
|
|
htlc_in_check(hin, __func__);
|
2017-06-20 08:22:03 +02:00
|
|
|
|
|
|
|
/* Tell peer, if we can. */
|
2018-02-12 11:13:04 +01:00
|
|
|
if (!hin->key.channel->owner)
|
2017-06-20 07:45:03 +02:00
|
|
|
return;
|
|
|
|
|
2018-02-20 11:23:37 +01:00
|
|
|
/* onchaind doesn't care, it can't do anything but wait */
|
|
|
|
if (channel_on_chain(hin->key.channel))
|
|
|
|
return;
|
|
|
|
|
2018-07-26 23:26:37 +02:00
|
|
|
failed_htlc.id = hin->key.id;
|
|
|
|
failed_htlc.failcode = hin->failcode;
|
|
|
|
failed_htlc.failreason = cast_const(u8 *, hin->failuremsg);
|
|
|
|
if (failed_htlc.failcode & UPDATE)
|
|
|
|
failed_htlc.scid = &hin->failoutchannel;
|
|
|
|
else
|
|
|
|
failed_htlc.scid = NULL;
|
2018-02-12 11:13:04 +01:00
|
|
|
subd_send_msg(hin->key.channel->owner,
|
2019-08-28 06:05:43 +02:00
|
|
|
take(towire_channel_fail_htlc(NULL, &failed_htlc,
|
|
|
|
get_block_height(hin->key.channel->owner->ld->topology))));
|
2017-06-20 07:45:03 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 08:22:03 +02:00
|
|
|
/* This is used for cases where we can immediately fail the HTLC. */
|
2017-11-28 06:04:20 +01:00
|
|
|
static void local_fail_htlc(struct htlc_in *hin, enum onion_type failcode,
|
|
|
|
const struct short_channel_id *out_channel)
|
2017-06-20 07:45:03 +02:00
|
|
|
{
|
2018-02-12 11:13:04 +01:00
|
|
|
log_info(hin->key.channel->log, "failed htlc %"PRIu64" code 0x%04x (%s)",
|
2017-06-20 08:07:03 +02:00
|
|
|
hin->key.id, failcode, onion_type_name(failcode));
|
2017-06-20 07:45:03 +02:00
|
|
|
|
2017-11-28 06:04:20 +01:00
|
|
|
fail_in_htlc(hin, failcode, NULL, out_channel);
|
2017-06-20 07:45:03 +02:00
|
|
|
}
|
|
|
|
|
2019-04-11 02:08:55 +02:00
|
|
|
void fail_htlc(struct htlc_in *hin, enum onion_type failcode)
|
|
|
|
{
|
|
|
|
assert(failcode);
|
|
|
|
/* Final hop never sends an UPDATE. */
|
|
|
|
assert(!(failcode & UPDATE));
|
|
|
|
local_fail_htlc(hin, failcode, NULL);
|
|
|
|
}
|
|
|
|
|
2017-06-20 08:12:03 +02:00
|
|
|
/* localfail are for handing to the local payer if it's local. */
|
|
|
|
static void fail_out_htlc(struct htlc_out *hout, const char *localfail)
|
|
|
|
{
|
|
|
|
htlc_out_check(hout, __func__);
|
2017-11-28 06:03:04 +01:00
|
|
|
assert(hout->failcode || hout->failuremsg);
|
2019-04-15 16:27:22 +02:00
|
|
|
|
2018-10-09 22:21:31 +02:00
|
|
|
if (hout->am_origin) {
|
2018-10-09 10:47:52 +02:00
|
|
|
payment_failed(hout->key.channel->peer->ld, hout, localfail);
|
|
|
|
} else if (hout->in) {
|
2017-11-28 06:04:20 +01:00
|
|
|
fail_in_htlc(hout->in, hout->failcode, hout->failuremsg,
|
2018-02-12 11:13:04 +01:00
|
|
|
hout->key.channel->scid);
|
2017-06-20 08:12:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-20 07:45:03 +02:00
|
|
|
/* BOLT #4:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* * `amt_to_forward`: The amount, in millisatoshis, to forward to the next
|
|
|
|
* receiving peer specified within the routing information.
|
2017-06-20 07:45:03 +02:00
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* This value amount MUST include the origin node's computed _fee_ for the
|
|
|
|
* receiving peer. When processing an incoming Sphinx packet and the HTLC
|
|
|
|
* message that it is encapsulated within, if the following inequality
|
|
|
|
* doesn't hold, then the HTLC should be rejected as it would indicate that
|
|
|
|
* a prior hop has deviated from the specified parameters:
|
2017-06-20 07:45:03 +02:00
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* incoming_htlc_amt - fee >= amt_to_forward
|
2017-06-20 07:45:03 +02:00
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* Where `fee` is either calculated according to the receiving peer's
|
|
|
|
* advertised fee schema (as described in [BOLT
|
|
|
|
* #7](07-routing-gossip.md#htlc-fees)) or is 0, if the processing node is
|
|
|
|
* the final node.
|
2017-06-20 07:45:03 +02:00
|
|
|
*/
|
2017-06-20 07:53:03 +02:00
|
|
|
static bool check_amount(struct htlc_in *hin,
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat amt_to_forward,
|
|
|
|
struct amount_msat amt_in_htlc,
|
|
|
|
struct amount_msat fee)
|
2017-06-20 07:45:03 +02:00
|
|
|
{
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat fwd;
|
|
|
|
|
|
|
|
if (amount_msat_sub(&fwd, amt_in_htlc, fee)
|
|
|
|
&& amount_msat_greater_eq(fwd, amt_to_forward))
|
2017-06-20 07:45:03 +02:00
|
|
|
return true;
|
2019-02-21 04:45:55 +01:00
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
log_debug(hin->key.channel->log, "HTLC %"PRIu64" incorrect amount:"
|
2019-02-21 04:45:55 +01:00
|
|
|
" %s in, %s out, fee reqd %s",
|
|
|
|
hin->key.id,
|
|
|
|
type_to_string(tmpctx, struct amount_msat, &amt_in_htlc),
|
|
|
|
type_to_string(tmpctx, struct amount_msat, &amt_to_forward),
|
|
|
|
type_to_string(tmpctx, struct amount_msat, &fee));
|
2017-06-20 07:45:03 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #4:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* * `outgoing_cltv_value`: The CLTV value that the _outgoing_ HTLC carrying
|
2017-06-20 07:45:03 +02:00
|
|
|
* the packet should have.
|
|
|
|
*
|
2017-10-23 06:16:57 +02:00
|
|
|
* cltv_expiry - cltv_expiry_delta >= outgoing_cltv_value
|
2017-06-20 07:45:03 +02:00
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* Inclusion of this field allows a hop to both authenticate the
|
|
|
|
* information specified by the origin node, and the parameters of the
|
|
|
|
* HTLC forwarded, and ensure the origin node is using the current
|
|
|
|
* `cltv_expiry_delta` value. If there is no next hop,
|
|
|
|
* `cltv_expiry_delta` is 0. If the values don't correspond, then the
|
|
|
|
* HTLC should be failed and rejected, as this indicates that either a
|
|
|
|
* forwarding node has tampered with the intended HTLC values or that the
|
|
|
|
* origin node has an obsolete `cltv_expiry_delta` value. The hop MUST be
|
|
|
|
* consistent in responding to an unexpected `outgoing_cltv_value`,
|
|
|
|
* whether it is the final node or not, to avoid leaking its position in
|
|
|
|
* the route.
|
2017-06-20 07:45:03 +02:00
|
|
|
*/
|
2017-10-09 03:23:13 +02:00
|
|
|
static bool check_cltv(struct htlc_in *hin,
|
|
|
|
u32 cltv_expiry, u32 outgoing_cltv_value, u32 delta)
|
2017-06-20 07:45:03 +02:00
|
|
|
{
|
2019-01-04 01:08:35 +01:00
|
|
|
if (delta < cltv_expiry && cltv_expiry - delta >= outgoing_cltv_value)
|
2017-06-20 07:45:03 +02:00
|
|
|
return true;
|
2018-02-12 11:13:04 +01:00
|
|
|
log_debug(hin->key.channel->log, "HTLC %"PRIu64" incorrect CLTV:"
|
2017-06-20 07:45:03 +02:00
|
|
|
" %u in, %u out, delta reqd %u",
|
2017-10-09 03:23:13 +02:00
|
|
|
hin->key.id, cltv_expiry, outgoing_cltv_value, delta);
|
2017-06-20 07:45:03 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-11 02:08:55 +02:00
|
|
|
void fulfill_htlc(struct htlc_in *hin, const struct preimage *preimage)
|
2017-06-20 07:45:03 +02:00
|
|
|
{
|
|
|
|
u8 *msg;
|
2018-03-24 09:26:08 +01:00
|
|
|
struct channel *channel = hin->key.channel;
|
|
|
|
struct wallet *wallet = channel->peer->ld->wallet;
|
2017-06-20 07:45:03 +02:00
|
|
|
|
2019-04-11 02:08:55 +02:00
|
|
|
if (hin->hstate != RCVD_ADD_ACK_REVOCATION) {
|
|
|
|
log_debug(channel->log,
|
|
|
|
"HTLC fulfilled, but not ready any more (%s).",
|
|
|
|
htlc_state_name(hin->hstate));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-20 07:59:03 +02:00
|
|
|
hin->preimage = tal_dup(hin, struct preimage, preimage);
|
2017-06-20 07:45:03 +02:00
|
|
|
|
2017-06-20 08:22:03 +02:00
|
|
|
/* We update state now to signal it's in progress, for persistence. */
|
2018-03-24 09:26:08 +01:00
|
|
|
htlc_in_update_state(channel, hin, SENT_REMOVE_HTLC);
|
2018-10-09 10:45:52 +02:00
|
|
|
|
|
|
|
htlc_in_check(hin, __func__);
|
|
|
|
|
2018-03-24 09:26:08 +01:00
|
|
|
/* Update channel stats */
|
|
|
|
wallet_channel_stats_incr_in_fulfilled(wallet,
|
|
|
|
channel->dbid,
|
2019-02-21 04:45:55 +01:00
|
|
|
hin->msat);
|
2017-06-20 08:22:03 +02:00
|
|
|
|
2017-09-20 06:45:41 +02:00
|
|
|
/* No owner? We'll either send to channeld in peer_htlcs, or
|
|
|
|
* onchaind in onchaind_tell_fulfill. */
|
2018-03-24 09:26:08 +01:00
|
|
|
if (!channel->owner) {
|
|
|
|
log_debug(channel->log, "HTLC fulfilled, but no owner.");
|
2017-09-20 06:45:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-24 09:26:08 +01:00
|
|
|
if (channel_on_chain(channel)) {
|
2017-09-20 06:45:41 +02:00
|
|
|
msg = towire_onchain_known_preimage(hin, preimage);
|
|
|
|
} else {
|
2018-07-26 23:26:37 +02:00
|
|
|
struct fulfilled_htlc fulfilled_htlc;
|
|
|
|
fulfilled_htlc.id = hin->key.id;
|
|
|
|
fulfilled_htlc.payment_preimage = *preimage;
|
|
|
|
msg = towire_channel_fulfill_htlc(hin, &fulfilled_htlc);
|
2017-09-20 06:45:41 +02:00
|
|
|
}
|
2018-03-24 09:26:08 +01:00
|
|
|
subd_send_msg(channel->owner, take(msg));
|
2017-06-20 07:45:03 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 07:53:03 +02:00
|
|
|
static void handle_localpay(struct htlc_in *hin,
|
2017-06-20 07:45:03 +02:00
|
|
|
u32 cltv_expiry,
|
|
|
|
const struct sha256 *payment_hash,
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat amt_to_forward,
|
2017-06-20 07:45:03 +02:00
|
|
|
u32 outgoing_cltv_value)
|
|
|
|
{
|
|
|
|
enum onion_type failcode;
|
2018-02-12 11:13:04 +01:00
|
|
|
struct lightningd *ld = hin->key.channel->peer->ld;
|
2017-06-20 07:45:03 +02:00
|
|
|
|
|
|
|
/* BOLT #4:
|
|
|
|
*
|
|
|
|
* 1. type: 19 (`final_incorrect_htlc_amount`)
|
|
|
|
* 2. data:
|
2019-07-16 01:20:37 +02:00
|
|
|
* * [`u64`:`incoming_htlc_amt`]
|
2018-06-17 12:13:44 +02:00
|
|
|
*
|
|
|
|
* The amount in the HTLC doesn't match the value in the onion.
|
2017-06-20 07:45:03 +02:00
|
|
|
*/
|
2019-02-21 04:45:55 +01:00
|
|
|
if (!check_amount(hin, amt_to_forward, hin->msat, AMOUNT_MSAT(0))) {
|
2017-06-20 07:45:03 +02:00
|
|
|
failcode = WIRE_FINAL_INCORRECT_HTLC_AMOUNT;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #4:
|
|
|
|
*
|
|
|
|
* 1. type: 18 (`final_incorrect_cltv_expiry`)
|
|
|
|
* 2. data:
|
2019-07-16 01:20:37 +02:00
|
|
|
* * [`u32`:`cltv_expiry`]
|
2018-06-17 12:13:44 +02:00
|
|
|
*
|
|
|
|
* The CLTV expiry in the HTLC doesn't match the value in the onion.
|
2017-06-20 07:45:03 +02:00
|
|
|
*/
|
2017-10-09 03:23:13 +02:00
|
|
|
if (!check_cltv(hin, cltv_expiry, outgoing_cltv_value, 0)) {
|
2017-06-20 07:45:03 +02:00
|
|
|
failcode = WIRE_FINAL_INCORRECT_CLTV_EXPIRY;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #4:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* - if the `cltv_expiry` value is unreasonably near the present:
|
|
|
|
* - MUST fail the HTLC.
|
2019-08-28 03:35:46 +02:00
|
|
|
* - MUST return an `incorrect_or_unknown_payment_details` error.
|
2017-06-20 07:45:03 +02:00
|
|
|
*/
|
2017-10-23 06:16:57 +02:00
|
|
|
if (get_block_height(ld->topology) + ld->config.cltv_final
|
2017-11-08 14:34:11 +01:00
|
|
|
> cltv_expiry) {
|
2018-02-12 11:13:04 +01:00
|
|
|
log_debug(hin->key.channel->log,
|
2017-11-08 14:34:11 +01:00
|
|
|
"Expiry cltv too soon %u < %u + %u",
|
2017-06-20 07:45:03 +02:00
|
|
|
cltv_expiry,
|
2017-06-20 07:53:03 +02:00
|
|
|
get_block_height(ld->topology),
|
2017-10-23 06:16:57 +02:00
|
|
|
ld->config.cltv_final);
|
2019-08-28 06:06:17 +02:00
|
|
|
failcode = WIRE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS;
|
2017-06-20 07:45:03 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2019-04-11 02:08:55 +02:00
|
|
|
invoice_try_pay(ld, hin, payment_hash, amt_to_forward);
|
2017-06-20 07:45:03 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
fail:
|
2019-04-11 02:08:55 +02:00
|
|
|
fail_htlc(hin, failcode);
|
2017-06-20 07:45:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A catchall in case outgoing peer disconnects before getting fwd.
|
|
|
|
*
|
|
|
|
* We could queue this and wait for it to come back, but this is simple.
|
|
|
|
*/
|
2018-02-14 02:53:13 +01:00
|
|
|
static void destroy_hout_subd_died(struct htlc_out *hout)
|
2017-06-20 07:45:03 +02:00
|
|
|
{
|
2018-02-12 11:13:04 +01:00
|
|
|
log_debug(hout->key.channel->log,
|
2017-06-20 07:45:03 +02:00
|
|
|
"Failing HTLC %"PRIu64" due to peer death",
|
2017-06-20 08:12:03 +02:00
|
|
|
hout->key.id);
|
2017-06-20 07:45:03 +02:00
|
|
|
|
2017-11-28 06:04:20 +01:00
|
|
|
hout->failcode = WIRE_TEMPORARY_CHANNEL_FAILURE;
|
2018-10-09 10:45:52 +02:00
|
|
|
|
|
|
|
/* Assign a temporary state (we're about to free it!) so checks
|
|
|
|
* are happy that it has a failure code */
|
|
|
|
assert(hout->hstate == SENT_ADD_HTLC);
|
|
|
|
hout->hstate = RCVD_REMOVE_HTLC;
|
|
|
|
|
2017-06-20 08:12:03 +02:00
|
|
|
fail_out_htlc(hout, "Outgoing subdaemon died");
|
2017-06-20 07:45:03 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 07:50:03 +02:00
|
|
|
/* This is where channeld gives us the HTLC id, and also reports if it
|
|
|
|
* failed immediately. */
|
2018-02-21 16:06:07 +01:00
|
|
|
static void rcvd_htlc_reply(struct subd *subd, const u8 *msg, const int *fds UNUSED,
|
2017-06-20 07:53:03 +02:00
|
|
|
struct htlc_out *hout)
|
2017-06-20 07:45:03 +02:00
|
|
|
{
|
|
|
|
u16 failure_code;
|
|
|
|
u8 *failurestr;
|
2018-02-12 11:13:04 +01:00
|
|
|
struct lightningd *ld = subd->ld;
|
2017-06-20 07:45:03 +02:00
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_channel_offer_htlc_reply(msg, msg,
|
2017-06-20 07:53:03 +02:00
|
|
|
&hout->key.id,
|
2017-06-20 07:45:03 +02:00
|
|
|
&failure_code,
|
|
|
|
&failurestr)) {
|
2018-02-12 11:13:04 +01:00
|
|
|
channel_internal_error(subd->channel,
|
2018-02-12 11:13:04 +01:00
|
|
|
"Bad channel_offer_htlc_reply");
|
2017-06-20 07:53:03 +02:00
|
|
|
tal_free(hout);
|
2017-10-12 08:35:04 +02:00
|
|
|
return;
|
2017-06-20 07:45:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (failure_code) {
|
2018-01-29 17:22:23 +01:00
|
|
|
hout->failcode = (enum onion_type) failure_code;
|
2018-10-09 22:21:31 +02:00
|
|
|
if (hout->am_origin) {
|
2017-08-30 03:21:48 +02:00
|
|
|
char *localfail = tal_fmt(msg, "%s: %.*s",
|
|
|
|
onion_type_name(failure_code),
|
2018-07-28 08:00:16 +02:00
|
|
|
(int)tal_count(failurestr),
|
2017-08-30 03:21:48 +02:00
|
|
|
(const char *)failurestr);
|
2018-02-12 11:13:04 +01:00
|
|
|
payment_failed(ld, hout, localfail);
|
2019-04-15 16:27:22 +02:00
|
|
|
|
|
|
|
} else if (hout->in) {
|
2017-11-28 06:04:20 +01:00
|
|
|
local_fail_htlc(hout->in, failure_code,
|
2019-04-15 16:27:22 +02:00
|
|
|
hout->key.channel->scid);
|
|
|
|
|
|
|
|
/* here we haven't called connect_htlc_out(),
|
|
|
|
* so set htlc field with NULL */
|
|
|
|
wallet_forwarded_payment_add(ld->wallet,
|
|
|
|
hout->in, NULL,
|
|
|
|
FORWARD_LOCAL_FAILED,
|
|
|
|
failure_code);
|
|
|
|
}
|
|
|
|
|
2018-01-29 19:54:08 +01:00
|
|
|
/* Prevent hout from being failed twice. */
|
2018-02-14 02:53:13 +01:00
|
|
|
tal_del_destructor(hout, destroy_hout_subd_died);
|
2017-12-15 11:29:45 +01:00
|
|
|
tal_free(hout);
|
2017-10-12 08:35:04 +02:00
|
|
|
return;
|
2017-06-20 07:45:03 +02:00
|
|
|
}
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
if (find_htlc_out(&subd->ld->htlcs_out, hout->key.channel, hout->key.id)
|
2017-12-21 04:50:47 +01:00
|
|
|
|| hout->key.id == HTLC_INVALID_ID) {
|
2018-02-12 11:13:04 +01:00
|
|
|
channel_internal_error(subd->channel,
|
2017-06-20 08:17:03 +02:00
|
|
|
"Bad offer_htlc_reply HTLC id %"PRIu64
|
|
|
|
" is a duplicate",
|
|
|
|
hout->key.id);
|
2017-06-20 08:04:03 +02:00
|
|
|
tal_free(hout);
|
2017-10-12 08:35:04 +02:00
|
|
|
return;
|
2017-06-20 08:04:03 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 07:50:03 +02:00
|
|
|
/* Add it to lookup table now we know id. */
|
2017-06-20 07:53:03 +02:00
|
|
|
connect_htlc_out(&subd->ld->htlcs_out, hout);
|
2017-06-20 07:45:03 +02:00
|
|
|
|
2017-06-20 07:50:03 +02:00
|
|
|
/* When channeld includes it in commitment, we'll make it persistent. */
|
2017-06-20 07:45:03 +02:00
|
|
|
}
|
|
|
|
|
2018-08-09 04:53:04 +02:00
|
|
|
static void htlc_offer_timeout(struct channel *channel)
|
|
|
|
{
|
2018-08-23 13:30:13 +02:00
|
|
|
/* Unset this in case we reconnect and start again. */
|
|
|
|
channel->htlc_timeout = NULL;
|
|
|
|
|
2018-08-09 04:53:04 +02:00
|
|
|
/* If owner died, we should already be taken care of. */
|
|
|
|
if (!channel->owner || channel->state != CHANNELD_NORMAL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
log_unusual(channel->owner->log,
|
2019-08-07 08:43:16 +02:00
|
|
|
"Adding HTLC too slow: killing connection");
|
2018-08-09 04:53:04 +02:00
|
|
|
tal_free(channel->owner);
|
|
|
|
channel_set_billboard(channel, false,
|
2019-08-07 08:43:16 +02:00
|
|
|
"Adding HTLC timed out: killed connection");
|
2018-08-09 04:53:04 +02:00
|
|
|
}
|
|
|
|
|
2019-02-21 04:45:55 +01:00
|
|
|
enum onion_type send_htlc_out(struct channel *out,
|
|
|
|
struct amount_msat amount, u32 cltv,
|
2017-06-20 08:14:03 +02:00
|
|
|
const struct sha256 *payment_hash,
|
|
|
|
const u8 *onion_routing_packet,
|
|
|
|
struct htlc_in *in,
|
|
|
|
struct htlc_out **houtp)
|
2017-06-20 08:12:03 +02:00
|
|
|
{
|
|
|
|
struct htlc_out *hout;
|
|
|
|
u8 *msg;
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
if (!channel_can_add_htlc(out)) {
|
2017-06-20 08:14:03 +02:00
|
|
|
log_info(out->log, "Attempt to send HTLC but not ready (%s)",
|
2018-02-12 11:13:04 +01:00
|
|
|
channel_state_name(out));
|
2017-06-20 08:14:03 +02:00
|
|
|
return WIRE_UNKNOWN_NEXT_PEER;
|
|
|
|
}
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
if (!out->owner) {
|
2017-06-20 08:14:03 +02:00
|
|
|
log_info(out->log, "Attempt to send HTLC but unowned (%s)",
|
2018-02-12 11:13:04 +01:00
|
|
|
channel_state_name(out));
|
2017-06-20 08:14:03 +02:00
|
|
|
return WIRE_TEMPORARY_CHANNEL_FAILURE;
|
|
|
|
}
|
|
|
|
|
2019-08-10 12:47:49 +02:00
|
|
|
if (!topology_synced(out->peer->ld->topology)) {
|
|
|
|
log_info(out->log, "Attempt to send HTLC but still syncing"
|
|
|
|
" with bitcoin network");
|
|
|
|
return WIRE_TEMPORARY_CHANNEL_FAILURE;
|
|
|
|
}
|
|
|
|
|
2017-06-20 08:12:03 +02:00
|
|
|
/* Make peer's daemon own it, catch if it dies. */
|
2018-02-12 11:13:04 +01:00
|
|
|
hout = new_htlc_out(out->owner, out, amount, cltv,
|
2018-10-09 10:47:52 +02:00
|
|
|
payment_hash, onion_routing_packet, in == NULL, in);
|
2018-02-14 02:53:13 +01:00
|
|
|
tal_add_destructor(hout, destroy_hout_subd_died);
|
2017-06-20 08:12:03 +02:00
|
|
|
|
2018-08-10 03:31:40 +02:00
|
|
|
/* Give channel 30 seconds to commit (first) htlc. */
|
2018-08-09 04:53:04 +02:00
|
|
|
if (!out->htlc_timeout)
|
2019-06-14 05:49:23 +02:00
|
|
|
out->htlc_timeout = new_reltimer(out->peer->ld->timers,
|
2018-08-09 04:53:04 +02:00
|
|
|
out, time_from_sec(30),
|
|
|
|
htlc_offer_timeout,
|
|
|
|
out);
|
2017-06-20 08:12:03 +02:00
|
|
|
msg = towire_channel_offer_htlc(out, amount, cltv, payment_hash,
|
|
|
|
onion_routing_packet);
|
2018-02-12 11:13:04 +01:00
|
|
|
subd_req(out->peer->ld, out->owner, take(msg), -1, 0, rcvd_htlc_reply, hout);
|
2017-06-20 08:14:03 +02:00
|
|
|
|
|
|
|
if (houtp)
|
|
|
|
*houtp = hout;
|
|
|
|
return 0;
|
2017-06-20 08:12:03 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 07:53:03 +02:00
|
|
|
static void forward_htlc(struct htlc_in *hin,
|
2017-06-20 07:45:03 +02:00
|
|
|
u32 cltv_expiry,
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat amt_to_forward,
|
2017-06-20 07:45:03 +02:00
|
|
|
u32 outgoing_cltv_value,
|
2019-04-08 11:58:32 +02:00
|
|
|
const struct node_id *next_hop,
|
2017-06-20 07:45:03 +02:00
|
|
|
const u8 next_onion[TOTAL_PACKET_SIZE])
|
|
|
|
{
|
|
|
|
enum onion_type failcode;
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat fee;
|
2018-02-12 11:13:04 +01:00
|
|
|
struct lightningd *ld = hin->key.channel->peer->ld;
|
2018-02-19 02:06:02 +01:00
|
|
|
struct channel *next = active_channel_by_id(ld, next_hop, NULL);
|
2019-04-15 16:27:22 +02:00
|
|
|
struct htlc_out *hout = NULL;
|
2017-06-20 07:45:03 +02:00
|
|
|
|
2017-11-28 06:04:20 +01:00
|
|
|
/* Unknown peer, or peer not ready. */
|
2018-02-12 11:13:04 +01:00
|
|
|
if (!next || !next->scid) {
|
2017-11-28 06:04:20 +01:00
|
|
|
local_fail_htlc(hin, WIRE_UNKNOWN_NEXT_PEER, NULL);
|
2019-04-15 16:27:22 +02:00
|
|
|
wallet_forwarded_payment_add(hin->key.channel->peer->ld->wallet,
|
|
|
|
hin, NULL,
|
|
|
|
FORWARD_LOCAL_FAILED,
|
|
|
|
hin->failcode);
|
2017-11-28 06:04:20 +01:00
|
|
|
return;
|
2017-06-20 07:45:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #7:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* The origin node:
|
|
|
|
* - SHOULD accept HTLCs that pay a fee equal to or greater than:
|
2018-10-26 08:01:30 +02:00
|
|
|
* - fee_base_msat + ( amount_to_forward * fee_proportional_millionths / 1000000 )
|
2017-06-20 07:45:03 +02:00
|
|
|
*/
|
2019-02-21 04:45:55 +01:00
|
|
|
if (!amount_msat_fee(&fee, amt_to_forward,
|
2019-03-09 21:20:13 +01:00
|
|
|
next->feerate_base,
|
|
|
|
next->feerate_ppm)) {
|
2019-02-21 04:45:55 +01:00
|
|
|
log_broken(ld->log, "Fee overflow forwarding %s!",
|
|
|
|
type_to_string(tmpctx, struct amount_msat,
|
|
|
|
&amt_to_forward));
|
2017-06-20 07:45:03 +02:00
|
|
|
failcode = WIRE_FEE_INSUFFICIENT;
|
|
|
|
goto fail;
|
|
|
|
}
|
2019-02-21 04:45:55 +01:00
|
|
|
if (!check_amount(hin, amt_to_forward, hin->msat, fee)) {
|
2017-06-20 07:45:03 +02:00
|
|
|
failcode = WIRE_FEE_INSUFFICIENT;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2017-10-09 03:23:13 +02:00
|
|
|
if (!check_cltv(hin, cltv_expiry, outgoing_cltv_value,
|
2017-10-23 06:16:57 +02:00
|
|
|
ld->config.cltv_expiry_delta)) {
|
2017-06-20 07:45:03 +02:00
|
|
|
failcode = WIRE_INCORRECT_CLTV_EXPIRY;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2017-11-02 00:50:47 +01:00
|
|
|
/* BOLT #2:
|
2017-06-20 07:45:03 +02:00
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* An offering node:
|
|
|
|
* - MUST estimate a timeout deadline for each HTLC it offers.
|
|
|
|
* - MUST NOT offer an HTLC with a timeout deadline before its
|
|
|
|
* `cltv_expiry`.
|
2017-06-20 07:45:03 +02:00
|
|
|
*/
|
2017-11-02 00:50:47 +01:00
|
|
|
/* In our case, G = 1, so we need to expire it one after it's expiration.
|
|
|
|
* But never offer an expired HTLC; that's dumb. */
|
2018-02-12 11:13:04 +01:00
|
|
|
if (get_block_height(ld->topology) >= outgoing_cltv_value) {
|
|
|
|
log_debug(hin->key.channel->log,
|
2017-11-02 00:50:47 +01:00
|
|
|
"Expiry cltv %u too close to current %u",
|
2017-06-20 07:45:03 +02:00
|
|
|
outgoing_cltv_value,
|
2018-02-12 11:13:04 +01:00
|
|
|
get_block_height(ld->topology));
|
2017-06-20 07:45:03 +02:00
|
|
|
failcode = WIRE_EXPIRY_TOO_SOON;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2017-11-01 03:21:00 +01:00
|
|
|
/* BOLT #4:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* - if the `cltv_expiry` is unreasonably far in the future:
|
|
|
|
* - return an `expiry_too_far` error.
|
2017-11-01 03:21:00 +01:00
|
|
|
*/
|
2018-02-12 11:13:04 +01:00
|
|
|
if (get_block_height(ld->topology)
|
2018-05-17 06:46:22 +02:00
|
|
|
+ ld->config.locktime_max < outgoing_cltv_value) {
|
2018-02-12 11:13:04 +01:00
|
|
|
log_debug(hin->key.channel->log,
|
2017-10-23 06:16:57 +02:00
|
|
|
"Expiry cltv %u too far from current %u + max %u",
|
|
|
|
outgoing_cltv_value,
|
2018-02-12 11:13:04 +01:00
|
|
|
get_block_height(ld->topology),
|
2018-05-17 06:46:22 +02:00
|
|
|
ld->config.locktime_max);
|
2017-11-01 03:21:00 +01:00
|
|
|
failcode = WIRE_EXPIRY_TOO_FAR;
|
2017-10-23 06:16:57 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2019-04-15 16:27:22 +02:00
|
|
|
hout = tal(tmpctx, struct htlc_out);
|
2017-06-20 08:14:03 +02:00
|
|
|
failcode = send_htlc_out(next, amt_to_forward,
|
|
|
|
outgoing_cltv_value, &hin->payment_hash,
|
2019-04-15 16:27:22 +02:00
|
|
|
next_onion, hin, &hout);
|
2017-06-20 08:14:03 +02:00
|
|
|
if (!failcode)
|
|
|
|
return;
|
2017-06-20 07:45:03 +02:00
|
|
|
|
2019-04-15 16:27:22 +02:00
|
|
|
/* In fact, we didn't get the new htlc_out in these 2 cases */
|
|
|
|
if (failcode == WIRE_UNKNOWN_NEXT_PEER ||
|
|
|
|
failcode == WIRE_TEMPORARY_CHANNEL_FAILURE) {
|
|
|
|
tal_free(hout);
|
|
|
|
hout = NULL;
|
|
|
|
}
|
|
|
|
|
2017-06-20 07:45:03 +02:00
|
|
|
fail:
|
2018-02-12 11:13:04 +01:00
|
|
|
local_fail_htlc(hin, failcode, next->scid);
|
2019-04-15 16:27:22 +02:00
|
|
|
wallet_forwarded_payment_add(ld->wallet,
|
|
|
|
hin, hout,
|
|
|
|
FORWARD_LOCAL_FAILED,
|
|
|
|
hin->failcode);
|
2017-06-20 07:45:03 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 07:53:03 +02:00
|
|
|
/* Temporary information, while we resolve the next hop */
|
|
|
|
struct gossip_resolve {
|
|
|
|
struct short_channel_id next_channel;
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat amt_to_forward;
|
2017-06-20 07:53:03 +02:00
|
|
|
u32 outgoing_cltv_value;
|
|
|
|
u8 *next_onion;
|
|
|
|
struct htlc_in *hin;
|
|
|
|
};
|
|
|
|
|
2017-06-20 07:45:03 +02:00
|
|
|
/* We received a resolver reply, which gives us the node_ids of the
|
|
|
|
* channel we want to forward over */
|
2017-10-12 08:35:04 +02:00
|
|
|
static void channel_resolve_reply(struct subd *gossip, const u8 *msg,
|
2018-02-21 16:06:07 +01:00
|
|
|
const int *fds UNUSED, struct gossip_resolve *gr)
|
2017-06-20 07:45:03 +02:00
|
|
|
{
|
2019-04-08 11:58:32 +02:00
|
|
|
struct node_id *peer_id;
|
2017-06-20 07:45:03 +02:00
|
|
|
|
2018-10-15 06:57:22 +02:00
|
|
|
if (!fromwire_gossip_get_channel_peer_reply(msg, msg, &peer_id)) {
|
2017-06-20 07:45:03 +02:00
|
|
|
log_broken(gossip->log,
|
2018-10-15 06:57:22 +02:00
|
|
|
"bad fromwire_gossip_get_channel_peer_reply %s",
|
2017-06-20 07:45:03 +02:00
|
|
|
tal_hex(msg, msg));
|
2017-10-12 08:35:04 +02:00
|
|
|
return;
|
2017-06-20 07:45:03 +02:00
|
|
|
}
|
|
|
|
|
2018-10-15 06:57:22 +02:00
|
|
|
if (!peer_id) {
|
2017-11-28 06:04:20 +01:00
|
|
|
local_fail_htlc(gr->hin, WIRE_UNKNOWN_NEXT_PEER, NULL);
|
2019-04-15 16:27:22 +02:00
|
|
|
wallet_forwarded_payment_add(gr->hin->key.channel->peer->ld->wallet,
|
|
|
|
gr->hin, NULL,
|
|
|
|
FORWARD_LOCAL_FAILED,
|
|
|
|
gr->hin->failcode);
|
2019-01-15 04:53:27 +01:00
|
|
|
tal_free(gr);
|
2017-10-12 08:35:04 +02:00
|
|
|
return;
|
2017-06-20 07:45:03 +02:00
|
|
|
}
|
|
|
|
|
2018-02-21 16:55:16 +01:00
|
|
|
forward_htlc(gr->hin, gr->hin->cltv_expiry,
|
2017-06-20 07:53:03 +02:00
|
|
|
gr->amt_to_forward, gr->outgoing_cltv_value, peer_id,
|
|
|
|
gr->next_onion);
|
|
|
|
tal_free(gr);
|
2017-06-20 07:45:03 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 17:27:46 +01:00
|
|
|
/**
|
|
|
|
* Data passed to the plugin, and as the context for the hook callback
|
|
|
|
*/
|
|
|
|
struct htlc_accepted_hook_payload {
|
|
|
|
struct route_step *route_step;
|
|
|
|
struct htlc_in *hin;
|
|
|
|
struct channel *channel;
|
|
|
|
struct lightningd *ld;
|
2019-01-09 16:07:49 +01:00
|
|
|
u8 *next_onion;
|
2019-01-03 17:27:46 +01:00
|
|
|
};
|
|
|
|
|
2019-01-09 16:12:35 +01:00
|
|
|
/* The possible return value types that a plugin may return for the
|
|
|
|
* `htlc_accepted` hook. */
|
|
|
|
enum htlc_accepted_result {
|
|
|
|
htlc_accepted_continue,
|
|
|
|
htlc_accepted_fail,
|
|
|
|
htlc_accepted_resolve,
|
|
|
|
};
|
|
|
|
|
2019-01-03 17:27:46 +01:00
|
|
|
/**
|
|
|
|
* Parses the JSON-RPC response into a struct understood by the callback.
|
|
|
|
*/
|
2019-05-30 11:52:58 +02:00
|
|
|
static enum htlc_accepted_result htlc_accepted_hook_deserialize(const char *buffer, const jsmntok_t *toks,
|
|
|
|
/* If accepted */
|
|
|
|
struct preimage *payment_preimage,
|
|
|
|
/* If rejected */
|
|
|
|
enum onion_type *failure_code,
|
|
|
|
u8 **channel_update)
|
2019-01-03 17:27:46 +01:00
|
|
|
{
|
2019-01-09 16:12:35 +01:00
|
|
|
const jsmntok_t *resulttok, *failcodetok, *paykeytok, *chanupdtok;
|
2019-05-30 11:52:58 +02:00
|
|
|
enum htlc_accepted_result result;
|
2019-01-09 16:12:35 +01:00
|
|
|
|
|
|
|
if (!toks || !buffer)
|
2019-05-30 11:52:58 +02:00
|
|
|
return htlc_accepted_continue;
|
2019-01-09 16:12:35 +01:00
|
|
|
|
|
|
|
resulttok = json_get_member(buffer, toks, "result");
|
|
|
|
|
|
|
|
/* If the result is "continue" we can just return NULL since
|
|
|
|
* this is the default behavior for this hook anyway */
|
|
|
|
if (!resulttok) {
|
|
|
|
fatal("Plugin return value does not contain 'result' key %s",
|
|
|
|
json_strdup(tmpctx, buffer, toks));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (json_tok_streq(buffer, resulttok, "continue")) {
|
2019-05-30 11:52:58 +02:00
|
|
|
return htlc_accepted_continue;
|
2019-01-09 16:12:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (json_tok_streq(buffer, resulttok, "fail")) {
|
2019-05-30 11:52:58 +02:00
|
|
|
result = htlc_accepted_fail;
|
2019-01-09 16:12:35 +01:00
|
|
|
failcodetok = json_get_member(buffer, toks, "failure_code");
|
|
|
|
chanupdtok = json_get_member(buffer, toks, "channel_update");
|
2019-05-30 11:52:58 +02:00
|
|
|
if (failcodetok &&
|
|
|
|
!json_to_number(buffer, failcodetok, failure_code))
|
2019-01-09 16:12:35 +01:00
|
|
|
fatal("Plugin provided a non-numeric failcode "
|
|
|
|
"in response to an htlc_accepted hook");
|
|
|
|
|
|
|
|
if (!failcodetok)
|
2019-05-30 11:52:58 +02:00
|
|
|
*failure_code = WIRE_TEMPORARY_NODE_FAILURE;
|
2019-01-09 16:12:35 +01:00
|
|
|
|
|
|
|
if (chanupdtok)
|
2019-05-30 11:52:58 +02:00
|
|
|
*channel_update =
|
|
|
|
json_tok_bin_from_hex(buffer, buffer, chanupdtok);
|
2019-01-09 16:12:35 +01:00
|
|
|
else
|
2019-05-30 11:52:58 +02:00
|
|
|
*channel_update = NULL;
|
2019-01-09 16:12:35 +01:00
|
|
|
|
|
|
|
} else if (json_tok_streq(buffer, resulttok, "resolve")) {
|
2019-05-30 11:52:58 +02:00
|
|
|
result = htlc_accepted_resolve;
|
2019-01-09 16:12:35 +01:00
|
|
|
paykeytok = json_get_member(buffer, toks, "payment_key");
|
|
|
|
if (!paykeytok)
|
|
|
|
fatal(
|
|
|
|
"Plugin did not specify a 'payment_key' in return "
|
|
|
|
"value to the htlc_accepted hook: %s",
|
|
|
|
json_strdup(tmpctx, buffer, resulttok));
|
|
|
|
|
|
|
|
if (!json_to_preimage(buffer, paykeytok,
|
2019-05-30 11:52:58 +02:00
|
|
|
payment_preimage))
|
2019-01-09 16:12:35 +01:00
|
|
|
fatal("Plugin specified an invalid 'payment_key': %s",
|
|
|
|
json_tok_full(buffer, resulttok));
|
|
|
|
} else {
|
|
|
|
fatal("Plugin responded with an unknown result to the "
|
|
|
|
"htlc_accepted hook: %s",
|
|
|
|
json_strdup(tmpctx, buffer, resulttok));
|
|
|
|
}
|
|
|
|
|
2019-05-30 11:52:58 +02:00
|
|
|
return result;
|
2019-01-03 17:27:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void htlc_accepted_hook_serialize(struct htlc_accepted_hook_payload *p,
|
|
|
|
struct json_stream *s)
|
|
|
|
{
|
2019-01-09 16:07:49 +01:00
|
|
|
const struct route_step *rs = p->route_step;
|
|
|
|
const struct htlc_in *hin = p->hin;
|
2019-02-21 14:21:28 +01:00
|
|
|
s32 expiry = hin->cltv_expiry, blockheight = p->ld->topology->tip->height;
|
2019-01-09 16:07:49 +01:00
|
|
|
json_object_start(s, "onion");
|
|
|
|
|
2019-02-21 14:39:50 +01:00
|
|
|
json_add_hex_talarr (s, "payload", rs->raw_payload);
|
2019-02-20 15:42:12 +01:00
|
|
|
if (rs->type == SPHINX_V0_PAYLOAD) {
|
2019-01-09 16:07:49 +01:00
|
|
|
json_object_start(s, "per_hop_v0");
|
2019-02-20 15:42:12 +01:00
|
|
|
json_add_string(s, "realm", "00");
|
|
|
|
json_add_short_channel_id(s, "short_channel_id", &rs->payload.v0.channel_id);
|
|
|
|
json_add_amount_msat_only(s, "forward_amount", rs->payload.v0.amt_forward);
|
|
|
|
json_add_u64(s, "outgoing_cltv_value", rs->payload.v0.outgoing_cltv);
|
2019-01-09 16:07:49 +01:00
|
|
|
json_object_end(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
json_add_hex_talarr(s, "next_onion", p->next_onion);
|
2019-06-08 10:58:31 +02:00
|
|
|
json_add_secret(s, "shared_secret", hin->shared_secret);
|
2019-01-09 16:07:49 +01:00
|
|
|
json_object_end(s);
|
|
|
|
|
|
|
|
json_object_start(s, "htlc");
|
|
|
|
json_add_amount_msat_only(s, "amount", hin->msat);
|
2019-02-21 14:21:28 +01:00
|
|
|
json_add_u32(s, "cltv_expiry", expiry);
|
|
|
|
json_add_s32(s, "cltv_expiry_relative", expiry - blockheight);
|
2019-08-10 10:39:04 +02:00
|
|
|
json_add_sha256(s, "payment_hash", &hin->payment_hash);
|
2019-01-09 16:07:49 +01:00
|
|
|
json_object_end(s);
|
2019-01-03 17:27:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback when a plugin answers to the htlc_accepted hook
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
htlc_accepted_hook_callback(struct htlc_accepted_hook_payload *request,
|
|
|
|
const char *buffer, const jsmntok_t *toks)
|
|
|
|
{
|
|
|
|
struct route_step *rs = request->route_step;
|
|
|
|
struct htlc_in *hin = request->hin;
|
|
|
|
struct channel *channel = request->channel;
|
|
|
|
struct lightningd *ld = request->ld;
|
2019-05-30 11:52:58 +02:00
|
|
|
struct preimage payment_preimage;
|
2019-01-03 17:27:46 +01:00
|
|
|
u8 *req;
|
2019-01-09 16:12:35 +01:00
|
|
|
enum htlc_accepted_result result;
|
2019-05-30 11:52:58 +02:00
|
|
|
enum onion_type failure_code;
|
|
|
|
u8 *channel_update;
|
2019-02-20 15:42:12 +01:00
|
|
|
struct hop_data *hop_data;
|
2019-05-30 11:52:58 +02:00
|
|
|
result = htlc_accepted_hook_deserialize(buffer, toks, &payment_preimage, &failure_code, &channel_update);
|
2019-01-09 16:12:35 +01:00
|
|
|
|
2019-02-20 15:42:12 +01:00
|
|
|
hop_data = &rs->payload.v0;
|
2019-01-09 16:12:35 +01:00
|
|
|
switch (result) {
|
|
|
|
case htlc_accepted_continue:
|
|
|
|
if (rs->nextcase == ONION_FORWARD) {
|
2019-02-20 15:42:12 +01:00
|
|
|
struct gossip_resolve *gr = tal(ld, struct gossip_resolve);
|
|
|
|
|
|
|
|
gr->next_onion = serialize_onionpacket(gr, rs->next);
|
|
|
|
gr->next_channel = hop_data->channel_id;
|
|
|
|
gr->amt_to_forward = hop_data->amt_forward;
|
|
|
|
gr->outgoing_cltv_value = hop_data->outgoing_cltv;
|
2019-01-09 16:12:35 +01:00
|
|
|
gr->hin = hin;
|
|
|
|
|
2019-02-20 15:42:12 +01:00
|
|
|
req = towire_gossip_get_channel_peer(tmpctx, &gr->next_channel);
|
|
|
|
log_debug(channel->log, "Asking gossip to resolve channel %s",
|
|
|
|
type_to_string(tmpctx, struct short_channel_id,
|
|
|
|
&gr->next_channel));
|
2019-01-09 16:12:35 +01:00
|
|
|
subd_req(hin, ld->gossip, req, -1, 0,
|
|
|
|
channel_resolve_reply, gr);
|
|
|
|
} else
|
2019-02-20 15:42:12 +01:00
|
|
|
handle_localpay(hin, hin->cltv_expiry, &hin->payment_hash,
|
|
|
|
hop_data->amt_forward,
|
|
|
|
hop_data->outgoing_cltv);
|
2019-01-09 16:12:35 +01:00
|
|
|
break;
|
|
|
|
case htlc_accepted_fail:
|
|
|
|
log_debug(channel->log,
|
|
|
|
"Failing incoming HTLC as instructed by plugin hook");
|
2019-08-15 02:40:46 +02:00
|
|
|
fail_in_htlc(hin, failure_code, NULL, &hop_data->channel_id);
|
2019-01-09 16:12:35 +01:00
|
|
|
break;
|
|
|
|
case htlc_accepted_resolve:
|
2019-05-30 11:52:58 +02:00
|
|
|
fulfill_htlc(hin, &payment_preimage);
|
2019-01-09 16:12:35 +01:00
|
|
|
break;
|
|
|
|
}
|
2019-01-03 17:27:46 +01:00
|
|
|
|
|
|
|
tal_free(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
REGISTER_PLUGIN_HOOK(htlc_accepted, htlc_accepted_hook_callback,
|
|
|
|
struct htlc_accepted_hook_payload *,
|
|
|
|
htlc_accepted_hook_serialize,
|
|
|
|
struct htlc_accepted_hook_payload *);
|
|
|
|
|
2019-05-21 17:48:51 +02:00
|
|
|
/**
|
|
|
|
* Everyone is committed to this htlc of theirs
|
|
|
|
*
|
|
|
|
* @param channel: The channel this HTLC was accepted from.
|
|
|
|
* @param id: the ID of the HTLC we accepted
|
|
|
|
* @param replay: Are we loading from the database and therefore should not
|
|
|
|
* perform the transition to RCVD_ADD_ACK_REVOCATION?
|
|
|
|
* @param[out] failcode: If we decide to fail right away this will be set to a
|
|
|
|
* non-zero failcode.
|
|
|
|
*/
|
|
|
|
static bool peer_accepted_htlc(struct channel *channel, u64 id,
|
|
|
|
bool replay, enum onion_type *failcode)
|
2017-06-20 07:45:03 +02:00
|
|
|
{
|
2017-06-20 07:53:03 +02:00
|
|
|
struct htlc_in *hin;
|
2017-06-20 07:45:03 +02:00
|
|
|
struct route_step *rs;
|
2017-06-20 07:50:03 +02:00
|
|
|
struct onionpacket *op;
|
2018-02-12 11:13:04 +01:00
|
|
|
struct lightningd *ld = channel->peer->ld;
|
2019-01-03 17:27:46 +01:00
|
|
|
struct htlc_accepted_hook_payload *hook_payload;
|
2017-06-20 07:50:03 +02:00
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
hin = find_htlc_in(&ld->htlcs_in, channel, id);
|
2017-06-20 07:53:03 +02:00
|
|
|
if (!hin) {
|
2018-02-12 11:13:04 +01:00
|
|
|
channel_internal_error(channel,
|
2017-06-20 08:17:03 +02:00
|
|
|
"peer_got_revoke unknown htlc %"PRIu64, id);
|
2017-06-20 07:50:03 +02:00
|
|
|
return false;
|
2017-06-20 07:45:03 +02:00
|
|
|
}
|
|
|
|
|
2019-05-21 17:48:51 +02:00
|
|
|
if (!replay && !htlc_in_update_state(channel, hin, RCVD_ADD_ACK_REVOCATION))
|
2017-06-20 07:50:03 +02:00
|
|
|
return false;
|
2018-10-09 10:45:52 +02:00
|
|
|
htlc_in_check(hin, __func__);
|
2017-06-20 07:50:03 +02:00
|
|
|
|
2018-04-03 09:19:42 +02:00
|
|
|
#if DEVELOPER
|
|
|
|
if (channel->peer->ignore_htlcs) {
|
|
|
|
log_debug(channel->log, "their htlc %"PRIu64" dev_ignore_htlcs",
|
|
|
|
id);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
2017-06-26 03:22:56 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* - SHOULD fail to route any HTLC added after it has sent `shutdown`.
|
|
|
|
*/
|
2018-02-12 11:13:04 +01:00
|
|
|
if (channel->state == CHANNELD_SHUTTING_DOWN) {
|
2017-06-26 03:22:56 +02:00
|
|
|
*failcode = WIRE_PERMANENT_CHANNEL_FAILURE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2017-11-02 00:52:39 +01:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* A fulfilling node:
|
|
|
|
* - for each HTLC it is attempting to fulfill:
|
|
|
|
* - MUST estimate a fulfillment deadline.
|
|
|
|
* - MUST fail (and not forward) an HTLC whose fulfillment deadline is
|
|
|
|
* already past.
|
2017-11-02 00:52:39 +01:00
|
|
|
*/
|
|
|
|
/* Our deadline is half the cltv_delta we insist on, so this check is
|
|
|
|
* a subset of the cltv check done in handle_localpay and
|
|
|
|
* forward_htlc. */
|
|
|
|
|
2019-01-08 01:19:50 +01:00
|
|
|
/* Channeld sets this to NULL if couldn't parse onion */
|
|
|
|
if (!hin->shared_secret) {
|
|
|
|
*failcode = WIRE_INVALID_ONION_KEY;
|
2017-06-20 07:50:03 +02:00
|
|
|
goto out;
|
2017-06-20 07:45:03 +02:00
|
|
|
}
|
|
|
|
|
2019-01-08 01:52:13 +01:00
|
|
|
/* FIXME: Have channeld hand through just the route_step! */
|
|
|
|
|
2019-01-08 01:19:50 +01:00
|
|
|
/* channeld tests this, so it should pass. */
|
|
|
|
op = parse_onionpacket(tmpctx, hin->onion_routing_packet,
|
2019-01-08 01:21:50 +01:00
|
|
|
sizeof(hin->onion_routing_packet),
|
|
|
|
failcode);
|
2019-01-08 01:19:50 +01:00
|
|
|
if (!op) {
|
|
|
|
channel_internal_error(channel,
|
|
|
|
"bad onion in got_revoke: %s",
|
|
|
|
tal_hexstr(channel, hin->onion_routing_packet,
|
|
|
|
sizeof(hin->onion_routing_packet)));
|
|
|
|
return false;
|
2017-06-20 07:50:03 +02:00
|
|
|
}
|
2017-06-20 07:45:03 +02:00
|
|
|
|
2017-06-20 07:50:03 +02:00
|
|
|
/* If it's crap, not channeld's fault, just fail it */
|
2019-01-08 01:19:50 +01:00
|
|
|
rs = process_onionpacket(tmpctx, op, hin->shared_secret->data,
|
2017-06-20 07:53:03 +02:00
|
|
|
hin->payment_hash.u.u8,
|
|
|
|
sizeof(hin->payment_hash));
|
2017-06-20 07:45:03 +02:00
|
|
|
if (!rs) {
|
2019-01-08 01:52:13 +01:00
|
|
|
channel_internal_error(channel,
|
|
|
|
"bad process_onionpacket in got_revoke: %s",
|
|
|
|
tal_hexstr(channel, hin->onion_routing_packet,
|
|
|
|
sizeof(hin->onion_routing_packet)));
|
|
|
|
return false;
|
2017-06-20 07:45:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Unknown realm isn't a bad onion, it's a normal failure. */
|
2019-01-03 17:27:46 +01:00
|
|
|
/* FIXME: if we want hooks to handle foreign realms we should
|
|
|
|
* move this check to the hook callback. */
|
2019-02-20 15:42:12 +01:00
|
|
|
if (rs->type != SPHINX_V0_PAYLOAD) {
|
2017-06-20 07:50:03 +02:00
|
|
|
*failcode = WIRE_INVALID_REALM;
|
|
|
|
goto out;
|
2017-06-20 07:45:03 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 17:27:46 +01:00
|
|
|
/* It's time to package up all the information and call the
|
|
|
|
* hook so plugins can interject if they want */
|
|
|
|
hook_payload = tal(hin, struct htlc_accepted_hook_payload);
|
|
|
|
hook_payload->route_step = tal_steal(hook_payload, rs);
|
|
|
|
hook_payload->ld = ld;
|
|
|
|
hook_payload->hin = hin;
|
|
|
|
hook_payload->channel = channel;
|
2019-01-09 16:07:49 +01:00
|
|
|
hook_payload->next_onion = serialize_onionpacket(hook_payload, rs->next);
|
|
|
|
|
2019-01-03 17:27:46 +01:00
|
|
|
plugin_hook_call_htlc_accepted(ld, hook_payload, hook_payload);
|
2017-06-20 07:50:03 +02:00
|
|
|
|
2019-01-03 17:27:46 +01:00
|
|
|
/* Falling through here is ok, after all the HTLC locked */
|
2017-06-20 07:50:03 +02:00
|
|
|
*failcode = 0;
|
|
|
|
out:
|
2018-02-12 11:13:04 +01:00
|
|
|
log_debug(channel->log, "their htlc %"PRIu64" %s",
|
2017-06-20 07:50:03 +02:00
|
|
|
id, *failcode ? onion_type_name(*failcode) : "locked");
|
|
|
|
|
|
|
|
return true;
|
2017-06-20 07:45:03 +02:00
|
|
|
}
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
static void fulfill_our_htlc_out(struct channel *channel, struct htlc_out *hout,
|
2017-08-23 03:55:16 +02:00
|
|
|
const struct preimage *preimage)
|
|
|
|
{
|
2018-02-12 11:13:04 +01:00
|
|
|
struct lightningd *ld = channel->peer->ld;
|
|
|
|
|
2018-01-02 04:48:22 +01:00
|
|
|
assert(!hout->preimage);
|
2017-08-23 03:55:16 +02:00
|
|
|
hout->preimage = tal_dup(hout, struct preimage, preimage);
|
|
|
|
htlc_out_check(hout, __func__);
|
|
|
|
|
2018-10-09 10:56:52 +02:00
|
|
|
wallet_htlc_update(ld->wallet, hout->dbid, hout->hstate,
|
|
|
|
hout->preimage, hout->failcode, hout->failuremsg);
|
2018-03-24 09:26:08 +01:00
|
|
|
/* Update channel stats */
|
|
|
|
wallet_channel_stats_incr_out_fulfilled(ld->wallet,
|
|
|
|
channel->dbid,
|
2019-02-21 04:45:55 +01:00
|
|
|
hout->msat);
|
2017-08-23 03:55:16 +02:00
|
|
|
|
2018-10-09 22:21:31 +02:00
|
|
|
if (hout->am_origin)
|
2018-02-12 11:13:04 +01:00
|
|
|
payment_succeeded(ld, hout, preimage);
|
2018-10-17 00:16:50 +02:00
|
|
|
else if (hout->in) {
|
2018-10-09 10:47:52 +02:00
|
|
|
fulfill_htlc(hout->in, preimage);
|
2018-10-17 00:16:50 +02:00
|
|
|
wallet_forwarded_payment_add(ld->wallet, hout->in, hout,
|
2019-04-15 16:27:22 +02:00
|
|
|
FORWARD_SETTLED, 0);
|
2018-10-17 00:16:50 +02:00
|
|
|
}
|
2017-08-23 03:55:16 +02:00
|
|
|
}
|
|
|
|
|
2018-02-12 11:12:55 +01:00
|
|
|
static bool peer_fulfilled_our_htlc(struct channel *channel,
|
2017-06-20 07:50:03 +02:00
|
|
|
const struct fulfilled_htlc *fulfilled)
|
2017-06-20 07:45:03 +02:00
|
|
|
{
|
2018-02-12 11:13:04 +01:00
|
|
|
struct lightningd *ld = channel->peer->ld;
|
2017-06-20 07:53:03 +02:00
|
|
|
struct htlc_out *hout;
|
2017-06-20 07:45:03 +02:00
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
hout = find_htlc_out(&ld->htlcs_out, channel, fulfilled->id);
|
2017-06-20 07:53:03 +02:00
|
|
|
if (!hout) {
|
2018-02-12 11:13:04 +01:00
|
|
|
channel_internal_error(channel,
|
2017-06-20 08:17:03 +02:00
|
|
|
"fulfilled_our_htlc unknown htlc %"PRIu64,
|
|
|
|
fulfilled->id);
|
2017-06-20 07:50:03 +02:00
|
|
|
return false;
|
2017-06-20 07:45:03 +02:00
|
|
|
}
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
if (!htlc_out_update_state(channel, hout, RCVD_REMOVE_COMMIT))
|
2017-06-20 07:50:03 +02:00
|
|
|
return false;
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
fulfill_our_htlc_out(channel, hout, &fulfilled->payment_preimage);
|
2017-08-23 03:55:16 +02:00
|
|
|
return true;
|
|
|
|
}
|
2017-06-20 07:50:03 +02:00
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
void onchain_fulfilled_htlc(struct channel *channel,
|
|
|
|
const struct preimage *preimage)
|
2017-08-23 03:55:16 +02:00
|
|
|
{
|
|
|
|
struct htlc_out_map_iter outi;
|
|
|
|
struct htlc_out *hout;
|
|
|
|
struct sha256 payment_hash;
|
2018-02-12 11:13:04 +01:00
|
|
|
struct lightningd *ld = channel->peer->ld;
|
2017-06-20 07:50:03 +02:00
|
|
|
|
2017-08-23 03:55:16 +02:00
|
|
|
sha256(&payment_hash, preimage, sizeof(*preimage));
|
|
|
|
|
|
|
|
/* FIXME: use db to look this up! */
|
2018-02-12 11:13:04 +01:00
|
|
|
for (hout = htlc_out_map_first(&ld->htlcs_out, &outi);
|
2017-08-23 03:55:16 +02:00
|
|
|
hout;
|
2018-02-12 11:13:04 +01:00
|
|
|
hout = htlc_out_map_next(&ld->htlcs_out, &outi)) {
|
2018-02-12 11:13:04 +01:00
|
|
|
if (hout->key.channel != channel)
|
2017-08-23 03:55:16 +02:00
|
|
|
continue;
|
|
|
|
|
2018-02-14 07:08:54 +01:00
|
|
|
/* It's possible that we failed some and succeeded one,
|
|
|
|
* if we got multiple errors. */
|
|
|
|
if (hout->failcode != 0 || hout->failuremsg)
|
|
|
|
continue;
|
|
|
|
|
2018-07-04 07:30:02 +02:00
|
|
|
if (!sha256_eq(&hout->payment_hash, &payment_hash))
|
2017-08-23 03:55:16 +02:00
|
|
|
continue;
|
|
|
|
|
2018-01-02 04:48:22 +01:00
|
|
|
/* We may have already fulfilled before going onchain, or
|
|
|
|
* we can fulfill onchain multiple times. */
|
2018-10-09 10:45:52 +02:00
|
|
|
if (!hout->preimage) {
|
|
|
|
/* Force state to something which allows a preimage */
|
|
|
|
hout->hstate = RCVD_REMOVE_HTLC;
|
2018-02-12 11:13:04 +01:00
|
|
|
fulfill_our_htlc_out(channel, hout, preimage);
|
2018-10-09 10:45:52 +02:00
|
|
|
}
|
2018-01-02 04:48:22 +01:00
|
|
|
|
2017-08-23 03:55:16 +02:00
|
|
|
/* We keep going: this is something of a leak, but onchain
|
|
|
|
* we have no real way of distinguishing HTLCs anyway */
|
|
|
|
}
|
2017-06-20 07:45:03 +02:00
|
|
|
}
|
|
|
|
|
2018-02-12 11:12:55 +01:00
|
|
|
static bool peer_failed_our_htlc(struct channel *channel,
|
2017-06-20 07:50:03 +02:00
|
|
|
const struct failed_htlc *failed)
|
2017-06-20 07:45:03 +02:00
|
|
|
{
|
2017-06-20 07:53:03 +02:00
|
|
|
struct htlc_out *hout;
|
2018-02-12 11:13:04 +01:00
|
|
|
struct lightningd *ld = channel->peer->ld;
|
2017-06-20 07:45:03 +02:00
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
hout = find_htlc_out(&ld->htlcs_out, channel, failed->id);
|
2017-06-20 07:53:03 +02:00
|
|
|
if (!hout) {
|
2018-02-12 11:13:04 +01:00
|
|
|
channel_internal_error(channel,
|
2017-06-20 08:17:03 +02:00
|
|
|
"failed_our_htlc unknown htlc %"PRIu64,
|
|
|
|
failed->id);
|
2017-06-20 07:50:03 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
if (!htlc_out_update_state(channel, hout, RCVD_REMOVE_COMMIT))
|
2017-06-20 07:50:03 +02:00
|
|
|
return false;
|
|
|
|
|
2018-07-02 06:30:22 +02:00
|
|
|
hout->failcode = failed->failcode;
|
|
|
|
if (!failed->failcode)
|
2017-06-20 08:07:03 +02:00
|
|
|
hout->failuremsg = tal_dup_arr(hout, u8, failed->failreason,
|
2018-07-28 08:00:16 +02:00
|
|
|
tal_count(failed->failreason), 0);
|
2017-11-28 06:03:04 +01:00
|
|
|
|
|
|
|
else
|
|
|
|
hout->failuremsg = NULL;
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
log_debug(channel->log, "Our HTLC %"PRIu64" failed (%u)", failed->id,
|
2017-11-28 06:03:04 +01:00
|
|
|
hout->failcode);
|
2017-06-20 07:53:03 +02:00
|
|
|
htlc_out_check(hout, __func__);
|
2018-10-17 00:16:50 +02:00
|
|
|
|
|
|
|
if (hout->in)
|
2019-04-15 16:27:22 +02:00
|
|
|
wallet_forwarded_payment_add(ld->wallet, hout->in,
|
|
|
|
hout, FORWARD_FAILED, hout->failcode);
|
2018-10-17 00:16:50 +02:00
|
|
|
|
2017-06-20 07:50:03 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
void onchain_failed_our_htlc(const struct channel *channel,
|
2017-09-26 23:02:47 +02:00
|
|
|
const struct htlc_stub *htlc,
|
|
|
|
const char *why)
|
|
|
|
{
|
2018-02-12 11:13:04 +01:00
|
|
|
struct lightningd *ld = channel->peer->ld;
|
2018-10-23 11:40:22 +02:00
|
|
|
struct htlc_out *hout;
|
|
|
|
|
|
|
|
hout = find_htlc_out(&ld->htlcs_out, channel, htlc->id);
|
|
|
|
if (!hout)
|
|
|
|
return;
|
2017-09-26 23:02:47 +02:00
|
|
|
|
2018-10-09 10:58:52 +02:00
|
|
|
/* Don't fail twice (or if already succeeded)! */
|
|
|
|
if (hout->failuremsg || hout->failcode || hout->preimage)
|
2017-09-26 23:02:47 +02:00
|
|
|
return;
|
|
|
|
|
2017-11-28 06:04:20 +01:00
|
|
|
hout->failcode = WIRE_PERMANENT_CHANNEL_FAILURE;
|
2017-09-26 23:02:47 +02:00
|
|
|
|
2018-10-09 10:45:52 +02:00
|
|
|
/* Force state to something which expects a failure, and save to db */
|
|
|
|
hout->hstate = RCVD_REMOVE_HTLC;
|
|
|
|
htlc_out_check(hout, __func__);
|
2018-10-23 11:40:22 +02:00
|
|
|
wallet_htlc_update(ld->wallet, hout->dbid, hout->hstate,
|
2018-10-09 10:56:52 +02:00
|
|
|
hout->preimage, hout->failcode, hout->failuremsg);
|
2018-10-09 10:45:52 +02:00
|
|
|
|
2018-10-09 22:21:31 +02:00
|
|
|
if (hout->am_origin) {
|
2018-03-01 11:32:38 +01:00
|
|
|
assert(why != NULL);
|
2018-02-12 11:13:04 +01:00
|
|
|
char *localfail = tal_fmt(channel, "%s: %s",
|
2017-09-26 23:02:47 +02:00
|
|
|
onion_type_name(WIRE_PERMANENT_CHANNEL_FAILURE),
|
|
|
|
why);
|
2018-02-12 11:13:04 +01:00
|
|
|
payment_failed(ld, hout, localfail);
|
2017-09-26 23:02:47 +02:00
|
|
|
tal_free(localfail);
|
2019-04-15 16:27:22 +02:00
|
|
|
} else if (hout->in) {
|
2017-11-28 06:04:20 +01:00
|
|
|
local_fail_htlc(hout->in, WIRE_PERMANENT_CHANNEL_FAILURE,
|
2018-02-12 11:13:04 +01:00
|
|
|
hout->key.channel->scid);
|
2019-04-15 16:27:22 +02:00
|
|
|
wallet_forwarded_payment_add(hout->key.channel->peer->ld->wallet,
|
|
|
|
hout->in, hout,
|
|
|
|
FORWARD_LOCAL_FAILED,
|
|
|
|
hout->failcode);
|
|
|
|
}
|
2017-09-26 23:02:47 +02:00
|
|
|
}
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
static void remove_htlc_in(struct channel *channel, struct htlc_in *hin)
|
2017-06-20 07:53:03 +02:00
|
|
|
{
|
|
|
|
htlc_in_check(hin, __func__);
|
2017-11-28 06:03:04 +01:00
|
|
|
assert(hin->failuremsg || hin->preimage || hin->failcode);
|
2017-06-20 07:59:03 +02:00
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
log_debug(channel->log, "Removing in HTLC %"PRIu64" state %s %s",
|
2017-06-20 07:59:03 +02:00
|
|
|
hin->key.id, htlc_state_name(hin->hstate),
|
2017-11-28 06:03:04 +01:00
|
|
|
hin->preimage ? "FULFILLED"
|
|
|
|
: hin->failcode ? onion_type_name(hin->failcode)
|
|
|
|
: "REMOTEFAIL");
|
2017-06-20 07:59:03 +02:00
|
|
|
|
|
|
|
/* If we fulfilled their HTLC, credit us. */
|
|
|
|
if (hin->preimage) {
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat oldamt = channel->our_msat;
|
|
|
|
if (!amount_msat_add(&channel->our_msat, channel->our_msat,
|
|
|
|
hin->msat)) {
|
|
|
|
channel_internal_error(channel,
|
|
|
|
"Overflow our_msat %s + HTLC %s",
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct amount_msat,
|
|
|
|
&channel->our_msat),
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct amount_msat,
|
|
|
|
&hin->msat));
|
|
|
|
}
|
|
|
|
log_debug(channel->log, "Balance %s -> %s",
|
|
|
|
type_to_string(tmpctx, struct amount_msat, &oldamt),
|
|
|
|
type_to_string(tmpctx, struct amount_msat,
|
|
|
|
&channel->our_msat));
|
|
|
|
if (amount_msat_greater(channel->our_msat,
|
|
|
|
channel->msat_to_us_max))
|
|
|
|
channel->msat_to_us_max = channel->our_msat;
|
2017-06-20 07:59:03 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 07:53:03 +02:00
|
|
|
tal_free(hin);
|
|
|
|
}
|
2017-06-20 07:50:03 +02:00
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
static void remove_htlc_out(struct channel *channel, struct htlc_out *hout)
|
2017-06-20 07:50:03 +02:00
|
|
|
{
|
2017-06-20 07:53:03 +02:00
|
|
|
htlc_out_check(hout, __func__);
|
2017-11-28 06:03:04 +01:00
|
|
|
assert(hout->failuremsg || hout->preimage || hout->failcode);
|
2018-02-12 11:13:04 +01:00
|
|
|
log_debug(channel->log, "Removing out HTLC %"PRIu64" state %s %s",
|
2017-06-20 07:59:03 +02:00
|
|
|
hout->key.id, htlc_state_name(hout->hstate),
|
2017-11-28 06:03:04 +01:00
|
|
|
hout->preimage ? "FULFILLED"
|
|
|
|
: hout->failcode ? onion_type_name(hout->failcode)
|
|
|
|
: "REMOTEFAIL");
|
2017-06-20 07:50:03 +02:00
|
|
|
|
|
|
|
/* If it's failed, now we can forward since it's completely locked-in */
|
2017-06-20 08:07:03 +02:00
|
|
|
if (!hout->preimage) {
|
2017-06-20 08:12:03 +02:00
|
|
|
fail_out_htlc(hout, NULL);
|
2017-06-20 07:59:03 +02:00
|
|
|
} else {
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat oldamt = channel->our_msat;
|
2017-06-20 07:59:03 +02:00
|
|
|
/* We paid for this HTLC, so deduct balance. */
|
2019-02-21 04:45:55 +01:00
|
|
|
if (!amount_msat_sub(&channel->our_msat, channel->our_msat,
|
|
|
|
hout->msat)) {
|
|
|
|
channel_internal_error(channel,
|
|
|
|
"Underflow our_msat %s - HTLC %s",
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct amount_msat,
|
|
|
|
&channel->our_msat),
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct amount_msat,
|
|
|
|
&hout->msat));
|
|
|
|
}
|
|
|
|
|
|
|
|
log_debug(channel->log, "Balance %s -> %s",
|
|
|
|
type_to_string(tmpctx, struct amount_msat, &oldamt),
|
|
|
|
type_to_string(tmpctx, struct amount_msat,
|
|
|
|
&channel->our_msat));
|
|
|
|
if (amount_msat_less(channel->our_msat, channel->msat_to_us_min))
|
|
|
|
channel->msat_to_us_min = channel->our_msat;
|
2017-06-20 07:45:03 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 07:53:03 +02:00
|
|
|
tal_free(hout);
|
2017-06-20 07:50:03 +02:00
|
|
|
}
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
static bool update_in_htlc(struct channel *channel,
|
|
|
|
u64 id, enum htlc_state newstate)
|
2017-06-20 07:50:03 +02:00
|
|
|
{
|
2017-06-20 07:53:03 +02:00
|
|
|
struct htlc_in *hin;
|
2018-02-12 11:13:04 +01:00
|
|
|
struct lightningd *ld = channel->peer->ld;
|
2017-06-20 07:50:03 +02:00
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
hin = find_htlc_in(&ld->htlcs_in, channel, id);
|
2017-06-20 07:53:03 +02:00
|
|
|
if (!hin) {
|
2018-02-12 11:13:04 +01:00
|
|
|
channel_internal_error(channel, "Can't find in HTLC %"PRIu64, id);
|
2017-06-20 07:53:03 +02:00
|
|
|
return false;
|
|
|
|
}
|
2017-06-20 07:50:03 +02:00
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
if (!htlc_in_update_state(channel, hin, newstate))
|
2017-06-20 07:53:03 +02:00
|
|
|
return false;
|
|
|
|
|
2018-10-09 10:45:52 +02:00
|
|
|
htlc_in_check(hin, __func__);
|
2017-06-20 07:53:03 +02:00
|
|
|
if (newstate == SENT_REMOVE_ACK_REVOCATION)
|
2018-02-12 11:13:04 +01:00
|
|
|
remove_htlc_in(channel, hin);
|
2017-06-20 07:53:03 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
static bool update_out_htlc(struct channel *channel,
|
|
|
|
u64 id, enum htlc_state newstate)
|
2017-06-20 07:53:03 +02:00
|
|
|
{
|
2018-02-12 11:13:04 +01:00
|
|
|
struct lightningd *ld = channel->peer->ld;
|
2017-06-20 07:53:03 +02:00
|
|
|
struct htlc_out *hout;
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
hout = find_htlc_out(&ld->htlcs_out, channel, id);
|
2017-06-20 07:53:03 +02:00
|
|
|
if (!hout) {
|
2018-02-12 11:13:04 +01:00
|
|
|
channel_internal_error(channel, "Can't find out HTLC %"PRIu64, id);
|
2017-06-20 07:50:03 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-23 21:54:19 +01:00
|
|
|
if (!hout->dbid) {
|
2018-02-12 11:13:04 +01:00
|
|
|
wallet_htlc_save_out(ld->wallet, channel, hout);
|
2018-03-24 09:26:08 +01:00
|
|
|
/* Update channel stats */
|
|
|
|
wallet_channel_stats_incr_out_offered(ld->wallet,
|
|
|
|
channel->dbid,
|
2019-02-21 04:45:55 +01:00
|
|
|
hout->msat);
|
2017-11-23 21:54:19 +01:00
|
|
|
|
2019-04-15 16:27:22 +02:00
|
|
|
if (hout->in) {
|
2018-10-17 00:16:50 +02:00
|
|
|
wallet_forwarded_payment_add(ld->wallet, hout->in, hout,
|
2019-04-15 16:27:22 +02:00
|
|
|
FORWARD_OFFERED, 0);
|
|
|
|
}
|
2018-10-17 00:16:50 +02:00
|
|
|
|
2018-01-17 21:29:49 +01:00
|
|
|
/* For our own HTLCs, we commit payment to db lazily */
|
|
|
|
if (hout->origin_htlc_id == 0)
|
2018-03-10 07:39:11 +01:00
|
|
|
payment_store(ld,
|
|
|
|
&hout->payment_hash);
|
2017-11-23 21:54:19 +01:00
|
|
|
}
|
2017-09-18 20:18:29 +02:00
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
if (!htlc_out_update_state(channel, hout, newstate))
|
2017-06-20 07:50:03 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* First transition into commitment; now it outlives peer. */
|
2017-06-20 07:53:03 +02:00
|
|
|
if (newstate == SENT_ADD_COMMIT) {
|
2018-02-14 02:53:13 +01:00
|
|
|
tal_del_destructor(hout, destroy_hout_subd_died);
|
2018-02-12 11:13:04 +01:00
|
|
|
tal_steal(ld, hout);
|
2017-06-20 07:50:03 +02:00
|
|
|
|
2017-06-20 07:53:03 +02:00
|
|
|
} else if (newstate == RCVD_REMOVE_ACK_REVOCATION) {
|
2018-02-12 11:13:04 +01:00
|
|
|
remove_htlc_out(channel, hout);
|
2017-06-20 07:50:03 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
static bool changed_htlc(struct channel *channel,
|
2017-06-20 07:53:03 +02:00
|
|
|
const struct changed_htlc *changed)
|
|
|
|
{
|
|
|
|
if (htlc_state_owner(changed->newstate) == LOCAL)
|
2018-02-12 11:13:04 +01:00
|
|
|
return update_out_htlc(channel, changed->id, changed->newstate);
|
2017-06-20 07:53:03 +02:00
|
|
|
else
|
2018-02-12 11:13:04 +01:00
|
|
|
return update_in_htlc(channel, changed->id, changed->newstate);
|
2017-06-20 07:53:03 +02:00
|
|
|
}
|
|
|
|
|
2019-06-27 02:25:17 +02:00
|
|
|
/* FIXME: This should be a complete check, not just a sanity check.
|
|
|
|
* Perhaps that means we need a cookie from the HSM? */
|
|
|
|
static bool valid_commitment_tx(struct channel *channel,
|
|
|
|
const struct bitcoin_tx *tx)
|
|
|
|
{
|
|
|
|
/* We've had past issues where all outputs are trimmed. */
|
|
|
|
if (tx->wtx->num_outputs == 0) {
|
|
|
|
channel_internal_error(channel,
|
|
|
|
"channel_got_commitsig: zero output tx! %s",
|
|
|
|
type_to_string(tmpctx, struct bitcoin_tx, tx));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
static bool peer_save_commitsig_received(struct channel *channel, u64 commitnum,
|
2018-01-10 03:43:23 +01:00
|
|
|
struct bitcoin_tx *tx,
|
2018-12-03 00:15:06 +01:00
|
|
|
const struct bitcoin_signature *commit_sig)
|
2017-06-20 08:18:03 +02:00
|
|
|
{
|
2018-02-12 11:13:04 +01:00
|
|
|
if (commitnum != channel->next_index[LOCAL]) {
|
|
|
|
channel_internal_error(channel,
|
2017-06-20 08:18:03 +02:00
|
|
|
"channel_got_commitsig: expected commitnum %"PRIu64
|
|
|
|
" got %"PRIu64,
|
2018-02-12 11:13:04 +01:00
|
|
|
channel->next_index[LOCAL], commitnum);
|
2017-06-20 08:18:03 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-27 02:25:17 +02:00
|
|
|
/* Basic sanity check */
|
|
|
|
if (!valid_commitment_tx(channel, tx))
|
|
|
|
return false;
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
channel->next_index[LOCAL]++;
|
2017-06-20 08:18:03 +02:00
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
/* Update channel->last_sig and channel->last_tx before saving to db */
|
2019-05-28 18:06:39 +02:00
|
|
|
channel_set_last_tx(channel, tx, commit_sig, TX_CHANNEL_UNILATERAL);
|
2018-01-10 03:43:23 +01:00
|
|
|
|
2017-06-20 08:18:03 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
static bool peer_save_commitsig_sent(struct channel *channel, u64 commitnum)
|
2017-06-20 08:18:03 +02:00
|
|
|
{
|
2018-02-12 11:13:04 +01:00
|
|
|
struct lightningd *ld = channel->peer->ld;
|
2018-02-12 11:13:04 +01:00
|
|
|
|
|
|
|
if (commitnum != channel->next_index[REMOTE]) {
|
|
|
|
channel_internal_error(channel,
|
2017-06-20 08:18:03 +02:00
|
|
|
"channel_sent_commitsig: expected commitnum %"PRIu64
|
|
|
|
" got %"PRIu64,
|
2018-02-12 11:13:04 +01:00
|
|
|
channel->next_index[REMOTE], commitnum);
|
2017-06-20 08:18:03 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
channel->next_index[REMOTE]++;
|
2017-06-20 08:18:03 +02:00
|
|
|
|
|
|
|
/* FIXME: Save to database, with sig and HTLCs. */
|
2018-02-12 11:13:04 +01:00
|
|
|
wallet_channel_save(ld->wallet, channel);
|
2017-06-20 08:18:03 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
void peer_sending_commitsig(struct channel *channel, const u8 *msg)
|
2017-06-20 07:50:03 +02:00
|
|
|
{
|
|
|
|
u64 commitnum;
|
2017-11-21 06:26:59 +01:00
|
|
|
u32 feerate;
|
2017-06-20 07:50:03 +02:00
|
|
|
struct changed_htlc *changed_htlcs;
|
2017-06-20 08:13:03 +02:00
|
|
|
size_t i, maxid = 0, num_local_added = 0;
|
2018-12-03 00:15:06 +01:00
|
|
|
struct bitcoin_signature commit_sig;
|
2017-06-20 08:08:03 +02:00
|
|
|
secp256k1_ecdsa_signature *htlc_sigs;
|
2018-02-12 11:13:04 +01:00
|
|
|
struct lightningd *ld = channel->peer->ld;
|
2017-06-20 07:50:03 +02:00
|
|
|
|
2018-08-09 04:53:04 +02:00
|
|
|
channel->htlc_timeout = tal_free(channel->htlc_timeout);
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_channel_sending_commitsig(msg, msg,
|
2017-06-20 07:50:03 +02:00
|
|
|
&commitnum,
|
2017-11-21 06:26:59 +01:00
|
|
|
&feerate,
|
2017-06-20 08:08:03 +02:00
|
|
|
&changed_htlcs,
|
|
|
|
&commit_sig, &htlc_sigs)) {
|
2018-02-12 11:13:04 +01:00
|
|
|
channel_internal_error(channel, "bad channel_sending_commitsig %s",
|
2018-02-12 11:13:04 +01:00
|
|
|
tal_hex(channel, msg));
|
2017-10-12 08:35:04 +02:00
|
|
|
return;
|
2017-06-20 07:45:03 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 07:50:03 +02:00
|
|
|
for (i = 0; i < tal_count(changed_htlcs); i++) {
|
2018-02-12 11:13:04 +01:00
|
|
|
if (!changed_htlc(channel, changed_htlcs + i)) {
|
2018-02-12 11:13:04 +01:00
|
|
|
channel_internal_error(channel,
|
2017-06-20 07:50:03 +02:00
|
|
|
"channel_sending_commitsig: update failed");
|
2017-10-12 08:35:04 +02:00
|
|
|
return;
|
2017-06-20 07:45:03 +02:00
|
|
|
}
|
2017-06-20 08:13:03 +02:00
|
|
|
|
|
|
|
/* While we're here, sanity check added ones are in
|
|
|
|
* ascending order. */
|
|
|
|
if (changed_htlcs[i].newstate == SENT_ADD_COMMIT) {
|
|
|
|
num_local_added++;
|
|
|
|
if (changed_htlcs[i].id > maxid)
|
|
|
|
maxid = changed_htlcs[i].id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num_local_added != 0) {
|
2018-02-12 11:13:04 +01:00
|
|
|
if (maxid != channel->next_htlc_id + num_local_added - 1) {
|
2018-02-12 11:13:04 +01:00
|
|
|
channel_internal_error(channel,
|
2017-06-20 08:13:03 +02:00
|
|
|
"channel_sending_commitsig:"
|
|
|
|
" Added %"PRIu64", maxid now %"PRIu64
|
|
|
|
" from %"PRIu64,
|
2018-02-12 11:13:04 +01:00
|
|
|
num_local_added, maxid, channel->next_htlc_id);
|
2017-10-12 08:35:04 +02:00
|
|
|
return;
|
2017-06-20 08:13:03 +02:00
|
|
|
}
|
2018-02-12 11:13:04 +01:00
|
|
|
channel->next_htlc_id += num_local_added;
|
2017-06-20 07:45:03 +02:00
|
|
|
}
|
|
|
|
|
2017-11-21 06:26:59 +01:00
|
|
|
/* Update their feerate. */
|
2018-02-19 02:06:12 +01:00
|
|
|
channel->channel_info.feerate_per_kw[REMOTE] = feerate;
|
2018-04-03 09:19:39 +02:00
|
|
|
if (feerate > channel->max_possible_feerate)
|
|
|
|
channel->max_possible_feerate = feerate;
|
|
|
|
if (feerate < channel->min_possible_feerate)
|
|
|
|
channel->min_possible_feerate = feerate;
|
2017-11-21 06:26:59 +01:00
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
if (!peer_save_commitsig_sent(channel, commitnum))
|
2017-10-12 08:35:04 +02:00
|
|
|
return;
|
2017-06-20 08:02:03 +02:00
|
|
|
|
2018-01-13 15:33:51 +01:00
|
|
|
/* Last was commit. */
|
2018-02-12 11:13:04 +01:00
|
|
|
channel->last_was_revoke = false;
|
|
|
|
tal_free(channel->last_sent_commit);
|
|
|
|
channel->last_sent_commit = tal_steal(channel, changed_htlcs);
|
|
|
|
wallet_channel_save(ld->wallet, channel);
|
2017-06-20 08:03:03 +02:00
|
|
|
|
2017-06-20 07:50:03 +02:00
|
|
|
/* Tell it we've got it, and to go ahead with commitment_signed. */
|
2018-02-12 11:13:04 +01:00
|
|
|
subd_send_msg(channel->owner,
|
2017-06-20 07:50:03 +02:00
|
|
|
take(towire_channel_sending_commitsig_reply(msg)));
|
2017-06-20 07:45:03 +02:00
|
|
|
}
|
|
|
|
|
2018-02-12 11:12:55 +01:00
|
|
|
static bool channel_added_their_htlc(struct channel *channel,
|
|
|
|
const struct added_htlc *added,
|
|
|
|
const struct secret *shared_secret)
|
2017-06-20 07:50:03 +02:00
|
|
|
{
|
2018-02-12 11:13:04 +01:00
|
|
|
struct lightningd *ld = channel->peer->ld;
|
2017-06-20 07:53:03 +02:00
|
|
|
struct htlc_in *hin;
|
2017-06-20 07:50:03 +02:00
|
|
|
|
2018-01-21 03:49:21 +01:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* - receiving an `amount_msat` equal to 0, OR less than its own `htlc_minimum_msat`:
|
|
|
|
* - SHOULD fail the channel.
|
|
|
|
*/
|
2019-02-21 04:45:55 +01:00
|
|
|
if (amount_msat_eq(added->amount, AMOUNT_MSAT(0))
|
|
|
|
|| amount_msat_less(added->amount, channel->our_config.htlc_minimum)) {
|
2018-02-12 11:13:04 +01:00
|
|
|
channel_internal_error(channel,
|
2019-02-21 04:45:55 +01:00
|
|
|
"trying to add HTLC amount %s"
|
|
|
|
" but minimum is %s",
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct amount_msat,
|
|
|
|
&added->amount),
|
2019-02-21 04:45:54 +01:00
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct amount_msat,
|
|
|
|
&channel->our_config.htlc_minimum));
|
2018-01-21 03:49:21 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-08 01:19:50 +01:00
|
|
|
/* FIXME: Our wire generator can't handle optional elems in arrays,
|
|
|
|
* so we translate all-zero-shared-secret to NULL. */
|
|
|
|
if (memeqzero(shared_secret, sizeof(&shared_secret)))
|
|
|
|
shared_secret = NULL;
|
|
|
|
|
2017-06-20 07:50:03 +02:00
|
|
|
/* This stays around even if we fail it immediately: it *is*
|
|
|
|
* part of the current commitment. */
|
2019-02-21 04:45:55 +01:00
|
|
|
hin = new_htlc_in(channel, channel, added->id, added->amount,
|
2017-06-20 07:53:03 +02:00
|
|
|
added->cltv_expiry, &added->payment_hash,
|
2017-06-20 07:54:03 +02:00
|
|
|
shared_secret, added->onion_routing_packet);
|
2017-06-20 07:53:03 +02:00
|
|
|
|
2017-09-18 20:18:29 +02:00
|
|
|
/* Save an incoming htlc to the wallet */
|
2018-02-12 11:13:04 +01:00
|
|
|
wallet_htlc_save_in(ld->wallet, channel, hin);
|
2018-03-24 09:26:08 +01:00
|
|
|
/* Update channel stats */
|
|
|
|
wallet_channel_stats_incr_in_offered(ld->wallet, channel->dbid,
|
2019-02-21 04:45:55 +01:00
|
|
|
added->amount);
|
2017-06-20 07:50:03 +02:00
|
|
|
|
2018-02-12 11:12:55 +01:00
|
|
|
log_debug(channel->log, "Adding their HTLC %"PRIu64, added->id);
|
|
|
|
connect_htlc_in(&channel->peer->ld->htlcs_in, hin);
|
2018-01-21 03:49:21 +01:00
|
|
|
return true;
|
2017-06-20 07:50:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The peer doesn't tell us this separately, but logically it's a separate
|
|
|
|
* step to receiving commitsig */
|
2018-02-12 11:12:55 +01:00
|
|
|
static bool peer_sending_revocation(struct channel *channel,
|
2017-06-20 07:50:03 +02:00
|
|
|
struct added_htlc *added,
|
|
|
|
struct fulfilled_htlc *fulfilled,
|
2018-02-08 02:24:46 +01:00
|
|
|
struct failed_htlc **failed,
|
2017-06-20 07:50:03 +02:00
|
|
|
struct changed_htlc *changed)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < tal_count(added); i++) {
|
2018-02-12 11:13:04 +01:00
|
|
|
if (!update_in_htlc(channel, added[i].id, SENT_ADD_REVOCATION))
|
2017-06-20 07:50:03 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (i = 0; i < tal_count(fulfilled); i++) {
|
2018-02-12 11:13:04 +01:00
|
|
|
if (!update_out_htlc(channel, fulfilled[i].id,
|
2017-06-20 07:53:03 +02:00
|
|
|
SENT_REMOVE_REVOCATION))
|
2017-06-20 07:50:03 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (i = 0; i < tal_count(failed); i++) {
|
2018-02-12 11:13:04 +01:00
|
|
|
if (!update_out_htlc(channel, failed[i]->id, SENT_REMOVE_REVOCATION))
|
2017-06-20 07:50:03 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (i = 0; i < tal_count(changed); i++) {
|
|
|
|
if (changed[i].newstate == RCVD_ADD_ACK_COMMIT) {
|
2018-02-12 11:13:04 +01:00
|
|
|
if (!update_out_htlc(channel, changed[i].id,
|
2017-06-20 07:53:03 +02:00
|
|
|
SENT_ADD_ACK_REVOCATION))
|
2017-06-20 07:50:03 +02:00
|
|
|
return false;
|
|
|
|
} else {
|
2018-02-12 11:13:04 +01:00
|
|
|
if (!update_in_htlc(channel, changed[i].id,
|
2017-06-20 07:53:03 +02:00
|
|
|
SENT_REMOVE_ACK_REVOCATION))
|
2017-06-20 07:50:03 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-12 11:12:55 +01:00
|
|
|
channel->last_was_revoke = true;
|
2017-06-20 07:50:03 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-10 12:48:13 +02:00
|
|
|
struct deferred_commitsig {
|
|
|
|
struct channel *channel;
|
|
|
|
const u8 *msg;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void retry_deferred_commitsig(struct chain_topology *topo,
|
|
|
|
struct deferred_commitsig *d)
|
|
|
|
{
|
|
|
|
peer_got_commitsig(d->channel, d->msg);
|
|
|
|
tal_free(d);
|
|
|
|
}
|
|
|
|
|
2017-06-20 07:50:03 +02:00
|
|
|
/* This also implies we're sending revocation */
|
2018-02-12 11:13:04 +01:00
|
|
|
void peer_got_commitsig(struct channel *channel, const u8 *msg)
|
2017-06-20 07:50:03 +02:00
|
|
|
{
|
|
|
|
u64 commitnum;
|
2017-11-21 06:26:59 +01:00
|
|
|
u32 feerate;
|
2018-12-03 00:15:06 +01:00
|
|
|
struct bitcoin_signature commit_sig;
|
2017-06-20 07:50:03 +02:00
|
|
|
secp256k1_ecdsa_signature *htlc_sigs;
|
|
|
|
struct added_htlc *added;
|
2017-06-20 07:54:03 +02:00
|
|
|
struct secret *shared_secrets;
|
2017-06-20 07:50:03 +02:00
|
|
|
struct fulfilled_htlc *fulfilled;
|
2018-02-08 02:24:46 +01:00
|
|
|
struct failed_htlc **failed;
|
2017-06-20 07:50:03 +02:00
|
|
|
struct changed_htlc *changed;
|
2018-02-08 02:25:12 +01:00
|
|
|
struct bitcoin_tx *tx;
|
2017-06-20 07:50:03 +02:00
|
|
|
size_t i;
|
2018-02-12 11:13:04 +01:00
|
|
|
struct lightningd *ld = channel->peer->ld;
|
2017-06-20 07:50:03 +02:00
|
|
|
|
2019-08-10 12:48:13 +02:00
|
|
|
/* If we're not synced with bitcoin network, we can't accept
|
|
|
|
* any HTLCs. We stall at this point, in the hope that it
|
|
|
|
* won't take long! */
|
|
|
|
if (!topology_synced(ld->topology)) {
|
|
|
|
struct deferred_commitsig *d;
|
|
|
|
|
|
|
|
log_unusual(channel->log,
|
|
|
|
"Deferring incoming commit until we sync");
|
|
|
|
|
|
|
|
/* If subdaemon dies, we want to forget this. */
|
|
|
|
d = tal(channel->owner, struct deferred_commitsig);
|
|
|
|
d->channel = channel;
|
|
|
|
d->msg = tal_dup_arr(d, u8, msg, tal_count(msg), 0);
|
|
|
|
topology_add_sync_waiter(d, ld->topology,
|
|
|
|
retry_deferred_commitsig, d);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_channel_got_commitsig(msg, msg,
|
2017-06-20 07:50:03 +02:00
|
|
|
&commitnum,
|
2017-11-21 06:26:59 +01:00
|
|
|
&feerate,
|
2017-06-20 07:50:03 +02:00
|
|
|
&commit_sig,
|
|
|
|
&htlc_sigs,
|
|
|
|
&added,
|
2017-06-20 07:54:03 +02:00
|
|
|
&shared_secrets,
|
2017-06-20 07:50:03 +02:00
|
|
|
&fulfilled,
|
|
|
|
&failed,
|
2017-08-18 06:43:52 +02:00
|
|
|
&changed,
|
2018-02-08 02:25:12 +01:00
|
|
|
&tx)) {
|
2018-02-12 11:13:04 +01:00
|
|
|
channel_internal_error(channel,
|
2017-06-20 08:17:03 +02:00
|
|
|
"bad fromwire_channel_got_commitsig %s",
|
2018-02-12 11:13:04 +01:00
|
|
|
tal_hex(channel, msg));
|
2017-10-12 08:35:04 +02:00
|
|
|
return;
|
2017-06-20 07:45:03 +02:00
|
|
|
}
|
2019-07-30 22:04:55 +02:00
|
|
|
tx->chainparams = get_chainparams(ld);
|
2017-06-20 07:45:03 +02:00
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
log_debug(channel->log,
|
2017-06-20 07:50:03 +02:00
|
|
|
"got commitsig %"PRIu64
|
2017-11-21 06:26:59 +01:00
|
|
|
": feerate %u, %zu added, %zu fulfilled, %zu failed, %zu changed",
|
|
|
|
commitnum, feerate, tal_count(added), tal_count(fulfilled),
|
2017-06-20 07:50:03 +02:00
|
|
|
tal_count(failed), tal_count(changed));
|
|
|
|
|
|
|
|
/* New HTLCs */
|
2018-01-21 03:49:21 +01:00
|
|
|
for (i = 0; i < tal_count(added); i++) {
|
2018-02-12 11:12:55 +01:00
|
|
|
if (!channel_added_their_htlc(channel, &added[i], &shared_secrets[i]))
|
2018-01-21 03:49:21 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-06-20 07:50:03 +02:00
|
|
|
|
|
|
|
/* Save information now for fulfilled & failed HTLCs */
|
|
|
|
for (i = 0; i < tal_count(fulfilled); i++) {
|
2018-02-12 11:12:55 +01:00
|
|
|
if (!peer_fulfilled_our_htlc(channel, &fulfilled[i]))
|
2017-10-12 08:35:04 +02:00
|
|
|
return;
|
2017-06-20 07:50:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < tal_count(failed); i++) {
|
2018-02-12 11:12:55 +01:00
|
|
|
if (!peer_failed_our_htlc(channel, failed[i]))
|
2017-10-12 08:35:04 +02:00
|
|
|
return;
|
2017-06-20 07:50:03 +02:00
|
|
|
}
|
2017-06-20 07:53:03 +02:00
|
|
|
|
2017-06-20 07:50:03 +02:00
|
|
|
for (i = 0; i < tal_count(changed); i++) {
|
2018-02-12 11:13:04 +01:00
|
|
|
if (!changed_htlc(channel, &changed[i])) {
|
2018-02-12 11:13:04 +01:00
|
|
|
channel_internal_error(channel,
|
2017-06-20 08:17:03 +02:00
|
|
|
"got_commitsig: update failed");
|
2017-10-12 08:35:04 +02:00
|
|
|
return;
|
2017-06-20 07:50:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-21 06:26:59 +01:00
|
|
|
/* Update both feerates: if we're funder, REMOTE should already be
|
|
|
|
* that feerate, if we're not, we're about to ACK anyway. */
|
2018-02-19 02:06:12 +01:00
|
|
|
channel->channel_info.feerate_per_kw[LOCAL]
|
|
|
|
= channel->channel_info.feerate_per_kw[REMOTE]
|
2017-11-21 06:26:59 +01:00
|
|
|
= feerate;
|
|
|
|
|
2018-04-03 09:19:39 +02:00
|
|
|
if (feerate > channel->max_possible_feerate)
|
|
|
|
channel->max_possible_feerate = feerate;
|
|
|
|
if (feerate < channel->min_possible_feerate)
|
|
|
|
channel->min_possible_feerate = feerate;
|
|
|
|
|
2017-06-20 07:50:03 +02:00
|
|
|
/* Since we're about to send revoke, bump state again. */
|
2018-02-12 11:12:55 +01:00
|
|
|
if (!peer_sending_revocation(channel, added, fulfilled, failed, changed))
|
2017-10-12 08:35:04 +02:00
|
|
|
return;
|
2017-06-20 07:50:03 +02:00
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
if (!peer_save_commitsig_received(channel, commitnum, tx, &commit_sig))
|
2017-10-12 08:35:04 +02:00
|
|
|
return;
|
2017-06-20 08:02:03 +02:00
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
wallet_channel_save(ld->wallet, channel);
|
2018-01-13 15:33:51 +01:00
|
|
|
|
2018-02-12 11:12:55 +01:00
|
|
|
tal_free(channel->last_htlc_sigs);
|
|
|
|
channel->last_htlc_sigs = tal_steal(channel, htlc_sigs);
|
2018-02-12 11:13:04 +01:00
|
|
|
wallet_htlc_sigs_save(ld->wallet, channel->dbid,
|
2018-02-12 11:12:55 +01:00
|
|
|
channel->last_htlc_sigs);
|
2017-08-18 06:43:52 +02:00
|
|
|
|
2017-06-20 07:50:03 +02:00
|
|
|
/* Tell it we've committed, and to go ahead with revoke. */
|
|
|
|
msg = towire_channel_got_commitsig_reply(msg);
|
2018-02-12 11:12:55 +01:00
|
|
|
subd_send_msg(channel->owner, take(msg));
|
2017-06-20 07:50:03 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 07:55:03 +02:00
|
|
|
/* Shuffle them over, forgetting the ancient one. */
|
2018-02-12 11:13:04 +01:00
|
|
|
void update_per_commit_point(struct channel *channel,
|
2017-06-20 08:15:03 +02:00
|
|
|
const struct pubkey *per_commitment_point)
|
2017-06-20 07:55:03 +02:00
|
|
|
{
|
2018-02-19 02:06:12 +01:00
|
|
|
struct channel_info *ci = &channel->channel_info;
|
2017-06-20 08:15:03 +02:00
|
|
|
ci->old_remote_per_commit = ci->remote_per_commit;
|
|
|
|
ci->remote_per_commit = *per_commitment_point;
|
2017-06-20 07:55:03 +02:00
|
|
|
}
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
void peer_got_revoke(struct channel *channel, const u8 *msg)
|
2017-06-20 07:50:03 +02:00
|
|
|
{
|
2017-06-20 08:02:03 +02:00
|
|
|
u64 revokenum;
|
2018-07-09 13:17:58 +02:00
|
|
|
struct secret per_commitment_secret;
|
2017-06-20 07:55:03 +02:00
|
|
|
struct pubkey next_per_commitment_point;
|
2017-06-20 07:50:03 +02:00
|
|
|
struct changed_htlc *changed;
|
|
|
|
enum onion_type *failcodes;
|
|
|
|
size_t i;
|
2018-02-12 11:13:04 +01:00
|
|
|
struct lightningd *ld = channel->peer->ld;
|
2018-08-22 02:09:57 +02:00
|
|
|
u32 feerate;
|
2017-06-20 07:50:03 +02:00
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_channel_got_revoke(msg, msg,
|
2017-06-20 07:50:03 +02:00
|
|
|
&revokenum, &per_commitment_secret,
|
2017-06-20 07:55:03 +02:00
|
|
|
&next_per_commitment_point,
|
2018-08-22 02:09:57 +02:00
|
|
|
&feerate,
|
2017-06-20 07:50:03 +02:00
|
|
|
&changed)) {
|
2018-02-12 11:13:04 +01:00
|
|
|
channel_internal_error(channel, "bad fromwire_channel_got_revoke %s",
|
2018-02-12 11:13:04 +01:00
|
|
|
tal_hex(channel, msg));
|
2017-10-12 08:35:04 +02:00
|
|
|
return;
|
2017-06-20 07:45:03 +02:00
|
|
|
}
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
log_debug(channel->log,
|
2017-06-20 07:54:03 +02:00
|
|
|
"got revoke %"PRIu64": %zu changed",
|
|
|
|
revokenum, tal_count(changed));
|
2017-06-20 07:50:03 +02:00
|
|
|
|
|
|
|
/* Save any immediate failures for after we reply. */
|
2017-06-20 07:54:03 +02:00
|
|
|
failcodes = tal_arrz(msg, enum onion_type, tal_count(changed));
|
2017-06-20 07:50:03 +02:00
|
|
|
for (i = 0; i < tal_count(changed); i++) {
|
2017-06-20 07:54:03 +02:00
|
|
|
/* If we're doing final accept, we need to forward */
|
|
|
|
if (changed[i].newstate == RCVD_ADD_ACK_REVOCATION) {
|
2019-05-21 17:48:51 +02:00
|
|
|
if (!peer_accepted_htlc(channel, changed[i].id, false,
|
2017-06-20 07:54:03 +02:00
|
|
|
&failcodes[i]))
|
2017-10-12 08:35:04 +02:00
|
|
|
return;
|
2017-06-20 07:54:03 +02:00
|
|
|
} else {
|
2018-02-12 11:13:04 +01:00
|
|
|
if (!changed_htlc(channel, &changed[i])) {
|
2018-02-12 11:13:04 +01:00
|
|
|
channel_internal_error(channel,
|
2017-06-20 08:17:03 +02:00
|
|
|
"got_revoke: update failed");
|
2017-10-12 08:35:04 +02:00
|
|
|
return;
|
2017-06-20 07:54:03 +02:00
|
|
|
}
|
2017-06-20 07:50:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-20 07:51:03 +02:00
|
|
|
if (revokenum >= (1ULL << 48)) {
|
2018-02-12 11:13:04 +01:00
|
|
|
channel_internal_error(channel, "got_revoke: too many txs %"PRIu64,
|
2017-06-20 08:17:03 +02:00
|
|
|
revokenum);
|
2017-10-12 08:35:04 +02:00
|
|
|
return;
|
2017-06-20 07:51:03 +02:00
|
|
|
}
|
|
|
|
|
2018-02-12 11:12:55 +01:00
|
|
|
if (revokenum != revocations_received(&channel->their_shachain.chain)) {
|
2018-02-12 11:13:04 +01:00
|
|
|
channel_internal_error(channel, "got_revoke: expected %"PRIu64
|
2017-06-20 08:17:03 +02:00
|
|
|
" got %"PRIu64,
|
2018-02-12 11:12:55 +01:00
|
|
|
revocations_received(&channel->their_shachain.chain), revokenum);
|
2017-10-12 08:35:04 +02:00
|
|
|
return;
|
2017-06-20 07:51:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* - if the `per_commitment_secret` was not generated by the protocol
|
|
|
|
* in [BOLT #3](03-transactions.md#per-commitment-secret-requirements):
|
|
|
|
* - MAY fail the channel.
|
2017-06-20 07:51:03 +02:00
|
|
|
*/
|
2018-02-12 11:13:04 +01:00
|
|
|
if (!wallet_shachain_add_hash(ld->wallet,
|
2018-02-12 11:12:55 +01:00
|
|
|
&channel->their_shachain,
|
2017-07-19 16:55:47 +02:00
|
|
|
shachain_index(revokenum),
|
|
|
|
&per_commitment_secret)) {
|
2018-02-12 11:13:04 +01:00
|
|
|
channel_fail_permanent(channel,
|
2017-06-20 08:17:03 +02:00
|
|
|
"Bad per_commitment_secret %s for %"PRIu64,
|
2018-07-09 13:17:58 +02:00
|
|
|
type_to_string(msg, struct secret,
|
2017-06-20 08:17:03 +02:00
|
|
|
&per_commitment_secret),
|
|
|
|
revokenum);
|
2017-10-12 08:35:04 +02:00
|
|
|
return;
|
2017-06-20 07:51:03 +02:00
|
|
|
}
|
|
|
|
|
2018-08-22 02:09:57 +02:00
|
|
|
/* Update feerate: if we are funder, their revoke_and_ack has set
|
|
|
|
* this for local feerate. */
|
|
|
|
channel->channel_info.feerate_per_kw[LOCAL] = feerate;
|
|
|
|
|
2017-06-20 07:55:03 +02:00
|
|
|
/* FIXME: Check per_commitment_secret -> per_commit_point */
|
2018-02-12 11:13:04 +01:00
|
|
|
update_per_commit_point(channel, &next_per_commitment_point);
|
2017-06-20 07:55:03 +02:00
|
|
|
|
2017-06-20 07:50:03 +02:00
|
|
|
/* Tell it we've committed, and to go ahead with revoke. */
|
|
|
|
msg = towire_channel_got_revoke_reply(msg);
|
2018-02-12 11:12:55 +01:00
|
|
|
subd_send_msg(channel->owner, take(msg));
|
2017-06-20 07:45:03 +02:00
|
|
|
|
2017-06-20 07:50:03 +02:00
|
|
|
/* Now, any HTLCs we need to immediately fail? */
|
2017-06-20 07:54:03 +02:00
|
|
|
for (i = 0; i < tal_count(changed); i++) {
|
2017-06-20 07:53:03 +02:00
|
|
|
struct htlc_in *hin;
|
2017-06-20 07:50:03 +02:00
|
|
|
|
|
|
|
if (!failcodes[i])
|
|
|
|
continue;
|
|
|
|
|
2017-11-28 06:04:20 +01:00
|
|
|
/* These are all errors before finding next hop. */
|
|
|
|
assert(!(failcodes[i] & UPDATE));
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
hin = find_htlc_in(&ld->htlcs_in, channel, changed[i].id);
|
2017-11-28 06:04:20 +01:00
|
|
|
local_fail_htlc(hin, failcodes[i], NULL);
|
2019-04-15 16:27:22 +02:00
|
|
|
// in fact, now we don't know if this htlc is a forward or localpay!
|
|
|
|
wallet_forwarded_payment_add(ld->wallet,
|
|
|
|
hin, NULL,
|
|
|
|
FORWARD_LOCAL_FAILED,
|
|
|
|
hin->failcode);
|
2017-06-20 07:50:03 +02:00
|
|
|
}
|
2018-02-12 11:13:04 +01:00
|
|
|
wallet_channel_save(ld->wallet, channel);
|
2017-06-20 07:45:03 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 08:04:03 +02:00
|
|
|
static void add_htlc(struct added_htlc **htlcs,
|
|
|
|
enum htlc_state **htlc_states,
|
|
|
|
u64 id,
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat amount,
|
2017-06-20 08:04:03 +02:00
|
|
|
const struct sha256 *payment_hash,
|
|
|
|
u32 cltv_expiry,
|
|
|
|
const u8 onion_routing_packet[TOTAL_PACKET_SIZE],
|
|
|
|
enum htlc_state state)
|
|
|
|
{
|
2019-01-15 04:51:27 +01:00
|
|
|
struct added_htlc a;
|
|
|
|
|
|
|
|
a.id = id;
|
2019-02-21 04:45:55 +01:00
|
|
|
a.amount = amount;
|
2019-01-15 04:51:27 +01:00
|
|
|
a.payment_hash = *payment_hash;
|
|
|
|
a.cltv_expiry = cltv_expiry;
|
|
|
|
memcpy(a.onion_routing_packet, onion_routing_packet,
|
|
|
|
sizeof(a.onion_routing_packet));
|
|
|
|
|
|
|
|
tal_arr_expand(htlcs, a);
|
|
|
|
tal_arr_expand(htlc_states, state);
|
2017-06-20 08:04:03 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 08:06:03 +02:00
|
|
|
static void add_fulfill(u64 id, enum side side,
|
|
|
|
const struct preimage *payment_preimage,
|
|
|
|
struct fulfilled_htlc **fulfilled_htlcs,
|
|
|
|
enum side **fulfilled_sides)
|
|
|
|
{
|
2019-01-15 04:51:27 +01:00
|
|
|
struct fulfilled_htlc f;
|
|
|
|
|
|
|
|
f.id = id;
|
|
|
|
f.payment_preimage = *payment_preimage;
|
|
|
|
|
|
|
|
tal_arr_expand(fulfilled_htlcs, f);
|
|
|
|
tal_arr_expand(fulfilled_sides, side);
|
2017-06-20 08:06:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void add_fail(u64 id, enum side side,
|
2018-07-02 06:30:22 +02:00
|
|
|
enum onion_type failcode,
|
2018-07-26 23:23:37 +02:00
|
|
|
const struct short_channel_id *failing_channel,
|
2017-06-20 08:06:03 +02:00
|
|
|
const u8 *failuremsg,
|
2018-02-08 02:24:46 +01:00
|
|
|
const struct failed_htlc ***failed_htlcs,
|
2017-06-20 08:06:03 +02:00
|
|
|
enum side **failed_sides)
|
|
|
|
{
|
2018-09-27 02:19:24 +02:00
|
|
|
struct failed_htlc *newf;
|
2017-06-20 08:06:03 +02:00
|
|
|
|
2018-09-27 02:19:24 +02:00
|
|
|
newf = tal(*failed_htlcs, struct failed_htlc);
|
|
|
|
newf->id = id;
|
|
|
|
newf->failcode = failcode;
|
2018-07-26 23:23:37 +02:00
|
|
|
if (failcode & UPDATE) {
|
|
|
|
assert(failing_channel);
|
2018-09-27 02:19:24 +02:00
|
|
|
newf->scid = tal_dup(newf, struct short_channel_id,
|
2018-07-26 23:23:37 +02:00
|
|
|
failing_channel);
|
|
|
|
} else
|
2018-09-27 02:19:24 +02:00
|
|
|
newf->scid = NULL;
|
2018-07-26 23:23:37 +02:00
|
|
|
|
2018-06-26 07:08:35 +02:00
|
|
|
if (failuremsg)
|
2018-09-27 02:19:24 +02:00
|
|
|
newf->failreason
|
|
|
|
= tal_dup_arr(newf, u8, failuremsg, tal_count(failuremsg), 0);
|
2018-06-26 07:08:35 +02:00
|
|
|
else
|
2018-09-27 02:19:24 +02:00
|
|
|
newf->failreason = NULL;
|
|
|
|
|
2019-01-15 04:51:27 +01:00
|
|
|
tal_arr_expand(failed_htlcs, newf);
|
|
|
|
tal_arr_expand(failed_sides, side);
|
2017-06-20 08:06:03 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 08:04:03 +02:00
|
|
|
/* FIXME: Load direct from db. */
|
|
|
|
void peer_htlcs(const tal_t *ctx,
|
2018-02-12 11:13:04 +01:00
|
|
|
const struct channel *channel,
|
2017-06-20 08:04:03 +02:00
|
|
|
struct added_htlc **htlcs,
|
2017-06-20 08:06:03 +02:00
|
|
|
enum htlc_state **htlc_states,
|
|
|
|
struct fulfilled_htlc **fulfilled_htlcs,
|
|
|
|
enum side **fulfilled_sides,
|
2018-02-08 02:24:46 +01:00
|
|
|
const struct failed_htlc ***failed_htlcs,
|
2017-06-20 08:06:03 +02:00
|
|
|
enum side **failed_sides)
|
2017-06-20 08:04:03 +02:00
|
|
|
{
|
|
|
|
struct htlc_in_map_iter ini;
|
|
|
|
struct htlc_out_map_iter outi;
|
|
|
|
struct htlc_in *hin;
|
|
|
|
struct htlc_out *hout;
|
2018-02-12 11:13:04 +01:00
|
|
|
struct lightningd *ld = channel->peer->ld;
|
2017-06-20 08:04:03 +02:00
|
|
|
|
|
|
|
*htlcs = tal_arr(ctx, struct added_htlc, 0);
|
|
|
|
*htlc_states = tal_arr(ctx, enum htlc_state, 0);
|
2017-06-20 08:06:03 +02:00
|
|
|
*fulfilled_htlcs = tal_arr(ctx, struct fulfilled_htlc, 0);
|
|
|
|
*fulfilled_sides = tal_arr(ctx, enum side, 0);
|
2018-02-08 02:24:46 +01:00
|
|
|
*failed_htlcs = tal_arr(ctx, const struct failed_htlc *, 0);
|
2017-06-20 08:06:03 +02:00
|
|
|
*failed_sides = tal_arr(ctx, enum side, 0);
|
2017-06-20 08:04:03 +02:00
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
for (hin = htlc_in_map_first(&ld->htlcs_in, &ini);
|
2017-06-20 08:04:03 +02:00
|
|
|
hin;
|
2018-02-12 11:13:04 +01:00
|
|
|
hin = htlc_in_map_next(&ld->htlcs_in, &ini)) {
|
|
|
|
if (hin->key.channel != channel)
|
2017-06-20 08:04:03 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
add_htlc(htlcs, htlc_states,
|
2019-02-21 04:45:55 +01:00
|
|
|
hin->key.id, hin->msat, &hin->payment_hash,
|
2017-06-20 08:04:03 +02:00
|
|
|
hin->cltv_expiry, hin->onion_routing_packet,
|
|
|
|
hin->hstate);
|
2017-06-20 08:06:03 +02:00
|
|
|
|
2018-06-26 07:08:35 +02:00
|
|
|
if (hin->failuremsg || hin->failcode)
|
|
|
|
add_fail(hin->key.id, REMOTE, hin->failcode,
|
2018-07-26 23:23:37 +02:00
|
|
|
&hin->failoutchannel,
|
2018-06-26 07:08:35 +02:00
|
|
|
hin->failuremsg, failed_htlcs, failed_sides);
|
2017-06-20 08:06:03 +02:00
|
|
|
if (hin->preimage)
|
|
|
|
add_fulfill(hin->key.id, REMOTE, hin->preimage,
|
|
|
|
fulfilled_htlcs, fulfilled_sides);
|
2017-06-20 08:04:03 +02:00
|
|
|
}
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
for (hout = htlc_out_map_first(&ld->htlcs_out, &outi);
|
2017-06-20 08:04:03 +02:00
|
|
|
hout;
|
2018-02-12 11:13:04 +01:00
|
|
|
hout = htlc_out_map_next(&ld->htlcs_out, &outi)) {
|
|
|
|
if (hout->key.channel != channel)
|
2017-06-20 08:04:03 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
add_htlc(htlcs, htlc_states,
|
2019-02-21 04:45:55 +01:00
|
|
|
hout->key.id, hout->msat, &hout->payment_hash,
|
2017-06-20 08:04:03 +02:00
|
|
|
hout->cltv_expiry, hout->onion_routing_packet,
|
|
|
|
hout->hstate);
|
2017-06-20 08:06:03 +02:00
|
|
|
|
2018-06-26 07:08:35 +02:00
|
|
|
if (hout->failuremsg || hout->failcode)
|
|
|
|
add_fail(hout->key.id, LOCAL, hout->failcode,
|
2018-07-26 23:23:37 +02:00
|
|
|
hout->key.channel->scid,
|
2018-06-26 07:08:35 +02:00
|
|
|
hout->failuremsg, failed_htlcs, failed_sides);
|
2017-06-20 08:06:03 +02:00
|
|
|
if (hout->preimage)
|
|
|
|
add_fulfill(hout->key.id, LOCAL, hout->preimage,
|
|
|
|
fulfilled_htlcs, fulfilled_sides);
|
2017-06-20 08:04:03 +02:00
|
|
|
}
|
|
|
|
}
|
2017-11-02 00:53:19 +01:00
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
/* If channel is NULL, free them all (for shutdown) */
|
|
|
|
void free_htlcs(struct lightningd *ld, const struct channel *channel)
|
|
|
|
{
|
|
|
|
struct htlc_out_map_iter outi;
|
|
|
|
struct htlc_out *hout;
|
|
|
|
struct htlc_in_map_iter ini;
|
|
|
|
struct htlc_in *hin;
|
|
|
|
bool deleted;
|
|
|
|
|
|
|
|
/* FIXME: Implement check_htlcs to ensure no dangling hout->in ptrs! */
|
|
|
|
|
|
|
|
do {
|
|
|
|
deleted = false;
|
|
|
|
for (hout = htlc_out_map_first(&ld->htlcs_out, &outi);
|
|
|
|
hout;
|
|
|
|
hout = htlc_out_map_next(&ld->htlcs_out, &outi)) {
|
|
|
|
if (channel && hout->key.channel != channel)
|
|
|
|
continue;
|
|
|
|
tal_free(hout);
|
|
|
|
deleted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (hin = htlc_in_map_first(&ld->htlcs_in, &ini);
|
|
|
|
hin;
|
|
|
|
hin = htlc_in_map_next(&ld->htlcs_in, &ini)) {
|
|
|
|
if (channel && hin->key.channel != channel)
|
|
|
|
continue;
|
|
|
|
tal_free(hin);
|
|
|
|
deleted = true;
|
|
|
|
}
|
|
|
|
/* Can skip over elements due to iterating while deleting. */
|
|
|
|
} while (deleted);
|
|
|
|
}
|
|
|
|
|
2017-11-02 00:54:00 +01:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* 2. the deadline for offered HTLCs: the deadline after which the channel has
|
|
|
|
* to be failed and timed out on-chain. This is `G` blocks after the HTLC's
|
|
|
|
* `cltv_expiry`: 1 block is reasonable.
|
2017-11-02 00:54:00 +01:00
|
|
|
*/
|
|
|
|
static u32 htlc_out_deadline(const struct htlc_out *hout)
|
2017-11-02 00:53:19 +01:00
|
|
|
{
|
2017-11-02 00:54:00 +01:00
|
|
|
return hout->cltv_expiry + 1;
|
2017-11-02 00:53:19 +01:00
|
|
|
}
|
|
|
|
|
2017-11-02 00:56:07 +01:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* 3. the deadline for received HTLCs this node has fulfilled: the deadline
|
|
|
|
* after which the channel has to be failed and the HTLC fulfilled on-chain
|
|
|
|
* before its `cltv_expiry`. See steps 4-7 above, which imply a deadline of
|
|
|
|
* `2R+G+S` blocks before `cltv_expiry`: 7 blocks is reasonable.
|
2017-11-02 00:56:07 +01:00
|
|
|
*/
|
|
|
|
/* We approximate this, by using half the cltv_expiry_delta (3R+2G+2S),
|
|
|
|
* rounded up. */
|
|
|
|
static u32 htlc_in_deadline(const struct lightningd *ld,
|
|
|
|
const struct htlc_in *hin)
|
|
|
|
{
|
|
|
|
return hin->cltv_expiry - (ld->config.cltv_expiry_delta + 1)/2;
|
|
|
|
}
|
|
|
|
|
2018-05-06 15:32:01 +02:00
|
|
|
void htlcs_notify_new_block(struct lightningd *ld, u32 height)
|
2017-11-02 00:54:00 +01:00
|
|
|
{
|
|
|
|
bool removed;
|
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* - if an HTLC which it offered is in either node's current
|
|
|
|
* commitment transaction, AND is past this timeout deadline:
|
|
|
|
* - MUST fail the channel.
|
2017-11-02 00:54:00 +01:00
|
|
|
*/
|
|
|
|
/* FIXME: use db to look this up in one go (earliest deadline per-peer) */
|
|
|
|
do {
|
|
|
|
struct htlc_out *hout;
|
|
|
|
struct htlc_out_map_iter outi;
|
|
|
|
|
|
|
|
removed = false;
|
|
|
|
|
|
|
|
for (hout = htlc_out_map_first(&ld->htlcs_out, &outi);
|
|
|
|
hout;
|
|
|
|
hout = htlc_out_map_next(&ld->htlcs_out, &outi)) {
|
|
|
|
/* Not timed out yet? */
|
|
|
|
if (height < htlc_out_deadline(hout))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Peer on chain already? */
|
2018-02-12 11:13:04 +01:00
|
|
|
if (channel_on_chain(hout->key.channel))
|
2017-11-02 00:54:00 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Peer already failed, or we hit it? */
|
2018-02-12 11:13:04 +01:00
|
|
|
if (hout->key.channel->error)
|
2017-11-02 00:54:00 +01:00
|
|
|
continue;
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
channel_fail_permanent(hout->key.channel,
|
2018-01-03 06:59:35 +01:00
|
|
|
"Offered HTLC %"PRIu64
|
|
|
|
" %s cltv %u hit deadline",
|
|
|
|
hout->key.id,
|
|
|
|
htlc_state_name(hout->hstate),
|
|
|
|
hout->cltv_expiry);
|
2017-11-02 00:54:00 +01:00
|
|
|
removed = true;
|
|
|
|
}
|
|
|
|
/* Iteration while removing is safe, but can skip entries! */
|
|
|
|
} while (removed);
|
2017-11-02 00:56:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* - for each HTLC it is attempting to fulfill:
|
|
|
|
* - MUST estimate a fulfillment deadline.
|
|
|
|
*...
|
|
|
|
* - if an HTLC it has fulfilled is in either node's current commitment
|
|
|
|
* transaction, AND is past this fulfillment deadline:
|
2019-07-10 01:58:57 +02:00
|
|
|
* - MUST fail the channel.
|
2017-11-02 00:56:07 +01:00
|
|
|
*/
|
|
|
|
do {
|
|
|
|
struct htlc_in *hin;
|
|
|
|
struct htlc_in_map_iter ini;
|
|
|
|
|
|
|
|
removed = false;
|
|
|
|
|
|
|
|
for (hin = htlc_in_map_first(&ld->htlcs_in, &ini);
|
|
|
|
hin;
|
|
|
|
hin = htlc_in_map_next(&ld->htlcs_in, &ini)) {
|
2018-02-12 11:13:04 +01:00
|
|
|
struct channel *channel = hin->key.channel;
|
2018-02-12 11:13:04 +01:00
|
|
|
|
2017-11-02 00:56:07 +01:00
|
|
|
/* Not fulfilled? If overdue, that's their problem... */
|
|
|
|
if (!hin->preimage)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Not timed out yet? */
|
|
|
|
if (height < htlc_in_deadline(ld, hin))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Peer on chain already? */
|
2018-02-12 11:13:04 +01:00
|
|
|
if (channel_on_chain(channel))
|
2017-11-02 00:56:07 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Peer already failed, or we hit it? */
|
2018-02-12 11:13:04 +01:00
|
|
|
if (channel->error)
|
2017-11-02 00:56:07 +01:00
|
|
|
continue;
|
|
|
|
|
2018-02-12 11:13:04 +01:00
|
|
|
channel_fail_permanent(channel,
|
2018-01-03 06:59:35 +01:00
|
|
|
"Fulfilled HTLC %"PRIu64
|
|
|
|
" %s cltv %u hit deadline",
|
|
|
|
hin->key.id,
|
|
|
|
htlc_state_name(hin->hstate),
|
|
|
|
hin->cltv_expiry);
|
2017-11-02 00:56:07 +01:00
|
|
|
removed = true;
|
|
|
|
}
|
|
|
|
/* Iteration while removing is safe, but can skip entries! */
|
|
|
|
} while (removed);
|
2017-11-02 00:54:00 +01:00
|
|
|
}
|
2017-11-21 04:34:36 +01:00
|
|
|
|
2018-10-09 10:56:52 +02:00
|
|
|
#ifdef COMPAT_V061
|
2018-10-09 10:54:52 +02:00
|
|
|
static void fixup_hout(struct lightningd *ld, struct htlc_out *hout)
|
|
|
|
{
|
|
|
|
const char *fix;
|
|
|
|
|
|
|
|
/* We didn't save HTLC failure information to the database. So when
|
|
|
|
* busy nodes restarted (y'know, our most important users!) they would
|
|
|
|
* find themselves with missing fields.
|
|
|
|
*
|
|
|
|
* Fortunately, most of the network is honest: re-sending an old HTLC
|
|
|
|
* just causes failure (though we assert() when we try to push the
|
|
|
|
* failure to the incoming HTLC which has already succeeded!).
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* We care about HTLCs being removed only, not those being added. */
|
|
|
|
if (hout->hstate < RCVD_REMOVE_HTLC)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Successful ones are fine. */
|
|
|
|
if (hout->preimage)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Failed ones (only happens after db fixed!) OK. */
|
|
|
|
if (hout->failcode || hout->failuremsg)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* payment_preimage for HTLC in *was* stored, so look for that. */
|
|
|
|
if (hout->in && hout->in->preimage) {
|
|
|
|
hout->preimage = tal_dup(hout, struct preimage,
|
|
|
|
hout->in->preimage);
|
|
|
|
fix = "restoring preimage from incoming HTLC";
|
|
|
|
} else {
|
|
|
|
hout->failcode = WIRE_TEMPORARY_CHANNEL_FAILURE;
|
|
|
|
fix = "subsituting temporary channel failure";
|
|
|
|
}
|
|
|
|
|
|
|
|
log_broken(ld->log, "HTLC #%"PRIu64" (%s) "
|
2019-02-21 04:45:55 +01:00
|
|
|
" for amount %s"
|
2018-10-09 10:54:52 +02:00
|
|
|
" to %s"
|
|
|
|
" is missing a resolution: %s.",
|
|
|
|
hout->key.id, htlc_state_name(hout->hstate),
|
2019-02-21 04:45:55 +01:00
|
|
|
type_to_string(tmpctx, struct amount_msat, &hout->msat),
|
2019-04-08 11:58:32 +02:00
|
|
|
type_to_string(tmpctx, struct node_id,
|
2018-10-09 10:54:52 +02:00
|
|
|
&hout->key.channel->peer->id),
|
|
|
|
fix);
|
|
|
|
}
|
2018-10-09 10:56:52 +02:00
|
|
|
#endif /* COMPAT_V061 */
|
2018-10-09 10:54:52 +02:00
|
|
|
|
2018-09-03 03:08:53 +02:00
|
|
|
/**
|
|
|
|
* htlcs_reconnect -- Link outgoing HTLCs to their origins after initial db load
|
|
|
|
*
|
|
|
|
* For each outgoing HTLC find the incoming HTLC that triggered it. If
|
|
|
|
* we are the origin of the transfer then we cannot resolve the
|
|
|
|
* incoming HTLC in which case we just leave it `NULL`.
|
2019-08-10 07:24:57 +02:00
|
|
|
*
|
|
|
|
* Returns a map of any htlcs we need to retry.
|
2018-09-03 03:08:53 +02:00
|
|
|
*/
|
2019-08-10 07:24:57 +02:00
|
|
|
struct htlc_in_map *htlcs_reconnect(struct lightningd *ld,
|
|
|
|
struct htlc_in_map *htlcs_in,
|
|
|
|
struct htlc_out_map *htlcs_out)
|
2018-09-03 03:08:53 +02:00
|
|
|
{
|
|
|
|
struct htlc_in_map_iter ini;
|
|
|
|
struct htlc_out_map_iter outi;
|
|
|
|
struct htlc_in *hin;
|
|
|
|
struct htlc_out *hout;
|
2019-08-10 07:24:57 +02:00
|
|
|
struct htlc_in_map *unprocessed = tal(NULL, struct htlc_in_map);
|
2018-10-10 08:32:46 +02:00
|
|
|
|
|
|
|
/* Any HTLCs which happened to be incoming and weren't forwarded before
|
|
|
|
* we shutdown/crashed: fail them now.
|
|
|
|
*
|
|
|
|
* Note that since we do local processing synchronously, so this never
|
|
|
|
* captures local payments. But if it did, it would be a tiny corner
|
|
|
|
* case. */
|
2019-08-10 07:24:57 +02:00
|
|
|
htlc_in_map_init(unprocessed);
|
2018-10-10 08:32:46 +02:00
|
|
|
for (hin = htlc_in_map_first(htlcs_in, &ini); hin;
|
|
|
|
hin = htlc_in_map_next(htlcs_in, &ini)) {
|
|
|
|
if (hin->hstate == RCVD_ADD_ACK_REVOCATION)
|
2019-08-10 07:24:57 +02:00
|
|
|
htlc_in_map_add(unprocessed, hin);
|
2018-10-10 08:32:46 +02:00
|
|
|
}
|
2018-09-03 03:08:53 +02:00
|
|
|
|
|
|
|
for (hout = htlc_out_map_first(htlcs_out, &outi); hout;
|
|
|
|
hout = htlc_out_map_next(htlcs_out, &outi)) {
|
|
|
|
|
2018-10-09 22:21:31 +02:00
|
|
|
if (hout->am_origin) {
|
2018-09-03 03:08:53 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-10-09 10:47:52 +02:00
|
|
|
/* For fulfilled HTLCs, we fulfill incoming before outgoing is
|
|
|
|
* completely resolved, so it's possible that we don't find
|
2018-10-09 10:50:52 +02:00
|
|
|
* the incoming. */
|
2018-09-03 03:08:53 +02:00
|
|
|
for (hin = htlc_in_map_first(htlcs_in, &ini); hin;
|
|
|
|
hin = htlc_in_map_next(htlcs_in, &ini)) {
|
|
|
|
if (hout->origin_htlc_id == hin->dbid) {
|
|
|
|
log_debug(ld->log,
|
|
|
|
"Found corresponding htlc_in %" PRIu64
|
|
|
|
" for htlc_out %" PRIu64,
|
|
|
|
hin->dbid, hout->dbid);
|
2018-10-09 10:48:52 +02:00
|
|
|
htlc_out_connect_htlc_in(hout, hin);
|
2018-09-03 03:08:53 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-10-09 10:50:52 +02:00
|
|
|
|
|
|
|
if (!hout->in && !hout->preimage) {
|
2018-10-09 10:51:52 +02:00
|
|
|
#ifdef COMPAT_V061
|
2018-10-09 10:50:52 +02:00
|
|
|
log_broken(ld->log,
|
|
|
|
"Missing preimage for orphaned HTLC; replacing with zeros");
|
|
|
|
hout->preimage = talz(hout, struct preimage);
|
2018-10-09 10:51:52 +02:00
|
|
|
#else
|
|
|
|
fatal("Unable to find corresponding htlc_in %"PRIu64
|
|
|
|
" for unfulfilled htlc_out %"PRIu64,
|
|
|
|
hout->origin_htlc_id, hout->dbid);
|
|
|
|
#endif
|
2018-10-09 10:50:52 +02:00
|
|
|
}
|
2018-10-09 10:56:52 +02:00
|
|
|
#ifdef COMPAT_V061
|
2018-10-09 10:54:52 +02:00
|
|
|
fixup_hout(ld, hout);
|
2018-10-09 10:56:52 +02:00
|
|
|
#endif
|
2018-10-09 10:54:52 +02:00
|
|
|
|
2018-10-10 08:32:46 +02:00
|
|
|
if (hout->in)
|
2019-08-10 07:24:57 +02:00
|
|
|
htlc_in_map_del(unprocessed, hout->in);
|
2018-09-03 03:08:53 +02:00
|
|
|
}
|
2018-10-10 08:32:46 +02:00
|
|
|
|
2019-08-10 07:24:57 +02:00
|
|
|
return unprocessed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void htlcs_resubmit(struct lightningd *ld, struct htlc_in_map *unprocessed)
|
|
|
|
{
|
|
|
|
struct htlc_in *hin;
|
|
|
|
struct htlc_in_map_iter ini;
|
|
|
|
enum onion_type failcode COMPILER_WANTS_INIT("gcc7.4.0 bad, 8.3 OK");
|
|
|
|
|
2018-10-10 08:32:46 +02:00
|
|
|
/* Now fail any which were stuck. */
|
2019-08-10 07:24:57 +02:00
|
|
|
for (hin = htlc_in_map_first(unprocessed, &ini);
|
|
|
|
hin;
|
|
|
|
hin = htlc_in_map_next(unprocessed, &ini)) {
|
2018-10-10 08:32:46 +02:00
|
|
|
log_unusual(hin->key.channel->log,
|
2019-05-21 14:10:34 +02:00
|
|
|
"Replaying old unprocessed HTLC #%"PRIu64,
|
2018-10-10 08:32:46 +02:00
|
|
|
hin->key.id);
|
2019-05-21 17:48:51 +02:00
|
|
|
if (!peer_accepted_htlc(hin->key.channel, hin->key.id, true, &failcode)) {
|
2019-05-21 14:10:34 +02:00
|
|
|
fail_in_htlc(hin,
|
|
|
|
failcode != 0
|
|
|
|
? failcode
|
|
|
|
: WIRE_TEMPORARY_NODE_FAILURE,
|
|
|
|
NULL, NULL);
|
|
|
|
} else if (failcode) {
|
|
|
|
fail_in_htlc(hin, failcode, NULL, NULL);
|
|
|
|
}
|
2018-10-10 08:32:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't leak memory! */
|
2019-08-10 07:24:57 +02:00
|
|
|
htlc_in_map_clear(unprocessed);
|
|
|
|
tal_free(unprocessed);
|
2018-09-03 03:08:53 +02:00
|
|
|
}
|
|
|
|
|
2018-04-03 09:19:42 +02:00
|
|
|
#if DEVELOPER
|
2018-12-16 05:52:06 +01:00
|
|
|
static struct command_result *json_dev_ignore_htlcs(struct command *cmd,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *obj UNNEEDED,
|
|
|
|
const jsmntok_t *params)
|
2018-04-03 09:19:42 +02:00
|
|
|
{
|
2019-04-08 11:58:32 +02:00
|
|
|
struct node_id *peerid;
|
2018-04-03 09:19:42 +02:00
|
|
|
struct peer *peer;
|
2018-08-13 22:31:40 +02:00
|
|
|
bool *ignore;
|
2018-04-03 09:19:42 +02:00
|
|
|
|
2018-07-20 03:14:02 +02:00
|
|
|
if (!param(cmd, buffer, params,
|
2019-04-08 11:58:32 +02:00
|
|
|
p_req("id", param_node_id, &peerid),
|
2018-12-16 05:50:06 +01:00
|
|
|
p_req("ignore", param_bool, &ignore),
|
2018-07-20 03:14:02 +02:00
|
|
|
NULL))
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_param_failed();
|
2018-04-03 09:19:42 +02:00
|
|
|
|
2018-08-14 23:19:31 +02:00
|
|
|
peer = peer_by_id(cmd->ld, peerid);
|
2018-04-03 09:19:42 +02:00
|
|
|
if (!peer) {
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_fail(cmd, LIGHTNINGD,
|
|
|
|
"Could not find channel with that peer");
|
2018-04-03 09:19:42 +02:00
|
|
|
}
|
2018-08-13 22:31:40 +02:00
|
|
|
peer->ignore_htlcs = *ignore;
|
2018-04-03 09:19:42 +02:00
|
|
|
|
2019-06-12 02:38:54 +02:00
|
|
|
return command_success(cmd, json_stream_success(cmd));
|
2018-04-03 09:19:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct json_command dev_ignore_htlcs = {
|
2019-05-22 16:08:16 +02:00
|
|
|
"dev-ignore-htlcs",
|
|
|
|
"developer",
|
|
|
|
json_dev_ignore_htlcs,
|
2018-04-03 09:19:42 +02:00
|
|
|
"Set ignoring incoming HTLCs for peer {id} to {ignore}", false,
|
|
|
|
"Set/unset ignoring of all incoming HTLCs. For testing only."
|
|
|
|
};
|
|
|
|
AUTODATA(json_command, &dev_ignore_htlcs);
|
|
|
|
#endif /* DEVELOPER */
|
2018-10-17 20:11:04 +02:00
|
|
|
|
2019-07-08 13:36:47 +02:00
|
|
|
/* Warp this process to ensure the consistent json object structure
|
|
|
|
* between 'listforwards' API and 'forward_event' notification. */
|
|
|
|
void json_format_forwarding_object(struct json_stream *response,
|
|
|
|
const char *fieldname,
|
|
|
|
const struct forwarding *cur)
|
2018-10-17 20:11:04 +02:00
|
|
|
{
|
2019-07-08 13:36:47 +02:00
|
|
|
json_object_start(response, fieldname);
|
|
|
|
|
2019-08-15 02:31:23 +02:00
|
|
|
/* See 6d333f16cc0f3aac7097269bf0985b5fa06d59b4: we may have deleted HTLC. */
|
2019-08-14 20:05:51 +02:00
|
|
|
if (cur->payment_hash)
|
2019-08-10 10:39:04 +02:00
|
|
|
json_add_sha256(response, "payment_hash", cur->payment_hash);
|
2019-07-08 13:36:47 +02:00
|
|
|
json_add_short_channel_id(response, "in_channel", &cur->channel_in);
|
2019-08-15 02:31:23 +02:00
|
|
|
|
|
|
|
/* This can be unknown if we failed before channel lookup */
|
|
|
|
if (cur->channel_out.u64 != 0 || deprecated_apis)
|
|
|
|
json_add_short_channel_id(response, "out_channel",
|
|
|
|
&cur->channel_out);
|
2019-07-08 13:36:47 +02:00
|
|
|
json_add_amount_msat_compat(response,
|
|
|
|
cur->msat_in,
|
|
|
|
"in_msatoshi", "in_msat");
|
2019-08-15 02:31:23 +02:00
|
|
|
|
|
|
|
/* These can be unset (aka zero) if we failed before channel lookup */
|
|
|
|
if (cur->channel_out.u64 != 0 || deprecated_apis) {
|
|
|
|
json_add_amount_msat_compat(response,
|
|
|
|
cur->msat_out,
|
|
|
|
"out_msatoshi", "out_msat");
|
|
|
|
json_add_amount_msat_compat(response,
|
|
|
|
cur->fee,
|
|
|
|
"fee", "fee_msat");
|
|
|
|
}
|
2019-07-08 13:36:47 +02:00
|
|
|
json_add_string(response, "status", forward_status_name(cur->status));
|
|
|
|
|
|
|
|
if(cur->failcode != 0) {
|
|
|
|
json_add_num(response, "failcode", cur->failcode);
|
|
|
|
json_add_string(response, "failreason",
|
|
|
|
onion_type_name(cur->failcode));
|
|
|
|
}
|
2019-04-26 09:19:04 +02:00
|
|
|
|
2019-04-03 21:28:33 +02:00
|
|
|
#ifdef COMPAT_V070
|
|
|
|
/* If a forwarding doesn't have received_time it was created
|
|
|
|
* before we added the tracking, do not include it here. */
|
2019-07-08 13:36:47 +02:00
|
|
|
if (cur->received_time.ts.tv_sec) {
|
2019-04-03 21:28:33 +02:00
|
|
|
json_add_timeabs(response, "received_time", cur->received_time);
|
|
|
|
if (cur->resolved_time)
|
|
|
|
json_add_timeabs(response, "resolved_time", *cur->resolved_time);
|
2019-07-08 13:36:47 +02:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
json_add_timeabs(response, "received_time", cur->received_time);
|
|
|
|
if (cur->resolved_time)
|
|
|
|
json_add_timeabs(response, "resolved_time", *cur->resolved_time);
|
2019-04-03 21:28:33 +02:00
|
|
|
#endif
|
2019-07-08 13:36:47 +02:00
|
|
|
json_object_end(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void listforwardings_add_forwardings(struct json_stream *response, struct wallet *wallet)
|
|
|
|
{
|
|
|
|
const struct forwarding *forwardings;
|
|
|
|
forwardings = wallet_forwarded_payments_get(wallet, tmpctx);
|
|
|
|
|
|
|
|
json_array_start(response, "forwards");
|
|
|
|
for (size_t i=0; i<tal_count(forwardings); i++) {
|
|
|
|
const struct forwarding *cur = &forwardings[i];
|
|
|
|
json_format_forwarding_object(response, NULL, cur);
|
2018-10-17 20:11:04 +02:00
|
|
|
}
|
|
|
|
json_array_end(response);
|
|
|
|
|
|
|
|
tal_free(forwardings);
|
|
|
|
}
|
|
|
|
|
2018-12-16 05:52:06 +01:00
|
|
|
static struct command_result *json_listforwards(struct command *cmd,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *obj UNNEEDED,
|
|
|
|
const jsmntok_t *params)
|
2018-10-17 20:11:04 +02:00
|
|
|
{
|
2018-10-20 04:32:29 +02:00
|
|
|
struct json_stream *response;
|
2018-10-17 20:11:04 +02:00
|
|
|
|
2018-10-18 14:06:29 +02:00
|
|
|
if (!param(cmd, buffer, params, NULL))
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_param_failed();
|
2018-10-17 20:11:04 +02:00
|
|
|
|
2018-10-20 04:32:29 +02:00
|
|
|
response = json_stream_success(cmd);
|
2018-10-18 14:06:29 +02:00
|
|
|
listforwardings_add_forwardings(response, cmd->ld->wallet);
|
2018-10-17 20:11:04 +02:00
|
|
|
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_success(cmd, response);
|
2018-10-17 20:11:04 +02:00
|
|
|
}
|
|
|
|
|
2018-10-18 14:06:29 +02:00
|
|
|
static const struct json_command listforwards_command = {
|
2019-05-22 16:08:16 +02:00
|
|
|
"listforwards",
|
|
|
|
"channels",
|
|
|
|
json_listforwards,
|
2018-10-18 14:06:29 +02:00
|
|
|
"List all forwarded payments and their information", false,
|
|
|
|
"List all forwarded payments and their information"
|
2018-10-17 20:11:04 +02:00
|
|
|
};
|
2018-10-18 14:06:29 +02:00
|
|
|
AUTODATA(json_command, &listforwards_command);
|