2017-01-10 06:08:33 +01:00
|
|
|
#ifndef LIGHTNING_LIGHTNINGD_PEER_CONTROL_H
|
|
|
|
#define LIGHTNING_LIGHTNINGD_PEER_CONTROL_H
|
|
|
|
#include "config.h"
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <ccan/compiler/compiler.h>
|
2017-04-01 12:24:59 +02:00
|
|
|
#include <ccan/list/list.h>
|
2017-05-22 13:24:47 +02:00
|
|
|
#include <daemon/htlc.h>
|
2017-04-01 12:24:59 +02:00
|
|
|
#include <daemon/json.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <daemon/netaddr.h>
|
2017-03-07 02:09:53 +01:00
|
|
|
#include <lightningd/channel_config.h>
|
2017-05-22 13:24:59 +02:00
|
|
|
#include <lightningd/peer_state.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
2017-03-20 17:09:12 +01:00
|
|
|
#define ANNOUNCE_MIN_DEPTH 6
|
|
|
|
|
2017-02-24 06:52:56 +01:00
|
|
|
struct crypto_state;
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
struct peer {
|
|
|
|
struct lightningd *ld;
|
|
|
|
|
|
|
|
/* Unique ID (works before we know their pubkey) */
|
|
|
|
u64 unique_id;
|
|
|
|
|
2017-05-22 13:24:59 +02:00
|
|
|
/* What's happening. */
|
|
|
|
enum peer_state state;
|
|
|
|
|
2017-05-22 13:24:47 +02:00
|
|
|
/* Which side offered channel? */
|
|
|
|
enum side funder;
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
/* Inside ld->peers. */
|
|
|
|
struct list_node list;
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
/* What stage is this in? NULL during first creation. */
|
2017-03-10 11:57:17 +01:00
|
|
|
struct subd *owner;
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
/* History */
|
|
|
|
struct log_book *log_book;
|
|
|
|
struct log *log;
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
/* ID of peer (NULL before initial handshake). */
|
|
|
|
struct pubkey *id;
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
/* Our fd to the peer (-1 when we don't have it). */
|
2017-01-10 06:08:33 +01:00
|
|
|
int fd;
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
/* Where we connected to, or it connected from. */
|
|
|
|
struct netaddr netaddr;
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
/* Json command which made us connect (if any) */
|
|
|
|
struct command *connect_cmd;
|
2017-03-07 02:09:53 +01:00
|
|
|
|
|
|
|
/* Our channel config. */
|
|
|
|
struct channel_config our_config;
|
|
|
|
|
2017-05-02 07:26:31 +02:00
|
|
|
/* Channel if locked. */
|
|
|
|
struct short_channel_id *scid;
|
|
|
|
|
2017-04-12 08:52:57 +02:00
|
|
|
/* Minimum funding depth (specified by us if they fund). */
|
|
|
|
u32 minimum_depth;
|
|
|
|
|
2017-03-07 02:09:53 +01:00
|
|
|
/* Funding txid and amounts (once known) */
|
|
|
|
struct sha256_double *funding_txid;
|
|
|
|
u16 funding_outnum;
|
|
|
|
u64 funding_satoshi, push_msat;
|
|
|
|
|
2017-04-01 12:58:30 +02:00
|
|
|
/* Channel balance (LOCAL and REMOTE); if we have one. */
|
|
|
|
u64 *balance;
|
|
|
|
|
2017-03-07 02:09:53 +01:00
|
|
|
/* Secret seed (FIXME: Move to hsm!) */
|
|
|
|
struct privkey *seed;
|
2017-03-09 14:24:32 +01:00
|
|
|
|
|
|
|
/* Gossip client fd, forwarded to the respective owner */
|
|
|
|
int gossip_client_fd;
|
2017-01-10 06:08:33 +01:00
|
|
|
};
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2017-05-22 13:24:59 +02:00
|
|
|
static inline bool peer_can_add_htlc(const struct peer *peer)
|
|
|
|
{
|
|
|
|
return peer->state == NORMAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool peer_can_remove_htlc(const struct peer *peer)
|
|
|
|
{
|
|
|
|
return peer->state == NORMAL
|
|
|
|
|| peer->state == SHUTDOWN_SENT
|
|
|
|
|| peer->state == SHUTDOWN_RCVD
|
|
|
|
|| peer->state == ONCHAIN_THEIR_UNILATERAL
|
|
|
|
|| peer->state == ONCHAIN_OUR_UNILATERAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool peer_on_chain(const struct peer *peer)
|
|
|
|
{
|
|
|
|
return peer->state == ONCHAIN_CHEATED
|
|
|
|
|| peer->state == ONCHAIN_THEIR_UNILATERAL
|
|
|
|
|| peer->state == ONCHAIN_OUR_UNILATERAL
|
|
|
|
|| peer->state == ONCHAIN_MUTUAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do we need to remember anything about this peer? */
|
|
|
|
static inline bool peer_persists(const struct peer *peer)
|
|
|
|
{
|
|
|
|
return peer->state > OPENING_NOT_LOCKED;
|
|
|
|
}
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
struct peer *peer_by_unique_id(struct lightningd *ld, u64 unique_id);
|
2017-02-24 06:52:56 +01:00
|
|
|
struct peer *peer_by_id(struct lightningd *ld, const struct pubkey *id);
|
2017-04-01 12:24:59 +02:00
|
|
|
struct peer *peer_from_json(struct lightningd *ld,
|
|
|
|
const char *buffer,
|
|
|
|
jsmntok_t *peeridtok);
|
2017-02-24 06:52:56 +01:00
|
|
|
|
|
|
|
void peer_accept_open(struct peer *peer,
|
|
|
|
const struct crypto_state *cs, const u8 *msg);
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2017-05-22 09:28:07 +02:00
|
|
|
/* Peer has failed. */
|
|
|
|
PRINTF_FMT(2,3) void peer_fail(struct peer *peer, const char *fmt, ...);
|
|
|
|
|
2017-05-22 13:24:59 +02:00
|
|
|
const char *peer_state_name(enum peer_state state);
|
|
|
|
void peer_set_condition(struct peer *peer, enum peer_state state);
|
2017-01-10 06:08:33 +01:00
|
|
|
void setup_listeners(struct lightningd *ld);
|
|
|
|
#endif /* LIGHTNING_LIGHTNINGD_PEER_CONTROL_H */
|