Skip to content

Commit 8feb43a

Browse files
committed
Splice: Clone psbts instead of steal
The interaction betwen libwally and CLN’s memory management is tricky. Let’s dodge that problem and just clone the PSBTs. Clean up some unused PSBT / ictx code while we’re at it
1 parent 0b52099 commit 8feb43a

File tree

1 file changed

+28
-35
lines changed

1 file changed

+28
-35
lines changed

channeld/channeld.c

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3829,7 +3829,7 @@ static void splice_accepter(struct peer *peer, const u8 *inmsg)
38293829
new_inflight->remote_funding = peer->splicing->remote_funding_pubkey;
38303830
new_inflight->outpoint = outpoint;
38313831
new_inflight->amnt = both_amount;
3832-
new_inflight->psbt = tal_steal(new_inflight, ictx->current_psbt);
3832+
new_inflight->psbt = clone_psbt(new_inflight, ictx->current_psbt);
38333833
new_inflight->splice_amnt = peer->splicing->accepter_relative;
38343834
new_inflight->last_tx = NULL;
38353835
new_inflight->i_am_initiator = false;
@@ -3856,15 +3856,11 @@ static void splice_initiator(struct peer *peer, const u8 *inmsg)
38563856
size_t input_index;
38573857
const u8 *wit_script, *new_wit_script;
38583858
u8 *outmsg;
3859-
struct interactivetx_context *ictx;
38603859
struct bitcoin_tx *prev_tx;
3860+
struct wally_psbt *psbt = peer->splicing->current_psbt;
38613861
u32 sequence = 0;
38623862
u8 *scriptPubkey;
38633863

3864-
/* DTODO: Remove ictx from this function as its no longer used. */
3865-
ictx = new_interactivetx_context(tmpctx, TX_INITIATOR,
3866-
peer->pps, peer->channel_id);
3867-
38683864
if (!fromwire_splice_ack(inmsg,
38693865
&channel_id,
38703866
&peer->splicing->accepter_relative,
@@ -3886,10 +3882,6 @@ static void splice_initiator(struct peer *peer, const u8 *inmsg)
38863882
peer->splice_state->locked_ready[LOCAL] = false;
38873883
peer->splice_state->locked_ready[REMOTE] = false;
38883884

3889-
ictx->next_update_fn = next_splice_step;
3890-
ictx->pause_when_complete = true;
3891-
ictx->desired_psbt = peer->splicing->current_psbt;
3892-
38933885
/* We go first as the receiver of the ack.
38943886
*
38953887
* BOLT-0d8b701614b09c6ee4172b04da2203e73deec7e2 #2:
@@ -3904,7 +3896,7 @@ static void splice_initiator(struct peer *peer, const u8 *inmsg)
39043896
&peer->channel->funding_pubkey[LOCAL],
39053897
&peer->splicing->remote_funding_pubkey);
39063898

3907-
input_index = ictx->desired_psbt->num_inputs;
3899+
input_index = psbt->num_inputs;
39083900

39093901
/* First we spend the existing channel outpoint
39103902
*
@@ -3913,21 +3905,21 @@ static void splice_initiator(struct peer *peer, const u8 *inmsg)
39133905
* - MUST `tx_add_input` an input which spends the current funding
39143906
* transaction output.
39153907
*/
3916-
psbt_append_input(ictx->desired_psbt, &peer->channel->funding, sequence,
3917-
NULL, wit_script, NULL);
3908+
psbt_append_input(psbt, &peer->channel->funding, sequence, NULL,
3909+
wit_script, NULL);
39183910

39193911
/* Segwit requires us to store the value of the outpoint being spent,
39203912
* so let's do that */
3921-
scriptPubkey = scriptpubkey_p2wsh(ictx->desired_psbt, wit_script);
3922-
psbt_input_set_wit_utxo(ictx->desired_psbt, input_index,
3913+
scriptPubkey = scriptpubkey_p2wsh(psbt, wit_script);
3914+
psbt_input_set_wit_utxo(psbt, input_index,
39233915
scriptPubkey, peer->channel->funding_sats);
39243916

39253917
/* We must loading the funding tx as our previous utxo */
39263918
prev_tx = bitcoin_tx_from_txid(peer, peer->channel->funding.txid);
3927-
psbt_input_set_utxo(ictx->desired_psbt, input_index, prev_tx->wtx);
3919+
psbt_input_set_utxo(psbt, input_index, prev_tx->wtx);
39283920

39293921
/* PSBT v2 requires this */
3930-
psbt_input_set_outpoint(ictx->desired_psbt, input_index,
3922+
psbt_input_set_outpoint(psbt, input_index,
39313923
peer->channel->funding);
39323924

39333925
/* Next we add the new channel outpoint, with a 0 amount for now. It
@@ -3939,26 +3931,23 @@ static void splice_initiator(struct peer *peer, const u8 *inmsg)
39393931
* - MUST `tx_add_output` a zero-value output which pays to the two
39403932
* funding keys using the higher of the two `generation` fields.
39413933
*/
3942-
psbt_append_output(ictx->desired_psbt,
3943-
scriptpubkey_p2wsh(ictx->desired_psbt, new_wit_script),
3934+
psbt_append_output(psbt,
3935+
scriptpubkey_p2wsh(psbt, new_wit_script),
39443936
calc_balance(peer));
39453937

3946-
psbt_add_serials(ictx->desired_psbt, ictx->our_role);
3947-
3948-
ictx->shared_outpoint = tal(ictx, struct bitcoin_outpoint);
3949-
*ictx->shared_outpoint = peer->channel->funding;
3950-
ictx->funding_tx = prev_tx;
3938+
psbt_add_serials(psbt, TX_INITIATOR);
39513939

39523940
peer->splicing->tx_add_input_count = 0;
39533941
peer->splicing->tx_add_output_count = 0;
39543942

39553943
peer->splicing->mode = true;
39563944

39573945
/* Return the current PSBT to the channel_control to give to user. */
3958-
outmsg = towire_channeld_splice_confirmed_init(NULL,
3959-
ictx->desired_psbt);
3946+
outmsg = towire_channeld_splice_confirmed_init(NULL, psbt);
39603947
wire_sync_write(MASTER_FD, take(outmsg));
39613948

3949+
/* We reset current_psbt to empty as now it represends the difference
3950+
* what we've sent our peer so far */
39623951
tal_free(peer->splicing->current_psbt);
39633952
peer->splicing->current_psbt = create_psbt(peer->splicing, 0, 0, 0);
39643953
}
@@ -3993,7 +3982,10 @@ static void splice_initiator_user_finalized(struct peer *peer)
39933982

39943983
ictx->next_update_fn = next_splice_step;
39953984
ictx->pause_when_complete = false;
3996-
ictx->desired_psbt = ictx->current_psbt = peer->splicing->current_psbt;
3985+
ictx->desired_psbt = ictx->current_psbt = clone_psbt(ictx,
3986+
peer->splicing->current_psbt);
3987+
tal_free(peer->splicing->current_psbt);
3988+
peer->splicing->current_psbt = NULL;
39973989
ictx->tx_add_input_count = peer->splicing->tx_add_input_count;
39983990
ictx->tx_add_output_count = peer->splicing->tx_add_output_count;
39993991

@@ -4066,9 +4058,7 @@ static void splice_initiator_user_finalized(struct peer *peer)
40664058
* normal in-memory copy of the psbt: peer->splicing/ictx->current_psbt.
40674059
* Since we have to support using the inflight psbt anyway, we default
40684060
* to it. */
4069-
new_inflight->psbt = tal_steal(new_inflight, ictx->current_psbt);
4070-
ictx->current_psbt = NULL;
4071-
peer->splicing->current_psbt = NULL;
4061+
new_inflight->psbt = clone_psbt(new_inflight, ictx->current_psbt);
40724062

40734063
current_push_val = relative_splice_balance_fundee(peer, our_role,
40744064
new_inflight->psbt,
@@ -4145,6 +4135,8 @@ static void splice_initiator_user_update(struct peer *peer, const u8 *inmsg)
41454135

41464136
/* Should already have a current_psbt from a previously initiated one */
41474137
assert(peer->splicing->current_psbt);
4138+
/* peer->splicing->current_psbt represents what PSBT we have sent to
4139+
* our peer so far. */
41484140
ictx->current_psbt = peer->splicing->current_psbt;
41494141
ictx->tx_add_input_count = peer->splicing->tx_add_input_count;
41504142
ictx->tx_add_output_count = peer->splicing->tx_add_output_count;
@@ -4170,8 +4162,8 @@ static void splice_initiator_user_update(struct peer *peer, const u8 *inmsg)
41704162

41714163
if (peer->splicing->current_psbt != ictx->current_psbt)
41724164
tal_free(peer->splicing->current_psbt);
4173-
peer->splicing->current_psbt = tal_steal(peer->splicing,
4174-
ictx->current_psbt);
4165+
peer->splicing->current_psbt = clone_psbt(peer->splicing,
4166+
ictx->current_psbt);
41754167

41764168
/* Peer may have modified our PSBT so we return it to the user here */
41774169
outmsg = towire_channeld_splice_confirmed_update(NULL,
@@ -4205,7 +4197,7 @@ static void splice_initiator_user_signed(struct peer *peer, const u8 *inmsg)
42054197
return;
42064198
}
42074199

4208-
if (!fromwire_channeld_splice_signed(tmpctx, inmsg, &signed_psbt,
4200+
if (!fromwire_channeld_splice_signed(inflight, inmsg, &signed_psbt,
42094201
&peer->splicing->force_sign_first))
42104202
master_badmsg(WIRE_CHANNELD_SPLICE_SIGNED, inmsg);
42114203

@@ -4244,7 +4236,7 @@ static void splice_initiator_user_signed(struct peer *peer, const u8 *inmsg)
42444236
fmt_bitcoin_txid(tmpctx, &current_psbt_txid));
42454237

42464238
tal_free(inflight->psbt);
4247-
inflight->psbt = tal_steal(inflight, signed_psbt);
4239+
inflight->psbt = clone_psbt(inflight, signed_psbt);
42484240

42494241
/* Save the user provided signatures to DB incase we have to
42504242
* restart and reestablish later. */
@@ -4255,7 +4247,7 @@ static void splice_initiator_user_signed(struct peer *peer, const u8 *inmsg)
42554247

42564248
wire_sync_write(MASTER_FD, take(outmsg));
42574249

4258-
sign_first = do_i_sign_first(peer, signed_psbt, TX_INITIATOR,
4250+
sign_first = do_i_sign_first(peer, inflight->psbt, TX_INITIATOR,
42594251
inflight->force_sign_first);
42604252

42614253
resume_splice_negotiation(peer, false, false, true, sign_first);
@@ -5624,6 +5616,7 @@ static void handle_funding_depth(struct peer *peer, const u8 *msg)
56245616
if (bitcoin_txid_eq(&inflight->outpoint.txid,
56255617
&txid)) {
56265618
inflight->is_locked = true;
5619+
assert(inflight->psbt);
56275620
msg = towire_channeld_update_inflight(NULL,
56285621
inflight->psbt,
56295622
NULL,

0 commit comments

Comments
 (0)