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
|
|
|
|
2023-03-20 10:49:51 +10:30
|
|
|
struct channel_type;
|
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 {
|
2023-01-18 15:34:32 +10:30
|
|
|
/* Master context (we're in the hashtable ld->peers) */
|
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
|
|
|
|
|
|
|
/* Swallow incoming HTLCs (for testing) */
|
2023-09-21 15:06:28 +09:30
|
|
|
bool dev_ignore_htlcs;
|
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,
|
connectd: fix transient memleak report.
We left their_features dangling allocated off the hook_payload, whereas
we explicitly move it to the peer, so steal/take it onto that.
```
- Node /tmp/ltests-gh55a_h2/test_connection_moved_1/lightning-2/ has memory leaks: [
{
"backtrace": [
"/home/runner/work/lightning/lightning/ccan/ccan/tal/tal.c:477 (tal_alloc_)",
"/home/runner/work/lightning/lightning/ccan/ccan/tal/tal.c:506 (tal_alloc_arr_)",
"/home/runner/work/lightning/lightning/connectd/connectd_wiregen.c:410 (fromwire_connectd_peer_connected)",
"/home/runner/work/lightning/lightning/lightningd/peer_control.c:1418 (peer_connected)",
"/home/runner/work/lightning/lightning/lightningd/connect_control.c:592 (connectd_msg)",
"/home/runner/work/lightning/lightning/lightningd/subd.c:557 (sd_msg_read)",
"/home/runner/work/lightning/lightning/ccan/ccan/io/io.c:59 (next_plan)",
"/home/runner/work/lightning/lightning/ccan/ccan/io/io.c:407 (do_plan)",
"/home/runner/work/lightning/lightning/ccan/ccan/io/io.c:417 (io_ready)",
"/home/runner/work/lightning/lightning/ccan/ccan/io/poll.c:453 (io_loop)",
"/home/runner/work/lightning/lightning/lightningd/io_loop_with_timers.c:22 (io_loop_with_timers)",
"/home/runner/work/lightning/lightning/lightningd/lightningd.c:1243 (main)"
],
"label": "connectd/connectd_wiregen.c:410:u8[]",
"parents": [
"lightningd/peer_control.c:1415:struct peer_connected_hook_payload",
"lightningd/plugin_hook.c:260:struct plugin_hook_request **NOTLEAK**",
"lightningd/plugin_hook.c:87:struct hook_instance *[] **NOTLEAK**"
],
"value": "0x5582622b1ff8"
}
]
```
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2023-07-21 07:15:48 +09:30
|
|
|
const u8 *their_features TAKES,
|
2021-03-25 14:43:12 +10:30
|
|
|
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 char *desc,
|
2023-10-22 14:37:32 +10:30
|
|
|
const u8 *err_for_them,
|
|
|
|
bool disconnect,
|
|
|
|
bool warning);
|
2018-02-21 07:29:09 +10:30
|
|
|
|
|
|
|
u8 *p2wpkh_for_keyidx(const tal_t *ctx, struct lightningd *ld, u64 keyidx);
|
2023-07-11 05:29:41 +09:30
|
|
|
u8 *p2tr_for_keyidx(const tal_t *ctx, struct lightningd *ld, u64 keyidx);
|
2018-02-21 07:29:09 +10:30
|
|
|
|
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
|
|
|
|
2023-01-18 15:34:32 +10:30
|
|
|
/* When database first writes peer into db, it sets the dbid */
|
|
|
|
void peer_set_dbid(struct peer *peer, u64 dbid);
|
|
|
|
|
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
|
|
|
|
2023-07-27 14:37:52 -07:00
|
|
|
void update_channel_from_inflight(struct lightningd *ld,
|
|
|
|
struct channel *channel,
|
|
|
|
const struct channel_inflight *inflight);
|
|
|
|
|
2018-02-21 07:29:04 +10:30
|
|
|
void channel_watch_funding(struct lightningd *ld, struct channel *channel);
|
2023-10-02 09:29:51 +10:30
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2023-10-02 09:29:49 +10:30
|
|
|
/* JSON parameter by channel_id or scid (caller must check state!) */
|
2021-09-15 10:29:23 +09:30
|
|
|
struct command_result *
|
|
|
|
command_find_channel(struct command *cmd,
|
2023-10-02 09:29:49 +10:30
|
|
|
const char *name,
|
2021-09-15 10:29:23 +09:30
|
|
|
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);
|
|
|
|
|
2023-01-18 15:34:32 +10:30
|
|
|
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 15:34:32 +10:30
|
|
|
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 15:38:33 +10:30
|
|
|
#endif /* LIGHTNING_LIGHTNINGD_PEER_CONTROL_H */
|