diff --git a/channeld/channeld.c b/channeld/channeld.c index d1b898754..cbf4c1742 100644 --- a/channeld/channeld.c +++ b/channeld/channeld.c @@ -2682,7 +2682,7 @@ static bool fromwire_channel_reestablish_notlvs(const void *p, struct channel_id static void peer_reconnect(struct peer *peer, const struct secret *last_remote_per_commit_secret, - u8 *reestablish_only) + bool reestablish_only) { struct channel_id channel_id; /* Note: BOLT #2 uses these names! */ @@ -2820,23 +2820,23 @@ skip_tlvs: peer_billboard(false, "Sent reestablish, waiting for theirs"); - /* If they sent reestablish, we analyze it for courtesy, but also - * in case *they* are ahead of us! */ - if (reestablish_only) { - msg = reestablish_only; - goto got_reestablish; - } - /* Read until they say something interesting (don't forward * gossip *to* them yet: we might try sending channel_update * before we've reestablished channel). */ do { clean_tmpctx(); msg = peer_read(tmpctx, peer->pps); + + /* connectd promised us the msg was reestablish? */ + if (reestablish_only) { + if (fromwire_peektype(msg) != WIRE_CHANNEL_REESTABLISH) + status_failed(STATUS_FAIL_INTERNAL_ERROR, + "Expected reestablish, got: %s", + tal_hex(tmpctx, msg)); + } } while (handle_peer_error(peer->pps, &peer->channel_id, msg) || capture_premature_msg(&premature_msgs, msg)); -got_reestablish: #if EXPERIMENTAL_FEATURES recv_tlvs = tlv_channel_reestablish_tlvs_new(tmpctx); @@ -3730,7 +3730,7 @@ static void init_channel(struct peer *peer) secp256k1_ecdsa_signature *remote_ann_node_sig; secp256k1_ecdsa_signature *remote_ann_bitcoin_sig; struct penalty_base *pbases; - u8 *reestablish_only; + bool reestablish_only; struct channel_type *channel_type; u32 *dev_disable_commit; /* Always NULL */ bool dev_fast_gossip; diff --git a/channeld/channeld_wire.csv b/channeld/channeld_wire.csv index 7100e5c56..b2179908b 100644 --- a/channeld/channeld_wire.csv +++ b/channeld/channeld_wire.csv @@ -80,8 +80,7 @@ msgdata,channeld_init,dev_fail_process_onionpacket,bool, msgdata,channeld_init,dev_disable_commit,?u32, msgdata,channeld_init,num_penalty_bases,u32, msgdata,channeld_init,pbases,penalty_base,num_penalty_bases -msgdata,channeld_init,reestablish_only_len,u16, -msgdata,channeld_init,reestablish_only,u8,reestablish_only_len +msgdata,channeld_init,reestablish_only,bool, msgdata,channeld_init,channel_update_len,u16, msgdata,channeld_init,channel_update,u8,channel_update_len diff --git a/lightningd/channel_control.c b/lightningd/channel_control.c index 53b433967..7e28b7814 100644 --- a/lightningd/channel_control.c +++ b/lightningd/channel_control.c @@ -568,7 +568,7 @@ void peer_start_channeld(struct channel *channel, struct peer_fd *peer_fd, const u8 *fwd_msg, bool reconnected, - const u8 *reestablish_only) + bool reestablish_only) { u8 *initmsg; int hsmfd; diff --git a/lightningd/channel_control.h b/lightningd/channel_control.h index db8fa838b..365e87655 100644 --- a/lightningd/channel_control.h +++ b/lightningd/channel_control.h @@ -14,7 +14,7 @@ void peer_start_channeld(struct channel *channel, struct peer_fd *peer_fd, const u8 *fwd_msg, bool reconnected, - const u8 *reestablish_only); + bool reestablish_only); /* Returns true if subd told, otherwise false. */ bool channel_tell_depth(struct lightningd *ld, diff --git a/lightningd/opening_common.c b/lightningd/opening_common.c index 81da45837..f018b1c91 100644 --- a/lightningd/opening_common.c +++ b/lightningd/opening_common.c @@ -166,38 +166,3 @@ void channel_config(struct lightningd *ld, /* This is filled in by lightning_openingd, for consistency. */ ours->channel_reserve = AMOUNT_SAT(UINT64_MAX); } - -void handle_reestablish(struct lightningd *ld, - const struct node_id *peer_id, - const struct channel_id *channel_id, - const u8 *reestablish, - struct peer_fd *peer_fd) -{ - struct peer *peer; - struct channel *c; - - /* We very carefully re-xmit the last reestablish, so they can get - * their secrets back. We don't otherwise touch them. */ - peer = peer_by_id(ld, peer_id); - if (peer) - c = find_channel_by_id(peer, channel_id); - else - c = NULL; - - if (c && channel_closed(c)) { - log_debug(c->log, "Reestablish on %s channel: using channeld to reply", - channel_state_name(c)); - peer_start_channeld(c, peer_fd, NULL, true, reestablish); - } else { - const u8 *err = towire_errorfmt(tmpctx, channel_id, - "Unknown channel for reestablish"); - log_debug(ld->log, "Reestablish on UNKNOWN channel %s", - type_to_string(tmpctx, struct channel_id, channel_id)); - /* Unless we're shutting down */ - if (ld->connectd) - subd_send_msg(ld->connectd, - take(towire_connectd_peer_final_msg(NULL, peer_id, - err))); - tal_free(peer_fd); - } -} diff --git a/lightningd/opening_common.h b/lightningd/opening_common.h index 4dc609f4d..8d1b1c490 100644 --- a/lightningd/opening_common.h +++ b/lightningd/opening_common.h @@ -123,10 +123,4 @@ void channel_config(struct lightningd *ld, u32 *max_to_self_delay, struct amount_msat *min_effective_htlc_capacity); -void handle_reestablish(struct lightningd *ld, - const struct node_id *peer_id, - const struct channel_id *channel_id, - const u8 *reestablish, - struct peer_fd *peer_fd); - #endif /* LIGHTNING_LIGHTNINGD_OPENING_COMMON_H */ diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index 10f556325..6c99ada1f 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -804,32 +804,6 @@ static void opening_got_offer(struct subd *openingd, plugin_hook_call_openchannel(openingd->ld, payload); } -static void opening_got_reestablish(struct subd *openingd, const u8 *msg, - const int fds[2], - struct uncommitted_channel *uc) -{ - struct lightningd *ld = openingd->ld; - struct node_id peer_id = uc->peer->id; - struct channel_id channel_id; - u8 *reestablish; - struct peer_fd *peer_fd; - - peer_fd = new_peer_fd_arr(tmpctx, fds); - - if (!fromwire_openingd_got_reestablish(tmpctx, msg, &channel_id, - &reestablish)) { - log_broken(openingd->log, "Malformed opening_got_reestablish %s", - tal_hex(tmpctx, msg)); - tal_free(openingd); - return; - } - - /* This could free peer */ - tal_free(uc); - - handle_reestablish(ld, &peer_id, &channel_id, reestablish, peer_fd); -} - static unsigned int openingd_msg(struct subd *openingd, const u8 *msg, const int *fds) { @@ -871,12 +845,6 @@ static unsigned int openingd_msg(struct subd *openingd, opening_got_offer(openingd, msg, uc); return 0; - case WIRE_OPENINGD_GOT_REESTABLISH: - if (tal_count(fds) != 1) - return 1; - opening_got_reestablish(openingd, msg, fds, uc); - return 0; - /* We send these! */ case WIRE_OPENINGD_INIT: case WIRE_OPENINGD_FUNDER_START: diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 28dc35acc..4a8d20654 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -1337,18 +1337,48 @@ void peer_active(struct lightningd *ld, const u8 *msg, int fd) &peer->id)); } } - } else { - /* OK, it's unsolicited. What kind of open do they want? */ - if (dual_fund) { - channel = new_unsaved_channel(peer, - peer->ld->config.fee_base, - peer->ld->config.fee_per_satoshi); - peer_start_dualopend(peer, peer_fd, channel); + return; + } + + /* It's possible that they want to reestablish a channel, but + * it's closed? */ + if (*msgtype == WIRE_CHANNEL_REESTABLISH && channel_id) { + channel = find_channel_by_id(peer, channel_id); + if (channel && channel_closed(channel)) { + log_debug(channel->log, + "Reestablish on %s channel: using channeld to reply", + channel_state_name(channel)); + peer_start_channeld(channel, peer_fd, NULL, true, true); + return; } else { - peer->uncommitted_channel = new_uncommitted_channel(peer); - peer_start_openingd(peer, peer_fd); + const u8 *err = towire_errorfmt(tmpctx, channel_id, + "Unknown channel for reestablish"); + log_peer_debug(ld->log, &peer->id, + "Reestablish on UNKNOWN channel %s", + type_to_string(tmpctx, struct channel_id, + channel_id)); + /* Unless we're shutting down, tell connectd to send err */ + if (ld->connectd) + subd_send_msg(ld->connectd, + take(towire_connectd_peer_final_msg(NULL, + &peer->id, + err))); + else + peer->is_connected = false; + return; } } + + /* OK, it's unsolicited. What kind of open do they want? */ + if (dual_fund) { + channel = new_unsaved_channel(peer, + peer->ld->config.fee_base, + peer->ld->config.fee_per_satoshi); + peer_start_dualopend(peer, peer_fd, channel); + } else { + peer->uncommitted_channel = new_uncommitted_channel(peer); + peer_start_openingd(peer, peer_fd); + } return; send_error: diff --git a/lightningd/test/run-invoice-select-inchan.c b/lightningd/test/run-invoice-select-inchan.c index 02d4d4964..8240e771b 100644 --- a/lightningd/test/run-invoice-select-inchan.c +++ b/lightningd/test/run-invoice-select-inchan.c @@ -187,6 +187,10 @@ bool feature_negotiated(const struct feature_set *our_features UNNEEDED, /* Generated stub for featurebits_or */ u8 *featurebits_or(const tal_t *ctx UNNEEDED, const u8 *f1 TAKES UNNEEDED, const u8 *f2 TAKES UNNEEDED) { fprintf(stderr, "featurebits_or called!\n"); abort(); } +/* Generated stub for find_channel_by_id */ +struct channel *find_channel_by_id(const struct peer *peer UNNEEDED, + const struct channel_id *cid UNNEEDED) +{ fprintf(stderr, "find_channel_by_id called!\n"); abort(); } /* Generated stub for find_plugin_for_command */ struct plugin *find_plugin_for_command(struct lightningd *ld UNNEEDED, const char *cmd_name UNNEEDED) @@ -594,7 +598,7 @@ void peer_start_channeld(struct channel *channel UNNEEDED, struct peer_fd *peer_fd UNNEEDED, const u8 *fwd_msg UNNEEDED, bool reconnected UNNEEDED, - const u8 *reestablish_only UNNEEDED) + bool reestablish_only UNNEEDED) { fprintf(stderr, "peer_start_channeld called!\n"); abort(); } /* Generated stub for peer_start_dualopend */ bool peer_start_dualopend(struct peer *peer UNNEEDED, struct peer_fd *peer_fd UNNEEDED, diff --git a/openingd/openingd.c b/openingd/openingd.c index 7bcff4de8..040168083 100644 --- a/openingd/openingd.c +++ b/openingd/openingd.c @@ -1251,11 +1251,6 @@ static u8 *handle_peer_in(struct state *state) extracted = extract_channel_id(msg, &channel_id); - /* Reestablish on some now-closed channel? Be nice. */ - if (extracted && fromwire_peektype(msg) == WIRE_CHANNEL_REESTABLISH) { - return towire_openingd_got_reestablish(NULL, - &channel_id, msg); - } peer_write(state->pps, take(towire_warningfmt(NULL, extracted ? &channel_id : NULL, @@ -1352,7 +1347,6 @@ static u8 *handle_master_in(struct state *state) case WIRE_OPENINGD_FAILED: case WIRE_OPENINGD_GOT_OFFER: case WIRE_OPENINGD_GOT_OFFER_REPLY: - case WIRE_OPENINGD_GOT_REESTABLISH: break; } diff --git a/openingd/openingd_wire.csv b/openingd/openingd_wire.csv index aa014e010..ee90e0894 100644 --- a/openingd/openingd_wire.csv +++ b/openingd/openingd_wire.csv @@ -25,12 +25,6 @@ msgdata,openingd_init,min_feerate,u32, msgdata,openingd_init,max_feerate,u32, msgdata,openingd_init,dev_temporary_channel_id,?byte,32 -# Openingd->master: they tried to reestablish a channel. -msgtype,openingd_got_reestablish,6001 -msgdata,openingd_got_reestablish,channel_id,channel_id, -msgdata,openingd_got_reestablish,len,u16, -msgdata,openingd_got_reestablish,msg,u8,len - # Openingd->master: they offered channel, should we continue? msgtype,openingd_got_offer,6005 msgdata,openingd_got_offer,funding_satoshis,amount_sat, diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index a38fb1808..62b6c2296 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -133,6 +133,10 @@ char *encode_scriptpubkey_to_addr(const tal_t *ctx UNNEEDED, /* Generated stub for fatal */ void fatal(const char *fmt UNNEEDED, ...) { fprintf(stderr, "fatal called!\n"); abort(); } +/* Generated stub for find_channel_by_id */ +struct channel *find_channel_by_id(const struct peer *peer UNNEEDED, + const struct channel_id *cid UNNEEDED) +{ fprintf(stderr, "find_channel_by_id called!\n"); abort(); } /* Generated stub for fromwire_channeld_dev_memleak_reply */ bool fromwire_channeld_dev_memleak_reply(const void *p UNNEEDED, bool *leak UNNEEDED) { fprintf(stderr, "fromwire_channeld_dev_memleak_reply called!\n"); abort(); } @@ -634,7 +638,7 @@ void peer_start_channeld(struct channel *channel UNNEEDED, struct peer_fd *peer_fd UNNEEDED, const u8 *fwd_msg UNNEEDED, bool reconnected UNNEEDED, - const u8 *reestablish_only UNNEEDED) + bool reestablish_only UNNEEDED) { fprintf(stderr, "peer_start_channeld called!\n"); abort(); } /* Generated stub for peer_start_dualopend */ bool peer_start_dualopend(struct peer *peer UNNEEDED, struct peer_fd *peer_fd UNNEEDED,