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 08:17:03 +02:00
|
|
|
/* Error message (iff in error state) */
|
|
|
|
u8 *error;
|
|
|
|
|
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-06-20 08:11:03 +02:00
|
|
|
/* If we've disconnected, this is set. */
|
|
|
|
bool reconnected;
|
2017-01-10 06:08:33 +01:00
|
|
|
|
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-04-12 08:52:57 +02:00
|
|
|
/* Minimum funding depth (specified by us if they fund). */
|
|
|
|
u32 minimum_depth;
|
|
|
|
|
2017-06-20 08:02:03 +02:00
|
|
|
/* Tracking commitment transaction numbers. */
|
2017-06-20 08:18:03 +02:00
|
|
|
u64 commit_index[NUM_SIDES];
|
|
|
|
u64 num_revocations_received;
|
2017-06-20 08:04:03 +02:00
|
|
|
u64 next_htlc_id;
|
2017-06-20 08:02:03 +02:00
|
|
|
|
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 08:15:03 +02:00
|
|
|
bool remote_funding_locked;
|
|
|
|
/* Channel if locked locally. */
|
|
|
|
struct short_channel_id *scid;
|
2017-03-07 02:09:53 +01:00
|
|
|
|
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-03-07 02:09:53 +01:00
|
|
|
/* Secret seed (FIXME: Move to hsm!) */
|
|
|
|
struct privkey *seed;
|
2017-03-09 14:24:32 +01:00
|
|
|
|
2017-06-20 08:03:03 +02:00
|
|
|
/* Reestablishment stuff: last sent commit and revocation details. */
|
|
|
|
bool last_was_revoke;
|
|
|
|
struct changed_htlc *last_sent_commit;
|
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-06-24 08:25:51 +02:00
|
|
|
return peer->state >= CHANNELD_AWAITING_LOCKIN;
|
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-06-24 08:25:53 +02:00
|
|
|
void peer_fundee_open(struct peer *peer, const u8 *msg,
|
2017-06-24 08:25:53 +02:00
|
|
|
const struct crypto_state *cs,
|
2017-06-24 08:50:23 +02:00
|
|
|
int peer_fd, int gossip_fd);
|
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-06-20 08:17:03 +02:00
|
|
|
|
|
|
|
/* Peer has failed, but try reconnected. */
|
|
|
|
PRINTF_FMT(2,3) void peer_fail_transient(struct peer *peer, const char *fmt,...);
|
|
|
|
/* Peer has failed, give up on it. */
|
|
|
|
void peer_fail_permanent(struct peer *peer, const u8 *msg);
|
|
|
|
/* Permanent error, but due to internal problems, not peer. */
|
|
|
|
void peer_internal_error(struct peer *peer, const char *fmt, ...);
|
2017-05-22 09:28:07 +02:00
|
|
|
|
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 */
|