mirror of
https://github.com/ElementsProject/lightning.git
synced 2024-11-20 02:27:51 +01:00
efee948d3a
'struct htlc' in channeld has a 'malformed' field, which is really only used in the "retransmit updates on reconnect" case. That's quite confusing, and I'm not entirely convinced that it can only be set to a failcode with the BADONION bit set. So generalize it, using the same logic we use in the master daemon: failcode: a locally generated error, for channeld to turn into the appropriate error message. fail: a remotely generated onion error, for forwarding. Either of these being non-zero/non-NULL means we've failed, and only one should be set at any time. We unify the "send htlc fail/fulfill update due to retransmit" and the normal send update paths, by always calling send_fail_or_fulfill. This unification revealed that we accidentally skipped the onion-wrapping stage when we retransmit failed htlcs! Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
84 lines
2.2 KiB
C
84 lines
2.2 KiB
C
#ifndef LIGHTNING_CHANNELD_CHANNELD_HTLC_H
|
|
#define LIGHTNING_CHANNELD_CHANNELD_HTLC_H
|
|
#include "config.h"
|
|
#include <bitcoin/locktime.h>
|
|
#include <ccan/short_types/short_types.h>
|
|
#include <common/htlc.h>
|
|
#include <common/pseudorand.h>
|
|
#include <wire/gen_onion_wire.h>
|
|
|
|
struct htlc {
|
|
/* What's the status. */
|
|
enum htlc_state state;
|
|
/* The unique ID for this peer and this direction (LOCAL or REMOTE) */
|
|
u64 id;
|
|
/* The amount in millisatoshi. */
|
|
u64 msatoshi;
|
|
/* When the HTLC can no longer be redeemed. */
|
|
struct abs_locktime expiry;
|
|
/* The hash of the preimage which can redeem this HTLC */
|
|
struct sha256 rhash;
|
|
/* The preimage which hashes to rhash (if known) */
|
|
struct preimage *r;
|
|
|
|
/* The routing shared secret (only for incoming) */
|
|
struct secret *shared_secret;
|
|
|
|
/* FIXME: We could union these together: */
|
|
/* Routing information sent with this HTLC. */
|
|
const u8 *routing;
|
|
|
|
/* Failure message we received or generated. */
|
|
const u8 *fail;
|
|
/* For a local failure, we might have to generate fail ourselves
|
|
* (or, if BADONION we send a update_fail_malformed_htlc). */
|
|
enum onion_type failcode;
|
|
};
|
|
|
|
static inline bool htlc_has(const struct htlc *h, int flag)
|
|
{
|
|
return htlc_state_flags(h->state) & flag;
|
|
}
|
|
|
|
static inline enum side htlc_owner(const struct htlc *h)
|
|
{
|
|
return htlc_state_owner(h->state);
|
|
}
|
|
|
|
/* htlc_map: ID -> htlc mapping. */
|
|
static inline u64 htlc_key(const struct htlc *h)
|
|
{
|
|
return h->id;
|
|
}
|
|
static inline bool htlc_cmp(const struct htlc *h, u64 id)
|
|
{
|
|
return h->id == id;
|
|
}
|
|
static inline size_t htlc_hash(u64 id)
|
|
{
|
|
return siphash24(siphash_seed(), &id, sizeof(id));
|
|
}
|
|
HTABLE_DEFINE_TYPE(struct htlc, htlc_key, htlc_hash, htlc_cmp, htlc_map);
|
|
|
|
static inline struct htlc *htlc_get(struct htlc_map *htlcs, u64 id, enum side owner)
|
|
{
|
|
struct htlc *h;
|
|
struct htlc_map_iter it;
|
|
|
|
for (h = htlc_map_getfirst(htlcs, id, &it);
|
|
h;
|
|
h = htlc_map_getnext(htlcs, id, &it)) {
|
|
if (h->id == id && htlc_has(h, HTLC_FLAG(owner,HTLC_F_OWNER)))
|
|
return h;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
/* FIXME: Move these out of the hash! */
|
|
static inline bool htlc_is_dead(const struct htlc *htlc)
|
|
{
|
|
return htlc->state == RCVD_REMOVE_ACK_REVOCATION
|
|
|| htlc->state == SENT_REMOVE_ACK_REVOCATION;
|
|
}
|
|
#endif /* LIGHTNING_CHANNELD_CHANNELD_HTLC_H */
|