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-08-28 18:05:01 +02:00
|
|
|
#include <common/channel_config.h>
|
2017-08-28 18:04:01 +02:00
|
|
|
#include <common/htlc.h>
|
|
|
|
#include <common/json.h>
|
2017-10-23 06:17:38 +02:00
|
|
|
#include <common/wireaddr.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-07-19 16:55:47 +02:00
|
|
|
#include <wallet/wallet.h>
|
2017-06-27 04:55:01 +02:00
|
|
|
#include <wire/peer_wire.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
|
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-08-14 22:06:59 +02:00
|
|
|
/* Database ID of the peer */
|
|
|
|
u64 dbid;
|
|
|
|
|
2017-06-06 05:08:42 +02:00
|
|
|
/* ID of peer */
|
|
|
|
struct pubkey id;
|
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
/* Global and local features bitfields. */
|
|
|
|
const u8 *gfeatures, *lfeatures;
|
|
|
|
|
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. */
|
2017-07-19 16:55:47 +02:00
|
|
|
struct wallet_shachain their_shachain;
|
2017-06-20 07:51:03 +02:00
|
|
|
|
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-10-12 08:35:03 +02:00
|
|
|
/* Is there a single subdaemon responsible for us? */
|
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-27 04:55:01 +02:00
|
|
|
/* Channel flags from opening message. */
|
|
|
|
u8 channel_flags;
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
/* Where we connected to, or it connected from. */
|
2017-10-23 06:17:38 +02:00
|
|
|
struct wireaddr addr;
|
2017-01-10 06:08:33 +01:00
|
|
|
|
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-27 04:55:06 +02:00
|
|
|
u64 next_index[NUM_SIDES];
|
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-07-05 07:33:40 +02:00
|
|
|
u64 *our_msatoshi;
|
2017-04-01 12:58:30 +02:00
|
|
|
|
2017-08-18 06:43:52 +02:00
|
|
|
/* Last tx they gave us (if any). */
|
|
|
|
struct bitcoin_tx *last_tx;
|
|
|
|
secp256k1_ecdsa_signature *last_sig;
|
2017-08-18 06:43:53 +02:00
|
|
|
secp256k1_ecdsa_signature *last_htlc_sigs;
|
2017-08-18 06:43:52 +02:00
|
|
|
|
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-26 03:16:43 +02:00
|
|
|
/* Their scriptpubkey if they sent shutdown. */
|
|
|
|
u8 *remote_shutdown_scriptpubkey;
|
|
|
|
/* Our key for shutdown (-1 if not chosen yet) */
|
|
|
|
s64 local_shutdown_idx;
|
2017-06-26 04:04:26 +02: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-08-18 06:43:53 +02:00
|
|
|
|
2017-08-07 14:36:37 +02:00
|
|
|
struct wallet_channel *channel;
|
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
|
|
|
}
|
|
|
|
|
2017-11-21 06:26:59 +01:00
|
|
|
static inline bool peer_fees_can_change(const struct peer *peer)
|
|
|
|
{
|
|
|
|
return peer->state == CHANNELD_NORMAL
|
|
|
|
|| peer->state == CHANNELD_SHUTTING_DOWN;
|
|
|
|
}
|
|
|
|
|
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
|
2017-06-26 03:16:43 +02:00
|
|
|
|| peer->state == CHANNELD_SHUTTING_DOWN
|
2017-05-22 13:27:20 +02:00
|
|
|
|| peer->state == ONCHAIND_THEIR_UNILATERAL
|
|
|
|
|| peer->state == ONCHAIND_OUR_UNILATERAL;
|
2017-05-22 13:24:59 +02:00
|
|
|
}
|
|
|
|
|
2017-08-23 03:55:16 +02:00
|
|
|
static inline bool peer_state_on_chain(enum peer_state state)
|
|
|
|
{
|
|
|
|
return state == ONCHAIND_CHEATED
|
|
|
|
|| state == ONCHAIND_THEIR_UNILATERAL
|
|
|
|
|| state == ONCHAIND_OUR_UNILATERAL
|
|
|
|
|| state == ONCHAIND_MUTUAL;
|
|
|
|
}
|
|
|
|
|
2017-05-22 13:24:59 +02:00
|
|
|
static inline bool peer_on_chain(const struct peer *peer)
|
|
|
|
{
|
2017-08-23 03:55:16 +02:00
|
|
|
return peer_state_on_chain(peer->state);
|
2017-05-22 13:24:59 +02:00
|
|
|
}
|
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
static inline bool peer_wants_reconnect(const struct peer *peer)
|
|
|
|
{
|
|
|
|
return peer->state >= CHANNELD_AWAITING_LOCKIN
|
|
|
|
&& peer->state <= CLOSINGD_COMPLETE;
|
|
|
|
}
|
|
|
|
|
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-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-08-18 06:43:52 +02:00
|
|
|
void peer_last_tx(struct peer *peer, struct bitcoin_tx *tx,
|
|
|
|
const secp256k1_ecdsa_signature *sig);
|
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
/* The three ways peers enter from the network:
|
|
|
|
*
|
|
|
|
* peer_connected - when it first connects to gossipd (after init exchange).
|
|
|
|
* peer_sent_nongossip - when it tries to fund a channel.
|
|
|
|
* gossip_peer_released - when we tell gossipd to release it so we can fund
|
|
|
|
* a channel.
|
|
|
|
*/
|
|
|
|
void peer_connected(struct lightningd *ld, const u8 *msg,
|
|
|
|
int peer_fd, int gossip_fd);
|
|
|
|
|
|
|
|
void peer_sent_nongossip(struct lightningd *ld,
|
|
|
|
const struct pubkey *id,
|
2017-10-23 06:17:38 +02:00
|
|
|
const struct wireaddr *addr,
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
const struct crypto_state *cs,
|
|
|
|
const u8 *gfeatures,
|
|
|
|
const u8 *lfeatures,
|
|
|
|
int peer_fd, int gossip_fd,
|
|
|
|
const u8 *in_msg);
|
2017-06-20 08:17:03 +02:00
|
|
|
|
2017-08-17 15:30:24 +02:00
|
|
|
/**
|
|
|
|
* populate_peer -- Populate daemon fields in a peer
|
|
|
|
*
|
|
|
|
* @ld: the daemon to wire the peer into
|
|
|
|
* @peer: the peer to populate
|
|
|
|
*
|
|
|
|
* Creating a new peer, or loading a peer from the database we need to
|
|
|
|
* populate a number of fields, e.g., the logging handler and the
|
|
|
|
* pointer to the daemon. populate_peer does exactly that.
|
|
|
|
*/
|
|
|
|
void populate_peer(struct lightningd *ld, struct peer *peer);
|
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
/* Returns true if these contain any unsupported features. */
|
|
|
|
bool unsupported_features(const u8 *gfeatures, const u8 *lfeatures);
|
|
|
|
|
|
|
|
/* For sending our features: tal_len() returns length. */
|
|
|
|
u8 *get_supported_global_features(const tal_t *ctx);
|
|
|
|
u8 *get_supported_local_features(const tal_t *ctx);
|
|
|
|
|
2017-06-27 04:55:01 +02:00
|
|
|
/* Could be configurable. */
|
|
|
|
#define OUR_CHANNEL_FLAGS CHANNEL_FLAGS_ANNOUNCE_CHANNEL
|
|
|
|
|
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. */
|
2017-06-27 05:51:05 +02:00
|
|
|
void peer_fail_permanent(struct peer *peer, const u8 *msg TAKES);
|
2017-11-02 00:54:00 +01:00
|
|
|
/* Version where we supply the reason string. */
|
|
|
|
void peer_fail_permanent_str(struct peer *peer, const char *str TAKES);
|
2017-06-20 08:17:03 +02:00
|
|
|
/* 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);
|
2017-11-10 03:01:10 +01:00
|
|
|
|
|
|
|
void free_htlcs(struct lightningd *ld, const struct peer *peer);
|
2017-01-10 06:08:33 +01:00
|
|
|
#endif /* LIGHTNING_LIGHTNINGD_PEER_CONTROL_H */
|