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>
|
2022-07-04 05:49:38 +02:00
|
|
|
#include <common/json_parse.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
|
|
|
|
2023-03-20 01:19:51 +01:00
|
|
|
struct channel_type;
|
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 {
|
2023-01-18 06:04:32 +01:00
|
|
|
/* Master context (we're in the hashtable ld->peers) */
|
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
|
|
|
|
2022-07-18 14:12:27 +02:00
|
|
|
/* Connection counter from connectd. */
|
|
|
|
u64 connectd_counter;
|
|
|
|
|
2022-07-28 03:30:36 +02:00
|
|
|
/* 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 14:12:28 +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-07-16 06:49:31 +02:00
|
|
|
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 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
|
|
|
|
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 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,
|
2023-06-05 18:17:36 +02:00
|
|
|
const u8 *their_features,
|
2021-03-25 05:13:12 +01:00
|
|
|
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-07-16 06:49:31 +02:00
|
|
|
/* connectd tells us what peer is doing */
|
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-07-18 14:12:18 +02:00
|
|
|
void peer_spoke(struct lightningd *ld, const u8 *msg);
|
|
|
|
|
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
|
|
|
|
2023-01-18 06:04:32 +01:00
|
|
|
/* When database first writes peer into db, it sets the dbid */
|
|
|
|
void peer_set_dbid(struct peer *peer, u64 dbid);
|
|
|
|
|
2022-07-14 09:41:11 +02:00
|
|
|
/* At startup, re-send any transactions we want bitcoind to have */
|
|
|
|
void resend_closing_transactions(struct lightningd *ld);
|
|
|
|
|
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);
|
|
|
|
|
2023-03-20 01:19:51 +01:00
|
|
|
/* Add channel_type object */
|
|
|
|
void json_add_channel_type(struct json_stream *response,
|
|
|
|
const char *fieldname,
|
|
|
|
const struct channel_type *channel_type);
|
|
|
|
|
2022-07-09 07:53:20 +02:00
|
|
|
/* Ancient (0.7.0 and before) releases could create invalid commitment txs! */
|
|
|
|
bool invalid_last_tx(const struct bitcoin_tx *tx);
|
|
|
|
|
2023-01-18 06:04:32 +01:00
|
|
|
static const struct node_id *peer_node_id(const struct peer *peer)
|
|
|
|
{
|
|
|
|
return &peer->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool peer_node_id_eq(const struct peer *peer,
|
|
|
|
const struct node_id *node_id)
|
|
|
|
{
|
|
|
|
return node_id_eq(&peer->id, node_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Defines struct peer_node_id_map */
|
|
|
|
HTABLE_DEFINE_TYPE(struct peer,
|
|
|
|
peer_node_id, node_id_hash, peer_node_id_eq,
|
|
|
|
peer_node_id_map);
|
|
|
|
|
2023-01-18 06:04:32 +01:00
|
|
|
static inline size_t dbid_hash(u64 dbid)
|
|
|
|
{
|
|
|
|
return siphash24(siphash_seed(), &dbid, sizeof(dbid));
|
|
|
|
}
|
|
|
|
|
|
|
|
static u64 peer_dbid(const struct peer *peer)
|
|
|
|
{
|
|
|
|
assert(peer->dbid);
|
|
|
|
return peer->dbid;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool peer_dbid_eq(const struct peer *peer, u64 dbid)
|
|
|
|
{
|
|
|
|
return peer->dbid == dbid;
|
|
|
|
}
|
|
|
|
/* Defines struct peer_dbid_map */
|
|
|
|
HTABLE_DEFINE_TYPE(struct peer,
|
|
|
|
peer_dbid, dbid_hash, peer_dbid_eq,
|
|
|
|
peer_dbid_map);
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
#endif /* LIGHTNING_LIGHTNINGD_PEER_CONTROL_H */
|