Files
palladum-lightning/lightningd/peer_control.h
Rusty Russell 65dccea5bd pytest: fix flake in test_reconnect_signed
We can fail the fundchannel because of a reconnect race: funder tells
us to reconnect so fast that we are still cleaning up from last time.

We deliberately defer clean up, to give the subds a chance to process any
final messages.  However, on reconnection we force them to exit immediately:
but this causes new `connect` commands to see the exit and fail.

The workaround is to do this cleanup when a `connect` command is
issued (as well as the current case, which covers an automatic
reconnection or an incoming reconnection)

```
2025-05-09T00:40:37.1769508Z     def test_reconnect_signed(node_factory):
2025-05-09T00:40:37.1770273Z         # This will fail *after* both sides consider channel opening.
2025-05-09T00:40:37.1770850Z         disconnects = ['<WIRE_FUNDING_SIGNED']
2025-05-09T00:40:37.1771298Z         if EXPERIMENTAL_DUAL_FUND:
2025-05-09T00:40:37.1771735Z             disconnects = ['<WIRE_COMMITMENT_SIGNED']
2025-05-09T00:40:37.1772155Z     
2025-05-09T00:40:37.1772598Z         l1 = node_factory.get_node(may_reconnect=True, disconnect=disconnects)
2025-05-09T00:40:37.1773210Z         l2 = node_factory.get_node(may_reconnect=True)
2025-05-09T00:40:37.1773632Z     
2025-05-09T00:40:37.1773917Z         l1.fundwallet(2000000)
2025-05-09T00:40:37.1774268Z     
2025-05-09T00:40:37.1774611Z         l1.rpc.connect(l2.info['id'], 'localhost', l2.port)
2025-05-09T00:40:37.1775151Z >       l1.rpc.fundchannel(l2.info['id'], CHANNEL_SIZE)
2025-05-09T00:40:37.1775388Z 
2025-05-09T00:40:37.1775485Z tests/test_connection.py:667:
...
2025-05-09T00:40:37.1799527Z >           raise RpcError(method, payload, resp['error'])
2025-05-09T00:40:37.1800993Z E           pyln.client.lightning.RpcError: RPC call failed: method: fundchannel, payload: {'id': '022d223620a359a47ff7f7ac447c85c46c923da53389221a0054c11c1e3ca31d59', 'amount': 50000, 'announce': True}, error: {'code': -1, 'message': 'Disconnected', 'data': {'id': '022d223620a359a47ff7f7ac447c85c46c923da53389221a0054c11c1e3ca31d59', 'method': 'openchannel_update'}}
```

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2025-05-11 11:25:40 +09:30

211 lines
6.3 KiB
C

#ifndef LIGHTNING_LIGHTNINGD_PEER_CONTROL_H
#define LIGHTNING_LIGHTNINGD_PEER_CONTROL_H
#include "config.h"
#include <ccan/compiler/compiler.h>
#include <ccan/crypto/shachain/shachain.h>
#include <ccan/list/list.h>
#include <common/channel_config.h>
#include <common/htlc.h>
#include <common/json_parse.h>
#include <common/node_id.h>
#include <common/wireaddr.h>
#include <wallet/wallet.h>
struct channel_type;
struct peer_fd;
struct wally_psbt;
struct peer {
/* Master context (we're in the hashtable ld->peers) */
struct lightningd *ld;
/* Database ID of the peer */
u64 dbid;
/* ID of peer */
struct node_id id;
/* Connection counter from connectd. */
u64 connectd_counter;
/* Last reconnect: if it's recent, we delay by reconnect_delay,
* doubling each time. */
struct timeabs last_connect_attempt;
u32 reconnect_delay;
/* Our channels */
struct list_head channels;
/* Are we connected? */
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;
/* Our (only) uncommitted channel, still opening. */
struct uncommitted_channel *uncommitted_channel;
/* Where we connected to, or it connected from. */
struct wireaddr_internal addr;
bool connected_incoming;
/* If we ever successfully connected out to an address, this is non-NULL */
struct wireaddr *last_known_addr;
/* They send what they see as our address as remote_addr */
struct wireaddr *remote_addr;
/* We keep a copy of their feature bits */
const u8 *their_features;
/* If we open a channel our direction will be this */
u8 direction;
/* Swallow incoming HTLCs (for testing) */
bool dev_ignore_htlcs;
};
struct peer *find_peer_by_dbid(struct lightningd *ld, u64 dbid);
struct peer *new_peer(struct lightningd *ld, u64 dbid,
const struct node_id *id,
const struct wireaddr_internal *addr,
const struct wireaddr *last_known_addr,
const u8 *their_features TAKES,
bool connected_incoming);
/* Last one out deletes peer. Also removes from db. */
void maybe_delete_peer(struct peer *peer);
struct peer *peer_by_id(struct lightningd *ld, const struct node_id *id);
struct peer *peer_from_json(struct lightningd *ld,
const char *buffer,
const jsmntok_t *peeridtok);
/* connectd tells us what peer is doing */
void peer_connected(struct lightningd *ld, const u8 *msg);
void peer_disconnect_done(struct lightningd *ld, const u8 *msg);
void peer_spoke(struct lightningd *ld, const u8 *msg);
/* Could be configurable. */
#define OUR_CHANNEL_FLAGS CHANNEL_FLAGS_ANNOUNCE_CHANNEL
void channel_errmsg(struct channel *channel,
struct peer_fd *peer_fd,
const char *desc,
const u8 *err_for_them,
bool disconnect,
bool warning);
/* Helper to create a peer_fd and an other fd from socketpair.
* Logs error to channel if it fails, and if warning non-NULL, creates
* a warning message */
struct peer_fd *sockpair(const tal_t *ctx, struct channel *channel,
int *otherfd, const u8 **warning);
u8 *p2wpkh_for_keyidx(const tal_t *ctx, struct lightningd *ld, u64 keyidx);
u8 *p2tr_for_keyidx(const tal_t *ctx, struct lightningd *ld, u64 keyidx);
/* We've loaded peers from database, set them going. */
void setup_peers(struct lightningd *ld);
/* When database first writes peer into db, it sets the dbid */
void peer_set_dbid(struct peer *peer, u64 dbid);
/* At startup, re-send any transactions we want bitcoind to have */
void resend_closing_transactions(struct lightningd *ld);
/* Initiate the close of a channel, maybe broadcast. If we've seen a
* unilateral close, pass it here (means we don't need to broadcast
* our own, or any anchors). */
void drop_to_chain(struct lightningd *ld, struct channel *channel,
bool cooperative,
const struct bitcoin_tx *unilateral_tx);
void update_channel_from_inflight(struct lightningd *ld,
struct channel *channel,
const struct channel_inflight *inflight);
void channel_watch_funding(struct lightningd *ld, struct channel *channel);
/* If this channel has a "wrong funding" shutdown, watch that too. */
void channel_watch_wrong_funding(struct lightningd *ld, struct channel *channel);
/* 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? */
struct amount_msat channel_amount_receivable(const struct channel *channel);
/* Pull peers, channels and HTLCs from db, and wire them up.
* Returns any HTLCs we have to resubmit via htlcs_resubmit.
*
* As a side-effect, count total channels loaded into *num_channels.
*/
struct htlc_in_map *load_channels_from_wallet(struct lightningd *ld,
size_t *num_channels);
struct leak_detect;
void peer_dev_memleak(struct lightningd *ld, struct leak_detect *leaks);
/* Triggered at each new block. */
void waitblockheight_notify_new_block(struct lightningd *ld,
u32 block_height);
/* JSON parameter by channel_id or scid (caller must check state!) */
struct command_result *
command_find_channel(struct command *cmd,
const char *name,
const char *buffer, const jsmntok_t *tok,
struct channel **channel);
/* We do this lazily, when reconnecting */
void peer_channels_cleanup(struct peer *peer);
/* Ancient (0.7.0 and before) releases could create invalid commitment txs! */
bool invalid_last_tx(const struct bitcoin_tx *tx);
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_NODUPS_TYPE(struct peer,
peer_node_id, node_id_hash, peer_node_id_eq,
peer_node_id_map);
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_NODUPS_TYPE(struct peer,
peer_dbid, dbid_hash, peer_dbid_eq,
peer_dbid_map);
#endif /* LIGHTNING_LIGHTNINGD_PEER_CONTROL_H */