inflights: split up adding sigs from making a new inflight

We're going to add the commitment transaction data at a different time
than when we init a new inflight. Split them up!
This commit is contained in:
niftynei 2023-10-26 11:41:34 -05:00 committed by Rusty Russell
parent d69f0aac60
commit 48d2760c56
7 changed files with 28 additions and 70 deletions

View File

@ -126,8 +126,6 @@ new_inflight(struct channel *channel,
struct amount_sat total_funds,
struct amount_sat our_funds,
struct wally_psbt *psbt STEALS,
struct bitcoin_tx *last_tx,
const struct bitcoin_signature last_sig,
const u32 lease_expiry,
const secp256k1_ecdsa_signature *lease_commit_sig,
const u32 lease_chan_max_msat, const u16 lease_chan_max_ppt,
@ -137,7 +135,6 @@ new_inflight(struct channel *channel,
s64 splice_amnt,
bool i_am_initiator)
{
struct wally_psbt *last_tx_psbt_clone;
struct channel_inflight *inflight
= tal(channel, struct channel_inflight);
struct funding_info *funding
@ -153,14 +150,7 @@ new_inflight(struct channel *channel,
inflight->channel = channel;
inflight->remote_tx_sigs = false;
inflight->funding_psbt = tal_steal(inflight, psbt);
/* Make a 'clone' of this tx */
inflight->last_tx = NULL;
if (last_tx) {
last_tx_psbt_clone = clone_psbt(inflight, last_tx->psbt);
inflight->last_tx = bitcoin_tx_with_psbt(inflight, last_tx_psbt_clone);
}
inflight->last_sig = last_sig;
inflight->tx_broadcast = false;
/* Channel lease infos */
@ -184,6 +174,19 @@ new_inflight(struct channel *channel,
return inflight;
}
void inflight_set_last_tx(struct channel_inflight *inflight,
struct bitcoin_tx *last_tx,
const struct bitcoin_signature last_sig)
{
struct wally_psbt *last_tx_psbt_clone;
assert(inflight->last_tx == NULL);
assert(last_tx);
last_tx_psbt_clone = clone_psbt(inflight, last_tx->psbt);
inflight->last_tx = bitcoin_tx_with_psbt(inflight, last_tx_psbt_clone);
inflight->last_sig = last_sig;
}
struct open_attempt *new_channel_open_attempt(struct channel *channel)
{
struct open_attempt *oa = tal(channel, struct open_attempt);

View File

@ -389,8 +389,6 @@ struct channel_inflight *new_inflight(struct channel *channel,
struct amount_sat funding_sat,
struct amount_sat our_funds,
struct wally_psbt *funding_psbt STEALS,
struct bitcoin_tx *last_tx STEALS,
const struct bitcoin_signature last_sig,
const u32 lease_expiry,
const secp256k1_ecdsa_signature *lease_commit_sig,
const u32 lease_chan_max_msat,
@ -401,6 +399,11 @@ struct channel_inflight *new_inflight(struct channel *channel,
s64 splice_amnt,
bool i_am_initiator);
/* Add a last_tx and sig to an inflight */
void inflight_set_last_tx(struct channel_inflight *inflight,
struct bitcoin_tx *last_tx STEALS,
const struct bitcoin_signature last_sig);
/* Given a txid, find an inflight channel stub. Returns NULL if none found */
struct channel_inflight *channel_inflight_find(struct channel *channel,
const struct bitcoin_txid *txid);

View File

@ -650,7 +650,6 @@ static void handle_add_inflight(struct lightningd *ld,
s64 splice_amnt;
struct wally_psbt *psbt;
struct channel_inflight *inflight;
struct bitcoin_signature last_sig;
bool i_am_initiator;
if (!fromwire_channeld_add_inflight(tmpctx,
@ -668,17 +667,12 @@ static void handle_add_inflight(struct lightningd *ld,
return;
}
/* FIXME: DTODO: Use a pointer to a sig instead of zero'ing one out. */
memset(&last_sig, 0, sizeof(last_sig));
inflight = new_inflight(channel,
&outpoint,
feerate,
satoshis,
channel->our_funds,
psbt,
NULL,
last_sig,
channel->lease_expiry,
channel->lease_commit_sig,
channel->lease_chan_max_msat,

View File

@ -1223,8 +1223,6 @@ void channel_update_reserve(struct channel *channel,
static struct channel_inflight *
wallet_update_channel(struct lightningd *ld,
struct channel *channel,
struct bitcoin_tx *remote_commit STEALS,
struct bitcoin_signature *remote_commit_sig,
const struct bitcoin_outpoint *funding,
struct amount_sat total_funding,
struct amount_sat our_funding,
@ -1275,10 +1273,6 @@ wallet_update_channel(struct lightningd *ld,
channel->opener,
&lease_blockheight_start);
channel_set_last_tx(channel,
tal_steal(channel, remote_commit),
remote_commit_sig);
/* Update in database */
wallet_channel_save(ld->wallet, channel);
@ -1289,8 +1283,6 @@ wallet_update_channel(struct lightningd *ld,
channel->funding_sats,
channel->our_funds,
psbt,
channel->last_tx,
channel->last_sig,
channel->lease_expiry,
channel->lease_commit_sig,
channel->lease_chan_max_msat,
@ -1309,8 +1301,6 @@ wallet_update_channel(struct lightningd *ld,
static struct channel_inflight *
wallet_commit_channel(struct lightningd *ld,
struct channel *channel,
struct bitcoin_tx *remote_commit,
struct bitcoin_signature *remote_commit_sig,
const struct bitcoin_outpoint *funding,
struct amount_sat total_funding,
struct amount_sat our_funding,
@ -1357,25 +1347,9 @@ wallet_commit_channel(struct lightningd *ld,
/* Promote the unsaved_dbid to the dbid */
assert(channel->unsaved_dbid != 0);
assert(channel->state == DUALOPEND_OPEN_INIT);
channel->dbid = channel->unsaved_dbid;
channel->unsaved_dbid = 0;
/* We can't call channel_set_state here: channel isn't in db, so
* really this is a "channel creation" event. */
assert(channel->state == DUALOPEND_OPEN_INIT);
log_info(channel->log, "State changed from %s to %s",
channel_state_name(channel),
channel_state_str(DUALOPEND_OPEN_COMMITTED));
channel->state = DUALOPEND_OPEN_COMMITTED;
notify_channel_state_changed(channel->peer->ld,
&channel->peer->id,
&channel->cid,
channel->scid,
time_now(),
DUALOPEND_OPEN_INIT,
DUALOPEND_OPEN_COMMITTED,
REASON_REMOTE,
"Commitment transaction committed");
channel->funding = *funding;
channel->funding_sats = total_funding;
channel->our_funds = our_funding;
@ -1386,9 +1360,7 @@ wallet_commit_channel(struct lightningd *ld,
channel->req_confirmed_ins[LOCAL] =
ld->config.require_confirmed_inputs;
channel->last_tx = tal_steal(channel, remote_commit);
channel->last_sig = *remote_commit_sig;
channel->last_tx = NULL;
channel->channel_info = *channel_info;
channel->fee_states = new_fee_states(channel,
channel->opener,
@ -1459,8 +1431,6 @@ wallet_commit_channel(struct lightningd *ld,
channel->funding_sats,
channel->our_funds,
psbt,
channel->last_tx,
channel->last_sig,
channel->lease_expiry,
channel->lease_commit_sig,
channel->lease_chan_max_msat,
@ -3298,10 +3268,9 @@ static void handle_commit_received(struct subd *dualopend,
&channel_info.their_config,
total_funding);
/* First time (not an RBF) */
if (channel->state == DUALOPEND_OPEN_INIT) {
if (!(inflight = wallet_commit_channel(ld, channel,
remote_commit,
&remote_commit_sig,
&funding,
total_funding,
funding_ours,
@ -3332,18 +3301,11 @@ static void handle_commit_received(struct subd *dualopend,
return;
}
/* FIXME: handle RBF pbases */
if (pbase)
wallet_penalty_base_add(ld->wallet,
channel->dbid,
pbase);
} else {
/* We're doing an RBF */
assert(channel->state == DUALOPEND_AWAITING_LOCKIN);
if (!(inflight = wallet_update_channel(ld, channel,
remote_commit,
&remote_commit_sig,
&funding,
total_funding,
funding_ours,
@ -3384,10 +3346,11 @@ static void handle_commit_received(struct subd *dualopend,
type_to_string(tmpctx,
struct channel_id,
&channel->cid));
json_add_psbt(response, "psbt", psbt);
json_add_psbt(response, "psbt", inflight->funding_psbt);
json_add_bool(response, "commitments_secured", true);
/* For convenience sake, we include the funding outnum */
json_add_num(response, "funding_outnum", funding.n);
assert(inflight->funding);
json_add_num(response, "funding_outnum", inflight->funding->outpoint.n);
if (oa->our_upfront_shutdown_script) {
json_add_hex_talarr(response, "close_to",
oa->our_upfront_shutdown_script);

View File

@ -188,8 +188,6 @@ struct channel_inflight *new_inflight(struct channel *channel UNNEEDED,
struct amount_sat funding_sat UNNEEDED,
struct amount_sat our_funds UNNEEDED,
struct wally_psbt *funding_psbt STEALS UNNEEDED,
struct bitcoin_tx *last_tx STEALS UNNEEDED,
const struct bitcoin_signature last_sig UNNEEDED,
const u32 lease_expiry UNNEEDED,
const secp256k1_ecdsa_signature *lease_commit_sig UNNEEDED,
const u32 lease_chan_max_msat UNNEEDED,

View File

@ -1919,14 +1919,14 @@ static bool test_channel_inflight_crud(struct lightningd *ld, const tal_t *ctx)
funding_sats,
our_sats,
funding_psbt,
last_tx,
sig,
1, lease_commit_sig, 2, 4, 22,
AMOUNT_MSAT(10),
AMOUNT_SAT(1111),
0,
false);
inflight_set_last_tx(inflight, last_tx, sig);
/* do inflights get correctly added to the channel? */
wallet_inflight_add(w, inflight);
@ -1945,13 +1945,12 @@ static bool test_channel_inflight_crud(struct lightningd *ld, const tal_t *ctx)
funding_sats,
our_sats,
funding_psbt,
last_tx,
sig,
0, NULL, 0, 0, 0,
AMOUNT_MSAT(0),
AMOUNT_SAT(0),
0,
false);
inflight_set_last_tx(inflight, last_tx, sig);
wallet_inflight_add(w, inflight);
CHECK_MSG(c2 = wallet_channel_load(w, chan->dbid),
tal_fmt(w, "Load from DB"));

View File

@ -1379,8 +1379,6 @@ wallet_stmt2inflight(struct wallet *w, struct db_stmt *stmt,
funding_sat,
our_funding_sat,
db_col_psbt(tmpctx, stmt, "funding_psbt"),
last_tx,
last_sig,
db_col_int(stmt, "lease_expiry"),
lease_commit_sig,
lease_chan_max_msat,