From 4dc72dd829e67eb4d92135ae73992eb9c6120c1e Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 25 Jan 2022 06:35:52 +1030 Subject: [PATCH] dualopend: tell lightningd about new channel as soon as it's locked in. Once we send funding_locked, gossipd could start seeing channel_updates from the peer (which get sent so we can use the channel in routehints even before it's announcable). Signed-off-by: Rusty Russell --- lightningd/dual_open_control.c | 23 +++++++++++++++++++++- openingd/dualopend.c | 36 ++++++++++++++++++++++++++++++++++ openingd/dualopend_wire.csv | 6 ++++++ 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/lightningd/dual_open_control.c b/lightningd/dual_open_control.c index 1728b3a45..ba9219f76 100644 --- a/lightningd/dual_open_control.c +++ b/lightningd/dual_open_control.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -1368,6 +1369,24 @@ static void handle_channel_closed(struct subd *dualopend, "Start closingd"); } +static void handle_local_private_channel(struct subd *dualopend, + const u8 *msg) +{ + struct amount_sat capacity; + u8 *features; + + if (!fromwire_dualopend_local_private_channel(msg, msg, &capacity, + &features)) { + channel_internal_error(dualopend->channel, + "bad dualopend_local_private_channel %s", + tal_hex(msg, msg)); + return; + } + + tell_gossipd_local_private_channel(dualopend->ld, dualopend->channel, + capacity, features); +} + struct channel_send { const struct wally_tx *wtx; struct channel *channel; @@ -2991,7 +3010,9 @@ static unsigned int dual_opend_msg(struct subd *dualopend, case WIRE_DUALOPEND_FAIL_FALLEN_BEHIND: channel_fail_fallen_behind(dualopend, msg); return 0; - + case WIRE_DUALOPEND_LOCAL_PRIVATE_CHANNEL: + handle_local_private_channel(dualopend, msg); + return 0; /* Messages we send */ case WIRE_DUALOPEND_INIT: case WIRE_DUALOPEND_REINIT: diff --git a/openingd/dualopend.c b/openingd/dualopend.c index 28c9a407d..87a38d352 100644 --- a/openingd/dualopend.c +++ b/openingd/dualopend.c @@ -3396,6 +3396,38 @@ static void send_funding_locked(struct state *state) billboard_update(state); } +/* FIXME: Maybe cache this? */ +static struct amount_sat channel_size(struct state *state) +{ + u32 funding_outnum; + const u8 *funding_wscript = + bitcoin_redeem_2of2(tmpctx, + &state->our_funding_pubkey, + &state->their_funding_pubkey); + + if (!find_txout(state->tx_state->psbt, + scriptpubkey_p2wsh(tmpctx, funding_wscript), + &funding_outnum)) { + open_err_fatal(state, "Cannot fund txout"); + } + + return psbt_output_get_amount(state->tx_state->psbt, funding_outnum); +} + +static void tell_gossipd_new_channel(struct state *state) +{ + u8 *msg; + const u8 *annfeatures = get_agreed_channelfeatures(tmpctx, + state->our_features, + state->their_features); + + /* Tell lightningd about local channel. */ + msg = towire_dualopend_local_private_channel(NULL, + channel_size(state), + annfeatures); + wire_sync_write(REQ_FD, take(msg)); +} + static u8 *handle_funding_depth(struct state *state, u8 *msg) { u32 depth; @@ -3410,6 +3442,9 @@ static u8 *handle_funding_depth(struct state *state, u8 *msg) /* We check this before we arrive here, but for sanity */ assert(state->minimum_depth <= depth); + /* Tell gossipd the new channel exists before we tell peer. */ + tell_gossipd_new_channel(state); + send_funding_locked(state); if (state->funding_locked[REMOTE]) return towire_dualopend_channel_locked(state); @@ -3665,6 +3700,7 @@ static u8 *handle_master_in(struct state *state) case WIRE_DUALOPEND_FAIL_FALLEN_BEHIND: case WIRE_DUALOPEND_DRY_RUN: case WIRE_DUALOPEND_VALIDATE_LEASE: + case WIRE_DUALOPEND_LOCAL_PRIVATE_CHANNEL: break; } diff --git a/openingd/dualopend_wire.csv b/openingd/dualopend_wire.csv index c87fbef2a..6c6543487 100644 --- a/openingd/dualopend_wire.csv +++ b/openingd/dualopend_wire.csv @@ -238,3 +238,9 @@ msgdata,dualopend_validate_lease,their_pubkey,pubkey, msgtype,dualopend_validate_lease_reply,7127 msgdata,dualopend_validate_lease_reply,err_msg,?wirestring, + +# Tell gossipd about this (as-yet) unannounced channel +msgtype,dualopend_local_private_channel,7015 +msgdata,dualopend_local_private_channel,capacity,amount_sat, +msgdata,dualopend_local_private_channel,len,u16, +msgdata,dualopend_local_private_channel,features,u8,len