Skip to content

Commit 2c40355

Browse files
committed
splice: track funding_tx_index on inflights and channel funding
Give every funding tx a stable index: 0 for the original funding (including RBF attempts), incrementing by 1 per splice. Threaded through the channeld inflight, the channel, the channeld_init and channeld_add_inflight wire messages, set on splice creation and carried onto the channel funding when a splice locks, and persisted in the channel_funding_inflights and channels tables.
1 parent 8208b07 commit 2c40355

28 files changed

Lines changed: 90 additions & 13 deletions

channeld/channeld.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@ static void check_mutual_splice_locked(struct peer *peer)
483483
fmt_channel(tmpctx, peer->channel));
484484

485485
error = channel_update_funding(peer->channel, &inflight->outpoint,
486+
inflight->funding_tx_index,
486487
inflight->amnt,
487488
inflight->splice_amnt);
488489
if (error)
@@ -4362,6 +4363,7 @@ static void splice_accepter(struct peer *peer, const u8 *inmsg)
43624363
&peer->splicing->remote_funding_pubkey,
43634364
&outpoint.txid,
43644365
outpoint.n,
4366+
peer->channel->funding_tx_index + 1,
43654367
funding_feerate_perkw,
43664368
both_amount,
43674369
peer->splicing->accepter_relative,
@@ -4379,6 +4381,8 @@ static void splice_accepter(struct peer *peer, const u8 *inmsg)
43794381
&new_inflight->outpoint.txid, NULL);
43804382
new_inflight->remote_funding = peer->splicing->remote_funding_pubkey;
43814383
new_inflight->outpoint = outpoint;
4384+
/* A splice's funding tx is the parent funding's index + 1. */
4385+
new_inflight->funding_tx_index = peer->channel->funding_tx_index + 1;
43824386
new_inflight->amnt = both_amount;
43834387
new_inflight->psbt = clone_psbt(new_inflight, ictx->current_psbt);
43844388
new_inflight->splice_amnt = peer->splicing->accepter_relative;
@@ -4657,6 +4661,7 @@ static void splice_initiator_user_finalized(struct peer *peer)
46574661
&peer->splicing->remote_funding_pubkey,
46584662
&current_psbt_txid,
46594663
chan_output_index,
4664+
peer->channel->funding_tx_index + 1,
46604665
peer->splicing->feerate_per_kw,
46614666
amount_sat(new_chan_output->amount),
46624667
peer->splicing->opener_relative,
@@ -4674,6 +4679,8 @@ static void splice_initiator_user_finalized(struct peer *peer)
46744679
NULL);
46754680
new_inflight->remote_funding = peer->splicing->remote_funding_pubkey;
46764681
new_inflight->outpoint.n = chan_output_index;
4682+
/* A splice's funding tx is the parent funding's index + 1. */
4683+
new_inflight->funding_tx_index = peer->channel->funding_tx_index + 1;
46774684
new_inflight->amnt = amount_sat(new_chan_output->amount);
46784685
new_inflight->splice_amnt = peer->splicing->opener_relative;
46794686
new_inflight->last_tx = NULL;
@@ -6825,6 +6832,7 @@ static void init_channel(struct peer *peer)
68256832
{
68266833
struct basepoints points[NUM_SIDES];
68276834
struct amount_sat funding_sats;
6835+
u32 funding_tx_index;
68286836
struct amount_msat local_msat;
68296837
struct pubkey funding_pubkey[NUM_SIDES];
68306838
struct channel_config conf[NUM_SIDES];
@@ -6854,6 +6862,7 @@ static void init_channel(struct peer *peer)
68546862
&peer->channel_id,
68556863
&funding,
68566864
&funding_sats,
6865+
&funding_tx_index,
68576866
&minimum_depth,
68586867
&peer->our_blockheight,
68596868
&blockheight_states,
@@ -6968,6 +6977,7 @@ static void init_channel(struct peer *peer)
69686977

69696978
peer->channel = new_full_channel(peer, &peer->channel_id,
69706979
&funding,
6980+
funding_tx_index,
69716981
minimum_depth,
69726982
take(blockheight_states),
69736983
lease_expiry,

channeld/channeld_wire.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ msgdata,channeld_init,hsm_capabilities,u32,num_hsm_capabilities
2121
msgdata,channeld_init,channel_id,channel_id,
2222
msgdata,channeld_init,funding,bitcoin_outpoint,
2323
msgdata,channeld_init,funding_satoshi,amount_sat,
24+
msgdata,channeld_init,funding_tx_index,u32,
2425
msgdata,channeld_init,minimum_depth,u32,
2526
msgdata,channeld_init,our_blockheight,u32,
2627
msgdata,channeld_init,blockheight_states,height_states,
@@ -261,6 +262,7 @@ msgtype,channeld_add_inflight,7216
261262
msgdata,channeld_add_inflight,remote_funding,pubkey,
262263
msgdata,channeld_add_inflight,tx_id,bitcoin_txid,
263264
msgdata,channeld_add_inflight,tx_outnum,u32,
265+
msgdata,channeld_add_inflight,funding_tx_index,u32,
264266
msgdata,channeld_add_inflight,feerate,u32,
265267
msgdata,channeld_add_inflight,satoshis,amount_sat,
266268
msgdata,channeld_add_inflight,splice_amount,s64,

channeld/full_channel.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ static bool balance_ok(const struct balance *balance,
7474
struct channel *new_full_channel(const tal_t *ctx,
7575
const struct channel_id *cid,
7676
const struct bitcoin_outpoint *funding,
77+
u32 funding_tx_index,
7778
u32 minimum_depth,
7879
const struct height_states *blockheight_states,
7980
u32 lease_expiry,
@@ -93,6 +94,7 @@ struct channel *new_full_channel(const tal_t *ctx,
9394
struct channel *channel = new_initial_channel(ctx,
9495
cid,
9596
funding,
97+
funding_tx_index,
9698
minimum_depth,
9799
blockheight_states,
98100
lease_expiry,

channeld/full_channel.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ struct existing_htlc;
3636
struct channel *new_full_channel(const tal_t *ctx,
3737
const struct channel_id *cid,
3838
const struct bitcoin_outpoint *funding,
39+
u32 funding_tx_index,
3940
u32 minimum_depth,
4041
const struct height_states *blockheight_states,
4142
u32 lease_expiry,

channeld/inflight.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ struct inflight *fromwire_inflight(const tal_t *ctx, const u8 **cursor, size_t *
99
struct inflight *inflight = tal(ctx, struct inflight);
1010

1111
fromwire_bitcoin_outpoint(cursor, max, &inflight->outpoint);
12+
inflight->funding_tx_index = fromwire_u32(cursor, max);
1213
fromwire_pubkey(cursor, max, &inflight->remote_funding);
1314
inflight->amnt = fromwire_amount_sat(cursor, max);
1415
inflight->remote_tx_sigs = fromwire_bool(cursor, max);
@@ -41,6 +42,7 @@ struct inflight *fromwire_inflight(const tal_t *ctx, const u8 **cursor, size_t *
4142
void towire_inflight(u8 **pptr, const struct inflight *inflight)
4243
{
4344
towire_bitcoin_outpoint(pptr, &inflight->outpoint);
45+
towire_u32(pptr, inflight->funding_tx_index);
4446
towire_pubkey(pptr, &inflight->remote_funding);
4547
towire_amount_sat(pptr, inflight->amnt);
4648
towire_bool(pptr, inflight->remote_tx_sigs);

channeld/inflight.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
struct inflight {
1111
/* The new channel outpoint */
1212
struct bitcoin_outpoint outpoint;
13+
/* Which funding tx this is: 0 for the original funding (incl. RBF
14+
* attempts), incrementing by 1 for each splice. */
15+
u32 funding_tx_index;
1316
struct pubkey remote_funding;
1417
struct amount_sat amnt;
1518
bool remote_tx_sigs;

channeld/test/run-full_channel.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ int main(int argc, const char *argv[])
492492
feerate_per_kw[LOCAL] = feerate_per_kw[REMOTE] = 15000;
493493
derive_channel_id(&cid, &funding);
494494
lchannel = new_full_channel(tmpctx, &cid,
495-
&funding, 0,
495+
&funding, 0, 0,
496496
take(new_height_states(NULL, LOCAL, &blockheight)),
497497
0, /* No channel lease */
498498
funding_amount, to_local,
@@ -505,7 +505,7 @@ int main(int argc, const char *argv[])
505505
&remote_funding_pubkey,
506506
take(channel_type_static_remotekey(NULL)), false, LOCAL);
507507
rchannel = new_full_channel(tmpctx, &cid,
508-
&funding, 0,
508+
&funding, 0, 0,
509509
take(new_height_states(NULL, REMOTE, &blockheight)),
510510
0, /* No channel lease */
511511
funding_amount, to_remote,

common/initial_channel.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
struct channel *new_initial_channel(const tal_t *ctx,
1313
const struct channel_id *cid,
1414
const struct bitcoin_outpoint *funding,
15+
u32 funding_tx_index,
1516
u32 minimum_depth,
1617
const struct height_states *height_states TAKES,
1718
u32 lease_expiry,
@@ -46,6 +47,7 @@ struct channel *new_initial_channel(const tal_t *ctx,
4647

4748
channel->cid = *cid;
4849
channel->funding = *funding;
50+
channel->funding_tx_index = funding_tx_index;
4951
channel->funding_sats = funding_sats;
5052
channel->minimum_depth = minimum_depth;
5153
channel->lease_expiry = lease_expiry;
@@ -156,6 +158,7 @@ struct bitcoin_tx *initial_channel_tx(const tal_t *ctx,
156158

157159
const char *channel_update_funding(struct channel *channel,
158160
const struct bitcoin_outpoint *funding,
161+
u32 funding_tx_index,
159162
struct amount_sat funding_sats,
160163
s64 splice_amnt)
161164
{
@@ -164,6 +167,7 @@ const char *channel_update_funding(struct channel *channel,
164167

165168
channel->funding = *funding;
166169
channel->funding_sats = funding_sats;
170+
channel->funding_tx_index = funding_tx_index;
167171

168172
if (splice_amnt * 1000 + channel->view[LOCAL].owed[LOCAL].millisatoshis < 0) /* Raw: splicing */
169173
return tal_fmt(tmpctx, "Channel funding update would make local"

common/initial_channel.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ struct channel {
3939
/* Funding txid and output. */
4040
struct bitcoin_outpoint funding;
4141

42+
/* Which funding tx this is: 0 for the original funding (incl. RBF
43+
* attempts), incrementing by 1 for each splice. */
44+
u32 funding_tx_index;
45+
4246
/* Keys used to spend funding tx. */
4347
struct pubkey funding_pubkey[NUM_SIDES];
4448

@@ -88,6 +92,7 @@ struct channel {
8892
* @ctx: tal context to allocate return value from.
8993
* @cid: The channel's id.
9094
* @funding: The commitment transaction id/outnum
95+
* @funding_tx_index: 0 for the original funding, +1 for each splice.
9196
* @minimum_depth: The minimum confirmations needed for funding transaction.
9297
* @height_states: The blockheight update states.
9398
* @lease_expiry: Block the lease expires.
@@ -109,6 +114,7 @@ struct channel {
109114
struct channel *new_initial_channel(const tal_t *ctx,
110115
const struct channel_id *cid,
111116
const struct bitcoin_outpoint *funding,
117+
u32 funding_tx_index,
112118
u32 minimum_depth,
113119
const struct height_states *height_states TAKES,
114120
u32 lease_expiry,
@@ -154,6 +160,7 @@ struct bitcoin_tx *initial_channel_tx(const tal_t *ctx,
154160
*/
155161
const char *channel_update_funding(struct channel *channel,
156162
const struct bitcoin_outpoint *funding,
163+
u32 funding_tx_index,
157164
struct amount_sat funding_sats,
158165
s64 splice_amnt);
159166

devtools/mkcommit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ int main(int argc, char *argv[])
399399

400400
channel = new_full_channel(NULL,
401401
&cid,
402-
&funding, 1,
402+
&funding, 0, 1,
403403
take(new_height_states(NULL, fee_payer,
404404
&blockheight)),
405405
0, /* Defaults to no lease */

0 commit comments

Comments
 (0)