From 893335244dde8b0d8ee61755287d45ba670375df Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 18 Aug 2017 14:13:53 +0930 Subject: [PATCH] openingd: supply initial tx as well as signature. And store in peer->last_tx/peer->last_sig like all other places, that way we broadcast it if we need to. Note: the removal of tmpctx in funder_channel() is needed because we use txs[0], which was allocated off tmpctx. Signed-off-by: Rusty Russell --- lightningd/opening/opening.c | 17 ++++++++--------- lightningd/opening/opening_wire.csv | 3 +++ lightningd/peer_control.c | 21 ++++++++++++++++++--- lightningd/peer_htlcs.c | 1 - lightningd/peer_htlcs.h | 1 - wallet/db.c | 1 - wallet/wallet.c | 8 ++------ wallet/wallet_tests.c | 2 -- 8 files changed, 31 insertions(+), 23 deletions(-) diff --git a/lightningd/opening/opening.c b/lightningd/opening/opening.c index 7c806e58c..67a4e319c 100644 --- a/lightningd/opening/opening.c +++ b/lightningd/opening/opening.c @@ -209,7 +209,6 @@ static u8 *funder_channel(struct state *state, const struct utxo *utxos, const struct ext_key *bip32_base) { - const tal_t *tmpctx = tal_tmpctx(state); struct channel_id channel_id, id_in; u8 *msg; struct bitcoin_tx **txs; @@ -243,7 +242,7 @@ static u8 *funder_channel(struct state *state, "push-msat must be < %"PRIu64, 1000 * state->funding_satoshis); - msg = towire_open_channel(tmpctx, + msg = towire_open_channel(state, &state->chainparams->genesis_blockhash.sha, &channel_id, state->funding_satoshis, state->push_msat, @@ -266,7 +265,7 @@ static u8 *funder_channel(struct state *state, state->remoteconf = tal(state, struct channel_config); - msg = read_next_peer_msg(state, tmpctx); + msg = read_next_peer_msg(state, state); if (!msg) peer_failed(PEER_FD, &state->cs, NULL, WIRE_OPENING_PEER_READ_FAILED, "Reading accept_channel"); @@ -361,7 +360,7 @@ static u8 *funder_channel(struct state *state, * for the initial commitment transactions. After receiving the * peer's signature, it will broadcast the funding transaction. */ - txs = channel_txs(tmpctx, NULL, &wscripts, state->channel, + txs = channel_txs(state, NULL, &wscripts, state->channel, &state->next_per_commit[REMOTE], 0, REMOTE); sign_tx_input(txs[0], 0, NULL, wscripts[0], @@ -372,7 +371,7 @@ static u8 *funder_channel(struct state *state, type_to_string(trc, struct bitcoin_tx, txs[0]), type_to_string(trc, struct pubkey, our_funding_pubkey)); - msg = towire_funding_created(tmpctx, &channel_id, + msg = towire_funding_created(state, &channel_id, &state->funding_txid.sha, state->funding_txout, &sig); @@ -388,7 +387,7 @@ static u8 *funder_channel(struct state *state, * commitment transaction, so they can broadcast it knowing they can * redeem their funds if they need to. */ - msg = read_next_peer_msg(state, tmpctx); + msg = read_next_peer_msg(state, state); if (!msg) peer_failed(PEER_FD, &state->cs, NULL, WIRE_OPENING_PEER_READ_FAILED, "Reading funding_signed"); @@ -419,7 +418,7 @@ static u8 *funder_channel(struct state *state, * * The recipient MUST fail the channel if `signature` is incorrect. */ - txs = channel_txs(tmpctx, NULL, &wscripts, state->channel, + txs = channel_txs(state, NULL, &wscripts, state->channel, &state->next_per_commit[LOCAL], 0, LOCAL); if (!check_tx_sig(txs[0], 0, NULL, wscripts[0], &their_funding_pubkey, @@ -433,8 +432,6 @@ static u8 *funder_channel(struct state *state, &their_funding_pubkey)); } - tal_free(tmpctx); - /* BOLT #2: * * Once the channel funder receives the `funding_signed` message, they @@ -442,6 +439,7 @@ static u8 *funder_channel(struct state *state, */ return towire_opening_funder_reply(state, state->remoteconf, + txs[0], &sig, &state->cs, &theirs.revocation, @@ -659,6 +657,7 @@ static u8 *fundee_channel(struct state *state, return towire_opening_fundee_reply(state, state->remoteconf, + txs[0], &theirsig, &state->cs, &theirs.revocation, diff --git a/lightningd/opening/opening_wire.csv b/lightningd/opening/opening_wire.csv index 5402a7179..e907cd063 100644 --- a/lightningd/opening/opening_wire.csv +++ b/lightningd/opening/opening_wire.csv @@ -26,6 +26,7 @@ opening_init,,crypto_state,struct crypto_state opening_init,,seed,struct privkey #include +#include # This means we offer the open. opening_funder,1 opening_funder,,funding_satoshis,8 @@ -43,6 +44,7 @@ opening_funder,,bip32,struct ext_key # This gives their sig, means we can broadcast tx: we're done. opening_funder_reply,101 opening_funder_reply,,their_config,struct channel_config +opening_funder_reply,,first_commit,struct bitcoin_tx opening_funder_reply,,first_commit_sig,secp256k1_ecdsa_signature opening_funder_reply,,crypto_state,struct crypto_state opening_funder_reply,,revocation_basepoint,33 @@ -65,6 +67,7 @@ opening_fundee,,msg,len*u8 # This gives their txid and info, means we can send funding_signed: we're done. opening_fundee_reply,103 opening_fundee_reply,,their_config,struct channel_config +opening_fundee_reply,,first_commit,struct bitcoin_tx opening_fundee_reply,,first_commit_sig,secp256k1_ecdsa_signature opening_fundee_reply,,crypto_state,struct crypto_state opening_fundee_reply,,revocation_basepoint,33 diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 6c40d477b..5495a5ff6 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -1633,7 +1633,7 @@ static bool peer_start_channeld(struct peer *peer, &peer->our_config, &peer->channel_info->their_config, peer->channel_info->feerate_per_kw, - &peer->channel_info->commit_sig, + peer->last_sig, cs, &peer->channel_info->remote_fundingkey, &peer->channel_info->theirbase.revocation, @@ -1694,16 +1694,20 @@ static bool opening_funder_finished(struct subd *opening, const u8 *resp, struct pubkey changekey; struct pubkey local_fundingkey; struct crypto_state cs; + secp256k1_ecdsa_signature remote_commit_sig; + struct bitcoin_tx *remote_commit; assert(tal_count(fds) == 2); /* At this point, we care about peer */ fc->peer->channel_info = channel_info = tal(fc->peer, struct channel_info); + remote_commit = tal(resp, struct bitcoin_tx); if (!fromwire_opening_funder_reply(resp, NULL, &channel_info->their_config, - &channel_info->commit_sig, + remote_commit, + &remote_commit_sig, &cs, &channel_info->theirbase.revocation, &channel_info->theirbase.payment, @@ -1721,6 +1725,9 @@ static bool opening_funder_finished(struct subd *opening, const u8 *resp, /* old_remote_per_commit not valid yet, copy valid one. */ channel_info->old_remote_per_commit = channel_info->remote_per_commit; + /* Now, keep the initial commit as our last-tx-to-broadast. */ + peer_last_tx(fc->peer, remote_commit, &remote_commit_sig); + /* Generate the funding tx. */ if (fc->change && !bip32_pubkey(fc->peer->ld->bip32_base, @@ -1789,16 +1796,21 @@ static bool opening_fundee_finished(struct subd *opening, u8 *funding_signed; struct channel_info *channel_info; struct crypto_state cs; + secp256k1_ecdsa_signature remote_commit_sig; + struct bitcoin_tx *remote_commit; log_debug(peer->log, "Got opening_fundee_finish_response"); assert(tal_count(fds) == 2); + remote_commit = tal(reply, struct bitcoin_tx); + /* At this point, we care about peer */ peer->channel_info = channel_info = tal(peer, struct channel_info); peer->funding_txid = tal(peer, struct sha256_double); if (!fromwire_opening_fundee_reply(peer, reply, NULL, &channel_info->their_config, - &channel_info->commit_sig, + remote_commit, + &remote_commit_sig, &cs, &channel_info->theirbase.revocation, &channel_info->theirbase.payment, @@ -1819,6 +1831,9 @@ static bool opening_fundee_finished(struct subd *opening, /* old_remote_per_commit not valid yet, copy valid one. */ channel_info->old_remote_per_commit = channel_info->remote_per_commit; + /* Now, keep the initial commit as our last-tx-to-broadast. */ + peer_last_tx(peer, remote_commit, &remote_commit_sig); + if (!peer_commit_initial(peer)) return false; diff --git a/lightningd/peer_htlcs.c b/lightningd/peer_htlcs.c index e5ec28ec7..ee411c0e2 100644 --- a/lightningd/peer_htlcs.c +++ b/lightningd/peer_htlcs.c @@ -1058,7 +1058,6 @@ int peer_got_commitsig(struct peer *peer, const u8 *msg) if (!peer_sending_revocation(peer, added, fulfilled, failed, changed)) return -1; - peer->channel_info->commit_sig = commit_sig; if (!peer_save_commitsig_received(peer, commitnum)) return -1; diff --git a/lightningd/peer_htlcs.h b/lightningd/peer_htlcs.h index 042c4c367..d39f1f585 100644 --- a/lightningd/peer_htlcs.h +++ b/lightningd/peer_htlcs.h @@ -8,7 +8,6 @@ /* FIXME: Define serialization primitive for this? */ struct channel_info { - secp256k1_ecdsa_signature commit_sig; struct channel_config their_config; struct pubkey remote_fundingkey; struct basepoints theirbase; diff --git a/wallet/db.c b/wallet/db.c index e42cde961..fc5a59fb6 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -58,7 +58,6 @@ char *dbmigrations[] = { " push_msatoshi INTEGER," " msatoshi_local INTEGER," /* our_msatoshi */ /* START channel_info */ - " commit_sig_remote BLOB," " fundingkey_remote BLOB," " revocation_basepoint_remote BLOB," " payment_basepoint_remote BLOB," diff --git a/wallet/wallet.c b/wallet/wallet.c index e06597dac..54252d520 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -474,7 +474,6 @@ static bool wallet_stmt2channel(struct wallet *w, sqlite3_stmt *stmt, channel_info = chan->peer->channel_info; /* Populate channel_info */ - ok &= sqlite3_column_sig(stmt, col++, &chan->peer->channel_info->commit_sig); ok &= sqlite3_column_pubkey(stmt, col++, &chan->peer->channel_info->remote_fundingkey); ok &= sqlite3_column_pubkey(stmt, col++, &channel_info->theirbase.revocation); ok &= sqlite3_column_pubkey(stmt, col++, &channel_info->theirbase.payment); @@ -485,7 +484,7 @@ static bool wallet_stmt2channel(struct wallet *w, sqlite3_stmt *stmt, wallet_channel_config_load(w, remote_config_id, &chan->peer->channel_info->their_config); } else { /* No channel_info, skip positions in the result */ - col += 8; + col += 7; } /* Load shachain */ @@ -525,7 +524,7 @@ static bool wallet_stmt2channel(struct wallet *w, sqlite3_stmt *stmt, col += 2; } - assert(col == 34); + assert(col == 33); return ok; } @@ -543,7 +542,6 @@ bool wallet_channel_load(struct wallet *w, const u64 id, "next_index_local, next_index_remote, num_revocations_received, " "next_htlc_id, funding_tx_id, funding_tx_outnum, funding_satoshi, " "funding_locked_remote, push_msatoshi, msatoshi_local, " - "commit_sig_remote, " "fundingkey_remote, revocation_basepoint_remote, " "payment_basepoint_remote, " "delayed_payment_basepoint_remote, per_commit_remote, " @@ -722,7 +720,6 @@ bool wallet_channel_save(struct wallet *w, struct wallet_channel *chan){ ok &= wallet_channel_config_save(w, &p->channel_info->their_config); ok &= db_exec(__func__, w->db, "UPDATE channels SET" - " commit_sig_remote=%s," " fundingkey_remote='%s'," " revocation_basepoint_remote='%s'," " payment_basepoint_remote='%s'," @@ -732,7 +729,6 @@ bool wallet_channel_save(struct wallet *w, struct wallet_channel *chan){ " feerate_per_kw=%d," " channel_config_remote=%"PRIu64 " WHERE id=%"PRIu64, - db_serialize_signature(tmpctx, &p->channel_info->commit_sig), db_serialize_pubkey(tmpctx, &p->channel_info->remote_fundingkey), db_serialize_pubkey(tmpctx, &p->channel_info->theirbase.revocation), db_serialize_pubkey(tmpctx, &p->channel_info->theirbase.payment), diff --git a/wallet/wallet_tests.c b/wallet/wallet_tests.c index 137944e04..9c29623b4 100644 --- a/wallet/wallet_tests.c +++ b/wallet/wallet_tests.c @@ -148,8 +148,6 @@ static bool channelseq(struct wallet_channel *c1, struct wallet_channel *c2) sizeof(struct sha256_double))); CHECK((ci1 != NULL) == (ci2 != NULL)); if(ci1) { - CHECK(memeq(&ci1->commit_sig, sizeof(secp256k1_ecdsa_signature), - &ci2->commit_sig, sizeof(secp256k1_ecdsa_signature))); CHECK(pubkey_eq(&ci1->remote_fundingkey, &ci2->remote_fundingkey)); CHECK(pubkey_eq(&ci1->theirbase.revocation, &ci2->theirbase.revocation)); CHECK(pubkey_eq(&ci1->theirbase.payment, &ci2->theirbase.payment));