From 71b606e0508537cfb3b6556c40af83b4152b0bc7 Mon Sep 17 00:00:00 2001 From: trueptolemy Date: Sun, 29 Sep 2019 15:35:45 +0800 Subject: [PATCH] lightningd: Add a new field `shutdown_scriptpubkey[NUM_SIDES]` `shutdown_scriptpubkey[REMOTE]` is original remote_shutdown_scriptpubkey; `shutdown_scriptpubkey[LOCAL]` is the script used for "to-local" output when `close`. Add the default is generated form `final_key_idx`; Store `shutdown_scriptpubkey[LOCAL]` into wallet; --- lightningd/channel.c | 10 +++++++++- lightningd/channel.h | 3 ++- lightningd/channel_control.c | 9 ++++----- lightningd/closing_control.c | 4 ++-- lightningd/onchain_control.c | 2 +- lightningd/opening_control.c | 3 ++- wallet/db.c | 2 ++ wallet/test/run-wallet.c | 22 +++++++++++----------- wallet/wallet.c | 17 ++++++++++++----- 9 files changed, 45 insertions(+), 27 deletions(-) diff --git a/lightningd/channel.c b/lightningd/channel.c index 86678dd75..a160b5a6d 100644 --- a/lightningd/channel.c +++ b/lightningd/channel.c @@ -170,6 +170,7 @@ struct channel *new_channel(struct peer *peer, u64 dbid, const struct channel_info *channel_info, /* NULL or stolen */ u8 *remote_shutdown_scriptpubkey, + u8 *local_shutdown_scriptpubkey, u64 final_key_idx, bool last_was_revoke, /* NULL or stolen */ @@ -238,9 +239,16 @@ struct channel *new_channel(struct peer *peer, u64 dbid, channel->last_sig = *last_sig; channel->last_htlc_sigs = tal_steal(channel, last_htlc_sigs); channel->channel_info = *channel_info; - channel->remote_shutdown_scriptpubkey + channel->shutdown_scriptpubkey[REMOTE] = tal_steal(channel, remote_shutdown_scriptpubkey); channel->final_key_idx = final_key_idx; + if (local_shutdown_scriptpubkey) + channel->shutdown_scriptpubkey[LOCAL] + = tal_steal(channel, local_shutdown_scriptpubkey); + else + channel->shutdown_scriptpubkey[LOCAL] + = p2wpkh_for_keyidx(channel, channel->peer->ld, + channel->final_key_idx); channel->last_was_revoke = last_was_revoke; channel->last_sent_commit = tal_steal(channel, last_sent_commit); channel->first_blocknum = first_blocknum; diff --git a/lightningd/channel.h b/lightningd/channel.h index 7ac530d7c..b2cd42b47 100644 --- a/lightningd/channel.h +++ b/lightningd/channel.h @@ -90,7 +90,7 @@ struct channel { struct pubkey local_funding_pubkey; /* Their scriptpubkey if they sent shutdown. */ - u8 *remote_shutdown_scriptpubkey; + u8 *shutdown_scriptpubkey[NUM_SIDES]; /* Address for any final outputs */ u64 final_key_idx; @@ -157,6 +157,7 @@ struct channel *new_channel(struct peer *peer, u64 dbid, const struct channel_info *channel_info, /* NULL or stolen */ u8 *remote_shutdown_scriptpubkey, + u8 *local_shutdown_scriptpubkey, u64 final_key_idx, bool last_was_revoke, /* NULL or stolen */ diff --git a/lightningd/channel_control.c b/lightningd/channel_control.c index f3f092e57..149ac2f9a 100644 --- a/lightningd/channel_control.c +++ b/lightningd/channel_control.c @@ -154,8 +154,8 @@ static void peer_got_shutdown(struct channel *channel, const u8 *msg) } /* FIXME: Add to spec that we must allow repeated shutdown! */ - tal_free(channel->remote_shutdown_scriptpubkey); - channel->remote_shutdown_scriptpubkey = scriptpubkey; + tal_free(channel->shutdown_scriptpubkey[REMOTE]); + channel->shutdown_scriptpubkey[REMOTE] = scriptpubkey; /* BOLT #2: * @@ -464,9 +464,8 @@ void peer_start_channeld(struct channel *channel, &scid, reconnected, channel->state == CHANNELD_SHUTTING_DOWN, - channel->remote_shutdown_scriptpubkey != NULL, - p2wpkh_for_keyidx(tmpctx, ld, - channel->final_key_idx), + channel->shutdown_scriptpubkey[REMOTE] != NULL, + channel->shutdown_scriptpubkey[LOCAL], channel->channel_flags, funding_signed, reached_announce_depth, diff --git a/lightningd/closing_control.c b/lightningd/closing_control.c index 419d02b6c..dd3d06099 100644 --- a/lightningd/closing_control.c +++ b/lightningd/closing_control.c @@ -182,7 +182,7 @@ void peer_start_closingd(struct channel *channel, struct secret last_remote_per_commit_secret; struct lightningd *ld = channel->peer->ld; - if (!channel->remote_shutdown_scriptpubkey) { + if (!channel->shutdown_scriptpubkey[REMOTE]) { channel_internal_error(channel, "Can't start closing: no remote info"); return; @@ -293,7 +293,7 @@ void peer_start_closingd(struct channel *channel, minfee, feelimit, startfee, p2wpkh_for_keyidx(tmpctx, ld, channel->final_key_idx), - channel->remote_shutdown_scriptpubkey, + channel->shutdown_scriptpubkey[REMOTE], reconnected, channel->next_index[LOCAL], channel->next_index[REMOTE], diff --git a/lightningd/onchain_control.c b/lightningd/onchain_control.c index 00776fc4d..c65e741ef 100644 --- a/lightningd/onchain_control.c +++ b/lightningd/onchain_control.c @@ -552,7 +552,7 @@ enum watch_result onchaind_funding_spent(struct channel *channel, &our_last_txid, p2wpkh_for_keyidx(tmpctx, ld, channel->final_key_idx), - channel->remote_shutdown_scriptpubkey, + channel->shutdown_scriptpubkey[REMOTE], &final_key, channel->funder, &channel->local_basepoints, diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index ad83ebb71..0061611c3 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -247,7 +247,8 @@ wallet_commit_channel(struct lightningd *ld, remote_commit_sig, NULL, /* No HTLC sigs yet */ channel_info, - NULL, /* No remote_shutdown_scriptpubkey yet */ + NULL, /* No shutdown_scriptpubkey[REMOTE] yet */ + NULL, /* No shutdown_scriptpubkey[LOCAL] yet. Generate the default one. */ final_key_idx, false, NULL, /* No commit sent yet */ /* If we're fundee, could be a little before this diff --git a/wallet/db.c b/wallet/db.c index 55cb55610..7ae27e3c3 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -475,6 +475,8 @@ static struct migration dbmigrations[] = { ", channel BIGINT REFERENCES channels(id)" ", UNIQUE(txid, idx)" ");"), NULL}, + {SQL("ALTER TABLE channels ADD shutdown_scriptpubkey_local BLOB;"), + NULL}, }; /* Leak tracking. */ diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 8dfb510b9..00026278f 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -925,11 +925,11 @@ static bool channelseq(struct channel *c1, struct channel *c2) CHECK((c1->scid == NULL && c2->scid == NULL) || short_channel_id_eq(c1->scid, c2->scid)); CHECK(amount_msat_eq(c1->our_msat, c2->our_msat)); - CHECK((c1->remote_shutdown_scriptpubkey == NULL && c2->remote_shutdown_scriptpubkey == NULL) || memeq( - c1->remote_shutdown_scriptpubkey, - tal_count(c1->remote_shutdown_scriptpubkey), - c2->remote_shutdown_scriptpubkey, - tal_count(c2->remote_shutdown_scriptpubkey))); + CHECK((c1->shutdown_scriptpubkey[REMOTE] == NULL && c2->shutdown_scriptpubkey[REMOTE] == NULL) || memeq( + c1->shutdown_scriptpubkey[REMOTE], + tal_count(c1->shutdown_scriptpubkey[REMOTE]), + c2->shutdown_scriptpubkey[REMOTE], + tal_count(c2->shutdown_scriptpubkey[REMOTE]))); CHECK(memeq( &c1->funding_txid, sizeof(struct sha256_double), @@ -959,10 +959,10 @@ static bool channelseq(struct channel *c1, struct channel *c2) &c2->last_sig, sizeof(c2->last_sig))); CHECK(c1->final_key_idx == c2->final_key_idx); - CHECK(memeq(c1->remote_shutdown_scriptpubkey, - tal_count(c1->remote_shutdown_scriptpubkey), - c2->remote_shutdown_scriptpubkey, - tal_count(c2->remote_shutdown_scriptpubkey))); + CHECK(memeq(c1->shutdown_scriptpubkey[REMOTE], + tal_count(c1->shutdown_scriptpubkey[REMOTE]), + c2->shutdown_scriptpubkey[REMOTE], + tal_count(c2->shutdown_scriptpubkey[REMOTE]))); CHECK(c1->last_was_revoke == c2->last_was_revoke); @@ -1091,8 +1091,8 @@ static bool test_channel_crud(struct lightningd *ld, const tal_t *ctx) CHECK(c1.peer->dbid == 1); CHECK(c1.their_shachain.id == 1); - /* Variant 4: update and add remote_shutdown_scriptpubkey */ - c1.remote_shutdown_scriptpubkey = scriptpubkey; + /* Variant 4: update and add shutdown_scriptpubkey[REMOTE] */ + c1.shutdown_scriptpubkey[REMOTE] = scriptpubkey; wallet_channel_save(w, &c1); CHECK_MSG(!wallet_err, tal_fmt(w, "Insert into DB: %s", wallet_err)); CHECK_MSG(c2 = wallet_channel_load(w, c1.dbid), tal_fmt(w, "Load from DB")); diff --git a/wallet/wallet.c b/wallet/wallet.c index fdcb554fe..534a75c23 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -822,6 +822,7 @@ static struct channel *wallet_stmt2channel(struct wallet *w, struct db_stmt *stm struct bitcoin_txid funding_txid; struct bitcoin_signature last_sig; u8 *remote_shutdown_scriptpubkey; + u8 *local_shutdown_scriptpubkey; struct changed_htlc *last_sent_commit; s64 final_key_idx, channel_config_id; struct basepoints local_basepoints; @@ -850,6 +851,7 @@ static struct channel *wallet_stmt2channel(struct wallet *w, struct db_stmt *stm ok &= wallet_shachain_load(w, db_column_u64(stmt, 27), &wshachain); remote_shutdown_scriptpubkey = db_column_arr(tmpctx, stmt, 28, u8); + local_shutdown_scriptpubkey = db_column_arr(tmpctx, stmt, 46, u8); /* Do we have a last_sent_commit, if yes, populate */ if (!db_column_is_null(stmt, 41)) { @@ -945,6 +947,7 @@ static struct channel *wallet_stmt2channel(struct wallet *w, struct db_stmt *stm db_column_u64(stmt, 0)), &channel_info, remote_shutdown_scriptpubkey, + local_shutdown_scriptpubkey, final_key_idx, db_column_int(stmt, 34) != 0, last_sent_commit, @@ -1030,6 +1033,7 @@ static bool wallet_channels_load_active(struct wallet *w) ", feerate_ppm" ", remote_upfront_shutdown_script" ", option_static_remotekey" + ", shutdown_scriptpubkey_local" " FROM channels WHERE state < ?;")); db_bind_int(stmt, 0, CLOSED); db_query_prepared(stmt); @@ -1292,7 +1296,8 @@ void wallet_channel_save(struct wallet *w, struct channel *chan) " feerate_base=?," " feerate_ppm=?," " remote_upfront_shutdown_script=?," - " option_static_remotekey=?" + " option_static_remotekey=?," + " shutdown_scriptpubkey_local=?" " WHERE id=?")); db_bind_u64(stmt, 0, chan->their_shachain.id); if (chan->scid) @@ -1316,9 +1321,9 @@ void wallet_channel_save(struct wallet *w, struct channel *chan) db_bind_amount_msat(stmt, 13, &chan->push); db_bind_amount_msat(stmt, 14, &chan->our_msat); - if (chan->remote_shutdown_scriptpubkey) - db_bind_blob(stmt, 15, chan->remote_shutdown_scriptpubkey, - tal_count(chan->remote_shutdown_scriptpubkey)); + if (chan->shutdown_scriptpubkey[REMOTE]) + db_bind_blob(stmt, 15, chan->shutdown_scriptpubkey[REMOTE], + tal_count(chan->shutdown_scriptpubkey[REMOTE])); else db_bind_null(stmt, 15); @@ -1340,7 +1345,9 @@ void wallet_channel_save(struct wallet *w, struct channel *chan) else db_bind_null(stmt, 27); db_bind_int(stmt, 28, chan->option_static_remotekey); - db_bind_u64(stmt, 29, chan->dbid); + db_bind_blob(stmt, 29, chan->shutdown_scriptpubkey[LOCAL], + tal_count(chan->shutdown_scriptpubkey[LOCAL])); + db_bind_u64(stmt, 30, chan->dbid); db_exec_prepared_v2(take(stmt)); wallet_channel_config_save(w, &chan->channel_info.their_config);