2017-04-01 21:31:13 +10:30
|
|
|
#ifndef LIGHTNING_LIGHTNINGD_HTLC_END_H
|
|
|
|
#define LIGHTNING_LIGHTNINGD_HTLC_END_H
|
|
|
|
#include "config.h"
|
|
|
|
#include <ccan/htable/htable_type.h>
|
2019-04-03 19:01:47 +02:00
|
|
|
#include <ccan/time/time.h>
|
2017-08-29 01:34:01 +09:30
|
|
|
#include <common/htlc_state.h>
|
2017-08-29 01:35:01 +09:30
|
|
|
#include <common/sphinx.h>
|
2017-04-01 21:31:13 +10:30
|
|
|
|
2018-02-12 20:43:04 +10:30
|
|
|
/* We look up HTLCs by channel & id */
|
2017-06-20 15:23:03 +09:30
|
|
|
struct htlc_key {
|
2018-02-12 20:43:04 +10:30
|
|
|
struct channel *channel;
|
2017-06-20 15:23:03 +09:30
|
|
|
u64 id;
|
|
|
|
};
|
2017-04-01 21:31:13 +10:30
|
|
|
|
2017-12-21 14:20:47 +10:30
|
|
|
#define HTLC_INVALID_ID (-1ULL)
|
|
|
|
|
2017-06-20 15:23:03 +09:30
|
|
|
/* Incoming HTLC */
|
|
|
|
struct htlc_in {
|
2017-09-14 21:27:41 +02:00
|
|
|
/* The database primary key for this htlc. Must be 0 until it
|
|
|
|
* is saved to the database, must be >0 after saving to the
|
|
|
|
* database. */
|
|
|
|
u64 dbid;
|
2017-06-20 15:23:03 +09:30
|
|
|
struct htlc_key key;
|
2019-02-21 14:15:55 +10:30
|
|
|
struct amount_msat msat;
|
2017-06-20 15:23:03 +09:30
|
|
|
u32 cltv_expiry;
|
|
|
|
struct sha256 payment_hash;
|
2017-04-30 23:49:15 +02:00
|
|
|
|
2017-06-20 15:20:03 +09:30
|
|
|
enum htlc_state hstate;
|
|
|
|
|
2017-06-20 15:23:03 +09:30
|
|
|
/* Onion information */
|
2020-12-08 17:18:53 +10:30
|
|
|
u8 onion_routing_packet[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)];
|
2017-06-20 15:23:03 +09:30
|
|
|
|
2019-01-08 10:49:50 +10:30
|
|
|
/* Shared secret for us to send any failure message (NULL if malformed) */
|
|
|
|
struct secret *shared_secret;
|
2017-06-20 15:23:03 +09:30
|
|
|
|
2020-02-21 15:40:44 +10:30
|
|
|
/* If we couldn't decode the onion, this contains the error code.. */
|
2020-08-31 10:43:25 +09:30
|
|
|
enum onion_wire badonion;
|
2017-06-20 15:23:03 +09:30
|
|
|
|
2020-02-21 15:40:44 +10:30
|
|
|
/* Otherwise, this contains the failure message to send. */
|
2020-02-18 10:23:58 +10:30
|
|
|
const struct onionreply *failonion;
|
2017-06-20 15:37:03 +09:30
|
|
|
|
2017-06-20 15:23:03 +09:30
|
|
|
/* If they fulfilled, here's the preimage. */
|
|
|
|
struct preimage *preimage;
|
2017-11-28 15:34:20 +10:30
|
|
|
|
2019-04-03 19:01:47 +02:00
|
|
|
/* Remember the timestamp we received this HTLC so we can later record
|
|
|
|
* it, and the resolution time, in the forwards table. */
|
|
|
|
struct timeabs received_time;
|
2020-04-11 12:52:40 +09:30
|
|
|
|
|
|
|
/* If it was blinded. */
|
|
|
|
struct pubkey *blinding;
|
2020-03-18 18:28:29 -05:00
|
|
|
/* true if we supplied the preimage */
|
2020-04-14 14:02:46 -05:00
|
|
|
bool *we_filled;
|
2021-09-28 14:08:11 -05:00
|
|
|
/* true if we immediately fail the htlc (too much dust) */
|
|
|
|
bool fail_immediate;
|
2021-06-04 10:47:47 +02:00
|
|
|
|
|
|
|
/* A simple text annotation shown in `listpeers` */
|
|
|
|
char *status;
|
2021-06-17 15:16:55 +02:00
|
|
|
|
|
|
|
/* The decoded onion payload after hooks processed it. */
|
|
|
|
struct onion_payload *payload;
|
2017-06-20 15:23:03 +09:30
|
|
|
};
|
|
|
|
|
|
|
|
struct htlc_out {
|
2017-09-14 21:27:41 +02:00
|
|
|
/* The database primary key for this htlc. Must be 0 until it
|
|
|
|
* is saved to the database, must be >0 after saving to the
|
|
|
|
* database. */
|
|
|
|
u64 dbid;
|
2017-06-20 15:23:03 +09:30
|
|
|
struct htlc_key key;
|
2019-02-21 14:15:55 +10:30
|
|
|
struct amount_msat msat;
|
2017-04-30 23:49:15 +02:00
|
|
|
u32 cltv_expiry;
|
|
|
|
struct sha256 payment_hash;
|
2017-05-03 11:01:58 +02:00
|
|
|
|
2017-06-20 15:23:03 +09:30
|
|
|
enum htlc_state hstate;
|
|
|
|
|
|
|
|
/* Onion information */
|
2020-12-08 17:18:53 +10:30
|
|
|
u8 onion_routing_packet[TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE)];
|
2017-06-20 15:23:03 +09:30
|
|
|
|
2020-02-21 15:36:58 +10:30
|
|
|
/* If a local error, this is non-NULL. */
|
|
|
|
const u8 *failmsg;
|
2017-06-20 15:12:03 +09:30
|
|
|
|
2017-11-28 15:34:20 +10:30
|
|
|
/* For a remote error. */
|
2020-02-18 10:23:58 +10:30
|
|
|
const struct onionreply *failonion;
|
2017-06-20 15:37:03 +09:30
|
|
|
|
2017-06-20 15:23:03 +09:30
|
|
|
/* If we fulfilled, here's the preimage. */
|
2018-10-10 06:51:31 +10:30
|
|
|
/* FIXME: This is basically unused, except as a bool! */
|
2017-06-20 15:23:03 +09:30
|
|
|
struct preimage *preimage;
|
2017-06-20 15:20:03 +09:30
|
|
|
|
2018-10-10 06:51:31 +10:30
|
|
|
/* Is this a locally-generated payment? Implies ->in is NULL. */
|
|
|
|
bool am_origin;
|
2018-10-09 19:17:52 +10:30
|
|
|
|
2021-12-07 14:09:28 -06:00
|
|
|
/* Amount of fees that this out htlc pays (if am_origin);
|
|
|
|
* otherwise fees collected by routing this out */
|
|
|
|
struct amount_msat fees;
|
|
|
|
|
2019-12-12 10:09:07 +10:30
|
|
|
/* If am_origin, this is the partid of the payment. */
|
|
|
|
u64 partid;
|
|
|
|
|
2021-09-29 12:33:57 +02:00
|
|
|
/* Is this is part of a group of HTLCs, which group is it? */
|
|
|
|
u64 groupid;
|
|
|
|
|
2017-06-20 15:23:03 +09:30
|
|
|
/* Where it's from, if not going to us. */
|
|
|
|
struct htlc_in *in;
|
2020-04-11 12:52:40 +09:30
|
|
|
|
|
|
|
/* Blinding to send alongside, if any. */
|
|
|
|
struct pubkey *blinding;
|
2021-05-21 11:45:54 +09:30
|
|
|
|
|
|
|
/* Timer we use in case they don't add an HTLC in a timely manner. */
|
|
|
|
struct oneshot *timeout;
|
2017-04-01 21:31:13 +10:30
|
|
|
};
|
|
|
|
|
2017-06-20 15:23:03 +09:30
|
|
|
static inline const struct htlc_key *keyof_htlc_in(const struct htlc_in *in)
|
|
|
|
{
|
|
|
|
return &in->key;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline const struct htlc_key *keyof_htlc_out(const struct htlc_out *out)
|
2017-04-01 21:31:13 +10:30
|
|
|
{
|
2017-06-20 15:23:03 +09:30
|
|
|
return &out->key;
|
2017-04-01 21:31:13 +10:30
|
|
|
}
|
|
|
|
|
2017-06-20 15:23:03 +09:30
|
|
|
size_t hash_htlc_key(const struct htlc_key *htlc_key);
|
|
|
|
|
|
|
|
static inline bool htlc_in_eq(const struct htlc_in *in, const struct htlc_key *k)
|
|
|
|
{
|
2018-02-12 20:43:04 +10:30
|
|
|
return in->key.channel == k->channel && in->key.id == k->id;
|
2017-06-20 15:23:03 +09:30
|
|
|
}
|
2017-04-01 21:31:13 +10:30
|
|
|
|
2017-06-20 15:23:03 +09:30
|
|
|
static inline bool htlc_out_eq(const struct htlc_out *out,
|
|
|
|
const struct htlc_key *k)
|
2017-04-01 21:31:13 +10:30
|
|
|
{
|
2018-02-12 20:43:04 +10:30
|
|
|
return out->key.channel == k->channel && out->key.id == k->id;
|
2017-04-01 21:31:13 +10:30
|
|
|
}
|
|
|
|
|
2017-06-20 15:23:03 +09:30
|
|
|
|
|
|
|
HTABLE_DEFINE_TYPE(struct htlc_in, keyof_htlc_in, hash_htlc_key, htlc_in_eq,
|
|
|
|
htlc_in_map);
|
|
|
|
|
|
|
|
HTABLE_DEFINE_TYPE(struct htlc_out, keyof_htlc_out, hash_htlc_key, htlc_out_eq,
|
|
|
|
htlc_out_map);
|
|
|
|
|
|
|
|
struct htlc_in *find_htlc_in(const struct htlc_in_map *map,
|
2018-02-12 20:43:04 +10:30
|
|
|
const struct channel *channel,
|
2017-06-20 15:23:03 +09:30
|
|
|
u64 htlc_id);
|
|
|
|
|
2019-12-12 10:09:10 +10:30
|
|
|
/* FIXME: Slow function only used at startup. */
|
|
|
|
struct htlc_in *remove_htlc_in_by_dbid(struct htlc_in_map *remaining_htlcs_in,
|
|
|
|
u64 dbid);
|
|
|
|
|
2017-06-20 15:23:03 +09:30
|
|
|
struct htlc_out *find_htlc_out(const struct htlc_out_map *map,
|
2018-02-12 20:43:04 +10:30
|
|
|
const struct channel *channel,
|
2017-06-20 15:23:03 +09:30
|
|
|
u64 htlc_id);
|
|
|
|
|
|
|
|
/* You still need to connect_htlc_in this! */
|
|
|
|
struct htlc_in *new_htlc_in(const tal_t *ctx,
|
2018-02-12 20:43:04 +10:30
|
|
|
struct channel *channel, u64 id,
|
2019-02-21 14:15:55 +10:30
|
|
|
struct amount_msat msat, u32 cltv_expiry,
|
2017-06-20 15:23:03 +09:30
|
|
|
const struct sha256 *payment_hash,
|
2019-01-08 10:49:50 +10:30
|
|
|
const struct secret *shared_secret TAKES,
|
2020-04-11 12:52:40 +09:30
|
|
|
const struct pubkey *blinding TAKES,
|
2021-09-28 14:08:11 -05:00
|
|
|
const u8 *onion_routing_packet,
|
|
|
|
bool fail_immediate);
|
2017-06-20 15:23:03 +09:30
|
|
|
|
2018-01-18 06:59:49 +10:30
|
|
|
/* You need to set the ID, then connect_htlc_out this! */
|
2017-06-20 15:23:03 +09:30
|
|
|
struct htlc_out *new_htlc_out(const tal_t *ctx,
|
2018-02-12 20:43:04 +10:30
|
|
|
struct channel *channel,
|
2019-02-21 14:15:55 +10:30
|
|
|
struct amount_msat msat,
|
|
|
|
u32 cltv_expiry,
|
2017-06-20 15:23:03 +09:30
|
|
|
const struct sha256 *payment_hash,
|
|
|
|
const u8 *onion_routing_packet,
|
2020-04-11 12:52:40 +09:30
|
|
|
const struct pubkey *blinding,
|
2018-10-10 06:51:31 +10:30
|
|
|
bool am_origin,
|
2021-12-07 14:09:28 -06:00
|
|
|
struct amount_msat final_msat,
|
2019-12-12 10:09:07 +10:30
|
|
|
u64 partid,
|
2021-09-29 12:33:57 +02:00
|
|
|
u64 groupid,
|
2018-02-02 11:25:06 +10:30
|
|
|
struct htlc_in *in);
|
2017-06-20 15:23:03 +09:30
|
|
|
|
|
|
|
void connect_htlc_in(struct htlc_in_map *map, struct htlc_in *hin);
|
|
|
|
void connect_htlc_out(struct htlc_out_map *map, struct htlc_out *hout);
|
2017-04-01 21:31:13 +10:30
|
|
|
|
2018-10-09 19:18:52 +10:30
|
|
|
/* Set up hout->in to be hin (non-NULL), and clear if hin freed. */
|
|
|
|
void htlc_out_connect_htlc_in(struct htlc_out *hout, struct htlc_in *hin);
|
|
|
|
|
2017-06-20 15:23:03 +09:30
|
|
|
struct htlc_out *htlc_out_check(const struct htlc_out *hout,
|
|
|
|
const char *abortstr);
|
|
|
|
struct htlc_in *htlc_in_check(const struct htlc_in *hin, const char *abortstr);
|
2017-12-15 20:47:54 +10:30
|
|
|
|
2017-04-01 21:31:13 +10:30
|
|
|
#endif /* LIGHTNING_LIGHTNINGD_HTLC_END_H */
|