Skip to content

Commit

Permalink
lightningd: ignore gossip messages from channeld, switch to our own.
Browse files Browse the repository at this point in the history
This commit is a bit messy, but it tries to do the minimal switchover.

Some tests change, so those are included here. 

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell committed Jan 31, 2024
1 parent a677fad commit 97ccf05
Show file tree
Hide file tree
Showing 23 changed files with 213 additions and 400 deletions.
17 changes: 15 additions & 2 deletions gossipd/routing.c
Original file line number Diff line number Diff line change
Expand Up @@ -587,9 +587,10 @@ static void bad_gossip_order(const u8 *msg,
const char *details)
{
status_peer_debug(source_peer,
"Bad gossip order: %s before announcement %s",
"Bad gossip order: %s before announcement %s from %s",
peer_wire_name(fromwire_peektype(msg)),
details);
details,
source_peer ? type_to_string(tmpctx, struct node_id, source_peer) : "local");
}

struct chan *new_chan(struct routing_state *rstate,
Expand Down Expand Up @@ -1791,6 +1792,18 @@ u8 *handle_channel_update(struct routing_state *rstate, const u8 *update TAKES,

owner = get_channel_owner(rstate, &short_channel_id, direction);
if (!owner) {
/* This may be a local channel we don't know about. If it's from a peer,
* check signature assuming it's from that peer, and if it's valid, hand to ld */
if (source_peer
&& check_channel_update(tmpctx, source_peer, &signature, serialized) == NULL) {
tell_lightningd_peer_update(rstate, source_peer,
short_channel_id, fee_base_msat,
fee_proportional_millionths,
expiry, htlc_minimum,
htlc_maximum);
return NULL;
}

if (unknown_scid)
*unknown_scid = short_channel_id;
bad_gossip_order(serialized,
Expand Down
18 changes: 14 additions & 4 deletions lightningd/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
#include <errno.h>
#include <hsmd/hsmd_wiregen.h>
#include <lightningd/channel.h>
#include <lightningd/channel_gossip.h>
#include <lightningd/channel_state_names_gen.h>
#include <lightningd/connect_control.h>
#include <lightningd/gossip_control.h>
#include <lightningd/hsm_control.h>
#include <lightningd/notification.h>
#include <lightningd/opening_common.h>
Expand Down Expand Up @@ -277,8 +279,6 @@ struct channel *new_unsaved_channel(struct peer *peer,
= CLOSING_FEE_NEGOTIATION_STEP_UNIT_PERCENTAGE;
channel->shutdown_wrong_funding = NULL;
channel->closing_feerate_range = NULL;
channel->peer_update = NULL;
channel->channel_update = NULL;
channel->alias[REMOTE] = NULL;
/* We don't even bother checking for clashes. */
channel->alias[LOCAL] = tal(channel, struct short_channel_id);
Expand Down Expand Up @@ -327,6 +327,8 @@ struct channel *new_unsaved_channel(struct peer *peer,
&channel->local_basepoints,
&channel->local_funding_pubkey);

/* channel->channel_gossip gets populated once we know if it's public. */
channel->channel_gossip = NULL;
channel->forgets = tal_arr(channel, struct command *, 0);
list_add_tail(&peer->channels, &channel->list);
channel->rr_number = peer->ld->rr_counter++;
Expand Down Expand Up @@ -572,9 +574,7 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
channel->lease_commit_sig = tal_steal(channel, lease_commit_sig);
channel->lease_chan_max_msat = lease_chan_max_msat;
channel->lease_chan_max_ppt = lease_chan_max_ppt;
channel->peer_update = tal_steal(channel, peer_update);
channel->blockheight_states = dup_height_states(channel, height_states);
channel->channel_update = NULL;

/* DB migration, for example, sets min to 0, max to large: fixup */
htlc_min = channel->channel_info.their_config.htlc_minimum;
Expand All @@ -598,6 +598,8 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
channel->close_blockheight = NULL;
channel->state_change_cause = reason;
channel->ignore_fee_limits = ignore_fee_limits;
/* Populate channel->channel_gossip */
channel_gossip_init(channel, take(peer_update));

/* Make sure we see any spends using this key */
if (!local_shutdown_scriptpubkey) {
Expand Down Expand Up @@ -1098,3 +1100,11 @@ channel_scid_or_local_alias(const struct channel *chan)
else
return chan->alias[LOCAL];
}

const u8 *channel_update_for_error(const tal_t *ctx,
struct channel *channel)
{
/* FIXME: Call directly from callers */
return channel_gossip_update_for_error(ctx, channel);
}

8 changes: 1 addition & 7 deletions lightningd/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,6 @@ struct channel {
/* Lease commited max part per thousandth channel fee (ppm * 1000) */
u16 lease_chan_max_ppt;

/* Channel incoming fee rates, cltv delta min/max htlc from
* peer. Used to generate route hints, blinded paths. */
const struct peer_update *peer_update;

/* Latest channel_update, for use in error messages. */
u8 *channel_update;

/* `Channel-shell` of this channel
* (Minimum information required to backup this channel). */
struct scb_chan *scb;
Expand All @@ -316,6 +309,7 @@ struct channel {
bool ignore_fee_limits;
};

/* Is channel owned (and should be talking to peer) */
bool channel_is_connected(const struct channel *channel);

/* For v2 opens, a channel that has not yet been committed/saved to disk */
Expand Down
83 changes: 21 additions & 62 deletions lightningd/channel_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <lightningd/chaintopology.h>
#include <lightningd/channel.h>
#include <lightningd/channel_control.h>
#include <lightningd/channel_gossip.h>
#include <lightningd/closing_control.h>
#include <lightningd/coin_mvts.h>
#include <lightningd/connect_control.h>
Expand Down Expand Up @@ -682,6 +683,10 @@ bool depthcb_update_scid(struct channel *channel,
return false;
}

/* No change? Great. */
if (channel->scid && short_channel_id_eq(channel->scid, &scid))
return true;

if (!channel->scid) {
wallet_annotate_txout(ld->wallet, outpoint,
TX_CHANNEL_FUNDING, channel->dbid);
Expand All @@ -696,17 +701,17 @@ bool depthcb_update_scid(struct channel *channel,
if (channel->minimum_depth == 0)
lockin_has_completed(channel, false);

wallet_channel_save(ld->wallet, channel);
} else if (!short_channel_id_eq(channel->scid, &scid)) {
} else {
/* We freaked out if required when original was
* removed, so just update now */
log_info(channel->log, "Short channel id changed from %s->%s",
type_to_string(tmpctx, struct short_channel_id, channel->scid),
type_to_string(tmpctx, struct short_channel_id, &scid));
*channel->scid = scid;
wallet_channel_save(ld->wallet, channel);
channel_gossip_scid_changed(channel);
}

wallet_channel_save(ld->wallet, channel);
return true;
}

Expand Down Expand Up @@ -886,12 +891,13 @@ void channel_record_open(struct channel *channel, u32 blockheight, bool record_p

void lockin_has_completed(struct channel *channel, bool record_push)
{
struct lightningd *ld = channel->peer->ld;

/* Fees might have changed (and we use IMMEDIATE once we're funded),
* so update now. */
try_update_feerates(channel->peer->ld, channel);
try_update_feerates(ld, channel);

try_update_blockheight(channel->peer->ld, channel,
get_block_height(channel->peer->ld->topology));
try_update_blockheight(ld, channel, get_block_height(ld->topology));

/* Emit an event for the channel open (or channel proposal if blockheight
* is zero) */
Expand Down Expand Up @@ -921,10 +927,6 @@ void lockin_complete(struct channel *channel,
return;
}

log_debug(channel->log, "Moving channel state from %s to %s",
channel_state_str(expected_state),
channel_state_str(CHANNELD_NORMAL));

channel_set_state(channel,
expected_state,
CHANNELD_NORMAL,
Expand Down Expand Up @@ -1057,10 +1059,10 @@ static void peer_got_announcement(struct channel *channel, const u8 *msg)
{
secp256k1_ecdsa_signature remote_ann_node_sig;
secp256k1_ecdsa_signature remote_ann_bitcoin_sig;
struct short_channel_id unused;
struct short_channel_id scid;

if (!fromwire_channeld_got_announcement(msg,
&unused,
&scid,
&remote_ann_node_sig,
&remote_ann_bitcoin_sig)) {
channel_internal_error(channel,
Expand All @@ -1069,9 +1071,9 @@ static void peer_got_announcement(struct channel *channel, const u8 *msg)
return;
}

wallet_announcement_save(channel->peer->ld->wallet, channel->dbid,
&remote_ann_node_sig,
&remote_ann_bitcoin_sig);
channel_gossip_got_announcement_sigs(channel, scid,
&remote_ann_node_sig,
&remote_ann_bitcoin_sig);
}

static void peer_got_shutdown(struct channel *channel, const u8 *msg)
Expand Down Expand Up @@ -1234,23 +1236,6 @@ static void handle_error_channel(struct channel *channel,
forget(channel);
}

static void handle_local_private_channel(struct channel *channel, const u8 *msg)
{
struct amount_sat capacity;
u8 *features;

if (!fromwire_channeld_local_private_channel(msg, msg, &capacity,
&features)) {
channel_internal_error(channel,
"bad channeld_local_private_channel %s",
tal_hex(channel, msg));
return;
}

tell_gossipd_local_private_channel(channel->peer->ld, channel,
capacity, features);
}

static void forget_channel(struct channel *channel, const char *why)
{
channel->error = towire_errorfmt(channel, &channel->cid, "%s", why);
Expand Down Expand Up @@ -1303,21 +1288,6 @@ static void handle_channel_upgrade(struct channel *channel,
wallet_channel_save(channel->peer->ld->wallet, channel);
}

static void handle_local_channel_update(struct channel *channel,
const u8 *msg)
{
bool enable;

if (!fromwire_channeld_local_channel_update(msg, &enable)) {
channel_internal_error(channel,
"bad channeld_local_channel_update %s",
tal_hex(channel, msg));
return;
}

tell_gossipd_local_channel_update(channel->peer->ld, channel, enable);
}

static void handle_local_anchors(struct channel *channel, const u8 *msg)
{
u64 remote_commitnum;
Expand Down Expand Up @@ -1384,14 +1354,10 @@ static unsigned channel_msg(struct subd *sd, const u8 *msg, const int *fds)
case WIRE_CHANNELD_SEND_ERROR_REPLY:
handle_error_channel(sd->channel, msg);
break;
/* FIXME: remove from channeld_wire.csv */
case WIRE_CHANNELD_LOCAL_CHANNEL_UPDATE:
handle_local_channel_update(sd->channel, msg);
break;
case WIRE_CHANNELD_LOCAL_CHANNEL_ANNOUNCEMENT:
tell_gossipd_local_channel_announce(sd->ld, sd->channel, msg);
break;
case WIRE_CHANNELD_LOCAL_PRIVATE_CHANNEL:
handle_local_private_channel(sd->channel, msg);
break;
case WIRE_CHANNELD_SPLICE_CONFIRMED_INIT:
handle_splice_confirmed_init(sd->ld, sd->channel, msg);
Expand Down Expand Up @@ -1476,8 +1442,7 @@ bool peer_start_channeld(struct channel *channel,
const struct config *cfg = &ld->config;
bool reached_announce_depth;
struct secret last_remote_per_commit_secret;
secp256k1_ecdsa_signature *remote_ann_node_sig, *remote_ann_bitcoin_sig;
struct penalty_base *pbases;
secp256k1_ecdsa_signature *remote_ann_node_sig, *remote_ann_bitcoin_sig; struct penalty_base *pbases;
u32 min_feerate, max_feerate, curr_blockheight;
struct channel_inflight *inflight;
struct inflight **inflights;
Expand Down Expand Up @@ -1695,8 +1660,8 @@ bool peer_start_channeld(struct channel *channel,
&last_remote_per_commit_secret,
channel->peer->their_features,
channel->remote_upfront_shutdown_script,
remote_ann_node_sig,
remote_ann_bitcoin_sig,
NULL,
NULL,
channel->type,
ld->dev_fast_gossip,
ld->dev_disable_commit == -1
Expand Down Expand Up @@ -1977,12 +1942,6 @@ struct command_result *cancel_channel_before_broadcast(struct command *cmd,
return command_still_pending(cmd);
}

void channel_replace_update(struct channel *channel, u8 *update TAKES)
{
tal_free(channel->channel_update);
channel->channel_update = tal_dup_talarr(channel, u8, update);
}

static struct command_result *param_channel_for_splice(struct command *cmd,
const char *name,
const char *buffer,
Expand Down
3 changes: 0 additions & 3 deletions lightningd/channel_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ void channel_record_open(struct channel *channel, u32 blockheight, bool record_p
/* A channel has unrecoverably fallen behind */
void channel_fallen_behind(struct channel *channel, const u8 *msg);

/* Fresh channel_update for this channel. */
void channel_replace_update(struct channel *channel, u8 *update TAKES);

/* Tell channel about new feerates (owner must be channeld!) */
void channel_update_feerates(struct lightningd *ld, const struct channel *channel);

Expand Down
7 changes: 0 additions & 7 deletions lightningd/closing_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -508,13 +508,6 @@ void peer_start_closingd(struct channel *channel, struct peer_fd *peer_fd)
/* We don't expect a response: it will give us feedback on
* signatures sent and received, then closing_complete. */
subd_send_msg(channel->owner, take(initmsg));

/* Now tell gossipd that we're closing and that neither direction should
* be used. */
if (channel->scid)
subd_send_msg(channel->peer->ld->gossip,
take(towire_gossipd_local_channel_close(
tmpctx, channel->scid)));
}

static struct command_result *param_outpoint(struct command *cmd,
Expand Down
24 changes: 5 additions & 19 deletions lightningd/dual_open_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <lightningd/chaintopology.h>
#include <lightningd/channel.h>
#include <lightningd/channel_control.h>
#include <lightningd/channel_gossip.h>
#include <lightningd/closing_control.h>
#include <lightningd/connect_control.h>
#include <lightningd/dual_open_control.h>
Expand Down Expand Up @@ -1632,24 +1633,6 @@ 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;
Expand Down Expand Up @@ -3433,6 +3416,9 @@ static void handle_commit_ready(struct subd *dualopend,

/* First time (not an RBF) */
if (channel->state == DUALOPEND_OPEN_INIT) {
/* Now we know if it's public or not, we can init channel_gossip */
assert(channel->channel_gossip == NULL);
channel_gossip_init(channel, NULL);
if (!(inflight = wallet_commit_channel(ld, channel,
&funding,
total_funding,
Expand Down Expand Up @@ -3643,8 +3629,8 @@ static unsigned int dual_opend_msg(struct subd *dualopend,
case WIRE_DUALOPEND_FAIL_FALLEN_BEHIND:
channel_fail_fallen_behind(dualopend, msg);
return 0;
/* FIXME: remove from dual_open_wire.csv */
case WIRE_DUALOPEND_LOCAL_PRIVATE_CHANNEL:
handle_local_private_channel(dualopend, msg);
return 0;
case WIRE_DUALOPEND_VALIDATE_INPUTS:
handle_validate_inputs(dualopend, msg);
Expand Down
Loading

0 comments on commit 97ccf05

Please sign in to comment.