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

lightningd: use CPFP on peer's commitment tx if we can't broadcast our own #6752

Merged
97 changes: 62 additions & 35 deletions channeld/channeld.c
Original file line number Diff line number Diff line change
Expand Up @@ -1169,9 +1169,7 @@ static u8 *sending_commitsig_msg(const tal_t *ctx,
struct penalty_base *pbase,
const struct fee_states *fee_states,
const struct height_states *blockheight_states,
const struct htlc **changed_htlcs,
const struct bitcoin_signature *commit_sig,
const struct bitcoin_signature *htlc_sigs)
const struct htlc **changed_htlcs)
{
struct changed_htlc *changed;
u8 *msg;
Expand All @@ -1181,8 +1179,7 @@ static u8 *sending_commitsig_msg(const tal_t *ctx,
changed = changed_htlc_arr(tmpctx, changed_htlcs);
msg = towire_channeld_sending_commitsig(ctx, remote_commit_index,
pbase, fee_states,
blockheight_states, changed,
commit_sig, htlc_sigs);
blockheight_states, changed);
return msg;
}

Expand Down Expand Up @@ -1504,14 +1501,17 @@ static bool want_blockheight_update(const struct peer *peer, u32 *height)
return true;
}

static u8 *send_commit_part(struct peer *peer,
const struct bitcoin_outpoint *funding,
struct amount_sat funding_sats,
const struct htlc **changed_htlcs,
bool notify_master,
s64 splice_amnt,
s64 remote_splice_amnt,
u64 remote_index)
/* Returns commitment_signed msg, sets @local_anchor */
static u8 *send_commit_part(const tal_t *ctx,
struct peer *peer,
const struct bitcoin_outpoint *funding,
struct amount_sat funding_sats,
const struct htlc **changed_htlcs,
bool notify_master,
s64 splice_amnt,
s64 remote_splice_amnt,
u64 remote_index,
struct local_anchor_info **anchor)
{
u8 *msg;
struct bitcoin_signature commit_sig, *htlc_sigs;
Expand All @@ -1520,6 +1520,7 @@ static u8 *send_commit_part(struct peer *peer,
const struct htlc **htlc_map;
struct wally_tx_output *direct_outputs[NUM_SIDES];
struct penalty_base *pbase;
int local_anchor_outnum;
struct tlv_commitment_signed_tlvs *cs_tlv
= tlv_commitment_signed_tlvs_new(tmpctx);

Expand All @@ -1536,11 +1537,11 @@ static u8 *send_commit_part(struct peer *peer,
derive_channel_id(cs_tlv->splice_info, funding);
}

txs = channel_splice_txs(tmpctx, funding, funding_sats, &htlc_map,
direct_outputs, &funding_wscript,
peer->channel, &peer->remote_per_commit,
remote_index, REMOTE,
splice_amnt, remote_splice_amnt);
txs = channel_txs(tmpctx, funding, funding_sats, &htlc_map,
direct_outputs, &funding_wscript,
peer->channel, &peer->remote_per_commit,
remote_index, REMOTE,
splice_amnt, remote_splice_amnt, &local_anchor_outnum);
htlc_sigs =
calc_commitsigs(tmpctx, peer, txs, funding_wscript, htlc_map,
remote_index, &commit_sig);
Expand All @@ -1555,6 +1556,16 @@ static u8 *send_commit_part(struct peer *peer,
} else
pbase = NULL;

if (local_anchor_outnum == -1) {
*anchor = NULL;
} else {
*anchor = tal(ctx, struct local_anchor_info);
bitcoin_txid(txs[0], &(*anchor)->anchor_point.txid);
(*anchor)->anchor_point.n = local_anchor_outnum;
(*anchor)->commitment_weight = bitcoin_tx_weight(txs[0]);
(*anchor)->commitment_fee = bitcoin_tx_compute_fee(txs[0]);
}

if (peer->dev_disable_commit) {
(*peer->dev_disable_commit)--;
if (*peer->dev_disable_commit == 0)
Expand All @@ -1568,9 +1579,7 @@ static u8 *send_commit_part(struct peer *peer,
msg = sending_commitsig_msg(NULL, remote_index, pbase,
peer->channel->fee_states,
peer->channel->blockheight_states,
changed_htlcs,
&commit_sig,
htlc_sigs);
changed_htlcs);
/* Message is empty; receiving it is the point. */
master_wait_sync_reply(tmpctx, peer, take(msg),
WIRE_CHANNELD_SENDING_COMMITSIG_REPLY);
Expand All @@ -1579,7 +1588,7 @@ static u8 *send_commit_part(struct peer *peer,
tal_count(htlc_sigs));
}

msg = towire_commitment_signed(NULL, &peer->channel_id,
msg = towire_commitment_signed(ctx, &peer->channel_id,
&commit_sig.s,
raw_sigs(tmpctx, htlc_sigs),
cs_tlv);
Expand All @@ -1599,6 +1608,7 @@ static void send_commit(struct peer *peer)
u32 feerate_target;
u8 **msgs = tal_arr(tmpctx, u8*, 1);
u8 *msg;
struct local_anchor_info *local_anchor, *anchors_info;

if (peer->dev_disable_commit && !*peer->dev_disable_commit) {
peer->commit_timer = NULL;
Expand Down Expand Up @@ -1710,9 +1720,13 @@ static void send_commit(struct peer *peer)
return;
}

msgs[0] = send_commit_part(peer, &peer->channel->funding,
anchors_info = tal_arr(tmpctx, struct local_anchor_info, 0);
msgs[0] = send_commit_part(msgs, peer, &peer->channel->funding,
peer->channel->funding_sats, changed_htlcs,
true, 0, 0, peer->next_index[REMOTE]);
true, 0, 0, peer->next_index[REMOTE],
&local_anchor);
if (local_anchor)
tal_arr_expand(&anchors_info, *local_anchor);

/* Loop over current inflights
* BOLT-0d8b701614b09c6ee4172b04da2203e73deec7e2 #2:
Expand All @@ -1730,15 +1744,23 @@ static void send_commit(struct peer *peer)
- peer->splice_state->inflights[i]->splice_amnt;

tal_arr_expand(&msgs,
send_commit_part(peer,
send_commit_part(msgs, peer,
&peer->splice_state->inflights[i]->outpoint,
peer->splice_state->inflights[i]->amnt,
changed_htlcs, false,
peer->splice_state->inflights[i]->splice_amnt,
remote_splice_amnt,
peer->next_index[REMOTE]));
peer->next_index[REMOTE],
&local_anchor));
if (local_anchor)
tal_arr_expand(&anchors_info, *local_anchor);
}

/* Now, tell master about the anchor on each of their commitments */
msg = towire_channeld_local_anchor_info(NULL, peer->next_index[REMOTE],
anchors_info);
wire_sync_write(MASTER_FD, take(msg));

peer->next_index[REMOTE]++;

for(u32 i = 0; i < tal_count(msgs); i++)
Expand Down Expand Up @@ -1972,6 +1994,7 @@ static struct commitsig *handle_peer_commit_sig(struct peer *peer,
struct amount_sat funding_sats;
struct channel_id active_id;
const struct commitsig **commitsigs;
int remote_anchor_outnum;

status_debug("handle_peer_commit_sig(splice: %d, remote_splice: %d)",
(int)splice_amnt, (int)remote_splice_amnt);
Expand Down Expand Up @@ -2052,11 +2075,11 @@ static struct commitsig *handle_peer_commit_sig(struct peer *peer,
funding_sats = peer->channel->funding_sats;
}

txs = channel_splice_txs(tmpctx, &outpoint, funding_sats, &htlc_map,
NULL, &funding_wscript, peer->channel,
&peer->next_local_per_commit,
peer->next_index[LOCAL], LOCAL, splice_amnt,
remote_splice_amnt);
txs = channel_txs(tmpctx, &outpoint, funding_sats, &htlc_map,
NULL, &funding_wscript, peer->channel,
&peer->next_local_per_commit,
peer->next_index[LOCAL], LOCAL, splice_amnt,
remote_splice_amnt, &remote_anchor_outnum);

/* Set the commit_sig on the commitment tx psbt */
if (!psbt_input_set_signature(txs[0]->psbt, 0,
Expand Down Expand Up @@ -4387,6 +4410,7 @@ static void resend_commitment(struct peer *peer, struct changed_htlc *last)
size_t i;
u8 *msg;
u8 **msgs = tal_arr(tmpctx, u8*, 1);
struct local_anchor_info *local_anchor;

status_debug("Retransmitting commitment, feerate LOCAL=%u REMOTE=%u,"
" blockheight LOCAL=%u REMOTE=%u",
Expand Down Expand Up @@ -4477,9 +4501,10 @@ static void resend_commitment(struct peer *peer, struct changed_htlc *last)
}
}

msgs[0] = send_commit_part(peer, &peer->channel->funding,
msgs[0] = send_commit_part(msgs, peer, &peer->channel->funding,
peer->channel->funding_sats, NULL,
false, 0, 0, peer->next_index[REMOTE] - 1);
false, 0, 0, peer->next_index[REMOTE] - 1,
&local_anchor);

/* Loop over current inflights
* BOLT-0d8b701614b09c6ee4172b04da2203e73deec7e2 #2:
Expand All @@ -4497,13 +4522,14 @@ static void resend_commitment(struct peer *peer, struct changed_htlc *last)
- peer->splice_state->inflights[i]->splice_amnt;

tal_arr_expand(&msgs,
send_commit_part(peer,
send_commit_part(msgs, peer,
&peer->splice_state->inflights[i]->outpoint,
peer->splice_state->inflights[i]->amnt,
NULL, false,
peer->splice_state->inflights[i]->splice_amnt,
remote_splice_amnt,
peer->next_index[REMOTE] - 1));
peer->next_index[REMOTE] - 1,
&local_anchor));
}

for(i = 0; i < tal_count(msgs); i++)
Expand Down Expand Up @@ -5787,6 +5813,7 @@ static void req_in(struct peer *peer, const u8 *msg)
case WIRE_CHANNELD_UPDATE_INFLIGHT:
case WIRE_CHANNELD_GOT_INFLIGHT:
case WIRE_CHANNELD_SPLICE_STATE_ERROR:
case WIRE_CHANNELD_LOCAL_ANCHOR_INFO:
break;
}
master_badmsg(-1, msg);
Expand Down
16 changes: 13 additions & 3 deletions channeld/channeld_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ msgdata,channeld_got_splice_locked,locked_txid,bitcoin_txid,

#include <common/penalty_base.h>

subtype,local_anchor_info
subtypedata,local_anchor_info,commitment_weight,u32,
subtypedata,local_anchor_info,commitment_fee,amount_sat,
subtypedata,local_anchor_info,anchor_point,bitcoin_outpoint,

# lightningd needs to track our anchor outputs on remote txs.
# This includes splices, so there could be more than one!
msgtype,channeld_local_anchor_info,1003
msgdata,channeld_local_anchor_info,remote_commitnum,u64,
msgdata,channeld_local_anchor_info,num_anchors,u16,
msgdata,channeld_local_anchor_info,anchors,local_anchor_info,num_anchors

# When we send a commitment_signed message, tell master.
msgtype,channeld_sending_commitsig,1020
msgdata,channeld_sending_commitsig,commitnum,u64,
Expand All @@ -137,9 +149,7 @@ msgdata,channeld_sending_commitsig,blockheight_states,height_states,
# SENT_ADD_COMMIT, SENT_REMOVE_ACK_COMMIT, SENT_ADD_ACK_COMMIT, SENT_REMOVE_COMMIT
msgdata,channeld_sending_commitsig,num_changed,u16,
msgdata,channeld_sending_commitsig,changed,changed_htlc,num_changed
msgdata,channeld_sending_commitsig,commit_sig,bitcoin_signature,
msgdata,channeld_sending_commitsig,num_htlc_sigs,u16,
msgdata,channeld_sending_commitsig,htlc_sigs,bitcoin_signature,num_htlc_sigs


# Wait for reply, to make sure it's on disk before we send commit.
msgtype,channeld_sending_commitsig_reply,1120
Expand Down
34 changes: 21 additions & 13 deletions channeld/commit_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
u64 obscured_commitment_number,
bool option_anchor_outputs,
bool option_anchors_zero_fee_htlc_tx,
enum side side)
enum side side,
int *anchor_outnum)
{
struct amount_sat base_fee;
struct amount_msat total_pay;
Expand All @@ -139,7 +140,8 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
u32 *cltvs;
bool to_local, to_remote;
struct htlc *dummy_to_local = (struct htlc *)0x01,
*dummy_to_remote = (struct htlc *)0x02;
*dummy_to_remote = (struct htlc *)0x02,
*dummy_other_anchor = (struct htlc *)0x03;
const u8 *funding_wscript = bitcoin_redeem_2of2(tmpctx,
local_funding_key,
remote_funding_key);
Expand Down Expand Up @@ -379,9 +381,11 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
n++;
}

/* With anchors, the caller really wants to know what
* is the LOCAL anchor for the REMOTE side. */
if (to_remote || untrimmed != 0) {
tx_add_anchor_output(tx, remote_funding_key);
(*htlcmap)[n] = NULL;
(*htlcmap)[n] = dummy_other_anchor;
n++;
}
}
Expand Down Expand Up @@ -433,17 +437,21 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
bitcoin_tx_add_input(tx, funding,
sequence, NULL, funding_sats, NULL, funding_wscript);

/* Identify the direct outputs (to_us, to_them). */
if (direct_outputs != NULL) {
/* Identify the direct outputs (to_us, to_them), and the local anchor */
if (direct_outputs != NULL)
direct_outputs[LOCAL] = direct_outputs[REMOTE] = NULL;
for (size_t i = 0; i < tx->wtx->num_outputs; i++) {
if ((*htlcmap)[i] == dummy_to_local) {
(*htlcmap)[i] = NULL;
direct_outputs[LOCAL] = tx->wtx->outputs + i;
} else if ((*htlcmap)[i] == dummy_to_remote) {
(*htlcmap)[i] = NULL;
direct_outputs[REMOTE] = tx->wtx->outputs + i;
}

*anchor_outnum = -1;
for (size_t i = 0; i < tx->wtx->num_outputs; i++) {
if ((*htlcmap)[i] == dummy_to_local) {
(*htlcmap)[i] = NULL;
direct_outputs[LOCAL] = tx->wtx->outputs + i;
} else if ((*htlcmap)[i] == dummy_to_remote) {
(*htlcmap)[i] = NULL;
direct_outputs[REMOTE] = tx->wtx->outputs + i;
} else if ((*htlcmap)[i] == dummy_other_anchor) {
(*htlcmap)[i] = NULL;
*anchor_outnum = i;
}
}

Expand Down
6 changes: 4 additions & 2 deletions channeld/commit_tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ bool commit_tx_amount_trimmed(const struct htlc **htlcs,
* @obscured_commitment_number: number to encode in commitment transaction
* @direct_outputs: If non-NULL, fill with pointers to the direct (non-HTLC) outputs (or NULL if none).
* @option_anchor_outputs: does option_anchor_outputs apply to this channel?
* @option_anchors_zero_fee_htlc_tx: does option_anchors_zero_fee_htlc_tx apply to this channel?
* @side: side to generate commitment transaction for.
* @option_anchor_outputs: does option_anchor_outputs apply to this channel?
* @anchor_outnum: set to index of local anchor, or -1 if none.
*
* We need to be able to generate the remote side's tx to create signatures,
* but the BOLT is expressed in terms of generating our local commitment
Expand All @@ -90,6 +91,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
u64 obscured_commitment_number,
bool option_anchor_outputs,
bool option_anchors_zero_fee_htlc_tx,
enum side side);
enum side side,
int *anchor_outnum);

#endif /* LIGHTNING_CHANNELD_COMMIT_TX_H */
28 changes: 7 additions & 21 deletions channeld/full_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,32 +299,18 @@ static void add_htlcs(struct bitcoin_tx ***txs,

/* FIXME: We could cache these. */
struct bitcoin_tx **channel_txs(const tal_t *ctx,
const struct bitcoin_outpoint *funding,
struct amount_sat funding_sats,
const struct htlc ***htlcmap,
struct wally_tx_output *direct_outputs[NUM_SIDES],
const u8 **funding_wscript,
const struct channel *channel,
const struct pubkey *per_commitment_point,
u64 commitment_number,
enum side side)
{
return channel_splice_txs(ctx, &channel->funding, channel->funding_sats,
htlcmap, direct_outputs, funding_wscript,
channel, per_commitment_point,
commitment_number, side, 0, 0);
}

struct bitcoin_tx **channel_splice_txs(const tal_t *ctx,
const struct bitcoin_outpoint *funding,
struct amount_sat funding_sats,
const struct htlc ***htlcmap,
struct wally_tx_output *direct_outputs[NUM_SIDES],
const u8 **funding_wscript,
const struct channel *channel,
const struct pubkey *per_commitment_point,
u64 commitment_number,
enum side side,
s64 splice_amnt,
s64 remote_splice_amnt)
enum side side,
s64 splice_amnt,
s64 remote_splice_amnt,
int *other_anchor_outnum)
{
struct bitcoin_tx **txs;
const struct htlc **committed;
Expand Down Expand Up @@ -378,7 +364,7 @@ struct bitcoin_tx **channel_splice_txs(const tal_t *ctx,
commitment_number ^ channel->commitment_number_obscurer,
channel_has(channel, OPT_ANCHOR_OUTPUTS),
channel_has(channel, OPT_ANCHORS_ZERO_FEE_HTLC_TX),
side);
side, other_anchor_outnum);

/* Set the remote/local pubkeys on the commitment tx psbt */
psbt_input_add_pubkey(txs[0]->psbt, 0,
Expand Down
Loading