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>
|
2019-04-08 11:58:32 +02:00
|
|
|
#include <common/node_id.h>
|
2017-10-23 06:17:38 +02:00
|
|
|
#include <common/wireaddr.h>
|
2017-07-19 16:55:47 +02:00
|
|
|
#include <wallet/wallet.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2022-01-11 02:13:59 +01:00
|
|
|
struct peer_fd;
|
2020-09-10 21:41:42 +02:00
|
|
|
struct wally_psbt;
|
2017-02-24 06:52:56 +01:00
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
struct peer {
|
2017-12-15 11:22:57 +01:00
|
|
|
/* Inside ld->peers. */
|
|
|
|
struct list_node list;
|
|
|
|
|
|
|
|
/* Master context */
|
2017-01-10 06:08:33 +01:00
|
|
|
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 */
|
2019-04-08 11:58:32 +02:00
|
|
|
struct node_id id;
|
2017-06-06 05:08:42 +02:00
|
|
|
|
2018-02-12 11:12:55 +01:00
|
|
|
/* Our channels */
|
|
|
|
struct list_head channels;
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2022-03-22 09:49:13 +01:00
|
|
|
/* Are we connected? */
|
2022-03-22 21:26:30 +01:00
|
|
|
bool is_connected;
|
2022-03-22 09:49:13 +01:00
|
|
|
|
2018-02-19 02:06:02 +01:00
|
|
|
/* Our (only) uncommitted channel, still opening. */
|
|
|
|
struct uncommitted_channel *uncommitted_channel;
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
/* Where we connected to, or it connected from. */
|
2018-05-07 06:29:21 +02:00
|
|
|
struct wireaddr_internal addr;
|
2021-03-25 05:13:12 +01:00
|
|
|
bool connected_incoming;
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2018-07-24 08:18:59 +02:00
|
|
|
/* We keep a copy of their feature bits */
|
2020-04-03 02:03:59 +02:00
|
|
|
const u8 *their_features;
|
2018-07-24 08:18:59 +02:00
|
|
|
|
2018-01-23 22:11:44 +01:00
|
|
|
/* If we open a channel our direction will be this */
|
|
|
|
u8 direction;
|
2018-04-03 09:19:42 +02:00
|
|
|
|
|
|
|
#if DEVELOPER
|
|
|
|
/* Swallow incoming HTLCs (for testing) */
|
|
|
|
bool ignore_htlcs;
|
|
|
|
#endif
|
2017-01-10 06:08:33 +01:00
|
|
|
};
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2018-02-12 11:12:55 +01:00
|
|
|
struct peer *find_peer_by_dbid(struct lightningd *ld, u64 dbid);
|
|
|
|
|
|
|
|
struct peer *new_peer(struct lightningd *ld, u64 dbid,
|
2019-04-08 11:58:32 +02:00
|
|
|
const struct node_id *id,
|
2021-03-25 05:13:12 +01:00
|
|
|
const struct wireaddr_internal *addr,
|
|
|
|
bool connected_incoming);
|
2017-05-22 13:24:59 +02:00
|
|
|
|
2018-08-02 08:49:55 +02:00
|
|
|
/* Last one out deletes peer. Also removes from db. */
|
|
|
|
void maybe_delete_peer(struct peer *peer);
|
2018-02-14 02:53:04 +01:00
|
|
|
|
2019-04-08 11:58:32 +02:00
|
|
|
struct peer *peer_by_id(struct lightningd *ld, const struct node_id *id);
|
2017-04-01 12:24:59 +02:00
|
|
|
struct peer *peer_from_json(struct lightningd *ld,
|
|
|
|
const char *buffer,
|
2018-07-20 03:14:02 +02:00
|
|
|
const jsmntok_t *peeridtok);
|
2017-02-24 06:52:56 +01:00
|
|
|
|
2022-03-22 21:27:29 +01:00
|
|
|
void peer_connected(struct lightningd *ld, const u8 *msg);
|
2022-03-22 21:26:30 +01:00
|
|
|
void peer_disconnect_done(struct lightningd *ld, const u8 *msg);
|
2022-03-22 21:27:29 +01:00
|
|
|
void peer_active(struct lightningd *ld, const u8 *msg, int peer_fd);
|
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
|
|
|
|
2017-06-27 04:55:01 +02:00
|
|
|
/* Could be configurable. */
|
|
|
|
#define OUR_CHANNEL_FLAGS CHANNEL_FLAGS_ANNOUNCE_CHANNEL
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
void channel_errmsg(struct channel *channel,
|
2022-01-11 02:13:59 +01:00
|
|
|
struct peer_fd *peer_fd,
|
2018-02-20 21:59:09 +01:00
|
|
|
const struct channel_id *channel_id,
|
|
|
|
const char *desc,
|
2021-02-02 13:47:01 +01:00
|
|
|
bool warning,
|
2018-02-20 21:59:09 +01:00
|
|
|
const u8 *err_for_them);
|
|
|
|
|
|
|
|
u8 *p2wpkh_for_keyidx(const tal_t *ctx, struct lightningd *ld, u64 keyidx);
|
|
|
|
|
2018-01-03 06:26:44 +01:00
|
|
|
/* We've loaded peers from database, set them going. */
|
2022-03-22 21:30:59 +01:00
|
|
|
void setup_peers(struct lightningd *ld);
|
2018-01-03 06:26:44 +01:00
|
|
|
|
2018-04-10 08:03:15 +02:00
|
|
|
void drop_to_chain(struct lightningd *ld, struct channel *channel, bool cooperative);
|
2018-02-12 11:13:04 +01:00
|
|
|
|
2018-02-20 21:59:04 +01:00
|
|
|
void channel_watch_funding(struct lightningd *ld, struct channel *channel);
|
2021-03-15 21:26:13 +01:00
|
|
|
/* If this channel has a "wrong funding" shutdown, watch that too. */
|
|
|
|
void channel_watch_wrong_funding(struct lightningd *ld, struct channel *channel);
|
2018-09-03 03:08:53 +02:00
|
|
|
|
2022-03-22 23:59:20 +01:00
|
|
|
/* How much can we spend in this channel? */
|
|
|
|
struct amount_msat channel_amount_spendable(const struct channel *channel);
|
|
|
|
|
|
|
|
/* How much can we receive in this channel? */
|
2020-09-08 05:48:35 +02:00
|
|
|
struct amount_msat channel_amount_receivable(const struct channel *channel);
|
|
|
|
|
2019-08-10 07:24:57 +02:00
|
|
|
/* Pull peers, channels and HTLCs from db, and wire them up.
|
|
|
|
* Returns any HTLCs we have to resubmit via htlcs_resubmit. */
|
|
|
|
struct htlc_in_map *load_channels_from_wallet(struct lightningd *ld);
|
2018-11-22 03:17:29 +01:00
|
|
|
|
|
|
|
#if DEVELOPER
|
2022-03-08 01:14:41 +01:00
|
|
|
struct leak_detect;
|
|
|
|
void peer_dev_memleak(struct lightningd *ld, struct leak_detect *leaks);
|
2021-07-13 23:04:30 +02:00
|
|
|
#endif /* DEVELOPER */
|
|
|
|
|
2019-12-26 11:19:09 +01:00
|
|
|
/* Triggered at each new block. */
|
|
|
|
void waitblockheight_notify_new_block(struct lightningd *ld,
|
|
|
|
u32 block_height);
|
|
|
|
|
2021-09-15 02:59:23 +02:00
|
|
|
|
|
|
|
/* JSON parameter by channel_id or scid */
|
|
|
|
struct command_result *
|
|
|
|
command_find_channel(struct command *cmd,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
struct channel **channel);
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
#endif /* LIGHTNING_LIGHTNINGD_PEER_CONTROL_H */
|