Skip to content

Commit

Permalink
lightningd: really fill in our own details when channeld says to make…
Browse files Browse the repository at this point in the history
… channel_update.

Now we've asserted that channeld would tell lightningd the same thing it
would do anyway, we can simply have channeld say "enable=True|False" and
lightningd fill in the other fields.

This means there's a pile of things channeld doesn't need to know any more!

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Oct 24, 2023
1 parent 14a578f commit fde56fd
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 139 deletions.
68 changes: 1 addition & 67 deletions channeld/channeld.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,6 @@ struct peer {
/* Which direction of the channel do we control? */
u16 channel_direction;

/* CLTV delta to announce to peers */
u16 cltv_delta;

/* We only really know these because we're the ones who create
* the channel_updates. */
u32 fee_base;
u32 fee_per_satoshi;
/* Note: the real min constraint is channel->config[REMOTE].htlc_minimum:
* they could kill the channel if we violate that! */
struct amount_msat htlc_minimum_msat, htlc_maximum_msat;

/* The scriptpubkey to use for shutting down. */
u32 *final_index;
struct ext_key *final_ext_key;
Expand Down Expand Up @@ -422,16 +411,7 @@ static void send_channel_update(struct peer *peer, bool enable)

assert(peer->short_channel_ids[LOCAL].u64);

msg = towire_channeld_local_channel_update(NULL,
&peer->short_channel_ids[LOCAL],
enable,
peer->cltv_delta,
peer->htlc_minimum_msat,
peer->fee_base,
peer->fee_per_satoshi,
peer->htlc_maximum_msat,
peer->channel_flags
& CHANNEL_FLAGS_ANNOUNCE_CHANNEL);
msg = towire_channeld_local_channel_update(NULL, enable);
wire_sync_write(MASTER_FD, take(msg));
}

Expand Down Expand Up @@ -5473,42 +5453,6 @@ static void handle_blockheight(struct peer *peer, const u8 *inmsg)
}
}

static void handle_config_channel(struct peer *peer, const u8 *inmsg)
{
u32 *base, *ppm;
struct amount_msat *htlc_min, *htlc_max;
bool changed;

if (!fromwire_channeld_config_channel(inmsg, inmsg,
&base, &ppm,
&htlc_min,
&htlc_max))
master_badmsg(WIRE_CHANNELD_CONFIG_CHANNEL, inmsg);

/* only send channel updates if values actually changed */
changed = false;
if (base && *base != peer->fee_base) {
peer->fee_base = *base;
changed = true;
}
if (ppm && *ppm != peer->fee_per_satoshi) {
peer->fee_per_satoshi = *ppm;
changed = true;
}
if (htlc_min && !amount_msat_eq(*htlc_min, peer->htlc_minimum_msat)) {
peer->htlc_minimum_msat = *htlc_min;
changed = true;
}
if (htlc_max && !amount_msat_eq(*htlc_max, peer->htlc_maximum_msat)) {
peer->htlc_maximum_msat = *htlc_max;
changed = true;
}

if (changed)
send_channel_update(peer, true);
}


static void handle_preimage(struct peer *peer, const u8 *inmsg)
{
struct fulfilled_htlc fulfilled_htlc;
Expand Down Expand Up @@ -5684,11 +5628,6 @@ static void req_in(struct peer *peer, const u8 *msg)
return;
handle_fail(peer, msg);
return;
case WIRE_CHANNELD_CONFIG_CHANNEL:
if (handle_master_request_later(peer, msg))
return;
handle_config_channel(peer, msg);
return;
case WIRE_CHANNELD_SEND_SHUTDOWN:
handle_shutdown_cmd(peer, msg);
return;
Expand Down Expand Up @@ -5811,17 +5750,12 @@ static void init_channel(struct peer *peer)
&peer->remote_per_commit,
&peer->old_remote_per_commit,
&opener,
&peer->fee_base,
&peer->fee_per_satoshi,
&peer->htlc_minimum_msat,
&peer->htlc_maximum_msat,
&local_msat,
&points[LOCAL],
&funding_pubkey[LOCAL],
&peer->node_ids[LOCAL],
&peer->node_ids[REMOTE],
&peer->commit_msec,
&peer->cltv_delta,
&peer->last_was_revoke,
&peer->last_sent_commit,
&peer->next_index[LOCAL],
Expand Down
21 changes: 1 addition & 20 deletions channeld/channeld_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,12 @@ msgdata,channeld_init,remote_basepoints,basepoints,
msgdata,channeld_init,remote_per_commit,pubkey,
msgdata,channeld_init,old_remote_per_commit,pubkey,
msgdata,channeld_init,opener,enum side,
msgdata,channeld_init,fee_base,u32,
msgdata,channeld_init,fee_proportional,u32,
msgdata,channeld_init,htlc_minimum_msat,amount_msat,
msgdata,channeld_init,htlc_maximum_msat,amount_msat,
msgdata,channeld_init,local_msatoshi,amount_msat,
msgdata,channeld_init,our_basepoints,basepoints,
msgdata,channeld_init,our_funding_pubkey,pubkey,
msgdata,channeld_init,local_node_id,node_id,
msgdata,channeld_init,remote_node_id,node_id,
msgdata,channeld_init,commit_msec,u32,
msgdata,channeld_init,cltv_delta,u16,
msgdata,channeld_init,last_was_revoke,bool,
msgdata,channeld_init,num_last_sent_commit,u16,
msgdata,channeld_init,last_sent_commit,changed_htlc,num_last_sent_commit
Expand Down Expand Up @@ -310,13 +305,6 @@ msgtype,channeld_fail_fallen_behind,1028
# This is NULL if option_static_remotekey.
msgdata,channeld_fail_fallen_behind,remote_per_commitment_point,?pubkey,

# Handle a channel-specific configuration change
msgtype,channeld_config_channel,1029
msgdata,channeld_config_channel,feerate_base,?u32,
msgdata,channeld_config_channel,feerate_ppm,?u32,
msgdata,channeld_config_channel,htlc_minimum,?amount_msat,
msgdata,channeld_config_channel,htlc_maximum,?amount_msat,

# When we receive announcement_signatures for channel announce
msgtype,channeld_got_announcement,1017
msgdata,channeld_got_announcement,remote_ann_node_sig,secp256k1_ecdsa_signature,
Expand All @@ -329,16 +317,9 @@ msgdata,channeld_send_error,reason,wirestring,
# Tell master channeld has sent the error message.
msgtype,channeld_send_error_reply,1108

# Channeld: tell gossipd to make this channel_update.
# Channeld: tell gossipd to make channel_update to enable/disable.
msgtype,channeld_local_channel_update,1013
msgdata,channeld_local_channel_update,short_channel_id,short_channel_id,
msgdata,channeld_local_channel_update,enable,bool,
msgdata,channeld_local_channel_update,cltv_expiry_delta,u16,
msgdata,channeld_local_channel_update,htlc_minimum_msat,amount_msat,
msgdata,channeld_local_channel_update,fee_base_msat,u32,
msgdata,channeld_local_channel_update,fee_proportional_millionths,u32,
msgdata,channeld_local_channel_update,htlc_maximum_msat,amount_msat,
msgdata,channeld_local_channel_update,public,bool,

# Channeld: tell gossipd about our channel_announcement
msgtype,channeld_local_channel_announcement,1014
Expand Down
2 changes: 1 addition & 1 deletion gossipd/test/run-check_node_announcement.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void daemon_conn_send(struct daemon_conn *dc UNNEEDED, const u8 *msg UNNEEDED)
struct peer *find_peer(struct daemon *daemon UNNEEDED, const struct node_id *id UNNEEDED)
{ fprintf(stderr, "find_peer called!\n"); abort(); }
/* Generated stub for fromwire_gossipd_local_channel_update */
bool fromwire_gossipd_local_channel_update(const void *p UNNEEDED, struct node_id *id UNNEEDED, struct short_channel_id *short_channel_id UNNEEDED, bool *disable UNNEEDED, u16 *cltv_expiry_delta UNNEEDED, struct amount_msat *htlc_minimum_msat UNNEEDED, u32 *fee_base_msat UNNEEDED, u32 *fee_proportional_millionths UNNEEDED, struct amount_msat *htlc_maximum_msat UNNEEDED, bool *public UNNEEDED)
bool fromwire_gossipd_local_channel_update(const void *p UNNEEDED, struct node_id *id UNNEEDED, struct short_channel_id *short_channel_id UNNEEDED, bool *enable UNNEEDED, u16 *cltv_expiry_delta UNNEEDED, struct amount_msat *htlc_minimum_msat UNNEEDED, u32 *fee_base_msat UNNEEDED, u32 *fee_proportional_millionths UNNEEDED, struct amount_msat *htlc_maximum_msat UNNEEDED, bool *public UNNEEDED)
{ fprintf(stderr, "fromwire_gossipd_local_channel_update called!\n"); abort(); }
/* Generated stub for fromwire_gossipd_used_local_channel_update */
bool fromwire_gossipd_used_local_channel_update(const void *p UNNEEDED, struct short_channel_id *scid UNNEEDED)
Expand Down
2 changes: 1 addition & 1 deletion gossipd/test/run-crc32_of_update.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ struct peer *first_random_peer(struct daemon *daemon UNNEEDED,
bool fromwire_gossipd_dev_set_max_scids_encode_size(const void *p UNNEEDED, u32 *max UNNEEDED)
{ fprintf(stderr, "fromwire_gossipd_dev_set_max_scids_encode_size called!\n"); abort(); }
/* Generated stub for fromwire_gossipd_local_channel_update */
bool fromwire_gossipd_local_channel_update(const void *p UNNEEDED, struct node_id *id UNNEEDED, struct short_channel_id *short_channel_id UNNEEDED, bool *disable UNNEEDED, u16 *cltv_expiry_delta UNNEEDED, struct amount_msat *htlc_minimum_msat UNNEEDED, u32 *fee_base_msat UNNEEDED, u32 *fee_proportional_millionths UNNEEDED, struct amount_msat *htlc_maximum_msat UNNEEDED, bool *public UNNEEDED)
bool fromwire_gossipd_local_channel_update(const void *p UNNEEDED, struct node_id *id UNNEEDED, struct short_channel_id *short_channel_id UNNEEDED, bool *enable UNNEEDED, u16 *cltv_expiry_delta UNNEEDED, struct amount_msat *htlc_minimum_msat UNNEEDED, u32 *fee_base_msat UNNEEDED, u32 *fee_proportional_millionths UNNEEDED, struct amount_msat *htlc_maximum_msat UNNEEDED, bool *public UNNEEDED)
{ fprintf(stderr, "fromwire_gossipd_local_channel_update called!\n"); abort(); }
/* Generated stub for fromwire_gossipd_used_local_channel_update */
bool fromwire_gossipd_used_local_channel_update(const void *p UNNEEDED, struct short_channel_id *scid UNNEEDED)
Expand Down
23 changes: 16 additions & 7 deletions lightningd/channel_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,21 @@ 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 unsigned channel_msg(struct subd *sd, const u8 *msg, const int *fds)
{
enum channeld_wire t = fromwire_peektype(msg);
Expand Down Expand Up @@ -1240,7 +1255,7 @@ static unsigned channel_msg(struct subd *sd, const u8 *msg, const int *fds)
handle_error_channel(sd->channel, msg);
break;
case WIRE_CHANNELD_LOCAL_CHANNEL_UPDATE:
tell_gossipd_local_channel_update(sd->ld, sd->channel, msg);
handle_local_channel_update(sd->channel, msg);
break;
case WIRE_CHANNELD_LOCAL_CHANNEL_ANNOUNCEMENT:
tell_gossipd_local_channel_announce(sd->ld, sd->channel, msg);
Expand Down Expand Up @@ -1294,7 +1309,6 @@ static unsigned channel_msg(struct subd *sd, const u8 *msg, const int *fds)
case WIRE_CHANNELD_DEV_REENABLE_COMMIT:
case WIRE_CHANNELD_FEERATES:
case WIRE_CHANNELD_BLOCKHEIGHT:
case WIRE_CHANNELD_CONFIG_CHANNEL:
case WIRE_CHANNELD_DEV_MEMLEAK:
case WIRE_CHANNELD_DEV_QUIESCE:
case WIRE_CHANNELD_GOT_INFLIGHT:
Expand Down Expand Up @@ -1510,17 +1524,12 @@ bool peer_start_channeld(struct channel *channel,
&channel->channel_info.remote_per_commit,
&channel->channel_info.old_remote_per_commit,
channel->opener,
channel->feerate_base,
channel->feerate_ppm,
channel->htlc_minimum_msat,
channel->htlc_maximum_msat,
channel->our_msat,
&channel->local_basepoints,
&channel->local_funding_pubkey,
&ld->id,
&channel->peer->id,
cfg->commit_time_ms,
cfg->cltv_expiry_delta,
channel->last_was_revoke,
channel->last_sent_commit,
channel->next_index[LOCAL],
Expand Down
30 changes: 2 additions & 28 deletions lightningd/gossip_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,41 +315,15 @@ void gossipd_notify_spends(struct lightningd *ld,
scids)));
}

/* We unwrap, add the peer id, and send to gossipd. */
/* Tell gossipd about latest channel_update. */
void tell_gossipd_local_channel_update(struct lightningd *ld,
struct channel *channel,
const u8 *msg)
bool enable)
{
struct short_channel_id scid;
bool enable, public;
u16 cltv_expiry_delta;
struct amount_msat htlc_minimum_msat;
u32 fee_base_msat, fee_proportional_millionths;
struct amount_msat htlc_maximum_msat;

if (!fromwire_channeld_local_channel_update(msg, &scid, &enable,
&cltv_expiry_delta,
&htlc_minimum_msat,
&fee_base_msat,
&fee_proportional_millionths,
&htlc_maximum_msat, &public)) {
channel_internal_error(channel,
"bad channeld_local_channel_update %s",
tal_hex(channel, msg));
return;
}

/* As we're shutting down, ignore */
if (!ld->gossip)
return;

assert(short_channel_id_eq(channel->scid ? channel->scid : channel->alias[LOCAL], &scid));
assert(cltv_expiry_delta == ld->config.cltv_expiry_delta);
assert(amount_msat_eq(htlc_minimum_msat, channel->htlc_minimum_msat));
assert(fee_base_msat == channel->feerate_base);
assert(fee_proportional_millionths == channel->feerate_ppm);
assert(amount_msat_eq(htlc_maximum_msat, channel->htlc_maximum_msat));

subd_send_msg(ld->gossip,
take(towire_gossipd_local_channel_update
(NULL,
Expand Down
2 changes: 1 addition & 1 deletion lightningd/gossip_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void gossip_notify_new_block(struct lightningd *ld, u32 blockheight);
/* channeld tells us stuff, we tell gossipd. */
void tell_gossipd_local_channel_update(struct lightningd *ld,
struct channel *channel,
const u8 *msg);
bool enabled);
void tell_gossipd_local_channel_announce(struct lightningd *ld,
struct channel *channel,
const u8 *msg);
Expand Down
18 changes: 10 additions & 8 deletions lightningd/peer_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include <lightningd/closing_control.h>
#include <lightningd/connect_control.h>
#include <lightningd/dual_open_control.h>
#include <lightningd/gossip_control.h>
#include <lightningd/hsm_control.h>
#include <lightningd/jsonrpc.h>
#include <lightningd/lightningd.h>
Expand Down Expand Up @@ -2856,16 +2857,17 @@ static void set_channel_config(struct command *cmd, struct channel *channel,
if (ignore_fee_limits)
channel->ignore_fee_limits = *ignore_fee_limits;

/* tell channeld to make a send_channel_update */
if (channel->owner && streq(channel->owner->name, "channeld")) {
subd_send_msg(channel->owner,
take(towire_channeld_config_channel(NULL, base, ppm,
htlc_min, htlc_max)));
/* Tell it about the new acceptable feerates */
if (ignore_fee_limits)
channel_update_feerates(cmd->ld, channel);
/* Tell channeld about the new acceptable feerates */
if (channel->owner
&& streq(channel->owner->name, "channeld")
&& ignore_fee_limits) {
channel_update_feerates(cmd->ld, channel);
}

/* Tell gossipd */
/* FIXME: this always enables channel, even if not enabled! */
tell_gossipd_local_channel_update(cmd->ld, channel, true);

/* save values to database */
wallet_channel_save(cmd->ld->wallet, channel);

Expand Down
8 changes: 5 additions & 3 deletions lightningd/test/run-invoice-select-inchan.c
Original file line number Diff line number Diff line change
Expand Up @@ -875,15 +875,17 @@ void subd_send_fd(struct subd *sd UNNEEDED, int fd UNNEEDED)
/* Generated stub for subd_send_msg */
void subd_send_msg(struct subd *sd UNNEEDED, const u8 *msg_out UNNEEDED)
{ fprintf(stderr, "subd_send_msg called!\n"); abort(); }
/* Generated stub for tell_gossipd_local_channel_update */
void tell_gossipd_local_channel_update(struct lightningd *ld UNNEEDED,
struct channel *channel UNNEEDED,
bool enabled UNNEEDED)
{ fprintf(stderr, "tell_gossipd_local_channel_update called!\n"); abort(); }
/* Generated stub for towire_bigsize */
void towire_bigsize(u8 **pptr UNNEEDED, const bigsize_t val UNNEEDED)
{ fprintf(stderr, "towire_bigsize called!\n"); abort(); }
/* Generated stub for towire_channel_id */
void towire_channel_id(u8 **pptr UNNEEDED, const struct channel_id *channel_id UNNEEDED)
{ fprintf(stderr, "towire_channel_id called!\n"); abort(); }
/* Generated stub for towire_channeld_config_channel */
u8 *towire_channeld_config_channel(const tal_t *ctx UNNEEDED, u32 *feerate_base UNNEEDED, u32 *feerate_ppm UNNEEDED, struct amount_msat *htlc_minimum UNNEEDED, struct amount_msat *htlc_maximum UNNEEDED)
{ fprintf(stderr, "towire_channeld_config_channel called!\n"); abort(); }
/* Generated stub for towire_channeld_dev_memleak */
u8 *towire_channeld_dev_memleak(const tal_t *ctx UNNEEDED)
{ fprintf(stderr, "towire_channeld_dev_memleak called!\n"); abort(); }
Expand Down
8 changes: 5 additions & 3 deletions wallet/test/run-wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,11 @@ void subkey_from_hmac(const char *prefix UNNEEDED,
const struct secret *base UNNEEDED,
struct secret *key UNNEEDED)
{ fprintf(stderr, "subkey_from_hmac called!\n"); abort(); }
/* Generated stub for tell_gossipd_local_channel_update */
void tell_gossipd_local_channel_update(struct lightningd *ld UNNEEDED,
struct channel *channel UNNEEDED,
bool enabled UNNEEDED)
{ fprintf(stderr, "tell_gossipd_local_channel_update called!\n"); abort(); }
/* Generated stub for to_canonical_invstr */
const char *to_canonical_invstr(const tal_t *ctx UNNEEDED, const char *invstring UNNEEDED)
{ fprintf(stderr, "to_canonical_invstr called!\n"); abort(); }
Expand All @@ -747,9 +752,6 @@ void topology_add_sync_waiter_(const tal_t *ctx UNNEEDED,
void *) UNNEEDED,
void *arg UNNEEDED)
{ fprintf(stderr, "topology_add_sync_waiter_ called!\n"); abort(); }
/* Generated stub for towire_channeld_config_channel */
u8 *towire_channeld_config_channel(const tal_t *ctx UNNEEDED, u32 *feerate_base UNNEEDED, u32 *feerate_ppm UNNEEDED, struct amount_msat *htlc_minimum UNNEEDED, struct amount_msat *htlc_maximum UNNEEDED)
{ fprintf(stderr, "towire_channeld_config_channel called!\n"); abort(); }
/* Generated stub for towire_channeld_dev_memleak */
u8 *towire_channeld_dev_memleak(const tal_t *ctx UNNEEDED)
{ fprintf(stderr, "towire_channeld_dev_memleak called!\n"); abort(); }
Expand Down

0 comments on commit fde56fd

Please sign in to comment.