Skip to content

Commit 1d4783a

Browse files
rustyrussellendothermicdev
authored andcommitted
lightningd: send CHANNEL_REESTABLISH ourselves on closed channels.
We used to fire up channeld to send this, but: 1. That's silly, we have all the information to make it ourselves. 2. We didn't do it if there was an error on the channel, which as of 24.02 there always is! 3. When it did work, running channeld *stops* onchaind, indefinitely slowing recovery. Fixes: Blockstream/greenlight#433 Changelog-Fixed: Protocol: we once again send CHANNEL_REESTABLISH responses on closing channels. Signed-off-by: Rusty Russell <[email protected]>
1 parent f9e7d56 commit 1d4783a

File tree

4 files changed

+53
-21
lines changed

4 files changed

+53
-21
lines changed

lightningd/peer_control.c

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,6 +1721,46 @@ void peer_connected(struct lightningd *ld, const u8 *msg)
17211721
plugin_hook_call_peer_connected(ld, cmd_id, hook_payload);
17221722
}
17231723

1724+
static void send_reestablish(struct lightningd *ld, struct channel *channel)
1725+
{
1726+
u8 *msg;
1727+
struct secret last_remote_per_commit_secret;
1728+
u64 num_revocations;
1729+
1730+
/* BOLT #2:
1731+
* - if `next_revocation_number` equals 0:
1732+
* - MUST set `your_last_per_commitment_secret` to all zeroes
1733+
* - otherwise:
1734+
* - MUST set `your_last_per_commitment_secret` to the last
1735+
* `per_commitment_secret` it received
1736+
*/
1737+
num_revocations = revocations_received(&channel->their_shachain.chain);
1738+
if (num_revocations == 0)
1739+
memset(&last_remote_per_commit_secret, 0,
1740+
sizeof(last_remote_per_commit_secret));
1741+
else if (!shachain_get_secret(&channel->their_shachain.chain,
1742+
num_revocations-1,
1743+
&last_remote_per_commit_secret)) {
1744+
channel_fail_permanent(channel,
1745+
REASON_LOCAL,
1746+
"Could not get revocation secret %"PRIu64,
1747+
num_revocations-1);
1748+
return;
1749+
}
1750+
1751+
msg = towire_channel_reestablish(tmpctx, &channel->cid,
1752+
channel->next_index[LOCAL],
1753+
num_revocations,
1754+
&last_remote_per_commit_secret,
1755+
&channel->channel_info.remote_per_commit,
1756+
/* No upgrade for you, since we're closed! */
1757+
NULL);
1758+
subd_send_msg(ld->connectd,
1759+
take(towire_connectd_peer_send_msg(NULL, &channel->peer->id,
1760+
channel->peer->connectd_counter,
1761+
msg)));
1762+
}
1763+
17241764
/* connectd tells us a peer has a message and we've not already attached
17251765
* a subd. Normally this is a race, but it happens for real when opening
17261766
* a new channel, or referring to a channel we no longer want to talk to
@@ -1749,6 +1789,11 @@ void peer_spoke(struct lightningd *ld, const u8 *msg)
17491789
/* Do we know what channel they're talking about? */
17501790
channel = find_channel_by_id(peer, &channel_id);
17511791
if (channel) {
1792+
/* In this case, we'll send an error below, but send reestablish reply first
1793+
* in case they lost their state and need it */
1794+
if (msgtype == WIRE_CHANNEL_REESTABLISH && channel_state_closed(channel->state))
1795+
send_reestablish(ld, channel);
1796+
17521797
/* If we have a canned error for this channel, send it now */
17531798
if (channel->error) {
17541799
error = channel->error;
@@ -1790,26 +1835,6 @@ void peer_spoke(struct lightningd *ld, const u8 *msg)
17901835
return;
17911836
}
17921837

1793-
if (msgtype == WIRE_CHANNEL_REESTABLISH) {
1794-
log_debug(channel->log,
1795-
"Reestablish on %s channel: using channeld to reply",
1796-
channel_state_name(channel));
1797-
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds) != 0) {
1798-
log_broken(channel->log,
1799-
"Failed to create socketpair: %s",
1800-
strerror(errno));
1801-
error = towire_warningfmt(tmpctx, &channel->cid,
1802-
"Trouble in paradise?");
1803-
goto send_error;
1804-
}
1805-
if (peer_start_channeld(channel, new_peer_fd(tmpctx, fds[0]), NULL, true, true)) {
1806-
goto tell_connectd;
1807-
}
1808-
/* FIXME: Send informative error? */
1809-
close(fds[1]);
1810-
return;
1811-
}
1812-
18131838
/* Send generic error. */
18141839
error = towire_errorfmt(tmpctx, &channel_id,
18151840
"channel in state %s",

lightningd/test/run-invoice-select-inchan.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,11 @@ void report_subd_memleak(struct leak_detect *leak_detect UNNEEDED, struct subd *
901901
void resolve_close_command(struct lightningd *ld UNNEEDED, struct channel *channel UNNEEDED,
902902
bool cooperative UNNEEDED, const struct bitcoin_tx *close_tx UNNEEDED)
903903
{ fprintf(stderr, "resolve_close_command called!\n"); abort(); }
904+
/* Generated stub for shachain_get_secret */
905+
bool shachain_get_secret(const struct shachain *shachain UNNEEDED,
906+
u64 commit_num UNNEEDED,
907+
struct secret *preimage UNNEEDED)
908+
{ fprintf(stderr, "shachain_get_secret called!\n"); abort(); }
904909
/* Generated stub for start_leak_request */
905910
void start_leak_request(const struct subd_req *req UNNEEDED,
906911
struct leak_detect *leak_detect UNNEEDED)

tests/test_closing.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4126,7 +4126,6 @@ def test_anchorspend_using_to_remote(node_factory, bitcoind, anchors):
41264126
bitcoind.generate_block(1, wait_for_mempool=2)
41274127

41284128

4129-
@pytest.mark.xfail(strict=True)
41304129
def test_onchain_reestablish_reply(node_factory, bitcoind, executor):
41314130
l1, l2, l3 = node_factory.line_graph(3, opts={'may_reconnect': True,
41324131
'dev-no-reconnect': None})

wallet/test/run-wallet.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,9 @@ void topology_add_sync_waiter_(const tal_t *ctx UNNEEDED,
986986
/* Generated stub for towire_announcement_signatures */
987987
u8 *towire_announcement_signatures(const tal_t *ctx UNNEEDED, const struct channel_id *channel_id UNNEEDED, struct short_channel_id short_channel_id UNNEEDED, const secp256k1_ecdsa_signature *node_signature UNNEEDED, const secp256k1_ecdsa_signature *bitcoin_signature UNNEEDED)
988988
{ fprintf(stderr, "towire_announcement_signatures called!\n"); abort(); }
989+
/* Generated stub for towire_channel_reestablish */
990+
u8 *towire_channel_reestablish(const tal_t *ctx UNNEEDED, const struct channel_id *channel_id UNNEEDED, u64 next_commitment_number UNNEEDED, u64 next_revocation_number UNNEEDED, const struct secret *your_last_per_commitment_secret UNNEEDED, const struct pubkey *my_current_per_commitment_point UNNEEDED, const struct tlv_channel_reestablish_tlvs *channel_reestablish UNNEEDED)
991+
{ fprintf(stderr, "towire_channel_reestablish called!\n"); abort(); }
989992
/* Generated stub for towire_channeld_dev_memleak */
990993
u8 *towire_channeld_dev_memleak(const tal_t *ctx UNNEEDED)
991994
{ fprintf(stderr, "towire_channeld_dev_memleak called!\n"); abort(); }

0 commit comments

Comments
 (0)