Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify use of channel_update #6786

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
123 changes: 14 additions & 109 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 @@ -217,9 +206,6 @@ struct peer {
* channel with the real scid. */
bool gossip_scid_announced;

/* Most recent channel_update message. */
u8 *channel_update;

/* --experimental-upgrade-protocol */
bool experimental_upgrade;
};
Expand Down Expand Up @@ -415,31 +401,17 @@ static void set_channel_type(struct channel *channel, const u8 *type)
/* Tell gossipd to create channel_update (then it goes into
* gossip_store, then streams out to peers, or sends it directly if
* it's a private channel) */
static void send_channel_update(struct peer *peer, int disable_flag)
static void send_channel_update(struct peer *peer, bool enable)
{
status_debug("send_channel_update %d", disable_flag);

u8 *msg;

assert(disable_flag == 0 || disable_flag == ROUTING_FLAGS_DISABLED);

/* Only send an update if we told gossipd */
if (!peer->channel_local_active)
return;

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

msg = towire_channeld_local_channel_update(NULL,
&peer->short_channel_ids[LOCAL],
disable_flag
== ROUTING_FLAGS_DISABLED,
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 All @@ -451,7 +423,7 @@ static void send_channel_initial_update(struct peer *peer)
* after creation. These mutations (ie. splice) must announce the
* channel when they finish anyway, so it is safe to skip it here */
if (!is_stfu_active(peer) && !peer->want_stfu)
send_channel_update(peer, 0);
send_channel_update(peer, true);
}

/**
Expand Down Expand Up @@ -596,7 +568,7 @@ static void announce_channel(struct peer *peer)
wire_sync_write(MASTER_FD,
take(towire_channeld_local_channel_announcement(NULL,
cannounce)));
send_channel_update(peer, 0);
send_channel_update(peer, true);
}

static void announce_channel_if_not_stfu(struct peer *peer)
Expand Down Expand Up @@ -765,7 +737,7 @@ static void check_mutual_splice_locked(struct peer *peer)

channel_announcement_negotiate(peer);
billboard_update(peer);
send_channel_update(peer, 0);
send_channel_update(peer, true);

peer->splice_state->inflights = tal_free(peer->splice_state->inflights);
peer->splice_state->count = 0;
Expand Down Expand Up @@ -1194,7 +1166,7 @@ static void maybe_send_shutdown(struct peer *peer)

/* Send a disable channel_update so others don't try to route
* over us */
send_channel_update(peer, ROUTING_FLAGS_DISABLED);
send_channel_update(peer, false);

if (peer->shutdown_wrong_funding) {
tlvs = tlv_shutdown_tlvs_new(tmpctx);
Expand Down Expand Up @@ -2509,7 +2481,7 @@ static void handle_peer_shutdown(struct peer *peer, const u8 *shutdown)
* completed in the spec */

/* Disable the channel. */
send_channel_update(peer, ROUTING_FLAGS_DISABLED);
send_channel_update(peer, false);

if (!fromwire_shutdown(tmpctx, shutdown, &channel_id, &scriptpubkey,
&tlvs))
Expand Down Expand Up @@ -3457,7 +3429,7 @@ static void resume_splice_negotiation(struct peer *peer,
chan_output_index);
wire_sync_write(MASTER_FD, take(msg));

send_channel_update(peer, 0);
send_channel_update(peer, true);
}

static struct inflight *inflights_new(struct peer *peer)
Expand Down Expand Up @@ -5299,15 +5271,6 @@ static void handle_funding_depth(struct peer *peer, const u8 *msg)
billboard_update(peer);
}

static const u8 *get_cupdate(const struct peer *peer)
{
/* Technically we only need to tell it the first time (unless it's
* changed). But it's not that common. */
wire_sync_write(MASTER_FD,
take(towire_channeld_used_channel_update(NULL)));
return peer->channel_update;
}

static void handle_offer_htlc(struct peer *peer, const u8 *inmsg)
{
u8 *msg;
Expand Down Expand Up @@ -5363,7 +5326,7 @@ static void handle_offer_htlc(struct peer *peer, const u8 *inmsg)
peer->htlc_id++;
return;
case CHANNEL_ERR_INVALID_EXPIRY:
failwiremsg = towire_incorrect_cltv_expiry(inmsg, cltv_expiry, get_cupdate(peer));
failwiremsg = towire_incorrect_cltv_expiry(inmsg, cltv_expiry, NULL);
failstr = tal_fmt(inmsg, "Invalid cltv_expiry %u", cltv_expiry);
goto failed;
case CHANNEL_ERR_DUPLICATE:
Expand All @@ -5377,18 +5340,18 @@ static void handle_offer_htlc(struct peer *peer, const u8 *inmsg)
goto failed;
/* FIXME: Fuzz the boundaries a bit to avoid probing? */
case CHANNEL_ERR_CHANNEL_CAPACITY_EXCEEDED:
failwiremsg = towire_temporary_channel_failure(inmsg, get_cupdate(peer));
failwiremsg = towire_temporary_channel_failure(inmsg, NULL);
failstr = tal_fmt(inmsg, "Capacity exceeded - HTLC fee: %s", fmt_amount_sat(inmsg, htlc_fee));
goto failed;
case CHANNEL_ERR_HTLC_BELOW_MINIMUM:
failwiremsg = towire_amount_below_minimum(inmsg, amount, get_cupdate(peer));
failwiremsg = towire_amount_below_minimum(inmsg, amount, NULL);
failstr = tal_fmt(inmsg, "HTLC too small (%s minimum)",
type_to_string(tmpctx,
struct amount_msat,
&peer->channel->config[REMOTE].htlc_minimum));
goto failed;
case CHANNEL_ERR_TOO_MANY_HTLCS:
failwiremsg = towire_temporary_channel_failure(inmsg, get_cupdate(peer));
failwiremsg = towire_temporary_channel_failure(inmsg, NULL);
failstr = "Too many HTLCs";
goto failed;
case CHANNEL_ERR_DUST_FAILURE:
Expand All @@ -5398,14 +5361,15 @@ static void handle_offer_htlc(struct peer *peer, const u8 *inmsg)
* - SHOULD NOT send this HTLC
* - SHOULD fail this HTLC if it's forwarded
*/
failwiremsg = towire_temporary_channel_failure(inmsg, get_cupdate(peer));
failwiremsg = towire_temporary_channel_failure(inmsg, NULL);
failstr = "HTLC too dusty, allowed dust limit reached";
goto failed;
}
/* Shouldn't return anything else! */
abort();

failed:
/* lightningd appends update to this for us */
msg = towire_channeld_offer_htlc_reply(NULL, 0, failwiremsg, failstr);
wire_sync_write(MASTER_FD, take(msg));
}
Expand Down Expand Up @@ -5489,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, 0);
}


static void handle_preimage(struct peer *peer, const u8 *inmsg)
{
struct fulfilled_htlc fulfilled_htlc;
Expand Down Expand Up @@ -5612,14 +5540,6 @@ static void handle_shutdown_cmd(struct peer *peer, const u8 *inmsg)
start_commit_timer(peer);
}

/* Lightningd tells us when channel_update has changed. */
static void handle_channel_update(struct peer *peer, const u8 *msg)
{
peer->channel_update = tal_free(peer->channel_update);
if (!fromwire_channeld_channel_update(peer, msg, &peer->channel_update))
master_badmsg(WIRE_CHANNELD_CHANNEL_UPDATE, msg);
}

static void handle_send_error(struct peer *peer, const u8 *msg)
{
char *reason;
Expand Down Expand Up @@ -5708,20 +5628,12 @@ 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;
case WIRE_CHANNELD_SEND_ERROR:
handle_send_error(peer, msg);
return;
case WIRE_CHANNELD_CHANNEL_UPDATE:
handle_channel_update(peer, msg);
return;
case WIRE_CHANNELD_SPLICE_INIT:
handle_splice_init(peer, msg);
return;
Expand Down Expand Up @@ -5776,7 +5688,6 @@ static void req_in(struct peer *peer, const u8 *msg)
case WIRE_CHANNELD_SEND_ERROR_REPLY:
case WIRE_CHANNELD_DEV_QUIESCE_REPLY:
case WIRE_CHANNELD_UPGRADED:
case WIRE_CHANNELD_USED_CHANNEL_UPDATE:
case WIRE_CHANNELD_LOCAL_CHANNEL_UPDATE:
case WIRE_CHANNELD_LOCAL_CHANNEL_ANNOUNCEMENT:
case WIRE_CHANNELD_LOCAL_PRIVATE_CHANNEL:
Expand Down Expand Up @@ -5839,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 Expand Up @@ -5879,7 +5785,6 @@ static void init_channel(struct peer *peer)
&peer->dev_disable_commit,
&pbases,
&reestablish_only,
&peer->channel_update,
&peer->experimental_upgrade,
&peer->splice_state->inflights)) {
master_badmsg(WIRE_CHANNELD_INIT, msg);
Expand Down
33 changes: 2 additions & 31 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 @@ -81,8 +76,6 @@ 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,bool,
msgdata,channeld_init,channel_update_len,u16,
msgdata,channeld_init,channel_update,u8,channel_update_len
msgdata,channeld_init,experimental_upgrade,bool,
msgdata,channeld_init,num_inflights,u16,
msgdata,channeld_init,inflights,inflight,num_inflights
Expand Down Expand Up @@ -312,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 @@ -331,24 +317,9 @@ msgdata,channeld_send_error,reason,wirestring,
# Tell master channeld has sent the error message.
msgtype,channeld_send_error_reply,1108

# Tell channeld about the latest channel_update
msgtype,channeld_channel_update,1001
msgdata,channeld_channel_update,len,u16,
msgdata,channeld_channel_update,msg,u8,len

# Tell lightningd we used the latest channel_update for an error.
msgtype,channeld_used_channel_update,1102

# 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,disable,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,
msgdata,channeld_local_channel_update,enable,bool,

# Channeld: tell gossipd about our channel_announcement
msgtype,channeld_local_channel_announcement,1014
Expand Down
2 changes: 1 addition & 1 deletion contrib/pyln-testing/pyln/testing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ def openchannel(self, remote_node, capacity=FUNDAMOUNT, addrtype="bech32", confi

if wait_for_announce:
self.bitcoin.generate_block(5)
wait_for(lambda: ['alias' in e for e in self.rpc.listnodes(remote_node.info['id'])['nodes']])
wait_for(lambda: ['alias' in e for e in self.rpc.listnodes(remote_node.info['id'])['nodes']] == [True])

return {'address': addr, 'wallettxid': wallettxid, 'fundingtx': res['tx']}

Expand Down