Skip to content

Commit d16d033

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.
1 parent 9ffd9b3 commit d16d033

File tree

1 file changed

+28
-34
lines changed

1 file changed

+28
-34
lines changed

channeld/channeld.c

Lines changed: 28 additions & 34 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;
@@ -3858,13 +3858,10 @@ static void splice_initiator(struct peer *peer, const u8 *inmsg)
38583858
u8 *outmsg;
38593859
struct interactivetx_context *ictx;
38603860
struct bitcoin_tx *prev_tx;
3861+
struct wally_psbt *psbt = peer->splicing->current_psbt;
38613862
u32 sequence = 0;
38623863
u8 *scriptPubkey;
38633864

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-
38683865
if (!fromwire_splice_ack(inmsg,
38693866
&channel_id,
38703867
&peer->splicing->accepter_relative,
@@ -3886,10 +3883,6 @@ static void splice_initiator(struct peer *peer, const u8 *inmsg)
38863883
peer->splice_state->locked_ready[LOCAL] = false;
38873884
peer->splice_state->locked_ready[REMOTE] = false;
38883885

3889-
ictx->next_update_fn = next_splice_step;
3890-
ictx->pause_when_complete = true;
3891-
ictx->desired_psbt = peer->splicing->current_psbt;
3892-
38933886
/* We go first as the receiver of the ack.
38943887
*
38953888
* BOLT-0d8b701614b09c6ee4172b04da2203e73deec7e2 #2:
@@ -3904,7 +3897,7 @@ static void splice_initiator(struct peer *peer, const u8 *inmsg)
39043897
&peer->channel->funding_pubkey[LOCAL],
39053898
&peer->splicing->remote_funding_pubkey);
39063899

3907-
input_index = ictx->desired_psbt->num_inputs;
3900+
input_index = psbt->num_inputs;
39083901

39093902
/* First we spend the existing channel outpoint
39103903
*
@@ -3913,21 +3906,21 @@ static void splice_initiator(struct peer *peer, const u8 *inmsg)
39133906
* - MUST `tx_add_input` an input which spends the current funding
39143907
* transaction output.
39153908
*/
3916-
psbt_append_input(ictx->desired_psbt, &peer->channel->funding, sequence,
3917-
NULL, wit_script, NULL);
3909+
psbt_append_input(psbt, &peer->channel->funding, sequence, NULL,
3910+
wit_script, NULL);
39183911

39193912
/* Segwit requires us to store the value of the outpoint being spent,
39203913
* 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,
3914+
scriptPubkey = scriptpubkey_p2wsh(psbt, wit_script);
3915+
psbt_input_set_wit_utxo(psbt, input_index,
39233916
scriptPubkey, peer->channel->funding_sats);
39243917

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

39293922
/* PSBT v2 requires this */
3930-
psbt_input_set_outpoint(ictx->desired_psbt, input_index,
3923+
psbt_input_set_outpoint(psbt, input_index,
39313924
peer->channel->funding);
39323925

39333926
/* Next we add the new channel outpoint, with a 0 amount for now. It
@@ -3939,26 +3932,23 @@ static void splice_initiator(struct peer *peer, const u8 *inmsg)
39393932
* - MUST `tx_add_output` a zero-value output which pays to the two
39403933
* funding keys using the higher of the two `generation` fields.
39413934
*/
3942-
psbt_append_output(ictx->desired_psbt,
3943-
scriptpubkey_p2wsh(ictx->desired_psbt, new_wit_script),
3935+
psbt_append_output(psbt,
3936+
scriptpubkey_p2wsh(psbt, new_wit_script),
39443937
calc_balance(peer));
39453938

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;
3939+
psbt_add_serials(psbt, TX_INITIATOR);
39513940

39523941
peer->splicing->tx_add_input_count = 0;
39533942
peer->splicing->tx_add_output_count = 0;
39543943

39553944
peer->splicing->mode = true;
39563945

39573946
/* Return the current PSBT to the channel_control to give to user. */
3958-
outmsg = towire_channeld_splice_confirmed_init(NULL,
3959-
ictx->desired_psbt);
3947+
outmsg = towire_channeld_splice_confirmed_init(NULL, psbt);
39603948
wire_sync_write(MASTER_FD, take(outmsg));
39613949

3950+
/* We reset current_psbt to empty as now it represends the difference
3951+
* what we've sent our peer so far */
39623952
tal_free(peer->splicing->current_psbt);
39633953
peer->splicing->current_psbt = create_psbt(peer->splicing, 0, 0, 0);
39643954
}
@@ -3993,7 +3983,10 @@ static void splice_initiator_user_finalized(struct peer *peer)
39933983

39943984
ictx->next_update_fn = next_splice_step;
39953985
ictx->pause_when_complete = false;
3996-
ictx->desired_psbt = ictx->current_psbt = peer->splicing->current_psbt;
3986+
ictx->desired_psbt = ictx->current_psbt = clone_psbt(ictx,
3987+
peer->splicing->current_psbt);
3988+
tal_free(peer->splicing->current_psbt);
3989+
peer->splicing->current_psbt = NULL;
39973990
ictx->tx_add_input_count = peer->splicing->tx_add_input_count;
39983991
ictx->tx_add_output_count = peer->splicing->tx_add_output_count;
39993992

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

40734064
current_push_val = relative_splice_balance_fundee(peer, our_role,
40744065
new_inflight->psbt,
@@ -4145,6 +4136,8 @@ static void splice_initiator_user_update(struct peer *peer, const u8 *inmsg)
41454136

41464137
/* Should already have a current_psbt from a previously initiated one */
41474138
assert(peer->splicing->current_psbt);
4139+
/* peer->splicing->current_psbt represents what PSBT we have sent to
4140+
* our peer so far. */
41484141
ictx->current_psbt = peer->splicing->current_psbt;
41494142
ictx->tx_add_input_count = peer->splicing->tx_add_input_count;
41504143
ictx->tx_add_output_count = peer->splicing->tx_add_output_count;
@@ -4170,8 +4163,8 @@ static void splice_initiator_user_update(struct peer *peer, const u8 *inmsg)
41704163

41714164
if (peer->splicing->current_psbt != ictx->current_psbt)
41724165
tal_free(peer->splicing->current_psbt);
4173-
peer->splicing->current_psbt = tal_steal(peer->splicing,
4174-
ictx->current_psbt);
4166+
peer->splicing->current_psbt = clone_psbt(peer->splicing,
4167+
ictx->current_psbt);
41754168

41764169
/* Peer may have modified our PSBT so we return it to the user here */
41774170
outmsg = towire_channeld_splice_confirmed_update(NULL,
@@ -4205,7 +4198,7 @@ static void splice_initiator_user_signed(struct peer *peer, const u8 *inmsg)
42054198
return;
42064199
}
42074200

4208-
if (!fromwire_channeld_splice_signed(tmpctx, inmsg, &signed_psbt,
4201+
if (!fromwire_channeld_splice_signed(inflight, inmsg, &signed_psbt,
42094202
&peer->splicing->force_sign_first))
42104203
master_badmsg(WIRE_CHANNELD_SPLICE_SIGNED, inmsg);
42114204

@@ -4244,7 +4237,7 @@ static void splice_initiator_user_signed(struct peer *peer, const u8 *inmsg)
42444237
fmt_bitcoin_txid(tmpctx, &current_psbt_txid));
42454238

42464239
tal_free(inflight->psbt);
4247-
inflight->psbt = tal_steal(inflight, signed_psbt);
4240+
inflight->psbt = clone_psbt(inflight, signed_psbt);
42484241

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

42564249
wire_sync_write(MASTER_FD, take(outmsg));
42574250

4258-
sign_first = do_i_sign_first(peer, signed_psbt, TX_INITIATOR,
4251+
sign_first = do_i_sign_first(peer, inflight->psbt, TX_INITIATOR,
42594252
inflight->force_sign_first);
42604253

42614254
resume_splice_negotiation(peer, false, false, true, sign_first);
@@ -5612,6 +5605,7 @@ static void handle_funding_depth(struct peer *peer, const u8 *msg)
56125605
if (bitcoin_txid_eq(&inflight->outpoint.txid,
56135606
&txid)) {
56145607
inflight->is_locked = true;
5608+
assert(inflight->psbt);
56155609
msg = towire_channeld_update_inflight(NULL,
56165610
inflight->psbt,
56175611
NULL,

0 commit comments

Comments
 (0)