diff --git a/lightningd/channel.c b/lightningd/channel.c index e1f059bc4..035af0895 100644 --- a/lightningd/channel.c +++ b/lightningd/channel.c @@ -212,6 +212,7 @@ struct channel *new_unsaved_channel(struct peer *peer, channel->openchannel_signed_cmd = NULL; channel->state = DUALOPEND_OPEN_INIT; channel->owner = NULL; + channel->scb = NULL; memset(&channel->billboard, 0, sizeof(channel->billboard)); channel->billboard.transient = tal_fmt(channel, "%s", "Empty channel init'd"); @@ -419,6 +420,14 @@ struct channel *new_channel(struct peer *peer, u64 dbid, channel->owner = NULL; memset(&channel->billboard, 0, sizeof(channel->billboard)); channel->billboard.transient = tal_strdup(channel, transient_billboard); + channel->scb = tal(channel, struct scb_chan); + channel->scb->id = dbid; + channel->scb->addr = peer->addr; + channel->scb->node_id = peer->id; + channel->scb->funding = *funding; + channel->scb->cid = *cid; + channel->scb->funding_sats = funding_sats; + channel->scb->type = channel_type_dup(channel->scb, type); if (!log) { channel->log = new_log(channel, @@ -447,9 +456,11 @@ struct channel *new_channel(struct peer *peer, u64 dbid, channel->our_msat = our_msat; channel->msat_to_us_min = msat_to_us_min; channel->msat_to_us_max = msat_to_us_max; - channel->last_tx = tal_steal(channel, last_tx); - channel->last_tx->chainparams = chainparams; - channel->last_tx_type = TX_UNKNOWN; + channel->last_tx = tal_steal(channel, last_tx); + if (channel->last_tx) { + channel->last_tx->chainparams = chainparams; + channel->last_tx_type = TX_UNKNOWN; + } channel->last_sig = *last_sig; channel->last_htlc_sigs = tal_steal(channel, last_htlc_sigs); channel->channel_info = *channel_info; diff --git a/lightningd/channel.h b/lightningd/channel.h index fbe86a63f..8851bd72f 100644 --- a/lightningd/channel.h +++ b/lightningd/channel.h @@ -3,6 +3,7 @@ #include "config.h" #include #include +#include #include #include #include @@ -260,6 +261,10 @@ struct channel { /* Latest channel_update, for use in error messages. */ u8 *channel_update; + + /* `Channel-shell` of this channel + * (Minimum information required to backup this channel). */ + struct scb_chan *scb; }; /* For v2 opens, a channel that has not yet been committed/saved to disk */ diff --git a/lightningd/dual_open_control.c b/lightningd/dual_open_control.c index d72a688f0..cac1152aa 100644 --- a/lightningd/dual_open_control.c +++ b/lightningd/dual_open_control.c @@ -1228,6 +1228,14 @@ wallet_commit_channel(struct lightningd *ld, &commitment_feerate); channel->min_possible_feerate = commitment_feerate; channel->max_possible_feerate = commitment_feerate; + channel->scb = tal(channel, struct scb_chan); + channel->scb->id = channel->dbid; + channel->scb->addr = channel->peer->addr; + channel->scb->node_id = channel->peer->id; + channel->scb->funding = *funding; + channel->scb->cid = channel->cid; + channel->scb->funding_sats = total_funding; + channel->scb->type = channel_type_dup(channel->scb, channel->type); /* We are connected */ channel->connected = true; diff --git a/wallet/wallet.c b/wallet/wallet.c index efd966d32..33694c9c4 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -1114,6 +1114,7 @@ wallet_stmt2inflight(struct wallet *w, struct db_stmt *stmt, struct amount_msat lease_fee; struct bitcoin_outpoint funding; struct bitcoin_signature last_sig; + struct bitcoin_tx *last_tx; struct channel_inflight *inflight; secp256k1_ecdsa_signature *lease_commit_sig; @@ -1149,12 +1150,19 @@ wallet_stmt2inflight(struct wallet *w, struct db_stmt *stmt, db_col_ignore(stmt, "lease_fee"); } + /* last_tx is null for stub channels used for recovering funds through + * Static channel backups. */ + if (!db_col_is_null(stmt, "last_tx")) + last_tx = db_col_psbt_to_tx(tmpctx, stmt, "last_tx"); + else + last_tx = NULL; + inflight = new_inflight(chan, &funding, db_col_int(stmt, "funding_feerate"), funding_sat, our_funding_sat, db_col_psbt(tmpctx, stmt, "funding_psbt"), - db_col_psbt_to_tx(tmpctx, stmt, "last_tx"), + last_tx, last_sig, db_col_int(stmt, "lease_expiry"), lease_commit_sig, @@ -1260,6 +1268,7 @@ static struct channel *wallet_stmt2channel(struct wallet *w, struct db_stmt *stm struct bitcoin_outpoint funding; struct bitcoin_outpoint *shutdown_wrong_funding; struct bitcoin_signature last_sig; + struct bitcoin_tx *last_tx; u8 *remote_shutdown_scriptpubkey; u8 *local_shutdown_scriptpubkey; struct changed_htlc *last_sent_commit; @@ -1445,6 +1454,13 @@ static struct channel *wallet_stmt2channel(struct wallet *w, struct db_stmt *stm else type = channel_type_none(NULL); + /* last_tx is null for stub channels used for recovering funds through + * Static channel backups. */ + if (!db_col_is_null(stmt, "last_tx")) + last_tx = db_col_psbt_to_tx(tmpctx, stmt, "last_tx"); + else + last_tx = NULL; + chan = new_channel(peer, db_col_u64(stmt, "id"), &wshachain, db_col_int(stmt, "state"), @@ -1469,7 +1485,7 @@ static struct channel *wallet_stmt2channel(struct wallet *w, struct db_stmt *stm our_msat, msat_to_us_min, /* msatoshi_to_us_min */ msat_to_us_max, /* msatoshi_to_us_max */ - db_col_psbt_to_tx(tmpctx, stmt, "last_tx"), + last_tx, &last_sig, wallet_htlc_sigs_load(tmpctx, w, db_col_u64(stmt, "id"),