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-06-20 07:51:03 +02:00
|
|
|
#include <ccan/crypto/shachain/shachain.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;
|
|
|
|
|
2017-06-06 05:08:42 +02:00
|
|
|
/* Unique ID of connection (works even if we have multiple to same id) */
|
2017-01-10 06:08:33 +01:00
|
|
|
u64 unique_id;
|
|
|
|
|
2017-06-06 05:08:42 +02:00
|
|
|
/* ID of peer */
|
|
|
|
struct pubkey id;
|
|
|
|
|
2017-06-20 07:51:03 +02:00
|
|
|
/* Their shachain. */
|
|
|
|
struct shachain their_shachain;
|
|
|
|
|
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
|
|
|
/* Our fd to the peer (-1 when we don't have it). */
|
2017-01-10 06:08:33 +01:00
|
|
|
int fd;
|
|
|
|
|
2017-05-23 13:03:17 +02:00
|
|
|
/* Crypto state (NULL if it's in daemon) */
|
|
|
|
struct crypto_state *cs;
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
/* Where we connected to, or it connected from. */
|
|
|
|
struct netaddr netaddr;
|
|
|
|
|
2017-03-07 02:09:53 +01:00
|
|
|
/* Our channel config. */
|
|
|
|
struct channel_config our_config;
|
|
|
|
|
2017-05-24 12:10:17 +02:00
|
|
|
/* funding_signed packet for fundee, waiting to send. */
|
|
|
|
const u8 *funding_signed;
|
|
|
|
|
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-06-20 07:59:03 +02:00
|
|
|
/* Amount going to us, not counting unfinished HTLCs; if we have one. */
|
2017-04-01 12:58:30 +02:00
|
|
|
u64 *balance;
|
|
|
|
|
2017-05-23 13:03:17 +02:00
|
|
|
/* Keys for channel. */
|
|
|
|
struct channel_info *channel_info;
|
|
|
|
|
2017-06-20 07:55:03 +02:00
|
|
|
/* Their next per-commit point, if known. */
|
|
|
|
struct pubkey *next_per_commitment_point;
|
|
|
|
|
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)
|
|
|
|
{
|
2017-05-22 13:27:20 +02:00
|
|
|
return peer->state == CHANNELD_NORMAL;
|
2017-05-22 13:24:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool peer_can_remove_htlc(const struct peer *peer)
|
|
|
|
{
|
2017-05-22 13:27:20 +02:00
|
|
|
return peer->state == CHANNELD_NORMAL
|
|
|
|
|| peer->state == SHUTDOWND_SENT
|
|
|
|
|| peer->state == SHUTDOWND_RCVD
|
|
|
|
|| peer->state == ONCHAIND_THEIR_UNILATERAL
|
|
|
|
|| peer->state == ONCHAIND_OUR_UNILATERAL;
|
2017-05-22 13:24:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool peer_on_chain(const struct peer *peer)
|
|
|
|
{
|
2017-05-22 13:27:20 +02:00
|
|
|
return peer->state == ONCHAIND_CHEATED
|
|
|
|
|| peer->state == ONCHAIND_THEIR_UNILATERAL
|
|
|
|
|| peer->state == ONCHAIND_OUR_UNILATERAL
|
|
|
|
|| peer->state == ONCHAIND_MUTUAL;
|
2017-05-22 13:24:59 +02:00
|
|
|
}
|
|
|
|
|
2017-05-22 13:27:20 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* On disconnection, the funder MUST remember the channel for
|
|
|
|
* reconnection if it has broadcast the funding transaction, otherwise it
|
2017-06-20 07:40:03 +02:00
|
|
|
* SHOULD NOT.
|
2017-05-22 13:27:20 +02:00
|
|
|
*
|
|
|
|
* On disconnection, the non-funding node MUST remember the channel for
|
|
|
|
* reconnection if it has sent the `funding_signed` message, otherwise
|
2017-06-20 07:40:03 +02:00
|
|
|
* it SHOULD NOT.
|
2017-05-22 13:27:20 +02:00
|
|
|
*/
|
2017-05-22 13:24:59 +02:00
|
|
|
static inline bool peer_persists(const struct peer *peer)
|
|
|
|
{
|
2017-05-23 13:07:42 +02:00
|
|
|
return peer->state > GETTING_SIG_FROM_HSM;
|
2017-05-22 13:24:59 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-05-23 13:04:17 +02:00
|
|
|
void peer_fundee_open(struct peer *peer, const u8 *msg);
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2017-05-22 13:26:49 +02:00
|
|
|
void add_peer(struct lightningd *ld, u64 unique_id,
|
|
|
|
int fd, const struct pubkey *id,
|
|
|
|
const struct crypto_state *cs);
|
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);
|
2017-05-22 13:27:20 +02:00
|
|
|
void peer_set_condition(struct peer *peer, enum peer_state oldstate,
|
|
|
|
enum peer_state state);
|
2017-01-10 06:08:33 +01:00
|
|
|
void setup_listeners(struct lightningd *ld);
|
|
|
|
#endif /* LIGHTNING_LIGHTNINGD_PEER_CONTROL_H */
|