Skip to content

Commit

Permalink
channeld: send update_fee messages.
Browse files Browse the repository at this point in the history
We only send them when we're not awaiting revoke_and_ack: our
simplified handling can't deal with multiple in flights.

Closes: #244
Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell authored and cdecker committed Nov 23, 2017
1 parent c3cb7f1 commit 552e56d
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 1 deletion.
62 changes: 62 additions & 0 deletions channeld/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ struct peer {
/* Don't accept a pong we didn't ping for. */
size_t num_pings_outstanding;

/* The feerate we want. */
u32 desired_feerate;

/* Announcement related information */
struct pubkey node_ids[NUM_SIDES];
struct short_channel_id short_channel_ids[NUM_SIDES];
Expand Down Expand Up @@ -483,6 +486,7 @@ static struct io_plan *handle_peer_feechange(struct io_conn *conn,
"update_fee %u unaffordable",
feerate);

status_trace("peer updated fee to %u", feerate);
return peer_read_message(conn, &peer->pcs, peer_in);
}

Expand Down Expand Up @@ -697,6 +701,29 @@ static void send_commit(struct peer *peer)
return;
}

/* If we wanted to update fees, do it now. */
if (peer->channel->funder == LOCAL
&& peer->desired_feerate != channel_feerate(peer->channel, REMOTE)) {
u8 *msg;
u32 feerate, max = approx_max_feerate(peer->channel);

feerate = peer->desired_feerate;

/* FIXME: We should avoid adding HTLCs until we can meet this
* feerate! */
if (feerate > max)
feerate = max;

if (!channel_update_feerate(peer->channel, feerate))
status_failed(STATUS_FAIL_INTERNAL_ERROR,
"Could not afford feerate %u"
" (vs max %u)",
feerate, max);

msg = towire_update_fee(peer, &peer->channel_id, feerate);
msg_enqueue(&peer->peer_out, take(msg));
}

/* BOLT #2:
*
* A node MUST NOT send a `commitment_signed` message which does not
Expand Down Expand Up @@ -1887,6 +1914,34 @@ static void handle_offer_htlc(struct peer *peer, const u8 *inmsg)
wire_sync_write(MASTER_FD, take(msg));
}

static void handle_feerates(struct peer *peer, const u8 *inmsg)
{
u32 feerate, min_feerate, max_feerate;

if (!fromwire_channel_feerates(inmsg, NULL, &feerate,
&min_feerate, &max_feerate))
master_badmsg(WIRE_CHANNEL_FEERATES, inmsg);

/* BOLT #2:
*
* The node which is responsible for paying the bitcoin fee SHOULD
* send `update_fee` to ensure the current fee rate is sufficient for
* timely processing of the commitment transaction by a significant
* margin. */
if (peer->channel->funder == LOCAL) {
peer->desired_feerate = feerate;
start_commit_timer(peer);
} else {
/* BOLT #2:
*
* The node which is not responsible for paying the bitcoin
* fee MUST NOT send `update_fee`.
*/
/* FIXME: We could drop to chain if fees are too low, but
* that's fraught too. */
}
}

static void handle_preimage(struct peer *peer, const u8 *inmsg)
{
u8 *msg;
Expand Down Expand Up @@ -2034,6 +2089,9 @@ static void req_in(struct peer *peer, const u8 *msg)
case WIRE_CHANNEL_OFFER_HTLC:
handle_offer_htlc(peer, msg);
goto out;
case WIRE_CHANNEL_FEERATES:
handle_feerates(peer, msg);
goto out;
case WIRE_CHANNEL_FULFILL_HTLC:
handle_preimage(peer, msg);
goto out;
Expand Down Expand Up @@ -2194,6 +2252,10 @@ static void init_channel(struct peer *peer)
peer->channel_direction = get_channel_direction(
&peer->node_ids[LOCAL], &peer->node_ids[REMOTE]);

/* Default desired feerate is the feerate we set for them last. */
if (peer->channel->funder == LOCAL)
peer->desired_feerate = feerate_per_kw[REMOTE];

/* OK, now we can process peer messages. */
if (reconnected)
peer_reconnect(peer);
Expand Down
5 changes: 5 additions & 0 deletions channeld/channel_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,8 @@ channel_shutdown_complete,,crypto_state,struct crypto_state
# Re-enable commit timer.
channel_dev_reenable_commit,1026
channel_dev_reenable_commit_reply,1126

channel_feerates,1027
channel_feerates,,feerate,u32
channel_feerates,,min_feerate,u32
channel_feerates,,max_feerate,u32
1 change: 1 addition & 0 deletions lightningd/peer_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -2009,6 +2009,7 @@ static unsigned channel_msg(struct subd *sd, const u8 *msg, const int *fds)
case WIRE_CHANNEL_SENDING_COMMITSIG_REPLY:
case WIRE_CHANNEL_SEND_SHUTDOWN:
case WIRE_CHANNEL_DEV_REENABLE_COMMIT:
case WIRE_CHANNEL_FEERATES:
/* Replies go to requests. */
case WIRE_CHANNEL_OFFER_HTLC_REPLY:
case WIRE_CHANNEL_PING_REPLY:
Expand Down
6 changes: 6 additions & 0 deletions lightningd/peer_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ static inline bool peer_can_add_htlc(const struct peer *peer)
return peer->state == CHANNELD_NORMAL;
}

static inline bool peer_fees_can_change(const struct peer *peer)
{
return peer->state == CHANNELD_NORMAL
|| peer->state == CHANNELD_SHUTTING_DOWN;
}

static inline bool peer_can_remove_htlc(const struct peer *peer)
{
return peer->state == CHANNELD_NORMAL
Expand Down
26 changes: 25 additions & 1 deletion lightningd/peer_htlcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1538,5 +1538,29 @@ void notify_new_block(struct lightningd *ld, u32 height)

void notify_feerate_change(struct lightningd *ld)
{
/* FIXME: Do something! */
struct peer *peer;

/* FIXME: We should notify onchaind about NORMAL fee change in case
* it's going to generate more txs. */
list_for_each(&ld->peers, peer, list) {
u8 *msg;

if (!peer_fees_can_change(peer))
continue;

/* FIXME: We choose not to drop to chain if we can't contact
* peer. We *could* do so, however. */
if (!peer->owner)
continue;

msg = towire_channel_feerates(peer,
get_feerate(ld->topology,
FEERATE_IMMEDIATE),
get_feerate(ld->topology,
FEERATE_NORMAL),
get_feerate(ld->topology,
FEERATE_IMMEDIATE)
* 5);
subd_send_msg(peer->owner, take(msg));
}
}

0 comments on commit 552e56d

Please sign in to comment.