2017-04-01 13:01:13 +02:00
|
|
|
#ifndef LIGHTNING_LIGHTNINGD_HTLC_END_H
|
|
|
|
#define LIGHTNING_LIGHTNINGD_HTLC_END_H
|
|
|
|
#include "config.h"
|
|
|
|
#include <ccan/htable/htable_type.h>
|
|
|
|
#include <ccan/short_types/short_types.h>
|
2017-08-28 18:04:01 +02:00
|
|
|
#include <common/htlc_state.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/sphinx.h>
|
2017-06-20 08:07:03 +02:00
|
|
|
#include <wire/gen_onion_wire.h>
|
2017-04-01 13:01:13 +02:00
|
|
|
|
2017-06-20 07:53:03 +02:00
|
|
|
/* We look up HTLCs by peer & id */
|
|
|
|
struct htlc_key {
|
2017-04-01 13:01:13 +02:00
|
|
|
struct peer *peer;
|
2017-06-20 07:53:03 +02:00
|
|
|
u64 id;
|
|
|
|
};
|
2017-04-01 13:01:13 +02:00
|
|
|
|
2017-12-21 04:50:47 +01:00
|
|
|
#define HTLC_INVALID_ID (-1ULL)
|
|
|
|
|
2017-06-20 07:53:03 +02:00
|
|
|
/* 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 07:53:03 +02:00
|
|
|
struct htlc_key key;
|
|
|
|
u64 msatoshi;
|
|
|
|
u32 cltv_expiry;
|
|
|
|
struct sha256 payment_hash;
|
2017-04-30 23:49:15 +02:00
|
|
|
|
2017-06-20 07:50:03 +02:00
|
|
|
enum htlc_state hstate;
|
|
|
|
|
2017-06-20 07:53:03 +02:00
|
|
|
/* Onion information */
|
|
|
|
u8 onion_routing_packet[TOTAL_PACKET_SIZE];
|
|
|
|
|
|
|
|
/* Shared secret for us to send any failure message. */
|
|
|
|
struct secret shared_secret;
|
|
|
|
|
2017-11-28 06:03:04 +01:00
|
|
|
/* If a local error, this is non-zero. */
|
|
|
|
enum onion_type failcode;
|
2017-06-20 07:53:03 +02:00
|
|
|
|
2017-11-28 06:04:20 +01:00
|
|
|
/* For a remote error. */
|
2017-11-28 06:03:04 +01:00
|
|
|
const u8 *failuremsg;
|
2017-06-20 08:07:03 +02:00
|
|
|
|
2017-11-28 06:04:20 +01:00
|
|
|
/* If failcode & UPDATE, this is the channel which failed. */
|
|
|
|
struct short_channel_id failoutchannel;
|
|
|
|
|
2017-06-20 07:53:03 +02:00
|
|
|
/* If they fulfilled, here's the preimage. */
|
|
|
|
struct preimage *preimage;
|
2017-11-28 06:04:20 +01:00
|
|
|
|
2017-06-20 07:53:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
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-09-25 21:50:20 +02:00
|
|
|
u64 origin_htlc_id;
|
2017-06-20 07:53:03 +02:00
|
|
|
struct htlc_key key;
|
|
|
|
u64 msatoshi;
|
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 07:53:03 +02:00
|
|
|
enum htlc_state hstate;
|
|
|
|
|
|
|
|
/* Onion information */
|
|
|
|
u8 onion_routing_packet[TOTAL_PACKET_SIZE];
|
|
|
|
|
2017-11-28 06:03:04 +01:00
|
|
|
/* If a local error, this is non-zero. */
|
|
|
|
enum onion_type failcode;
|
2017-06-20 07:42:03 +02:00
|
|
|
|
2017-11-28 06:04:20 +01:00
|
|
|
/* For a remote error. */
|
2017-11-28 06:03:04 +01:00
|
|
|
const u8 *failuremsg;
|
2017-06-20 08:07:03 +02:00
|
|
|
|
2017-06-20 07:53:03 +02:00
|
|
|
/* If we fulfilled, here's the preimage. */
|
|
|
|
struct preimage *preimage;
|
2017-06-20 07:50:03 +02:00
|
|
|
|
2017-06-20 07:53:03 +02:00
|
|
|
/* Where it's from, if not going to us. */
|
|
|
|
struct htlc_in *in;
|
2017-05-03 11:01:58 +02:00
|
|
|
|
2018-01-17 21:29:49 +01:00
|
|
|
/* Otherwise, this MAY be non-null if there's a pay command waiting */
|
|
|
|
struct command *cmd;
|
2017-04-01 13:01:13 +02:00
|
|
|
};
|
|
|
|
|
2017-06-20 07:53:03 +02:00
|
|
|
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 13:01:13 +02:00
|
|
|
{
|
2017-06-20 07:53:03 +02:00
|
|
|
return &out->key;
|
2017-04-01 13:01:13 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 07:53:03 +02:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
return in->key.peer == k->peer && in->key.id == k->id;
|
|
|
|
}
|
2017-04-01 13:01:13 +02:00
|
|
|
|
2017-06-20 07:53:03 +02:00
|
|
|
static inline bool htlc_out_eq(const struct htlc_out *out,
|
|
|
|
const struct htlc_key *k)
|
2017-04-01 13:01:13 +02:00
|
|
|
{
|
2017-06-20 07:53:03 +02:00
|
|
|
return out->key.peer == k->peer && out->key.id == k->id;
|
2017-04-01 13:01:13 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 07:53:03 +02:00
|
|
|
|
|
|
|
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,
|
|
|
|
const struct peer *peer,
|
|
|
|
u64 htlc_id);
|
|
|
|
|
|
|
|
struct htlc_out *find_htlc_out(const struct htlc_out_map *map,
|
2017-04-01 13:01:13 +02:00
|
|
|
const struct peer *peer,
|
2017-06-20 07:53:03 +02:00
|
|
|
u64 htlc_id);
|
|
|
|
|
|
|
|
/* You still need to connect_htlc_in this! */
|
|
|
|
struct htlc_in *new_htlc_in(const tal_t *ctx,
|
|
|
|
struct peer *peer, u64 id,
|
|
|
|
u64 msatoshi, u32 cltv_expiry,
|
|
|
|
const struct sha256 *payment_hash,
|
|
|
|
const struct secret *shared_secret,
|
|
|
|
const u8 *onion_routing_packet);
|
|
|
|
|
2018-01-17 21:29:49 +01:00
|
|
|
/* You need to set the ID, then connect_htlc_out this! */
|
2017-06-20 07:53:03 +02:00
|
|
|
struct htlc_out *new_htlc_out(const tal_t *ctx,
|
|
|
|
struct peer *peer,
|
|
|
|
u64 msatoshi, u32 cltv_expiry,
|
|
|
|
const struct sha256 *payment_hash,
|
|
|
|
const u8 *onion_routing_packet,
|
|
|
|
struct htlc_in *in,
|
2018-01-17 21:29:49 +01:00
|
|
|
struct command *cmd);
|
2017-06-20 07:53:03 +02:00
|
|
|
|
|
|
|
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 13:01:13 +02:00
|
|
|
|
2017-06-20 07:53:03 +02:00
|
|
|
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 11:17:54 +01:00
|
|
|
|
|
|
|
#if DEVELOPER
|
|
|
|
struct htable;
|
|
|
|
void htlc_inmap_mark_pointers_used(struct htable *memtable,
|
|
|
|
const struct htlc_in_map *map);
|
|
|
|
void htlc_outmap_mark_pointers_used(struct htable *memtable,
|
|
|
|
const struct htlc_out_map *map);
|
|
|
|
#endif /* DEVELOPER */
|
2017-04-01 13:01:13 +02:00
|
|
|
#endif /* LIGHTNING_LIGHTNINGD_HTLC_END_H */
|