daemon: code for HTLC maps.

We currently keep copies of HTLCs in each commit_info structure, but
that's redundant.  Keep per-peer per-direction maps of HTLCs, then we can
just throw pointers around (next patch).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2016-06-30 09:08:11 +09:30
parent fecd91ab2a
commit 67ac2d2081
2 changed files with 50 additions and 0 deletions

View File

@ -52,6 +52,7 @@ DAEMON_HEADERS := \
daemon/controlled_time.h \
daemon/cryptopkt.h \
daemon/dns.h \
daemon/htlc.h \
daemon/json.h \
daemon/jsonrpc.h \
daemon/lightningd.h \

49
daemon/htlc.h Normal file
View File

@ -0,0 +1,49 @@
#ifndef LIGHTNING_DAEMON_HTLC_H
#define LIGHTNING_DAEMON_HTLC_H
#include "config.h"
#include "bitcoin/locktime.h"
#include "pseudorand.h"
#include <ccan/crypto/sha256/sha256.h>
#include <ccan/crypto/siphash24/siphash24.h>
#include <ccan/htable/htable_type.h>
#include <ccan/short_types/short_types.h>
struct htlc {
/* Useful for debugging, and decoding via ->src. */
struct peer *peer;
/* The unique ID for this peer and this direction (ours or theirs) */
u64 id;
/* The amount in millisatoshi. */
u64 msatoshis;
/* 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;
/* FIXME: We could union these together: */
/* Routing information sent with this HTLC. */
const u8 *routing;
/* Previous HTLC (if any) which made us offer this (OURS only) */
struct htlc *src;
};
/* 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 size_t htlc_map_count(const struct htlc_map *htlcs)
{
return htlcs->raw.elems;
}
#endif /* LIGHTNING_DAEMON_HTLC_H */