2017-01-10 15:38:33 +10:30
|
|
|
#ifndef LIGHTNING_LIGHTNINGD_PEER_CONTROL_H
|
|
|
|
#define LIGHTNING_LIGHTNINGD_PEER_CONTROL_H
|
|
|
|
#include "config.h"
|
2017-01-10 15:38:33 +10:30
|
|
|
#include <ccan/compiler/compiler.h>
|
2017-06-20 15:21:03 +09:30
|
|
|
#include <ccan/crypto/shachain/shachain.h>
|
2017-04-01 20:54:59 +10:30
|
|
|
#include <ccan/list/list.h>
|
2017-08-29 01:35:01 +09:30
|
|
|
#include <common/channel_config.h>
|
2017-08-29 01:34:01 +09:30
|
|
|
#include <common/htlc.h>
|
2022-07-04 13:19:38 +09:30
|
|
|
#include <common/json_parse.h>
|
2019-04-08 19:28:32 +09:30
|
|
|
#include <common/node_id.h>
|
2017-10-23 14:47:38 +10:30
|
|
|
#include <common/wireaddr.h>
|
2017-07-19 16:55:47 +02:00
|
|
|
#include <wallet/wallet.h>
|
2017-01-10 15:38:33 +10:30
|
|
|
|
2022-01-11 11:43:59 +10:30
|
|
|
struct peer_fd;
|
2020-09-10 14:41:42 -05:00
|
|
|
struct wally_psbt;
|
2017-02-24 16:22:56 +10:30
|
|
|
|
2017-01-10 15:38:33 +10:30
|
|
|
struct peer {
|
2017-12-15 20:52:57 +10:30
|
|
|
/* Inside ld->peers. */
|
|
|
|
struct list_node list;
|
|
|
|
|
|
|
|
/* Master context */
|
2017-01-10 15:38:33 +10:30
|
|
|
struct lightningd *ld;
|
|
|
|
|
2017-08-14 22:06:59 +02:00
|
|
|
/* Database ID of the peer */
|
|
|
|
u64 dbid;
|
|
|
|
|
2017-06-06 12:38:42 +09:30
|
|
|
/* ID of peer */
|
2019-04-08 19:28:32 +09:30
|
|
|
struct node_id id;
|
2017-06-06 12:38:42 +09:30
|
|
|
|
2022-07-18 21:42:27 +09:30
|
|
|
/* Connection counter from connectd. */
|
|
|
|
u64 connectd_counter;
|
|
|
|
|
2022-07-28 11:00:36 +09:30
|
|
|
/* Last reconnect: if it's recent, we delay by reconnect_delay,
|
|
|
|
* doubling each time. */
|
|
|
|
struct timeabs last_connect_attempt;
|
|
|
|
u32 reconnect_delay;
|
2022-07-18 21:42:28 +09:30
|
|
|
|
2018-02-12 20:42:55 +10:30
|
|
|
/* Our channels */
|
|
|
|
struct list_head channels;
|
2017-01-10 15:38:33 +10:30
|
|
|
|
2022-03-22 19:19:13 +10:30
|
|
|
/* Are we connected? */
|
2022-07-16 14:19:31 +09:30
|
|
|
enum {
|
|
|
|
/* Connectd said we're connecting, we called hooks... */
|
|
|
|
PEER_CONNECTING,
|
|
|
|
/* Hooks succeeded, we're connected. */
|
|
|
|
PEER_CONNECTED,
|
|
|
|
/* Start state, also connectd told us we're disconnected */
|
|
|
|
PEER_DISCONNECTED,
|
|
|
|
} connected;
|
2022-03-22 19:19:13 +10:30
|
|
|
|
2018-02-19 11:36:02 +10:30
|
|
|
/* Our (only) uncommitted channel, still opening. */
|
|
|
|
struct uncommitted_channel *uncommitted_channel;
|
|
|
|
|
2017-01-10 15:38:33 +10:30
|
|
|
/* Where we connected to, or it connected from. */
|
2018-05-07 13:59:21 +09:30
|
|
|
struct wireaddr_internal addr;
|
2021-03-25 14:43:12 +10:30
|
|
|
bool connected_incoming;
|
2017-01-10 15:38:33 +10:30
|
|
|
|
2022-06-01 13:18:55 +02:00
|
|
|
/* They send what they see as our address as remote_addr */
|
|
|
|
struct wireaddr *remote_addr;
|
|
|
|
|
2018-07-24 15:48:59 +09:30
|
|
|
/* We keep a copy of their feature bits */
|
2020-04-03 10:33:59 +10:30
|
|
|
const u8 *their_features;
|
2018-07-24 15:48:59 +09:30
|
|
|
|
2018-01-23 22:11:44 +01:00
|
|
|
/* If we open a channel our direction will be this */
|
|
|
|
u8 direction;
|
2018-04-03 16:49:42 +09:30
|
|
|
|
|
|
|
#if DEVELOPER
|
|
|
|
/* Swallow incoming HTLCs (for testing) */
|
|
|
|
bool ignore_htlcs;
|
|
|
|
#endif
|
2017-01-10 15:38:33 +10:30
|
|
|
};
|
2017-01-10 15:38:33 +10:30
|
|
|
|
2018-02-12 20:42:55 +10:30
|
|
|
struct peer *find_peer_by_dbid(struct lightningd *ld, u64 dbid);
|
|
|
|
|
|
|
|
struct peer *new_peer(struct lightningd *ld, u64 dbid,
|
2019-04-08 19:28:32 +09:30
|
|
|
const struct node_id *id,
|
2021-03-25 14:43:12 +10:30
|
|
|
const struct wireaddr_internal *addr,
|
|
|
|
bool connected_incoming);
|
2017-05-22 20:54:59 +09:30
|
|
|
|
2018-08-02 16:19:55 +09:30
|
|
|
/* Last one out deletes peer. Also removes from db. */
|
|
|
|
void maybe_delete_peer(struct peer *peer);
|
2018-02-14 12:23:04 +10:30
|
|
|
|
2019-04-08 19:28:32 +09:30
|
|
|
struct peer *peer_by_id(struct lightningd *ld, const struct node_id *id);
|
2017-04-01 20:54:59 +10:30
|
|
|
struct peer *peer_from_json(struct lightningd *ld,
|
|
|
|
const char *buffer,
|
2018-07-19 20:14:02 -05:00
|
|
|
const jsmntok_t *peeridtok);
|
2017-02-24 16:22:56 +10:30
|
|
|
|
2022-07-16 14:19:31 +09:30
|
|
|
/* connectd tells us what peer is doing */
|
2022-03-23 06:57:29 +10:30
|
|
|
void peer_connected(struct lightningd *ld, const u8 *msg);
|
2022-03-23 06:56:30 +10:30
|
|
|
void peer_disconnect_done(struct lightningd *ld, const u8 *msg);
|
2022-07-18 21:42:18 +09:30
|
|
|
void peer_spoke(struct lightningd *ld, const u8 *msg);
|
|
|
|
|
2017-06-27 12:25:01 +09:30
|
|
|
/* Could be configurable. */
|
|
|
|
#define OUR_CHANNEL_FLAGS CHANNEL_FLAGS_ANNOUNCE_CHANNEL
|
|
|
|
|
2018-02-21 07:29:09 +10:30
|
|
|
void channel_errmsg(struct channel *channel,
|
2022-01-11 11:43:59 +10:30
|
|
|
struct peer_fd *peer_fd,
|
2018-02-21 07:29:09 +10:30
|
|
|
const struct channel_id *channel_id,
|
|
|
|
const char *desc,
|
2021-02-02 23:17:01 +10:30
|
|
|
bool warning,
|
2018-02-21 07:29:09 +10:30
|
|
|
const u8 *err_for_them);
|
|
|
|
|
|
|
|
u8 *p2wpkh_for_keyidx(const tal_t *ctx, struct lightningd *ld, u64 keyidx);
|
|
|
|
|
2018-01-03 15:56:44 +10:30
|
|
|
/* We've loaded peers from database, set them going. */
|
2022-03-23 07:00:59 +10:30
|
|
|
void setup_peers(struct lightningd *ld);
|
2018-01-03 15:56:44 +10:30
|
|
|
|
2022-07-14 17:11:11 +09:30
|
|
|
/* At startup, re-send any transactions we want bitcoind to have */
|
|
|
|
void resend_closing_transactions(struct lightningd *ld);
|
|
|
|
|
2018-04-10 06:03:15 +00:00
|
|
|
void drop_to_chain(struct lightningd *ld, struct channel *channel, bool cooperative);
|
2018-02-12 20:43:04 +10:30
|
|
|
|
2018-02-21 07:29:04 +10:30
|
|
|
void channel_watch_funding(struct lightningd *ld, struct channel *channel);
|
2021-03-16 06:56:13 +10:30
|
|
|
/* 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 10:38:53 +09:30
|
|
|
|
2022-03-23 09:29:20 +10:30
|
|
|
/* 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 13:18:35 +09:30
|
|
|
struct amount_msat channel_amount_receivable(const struct channel *channel);
|
|
|
|
|
2019-08-10 14:54:57 +09:30
|
|
|
/* 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 12:47:29 +10:30
|
|
|
|
|
|
|
#if DEVELOPER
|
2022-03-08 10:44:41 +10:30
|
|
|
struct leak_detect;
|
|
|
|
void peer_dev_memleak(struct lightningd *ld, struct leak_detect *leaks);
|
2021-07-13 16:04:30 -05:00
|
|
|
#endif /* DEVELOPER */
|
|
|
|
|
2019-12-26 10:19:09 +00:00
|
|
|
/* Triggered at each new block. */
|
|
|
|
void waitblockheight_notify_new_block(struct lightningd *ld,
|
|
|
|
u32 block_height);
|
|
|
|
|
2021-09-15 10:29:23 +09:30
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
2022-07-09 15:23:20 +09:30
|
|
|
/* Ancient (0.7.0 and before) releases could create invalid commitment txs! */
|
|
|
|
bool invalid_last_tx(const struct bitcoin_tx *tx);
|
|
|
|
|
2017-01-10 15:38:33 +10:30
|
|
|
#endif /* LIGHTNING_LIGHTNINGD_PEER_CONTROL_H */
|