diff --git a/channeld/channeld.c b/channeld/channeld.c index bae50819353a..cdd0d2240820 100644 --- a/channeld/channeld.c +++ b/channeld/channeld.c @@ -3992,6 +3992,7 @@ static void splice_initiator_user_finalized(struct peer *peer) char *error; u32 chan_output_index, splice_funding_index; struct wally_psbt_output *new_chan_output; + struct wally_psbt *psbt; struct inflight *new_inflight; struct bitcoin_txid current_psbt_txid; struct amount_sat both_amount; @@ -4003,7 +4004,7 @@ static void splice_initiator_user_finalized(struct peer *peer) /* We must loading the funding tx as our previous utxo */ prev_tx = bitcoin_tx_from_txid(peer, peer->channel->funding.txid); - ictx = new_interactivetx_context(tmpctx, our_role, + ictx = new_interactivetx_context(NULL, our_role, peer->pps, peer->channel_id); ictx->next_update_fn = next_splice_step; @@ -4015,60 +4016,82 @@ static void splice_initiator_user_finalized(struct peer *peer) ictx->tx_add_input_count = peer->splicing->tx_add_input_count; ictx->tx_add_output_count = peer->splicing->tx_add_output_count; + clone_psbt(NULL, ictx->current_psbt); + ictx->shared_outpoint = tal(ictx, struct bitcoin_outpoint); *ictx->shared_outpoint = peer->channel->funding; ictx->funding_tx = prev_tx; - error = process_interactivetx_updates(tmpctx, ictx, + clone_psbt(NULL, ictx->current_psbt); + + error = process_interactivetx_updates(ictx, ictx, &peer->splicing->received_tx_complete, &abort_msg); if (error) peer_failed_warn(peer->pps, &peer->channel_id, "Splice interactivetx error: %s", error); + clone_psbt(NULL, ictx->current_psbt); + check_tx_abort(peer, abort_msg); + psbt = ictx->current_psbt; + + clone_psbt(NULL, psbt); + /* With pause_when_complete fase, this assert should never fail */ assert(peer->splicing->received_tx_complete); peer->splicing->sent_tx_complete = true; - psbt_sort_by_serial_id(ictx->current_psbt); + clone_psbt(NULL, psbt); - new_chan_output = find_channel_output(peer, ictx->current_psbt, + new_chan_output = find_channel_output(peer, psbt, &chan_output_index, &peer->splicing->remote_funding_pubkey); + + clone_psbt(NULL, psbt); - splice_funding_index = find_channel_funding_input(ictx->current_psbt, + splice_funding_index = find_channel_funding_input(psbt, &peer->channel->funding); + + clone_psbt(NULL, psbt); - both_amount = check_balances(peer, our_role, ictx->current_psbt, + both_amount = check_balances(peer, our_role, psbt, chan_output_index, splice_funding_index); new_chan_output->amount = both_amount.satoshis; /* Raw: type conv */ + + clone_psbt(NULL, psbt); - psbt_elements_normalize_fees(ictx->current_psbt); + psbt_elements_normalize_fees(psbt); + + clone_psbt(NULL, psbt); status_debug("Splice adding inflight: %s", - fmt_wally_psbt(tmpctx, ictx->current_psbt)); + fmt_wally_psbt(tmpctx, psbt)); - psbt_txid(tmpctx, ictx->current_psbt, ¤t_psbt_txid, NULL); + psbt_txid(ictx, psbt, ¤t_psbt_txid, NULL); + + clone_psbt(NULL, psbt); - outmsg = towire_channeld_add_inflight(tmpctx, + outmsg = towire_channeld_add_inflight(NULL, &peer->splicing->remote_funding_pubkey, ¤t_psbt_txid, chan_output_index, peer->splicing->feerate_per_kw, amount_sat(new_chan_output->amount), peer->splicing->opener_relative, - ictx->current_psbt, + psbt, true, peer->splicing->force_sign_first); + + clone_psbt(NULL, psbt); master_wait_sync_reply(tmpctx, peer, take(outmsg), WIRE_CHANNELD_GOT_INFLIGHT); new_inflight = inflights_new(peer); - psbt_txid(tmpctx, ictx->current_psbt, &new_inflight->outpoint.txid, + psbt_txid(new_inflight, psbt, &new_inflight->outpoint.txid, NULL); new_inflight->remote_funding = peer->splicing->remote_funding_pubkey; new_inflight->outpoint.n = chan_output_index; @@ -4084,7 +4107,7 @@ static void splice_initiator_user_finalized(struct peer *peer) * normal in-memory copy of the psbt: peer->splicing/ictx->current_psbt. * Since we have to support using the inflight psbt anyway, we default * to it. */ - new_inflight->psbt = clone_psbt(new_inflight, ictx->current_psbt); + new_inflight->psbt = clone_psbt(new_inflight, psbt); // wally_psbt_clone_alloc fails here current_push_val = relative_splice_balance_fundee(peer, our_role, new_inflight->psbt, @@ -4121,6 +4144,8 @@ static void splice_initiator_user_finalized(struct peer *peer) true, !sign_first); wire_sync_write(MASTER_FD, take(outmsg)); + + tal_free(ictx); } /* During a splice the user may call splice_update mulitple times adding @@ -4141,7 +4166,7 @@ static void splice_initiator_user_update(struct peer *peer, const u8 *inmsg) return; } - ictx = new_interactivetx_context(tmpctx, TX_INITIATOR, + ictx = new_interactivetx_context(NULL, TX_INITIATOR, peer->pps, peer->channel_id); if (!fromwire_channeld_splice_update(ictx, inmsg, &ictx->desired_psbt)) @@ -4152,6 +4177,7 @@ static void splice_initiator_user_update(struct peer *peer, const u8 *inmsg) " splice when not in" " splice mode."); wire_sync_write(MASTER_FD, take(msg)); + tal_free(ictx); return; } @@ -4175,10 +4201,11 @@ static void splice_initiator_user_update(struct peer *peer, const u8 *inmsg) if (!interactivetx_has_changes(ictx, ictx->desired_psbt)) { splice_initiator_user_finalized(peer); tal_steal(last_inflight(peer), last_inflight(peer)->psbt); + tal_free(ictx); return; } - error = process_interactivetx_updates(tmpctx, ictx, + error = process_interactivetx_updates(ictx, ictx, &peer->splicing->received_tx_complete, &abort_msg); if (error) @@ -4199,6 +4226,7 @@ static void splice_initiator_user_update(struct peer *peer, const u8 *inmsg) ictx->current_psbt, false, false); wire_sync_write(MASTER_FD, take(outmsg)); + tal_free(ictx); } /* This occurs when the user has signed the final version of the PSBT. At this diff --git a/common/interactivetx.c b/common/interactivetx.c index 30854a6e04c8..c61fb2149812 100644 --- a/common/interactivetx.c +++ b/common/interactivetx.c @@ -391,19 +391,28 @@ char *process_interactivetx_updates(const tal_t *ctx, if (received_tx_complete) they_complete = *received_tx_complete; - /* Build change_set and handle PSBT variables */ - ictx->change_set = tal_free(ictx->change_set); + clone_psbt(NULL, ictx->current_psbt); + clone_psbt(NULL, ictx->desired_psbt); /* Call next_update_fn or default to 'desired_psbt' */ next_psbt = ictx->next_update_fn(ictx, ictx); + clone_psbt(NULL, ictx->current_psbt); + clone_psbt(NULL, ictx->desired_psbt); + /* Returning NULL from next_update_fn is the same as using `current_psbt` * with no changes -- both indicate no changes */ if (!next_psbt) next_psbt = ictx->current_psbt; + clone_psbt(NULL, ictx->current_psbt); + clone_psbt(NULL, ictx->desired_psbt); + ictx->change_set = get_changes(ctx, ictx, next_psbt); + clone_psbt(NULL, ictx->current_psbt); + clone_psbt(NULL, ictx->desired_psbt); + /* If current_psbt and next_psbt are the same, dont double free it! * Otherwise we advance `current_psbt` to `next_psbt` and begin * processing the change set in `ictx->change_set` */ @@ -411,6 +420,9 @@ char *process_interactivetx_updates(const tal_t *ctx, ictx->current_psbt = next_psbt; } + clone_psbt(NULL, ictx->current_psbt); + clone_psbt(NULL, ictx->desired_psbt); + /* As initiator we always start with a single send to start it off */ if (ictx->our_role == TX_INITIATOR) { error = send_next(ctx, ictx, &we_complete); @@ -423,6 +435,9 @@ char *process_interactivetx_updates(const tal_t *ctx, } } + clone_psbt(NULL, ictx->current_psbt); + clone_psbt(NULL, ictx->desired_psbt); + /* Loop through tx update turns with peer */ while (!(we_complete && they_complete)) { struct channel_id cid; @@ -436,10 +451,16 @@ char *process_interactivetx_updates(const tal_t *ctx, if (received_tx_complete) *received_tx_complete = false; + clone_psbt(NULL, ictx->current_psbt); + clone_psbt(NULL, ictx->desired_psbt); + msg = read_next_msg(ctx, ictx, &error); if (error) return error; + clone_psbt(NULL, ictx->current_psbt); + clone_psbt(NULL, ictx->desired_psbt); + t = fromwire_peektype(msg); switch (t) { case WIRE_TX_ADD_INPUT: { @@ -800,8 +821,14 @@ char *process_interactivetx_updates(const tal_t *ctx, send_next(ctx, ictx, &we_complete); } + clone_psbt(NULL, ictx->current_psbt); + clone_psbt(NULL, ictx->desired_psbt); + /* Sort psbt! */ psbt_sort_by_serial_id(ictx->current_psbt); + + clone_psbt(NULL, ictx->current_psbt); + clone_psbt(NULL, ictx->desired_psbt); return NULL; }