2019-12-12 00:55:45 +01:00
|
|
|
#ifndef LIGHTNING_LIGHTNINGD_HTLC_SET_H
|
|
|
|
#define LIGHTNING_LIGHTNINGD_HTLC_SET_H
|
|
|
|
#include "config.h"
|
|
|
|
#include <ccan/crypto/sha256/sha256.h>
|
|
|
|
#include <ccan/crypto/siphash24/siphash24.h>
|
|
|
|
#include <ccan/htable/htable_type.h>
|
|
|
|
#include <common/amount.h>
|
|
|
|
#include <common/pseudorand.h>
|
|
|
|
#include <common/utils.h>
|
2020-09-07 23:06:50 +02:00
|
|
|
#include <wire/onion_wire.h>
|
2019-12-12 00:55:45 +01:00
|
|
|
|
|
|
|
struct lightningd;
|
2024-10-22 02:39:50 +02:00
|
|
|
struct logger;
|
|
|
|
|
|
|
|
/* Could be an incoming HTLC, could be a local payment */
|
|
|
|
struct incoming_payment {
|
|
|
|
/* Where to log */
|
|
|
|
struct logger *log;
|
|
|
|
/* Amount of this payment */
|
|
|
|
struct amount_msat msat;
|
|
|
|
/* If it fails */
|
|
|
|
void (*fail)(void *arg, const u8 *failmsg TAKES);
|
|
|
|
/* If it succeeded: here's the preimage. */
|
|
|
|
void (*succeeded)(void *arg, const struct preimage *preimage);
|
|
|
|
void *arg;
|
|
|
|
};
|
2019-12-12 00:55:45 +01:00
|
|
|
|
|
|
|
/* Set of incoming HTLCs for multi-part-payments */
|
|
|
|
struct htlc_set {
|
2024-10-22 02:39:46 +02:00
|
|
|
struct lightningd *ld;
|
2019-12-12 00:55:45 +01:00
|
|
|
struct amount_msat total_msat, so_far;
|
|
|
|
struct sha256 payment_hash;
|
2024-10-22 02:39:50 +02:00
|
|
|
struct incoming_payment **inpays;
|
2019-12-12 11:56:07 +01:00
|
|
|
struct oneshot *timeout;
|
2019-12-12 00:55:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static inline const struct sha256 *keyof_htlc_set(const struct htlc_set *set)
|
|
|
|
{
|
|
|
|
return &set->payment_hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline size_t hash_payment_hash(const struct sha256 *payment_hash)
|
|
|
|
{
|
|
|
|
return siphash24(siphash_seed(), payment_hash, sizeof(&payment_hash));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool htlc_set_eq(const struct htlc_set *set,
|
|
|
|
const struct sha256 *payment_hash)
|
|
|
|
{
|
|
|
|
return sha256_eq(payment_hash, &set->payment_hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
HTABLE_DEFINE_TYPE(struct htlc_set,
|
|
|
|
keyof_htlc_set,
|
|
|
|
hash_payment_hash,
|
|
|
|
htlc_set_eq,
|
|
|
|
htlc_set_map);
|
|
|
|
|
2024-10-22 02:39:50 +02:00
|
|
|
/* Handles arg: if it completes a set, calls invoice_try_pay */
|
|
|
|
void htlc_set_add_(struct lightningd *ld,
|
|
|
|
struct logger *log,
|
|
|
|
struct amount_msat msat,
|
|
|
|
struct amount_msat total_msat,
|
|
|
|
const struct sha256 *payment_hash,
|
|
|
|
const struct secret *payment_secret,
|
|
|
|
void (*fail)(void *, const u8 *),
|
|
|
|
void (*succeeded)(void *, const struct preimage *),
|
|
|
|
void *arg);
|
|
|
|
|
|
|
|
#define htlc_set_add(ld, log, msat, total_msat, payment_hash, payment_secret, \
|
|
|
|
fail, succeeded, arg) \
|
|
|
|
htlc_set_add_((ld), (log), (msat), (total_msat), (payment_hash), \
|
|
|
|
(payment_secret), \
|
|
|
|
typesafe_cb_postargs(void, void *, \
|
|
|
|
(fail), (arg), \
|
|
|
|
const u8 *), \
|
|
|
|
typesafe_cb_postargs(void, void *, \
|
|
|
|
(succeeded), (arg), \
|
|
|
|
const struct preimage *), \
|
|
|
|
(arg))
|
2019-12-12 00:55:45 +01:00
|
|
|
|
2024-10-22 02:39:46 +02:00
|
|
|
/* Fail every htlc in the set: frees set. If failmsg is NULL/zero-length,
|
|
|
|
* it sends each one a WIRE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS. */
|
|
|
|
#define htlc_set_fail(set, failmsg) \
|
|
|
|
htlc_set_fail_((set), (failmsg), __FILE__, __LINE__)
|
|
|
|
void htlc_set_fail_(struct htlc_set *set, const u8 *failmsg TAKES,
|
|
|
|
const char *file, int line);
|
2019-12-12 00:55:45 +01:00
|
|
|
|
|
|
|
/* Fulfill every htlc in the set: frees set */
|
|
|
|
void htlc_set_fulfill(struct htlc_set *set, const struct preimage *preimage);
|
|
|
|
#endif /* LIGHTNING_LIGHTNINGD_HTLC_SET_H */
|