Browse Source

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;
travis-debug
trueptolemy 5 years ago
committed by neil saitug
parent
commit
71b606e050
  1. 10
      lightningd/channel.c
  2. 3
      lightningd/channel.h
  3. 9
      lightningd/channel_control.c
  4. 4
      lightningd/closing_control.c
  5. 2
      lightningd/onchain_control.c
  6. 3
      lightningd/opening_control.c
  7. 2
      wallet/db.c
  8. 22
      wallet/test/run-wallet.c
  9. 17
      wallet/wallet.c

10
lightningd/channel.c

@ -170,6 +170,7 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
const struct channel_info *channel_info, const struct channel_info *channel_info,
/* NULL or stolen */ /* NULL or stolen */
u8 *remote_shutdown_scriptpubkey, u8 *remote_shutdown_scriptpubkey,
u8 *local_shutdown_scriptpubkey,
u64 final_key_idx, u64 final_key_idx,
bool last_was_revoke, bool last_was_revoke,
/* NULL or stolen */ /* NULL or stolen */
@ -238,9 +239,16 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
channel->last_sig = *last_sig; channel->last_sig = *last_sig;
channel->last_htlc_sigs = tal_steal(channel, last_htlc_sigs); channel->last_htlc_sigs = tal_steal(channel, last_htlc_sigs);
channel->channel_info = *channel_info; channel->channel_info = *channel_info;
channel->remote_shutdown_scriptpubkey channel->shutdown_scriptpubkey[REMOTE]
= tal_steal(channel, remote_shutdown_scriptpubkey); = tal_steal(channel, remote_shutdown_scriptpubkey);
channel->final_key_idx = final_key_idx; 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_was_revoke = last_was_revoke;
channel->last_sent_commit = tal_steal(channel, last_sent_commit); channel->last_sent_commit = tal_steal(channel, last_sent_commit);
channel->first_blocknum = first_blocknum; channel->first_blocknum = first_blocknum;

3
lightningd/channel.h

@ -90,7 +90,7 @@ struct channel {
struct pubkey local_funding_pubkey; struct pubkey local_funding_pubkey;
/* Their scriptpubkey if they sent shutdown. */ /* Their scriptpubkey if they sent shutdown. */
u8 *remote_shutdown_scriptpubkey; u8 *shutdown_scriptpubkey[NUM_SIDES];
/* Address for any final outputs */ /* Address for any final outputs */
u64 final_key_idx; u64 final_key_idx;
@ -157,6 +157,7 @@ struct channel *new_channel(struct peer *peer, u64 dbid,
const struct channel_info *channel_info, const struct channel_info *channel_info,
/* NULL or stolen */ /* NULL or stolen */
u8 *remote_shutdown_scriptpubkey, u8 *remote_shutdown_scriptpubkey,
u8 *local_shutdown_scriptpubkey,
u64 final_key_idx, u64 final_key_idx,
bool last_was_revoke, bool last_was_revoke,
/* NULL or stolen */ /* NULL or stolen */

9
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! */ /* FIXME: Add to spec that we must allow repeated shutdown! */
tal_free(channel->remote_shutdown_scriptpubkey); tal_free(channel->shutdown_scriptpubkey[REMOTE]);
channel->remote_shutdown_scriptpubkey = scriptpubkey; channel->shutdown_scriptpubkey[REMOTE] = scriptpubkey;
/* BOLT #2: /* BOLT #2:
* *
@ -464,9 +464,8 @@ void peer_start_channeld(struct channel *channel,
&scid, &scid,
reconnected, reconnected,
channel->state == CHANNELD_SHUTTING_DOWN, channel->state == CHANNELD_SHUTTING_DOWN,
channel->remote_shutdown_scriptpubkey != NULL, channel->shutdown_scriptpubkey[REMOTE] != NULL,
p2wpkh_for_keyidx(tmpctx, ld, channel->shutdown_scriptpubkey[LOCAL],
channel->final_key_idx),
channel->channel_flags, channel->channel_flags,
funding_signed, funding_signed,
reached_announce_depth, reached_announce_depth,

4
lightningd/closing_control.c

@ -182,7 +182,7 @@ void peer_start_closingd(struct channel *channel,
struct secret last_remote_per_commit_secret; struct secret last_remote_per_commit_secret;
struct lightningd *ld = channel->peer->ld; struct lightningd *ld = channel->peer->ld;
if (!channel->remote_shutdown_scriptpubkey) { if (!channel->shutdown_scriptpubkey[REMOTE]) {
channel_internal_error(channel, channel_internal_error(channel,
"Can't start closing: no remote info"); "Can't start closing: no remote info");
return; return;
@ -293,7 +293,7 @@ void peer_start_closingd(struct channel *channel,
minfee, feelimit, startfee, minfee, feelimit, startfee,
p2wpkh_for_keyidx(tmpctx, ld, p2wpkh_for_keyidx(tmpctx, ld,
channel->final_key_idx), channel->final_key_idx),
channel->remote_shutdown_scriptpubkey, channel->shutdown_scriptpubkey[REMOTE],
reconnected, reconnected,
channel->next_index[LOCAL], channel->next_index[LOCAL],
channel->next_index[REMOTE], channel->next_index[REMOTE],

2
lightningd/onchain_control.c

@ -552,7 +552,7 @@ enum watch_result onchaind_funding_spent(struct channel *channel,
&our_last_txid, &our_last_txid,
p2wpkh_for_keyidx(tmpctx, ld, p2wpkh_for_keyidx(tmpctx, ld,
channel->final_key_idx), channel->final_key_idx),
channel->remote_shutdown_scriptpubkey, channel->shutdown_scriptpubkey[REMOTE],
&final_key, &final_key,
channel->funder, channel->funder,
&channel->local_basepoints, &channel->local_basepoints,

3
lightningd/opening_control.c

@ -247,7 +247,8 @@ wallet_commit_channel(struct lightningd *ld,
remote_commit_sig, remote_commit_sig,
NULL, /* No HTLC sigs yet */ NULL, /* No HTLC sigs yet */
channel_info, 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, final_key_idx, false,
NULL, /* No commit sent yet */ NULL, /* No commit sent yet */
/* If we're fundee, could be a little before this /* If we're fundee, could be a little before this

2
wallet/db.c

@ -475,6 +475,8 @@ static struct migration dbmigrations[] = {
", channel BIGINT REFERENCES channels(id)" ", channel BIGINT REFERENCES channels(id)"
", UNIQUE(txid, idx)" ", UNIQUE(txid, idx)"
");"), NULL}, ");"), NULL},
{SQL("ALTER TABLE channels ADD shutdown_scriptpubkey_local BLOB;"),
NULL},
}; };
/* Leak tracking. */ /* Leak tracking. */

22
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) CHECK((c1->scid == NULL && c2->scid == NULL)
|| short_channel_id_eq(c1->scid, c2->scid)); || short_channel_id_eq(c1->scid, c2->scid));
CHECK(amount_msat_eq(c1->our_msat, c2->our_msat)); CHECK(amount_msat_eq(c1->our_msat, c2->our_msat));
CHECK((c1->remote_shutdown_scriptpubkey == NULL && c2->remote_shutdown_scriptpubkey == NULL) || memeq( CHECK((c1->shutdown_scriptpubkey[REMOTE] == NULL && c2->shutdown_scriptpubkey[REMOTE] == NULL) || memeq(
c1->remote_shutdown_scriptpubkey, c1->shutdown_scriptpubkey[REMOTE],
tal_count(c1->remote_shutdown_scriptpubkey), tal_count(c1->shutdown_scriptpubkey[REMOTE]),
c2->remote_shutdown_scriptpubkey, c2->shutdown_scriptpubkey[REMOTE],
tal_count(c2->remote_shutdown_scriptpubkey))); tal_count(c2->shutdown_scriptpubkey[REMOTE])));
CHECK(memeq( CHECK(memeq(
&c1->funding_txid, &c1->funding_txid,
sizeof(struct sha256_double), sizeof(struct sha256_double),
@ -959,10 +959,10 @@ static bool channelseq(struct channel *c1, struct channel *c2)
&c2->last_sig, sizeof(c2->last_sig))); &c2->last_sig, sizeof(c2->last_sig)));
CHECK(c1->final_key_idx == c2->final_key_idx); CHECK(c1->final_key_idx == c2->final_key_idx);
CHECK(memeq(c1->remote_shutdown_scriptpubkey, CHECK(memeq(c1->shutdown_scriptpubkey[REMOTE],
tal_count(c1->remote_shutdown_scriptpubkey), tal_count(c1->shutdown_scriptpubkey[REMOTE]),
c2->remote_shutdown_scriptpubkey, c2->shutdown_scriptpubkey[REMOTE],
tal_count(c2->remote_shutdown_scriptpubkey))); tal_count(c2->shutdown_scriptpubkey[REMOTE])));
CHECK(c1->last_was_revoke == c2->last_was_revoke); 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.peer->dbid == 1);
CHECK(c1.their_shachain.id == 1); CHECK(c1.their_shachain.id == 1);
/* Variant 4: update and add remote_shutdown_scriptpubkey */ /* Variant 4: update and add shutdown_scriptpubkey[REMOTE] */
c1.remote_shutdown_scriptpubkey = scriptpubkey; c1.shutdown_scriptpubkey[REMOTE] = scriptpubkey;
wallet_channel_save(w, &c1); wallet_channel_save(w, &c1);
CHECK_MSG(!wallet_err, tal_fmt(w, "Insert into DB: %s", wallet_err)); 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")); CHECK_MSG(c2 = wallet_channel_load(w, c1.dbid), tal_fmt(w, "Load from DB"));

17
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_txid funding_txid;
struct bitcoin_signature last_sig; struct bitcoin_signature last_sig;
u8 *remote_shutdown_scriptpubkey; u8 *remote_shutdown_scriptpubkey;
u8 *local_shutdown_scriptpubkey;
struct changed_htlc *last_sent_commit; struct changed_htlc *last_sent_commit;
s64 final_key_idx, channel_config_id; s64 final_key_idx, channel_config_id;
struct basepoints local_basepoints; 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); ok &= wallet_shachain_load(w, db_column_u64(stmt, 27), &wshachain);
remote_shutdown_scriptpubkey = db_column_arr(tmpctx, stmt, 28, u8); 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 */ /* Do we have a last_sent_commit, if yes, populate */
if (!db_column_is_null(stmt, 41)) { 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)), db_column_u64(stmt, 0)),
&channel_info, &channel_info,
remote_shutdown_scriptpubkey, remote_shutdown_scriptpubkey,
local_shutdown_scriptpubkey,
final_key_idx, final_key_idx,
db_column_int(stmt, 34) != 0, db_column_int(stmt, 34) != 0,
last_sent_commit, last_sent_commit,
@ -1030,6 +1033,7 @@ static bool wallet_channels_load_active(struct wallet *w)
", feerate_ppm" ", feerate_ppm"
", remote_upfront_shutdown_script" ", remote_upfront_shutdown_script"
", option_static_remotekey" ", option_static_remotekey"
", shutdown_scriptpubkey_local"
" FROM channels WHERE state < ?;")); " FROM channels WHERE state < ?;"));
db_bind_int(stmt, 0, CLOSED); db_bind_int(stmt, 0, CLOSED);
db_query_prepared(stmt); db_query_prepared(stmt);
@ -1292,7 +1296,8 @@ void wallet_channel_save(struct wallet *w, struct channel *chan)
" feerate_base=?," " feerate_base=?,"
" feerate_ppm=?," " feerate_ppm=?,"
" remote_upfront_shutdown_script=?," " remote_upfront_shutdown_script=?,"
" option_static_remotekey=?" " option_static_remotekey=?,"
" shutdown_scriptpubkey_local=?"
" WHERE id=?")); " WHERE id=?"));
db_bind_u64(stmt, 0, chan->their_shachain.id); db_bind_u64(stmt, 0, chan->their_shachain.id);
if (chan->scid) 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, 13, &chan->push);
db_bind_amount_msat(stmt, 14, &chan->our_msat); db_bind_amount_msat(stmt, 14, &chan->our_msat);
if (chan->remote_shutdown_scriptpubkey) if (chan->shutdown_scriptpubkey[REMOTE])
db_bind_blob(stmt, 15, chan->remote_shutdown_scriptpubkey, db_bind_blob(stmt, 15, chan->shutdown_scriptpubkey[REMOTE],
tal_count(chan->remote_shutdown_scriptpubkey)); tal_count(chan->shutdown_scriptpubkey[REMOTE]));
else else
db_bind_null(stmt, 15); db_bind_null(stmt, 15);
@ -1340,7 +1345,9 @@ void wallet_channel_save(struct wallet *w, struct channel *chan)
else else
db_bind_null(stmt, 27); db_bind_null(stmt, 27);
db_bind_int(stmt, 28, chan->option_static_remotekey); 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)); db_exec_prepared_v2(take(stmt));
wallet_channel_config_save(w, &chan->channel_info.their_config); wallet_channel_config_save(w, &chan->channel_info.their_config);

Loading…
Cancel
Save