2019-12-12 00:55:45 +01:00
|
|
|
#include <common/timeout.h>
|
|
|
|
#include <lightningd/htlc_end.h>
|
|
|
|
#include <lightningd/htlc_set.h>
|
|
|
|
#include <lightningd/lightningd.h>
|
|
|
|
#include <lightningd/peer_htlcs.h>
|
|
|
|
|
|
|
|
/* If an HTLC times out, we need to free entire set, since we could be processing
|
|
|
|
* it in invoice.c right now. */
|
|
|
|
static void htlc_set_hin_destroyed(struct htlc_in *hin,
|
|
|
|
struct htlc_set *set)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < tal_count(set->htlcs); i++) {
|
|
|
|
if (set->htlcs[i] == hin) {
|
|
|
|
/* Don't try to re-fail this HTLC! */
|
|
|
|
tal_arr_remove(&set->htlcs, i);
|
|
|
|
/* Kind of the correct failure code. */
|
2020-02-21 06:08:39 +01:00
|
|
|
htlc_set_fail(set, take(towire_mpp_timeout(NULL)));
|
2019-12-12 00:55:45 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy_htlc_set(struct htlc_set *set,
|
|
|
|
struct htlc_set_map *map)
|
|
|
|
{
|
|
|
|
htlc_set_map_del(map, set);
|
|
|
|
}
|
|
|
|
|
2020-01-31 02:40:36 +01:00
|
|
|
/* BOLT #4:
|
2019-12-12 00:55:45 +01:00
|
|
|
* - MUST fail all HTLCs in the HTLC set after some reasonable
|
|
|
|
* timeout.
|
2020-01-31 02:40:36 +01:00
|
|
|
*...
|
2019-12-12 00:55:45 +01:00
|
|
|
* - SHOULD use `mpp_timeout` for the failure message.
|
|
|
|
*/
|
|
|
|
static void timeout_htlc_set(struct htlc_set *set)
|
|
|
|
{
|
2020-02-21 06:08:39 +01:00
|
|
|
htlc_set_fail(set, take(towire_mpp_timeout(NULL)));
|
2019-12-12 00:55:45 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 06:08:39 +01:00
|
|
|
void htlc_set_fail(struct htlc_set *set, const u8 *failmsg TAKES)
|
2019-12-12 00:55:45 +01:00
|
|
|
{
|
2020-02-21 06:08:39 +01:00
|
|
|
/* Don't let local_fail_in_htlc take! */
|
|
|
|
if (taken(failmsg))
|
|
|
|
tal_steal(set, failmsg);
|
|
|
|
|
2019-12-12 00:55:45 +01:00
|
|
|
for (size_t i = 0; i < tal_count(set->htlcs); i++) {
|
|
|
|
/* Don't remove from set */
|
|
|
|
tal_del_destructor2(set->htlcs[i], htlc_set_hin_destroyed, set);
|
2020-02-21 06:08:39 +01:00
|
|
|
local_fail_in_htlc(set->htlcs[i], failmsg);
|
2019-12-12 00:55:45 +01:00
|
|
|
}
|
|
|
|
tal_free(set);
|
|
|
|
}
|
|
|
|
|
|
|
|
void htlc_set_fulfill(struct htlc_set *set, const struct preimage *preimage)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < tal_count(set->htlcs); i++) {
|
|
|
|
/* Don't remove from set */
|
|
|
|
tal_del_destructor2(set->htlcs[i], htlc_set_hin_destroyed, set);
|
2020-03-19 00:28:29 +01:00
|
|
|
|
|
|
|
/* mark that we filled -- needed for tagging coin mvt */
|
2020-04-14 21:02:46 +02:00
|
|
|
set->htlcs[i]->we_filled = tal(set->htlcs[i], bool);
|
|
|
|
*set->htlcs[i]->we_filled = true;
|
2019-12-12 00:55:45 +01:00
|
|
|
fulfill_htlc(set->htlcs[i], preimage);
|
|
|
|
}
|
|
|
|
tal_free(set);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct htlc_set *new_htlc_set(struct lightningd *ld,
|
|
|
|
struct htlc_in *hin,
|
|
|
|
struct amount_msat total_msat)
|
|
|
|
{
|
|
|
|
struct htlc_set *set;
|
|
|
|
|
|
|
|
set = tal(ld, struct htlc_set);
|
|
|
|
set->total_msat = total_msat;
|
|
|
|
set->payment_hash = hin->payment_hash;
|
|
|
|
set->so_far = AMOUNT_MSAT(0);
|
|
|
|
set->htlcs = tal_arr(set, struct htlc_in *, 1);
|
|
|
|
set->htlcs[0] = hin;
|
|
|
|
|
2020-01-31 02:40:36 +01:00
|
|
|
/* BOLT #4:
|
2019-12-12 00:55:45 +01:00
|
|
|
* - MUST fail all HTLCs in the HTLC set after some reasonable
|
|
|
|
* timeout.
|
|
|
|
* - SHOULD wait for at least 60 seconds after the initial
|
|
|
|
* HTLC.
|
|
|
|
*/
|
2019-12-12 11:56:07 +01:00
|
|
|
set->timeout = new_reltimer(ld->timers, set, time_from_sec(70),
|
|
|
|
timeout_htlc_set, set);
|
2019-12-12 00:55:45 +01:00
|
|
|
htlc_set_map_add(&ld->htlc_sets, set);
|
|
|
|
tal_add_destructor2(set, destroy_htlc_set, &ld->htlc_sets);
|
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
|
|
|
void htlc_set_add(struct lightningd *ld,
|
|
|
|
struct htlc_in *hin,
|
|
|
|
struct amount_msat total_msat,
|
|
|
|
const struct secret *payment_secret)
|
|
|
|
{
|
|
|
|
struct htlc_set *set;
|
|
|
|
const struct invoice_details *details;
|
|
|
|
|
2020-01-31 02:40:36 +01:00
|
|
|
/* BOLT #4:
|
2019-12-12 00:55:45 +01:00
|
|
|
* The final node:
|
|
|
|
* - MUST fail the HTLC if dictated by Requirements under
|
|
|
|
* [Failure Messages](#failure-messages)
|
|
|
|
* - Note: "amount paid" specified there is the `total_msat` field.
|
|
|
|
*/
|
|
|
|
details = invoice_check_payment(tmpctx, ld, &hin->payment_hash,
|
|
|
|
total_msat, payment_secret);
|
|
|
|
if (!details) {
|
2020-02-21 06:08:39 +01:00
|
|
|
local_fail_in_htlc(hin,
|
2020-04-15 12:19:46 +02:00
|
|
|
take(failmsg_incorrect_or_unknown(NULL, ld, hin)));
|
2019-12-12 00:55:45 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-31 02:40:36 +01:00
|
|
|
/* BOLT #4:
|
2019-12-12 00:55:45 +01:00
|
|
|
* - otherwise, if it supports `basic_mpp`:
|
|
|
|
* - MUST add it to the HTLC set corresponding to that `payment_hash`.
|
|
|
|
*/
|
|
|
|
set = htlc_set_map_get(&ld->htlc_sets, &hin->payment_hash);
|
|
|
|
if (!set)
|
|
|
|
set = new_htlc_set(ld, hin, total_msat);
|
2019-12-12 00:59:33 +01:00
|
|
|
else {
|
2020-01-31 02:40:36 +01:00
|
|
|
/* BOLT #4:
|
2019-12-12 00:59:33 +01:00
|
|
|
*
|
|
|
|
* if it supports `basic_mpp`:
|
|
|
|
* ...
|
|
|
|
* - otherwise, if the total `amount_msat` of this HTLC set is
|
|
|
|
* less than `total_msat`:
|
|
|
|
* ...
|
|
|
|
* - MUST require `payment_secret` for all HTLCs in the set.
|
|
|
|
*/
|
|
|
|
/* We check this now, since we want to fail with this as soon
|
|
|
|
* as possible, to avoid other probing attacks. */
|
|
|
|
if (!payment_secret) {
|
2020-04-15 12:19:46 +02:00
|
|
|
local_fail_in_htlc(hin, take(failmsg_incorrect_or_unknown(NULL, ld, hin)));
|
2019-12-12 00:59:33 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-12-12 00:55:45 +01:00
|
|
|
tal_arr_expand(&set->htlcs, hin);
|
2019-12-12 00:59:33 +01:00
|
|
|
}
|
2019-12-12 00:55:45 +01:00
|
|
|
|
|
|
|
/* Remove from set should hin get destroyed somehow */
|
|
|
|
tal_add_destructor2(hin, htlc_set_hin_destroyed, set);
|
|
|
|
|
2020-01-31 02:40:36 +01:00
|
|
|
/* BOLT #4:
|
2019-12-12 00:55:45 +01:00
|
|
|
* - SHOULD fail the entire HTLC set if `total_msat` is not
|
|
|
|
* the same for all HTLCs in the set.
|
|
|
|
*/
|
|
|
|
if (!amount_msat_eq(total_msat, set->total_msat)) {
|
|
|
|
log_unusual(ld->log, "Failing HTLC set %s:"
|
|
|
|
" total_msat %s new htlc total %s",
|
|
|
|
type_to_string(tmpctx, struct sha256,
|
|
|
|
&set->payment_hash),
|
|
|
|
type_to_string(tmpctx, struct amount_msat,
|
|
|
|
&set->total_msat),
|
|
|
|
type_to_string(tmpctx, struct amount_msat,
|
|
|
|
&total_msat));
|
2020-02-21 06:08:39 +01:00
|
|
|
htlc_set_fail(set,
|
|
|
|
take(towire_final_incorrect_htlc_amount(NULL,
|
|
|
|
hin->msat)));
|
2019-12-12 00:55:45 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-31 02:40:36 +01:00
|
|
|
/* BOLT #4:
|
2019-12-12 00:55:45 +01:00
|
|
|
* - if the total `amount_msat` of this HTLC set equals `total_msat`:
|
|
|
|
* - SHOULD fulfill all HTLCs in the HTLC set
|
|
|
|
*/
|
|
|
|
if (!amount_msat_add(&set->so_far, set->so_far, hin->msat)) {
|
|
|
|
log_unusual(ld->log, "Failing HTLC set %s:"
|
|
|
|
" overflow adding %s+%s",
|
|
|
|
type_to_string(tmpctx, struct sha256,
|
|
|
|
&set->payment_hash),
|
|
|
|
type_to_string(tmpctx, struct amount_msat,
|
|
|
|
&set->so_far),
|
|
|
|
type_to_string(tmpctx, struct amount_msat,
|
|
|
|
&hin->msat));
|
2020-02-21 06:08:39 +01:00
|
|
|
htlc_set_fail(set,
|
|
|
|
take(towire_final_incorrect_htlc_amount(NULL,
|
|
|
|
hin->msat)));
|
2019-12-12 00:55:45 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-01 18:59:42 +02:00
|
|
|
log_debug(ld->log,
|
|
|
|
"HTLC set contains %zu HTLCs, for a total of %s out of %s",
|
|
|
|
tal_count(set->htlcs),
|
|
|
|
type_to_string(tmpctx, struct amount_msat, &set->so_far),
|
|
|
|
type_to_string(tmpctx, struct amount_msat, &total_msat)
|
|
|
|
);
|
|
|
|
|
2019-12-12 00:55:45 +01:00
|
|
|
if (amount_msat_eq(set->so_far, total_msat)) {
|
2019-12-12 11:56:07 +01:00
|
|
|
/* Disable timer now, in case invoice_hook is slow! */
|
|
|
|
tal_free(set->timeout);
|
2019-12-12 00:55:45 +01:00
|
|
|
invoice_try_pay(ld, set, details);
|
2019-12-12 00:55:45 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-31 02:40:36 +01:00
|
|
|
/* BOLT #4:
|
2019-12-12 00:55:45 +01:00
|
|
|
* - otherwise, if the total `amount_msat` of this HTLC set is less than
|
|
|
|
* `total_msat`:
|
2019-12-12 00:59:33 +01:00
|
|
|
* - MUST NOT fulfill any HTLCs in the HTLC set
|
|
|
|
*...
|
|
|
|
* - MUST require `payment_secret` for all HTLCs in the set. */
|
|
|
|
/* This catches the case of the first payment in a set. */
|
|
|
|
if (!payment_secret) {
|
2020-02-21 06:08:39 +01:00
|
|
|
htlc_set_fail(set,
|
2020-04-15 12:19:46 +02:00
|
|
|
take(failmsg_incorrect_or_unknown(NULL, ld, hin)));
|
2019-12-12 00:59:33 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-12-12 00:55:45 +01:00
|
|
|
}
|