1 #ifndef LIGHTNING_CHANNELD_CHANNELD_HTLC_H
2 #define LIGHTNING_CHANNELD_CHANNELD_HTLC_H
3 #include "config.h"
4 #include <ccan/crypto/siphash24/siphash24.h>
5 #include <common/htlc.h>
6 #include <common/pseudorand.h>
7 #include <wire/onion_wire.h>
8 
9 struct htlc {
10 	/* What's the status. */
11 	enum htlc_state state;
12 	/* The unique ID for this peer and this direction (LOCAL or REMOTE) */
13 	u64 id;
14 	/* The amount in millisatoshi. */
15 	struct amount_msat amount;
16 	/* When the HTLC can no longer be redeemed. */
17 	struct abs_locktime expiry;
18 	/* The hash of the preimage which can redeem this HTLC */
19 	struct sha256 rhash;
20 	/* The preimage which hashes to rhash (if known) */
21 	struct preimage *r;
22 
23 	/* If they fail the HTLC, we store why here. */
24 	const struct failed_htlc *failed;
25 
26 	/* Routing information sent with this HTLC (outgoing only). */
27 	const u8 *routing;
28 
29 	/* Blinding (optional). */
30 	struct pubkey *blinding;
31 
32 	/* Should we immediately fail this htlc? */
33 	bool fail_immediate;
34 };
35 
htlc_has(const struct htlc * h,int flag)36 static inline bool htlc_has(const struct htlc *h, int flag)
37 {
38 	return htlc_state_flags(h->state) & flag;
39 }
40 
htlc_owner(const struct htlc * h)41 static inline enum side htlc_owner(const struct htlc *h)
42 {
43 	return htlc_state_owner(h->state);
44 }
45 
46 /* htlc_map: ID -> htlc mapping. */
htlc_key(const struct htlc * h)47 static inline u64 htlc_key(const struct htlc *h)
48 {
49 	return h->id;
50 }
htlc_cmp(const struct htlc * h,u64 id)51 static inline bool htlc_cmp(const struct htlc *h, u64 id)
52 {
53 	return h->id == id;
54 }
htlc_hash(u64 id)55 static inline size_t htlc_hash(u64 id)
56 {
57 	return siphash24(siphash_seed(), &id, sizeof(id));
58 }
59 HTABLE_DEFINE_TYPE(struct htlc, htlc_key, htlc_hash, htlc_cmp, htlc_map);
60 
htlc_get(struct htlc_map * htlcs,u64 id,enum side owner)61 static inline struct htlc *htlc_get(struct htlc_map *htlcs, u64 id, enum side owner)
62 {
63 	struct htlc *h;
64 	struct htlc_map_iter it;
65 
66 	for (h = htlc_map_getfirst(htlcs, id, &it);
67 	     h;
68 	     h = htlc_map_getnext(htlcs, id, &it)) {
69 		if (h->id == id && htlc_has(h, HTLC_FLAG(owner,HTLC_F_OWNER)))
70 			return h;
71 	}
72 	return NULL;
73 }
74 
75 /* FIXME: Move these out of the hash! */
htlc_is_dead(const struct htlc * htlc)76 static inline bool htlc_is_dead(const struct htlc *htlc)
77 {
78 	return htlc->state == RCVD_REMOVE_ACK_REVOCATION
79 		|| htlc->state == SENT_REMOVE_ACK_REVOCATION;
80 }
81 #endif /* LIGHTNING_CHANNELD_CHANNELD_HTLC_H */
82