diff --git a/channeld/channel.c b/channeld/channel.c index 14d639c5d..8b1d979f9 100644 --- a/channeld/channel.c +++ b/channeld/channel.c @@ -146,9 +146,12 @@ struct peer { /* We save calculated commit sigs while waiting for master approval */ struct commit_sigs *next_commit_sigs; - /* If master told us to shut down, this contains scriptpubkey until - * we're ready to send it. */ - u8 *unsent_shutdown_scriptpubkey; + /* The scriptpubkey to use for shutting down. */ + u8 *final_scriptpubkey; + + /* If master told us to shut down */ + bool send_shutdown; + /* Has shutdown been sent by each side? */ bool shutdown_sent[NUM_SIDES]; /* Information used for reestablishment. */ @@ -769,7 +772,7 @@ static void maybe_send_shutdown(struct peer *peer) { u8 *msg; - if (!peer->unsent_shutdown_scriptpubkey) + if (!peer->send_shutdown) return; /* Send a disable channel_update so others don't try to route @@ -778,11 +781,9 @@ static void maybe_send_shutdown(struct peer *peer) wire_sync_write(GOSSIP_FD, msg); enqueue_peer_msg(peer, take(msg)); - msg = towire_shutdown(peer, &peer->channel_id, - peer->unsent_shutdown_scriptpubkey); + msg = towire_shutdown(peer, &peer->channel_id, peer->final_scriptpubkey); enqueue_peer_msg(peer, take(msg)); - peer->unsent_shutdown_scriptpubkey - = tal_free(peer->unsent_shutdown_scriptpubkey); + peer->send_shutdown = false; peer->shutdown_sent[LOCAL] = true; billboard_update(peer); } @@ -1589,11 +1590,24 @@ static void handle_peer_shutdown(struct peer *peer, const u8 *shutdown) &peer->channel_id, "Bad shutdown %s", tal_hex(peer, shutdown)); - /* Tell master, it will tell us what to send (if any). */ + /* Tell master: we don't have to wait because on reconnect other end + * will re-send anyway. */ wire_sync_write(MASTER_FD, take(towire_channel_got_shutdown(peer, scriptpubkey))); peer->shutdown_sent[REMOTE] = true; + /* BOLT #2: + * + * A receiving node: + * ... + * - once there are no outstanding updates on the peer, UNLESS + * it has already sent a `shutdown`: + * - MUST reply to a `shutdown` message with a `shutdown` + */ + if (!peer->shutdown_sent[LOCAL]) { + peer->send_shutdown = true; + start_commit_timer(peer); + } billboard_update(peer); } @@ -2377,13 +2391,11 @@ static void handle_ping_cmd(struct peer *peer, const u8 *inmsg) static void handle_shutdown_cmd(struct peer *peer, const u8 *inmsg) { - u8 *scriptpubkey; - - if (!fromwire_channel_send_shutdown(peer, inmsg, &scriptpubkey)) + if (!fromwire_channel_send_shutdown(inmsg)) master_badmsg(WIRE_CHANNEL_SEND_SHUTDOWN, inmsg); - /* We can't send this until commit (if any) is done, so start timer<. */ - peer->unsent_shutdown_scriptpubkey = scriptpubkey; + /* We can't send this until commit (if any) is done, so start timer. */ + peer->send_shutdown = true; start_commit_timer(peer); } @@ -2537,8 +2549,9 @@ static void init_channel(struct peer *peer) &peer->funding_locked[REMOTE], &peer->short_channel_ids[LOCAL], &reconnected, - &peer->unsent_shutdown_scriptpubkey, + &peer->send_shutdown, &peer->shutdown_sent[REMOTE], + &peer->final_scriptpubkey, &peer->channel_flags, &funding_signed)) master_badmsg(WIRE_CHANNEL_INIT, msg); diff --git a/channeld/channel_wire.csv b/channeld/channel_wire.csv index cd255b3ad..da809d30e 100644 --- a/channeld/channel_wire.csv +++ b/channeld/channel_wire.csv @@ -55,9 +55,10 @@ channel_init,,local_funding_locked,bool channel_init,,remote_funding_locked,bool channel_init,,funding_short_id,struct short_channel_id channel_init,,reestablish,bool -channel_init,,shutdown_scriptpubkey_len,u16 -channel_init,,shutdown_scriptpubkey,shutdown_scriptpubkey_len*u8 +channel_init,,send_shutdown,bool channel_init,,remote_shutdown_received,bool +channel_init,,final_scriptpubkey_len,u16 +channel_init,,final_scriptpubkey,final_scriptpubkey_len*u8 channel_init,,flags,u8 channel_init,,init_peer_pkt_len,u16 channel_init,,init_peer_pkt,init_peer_pkt_len*u8 @@ -164,10 +165,8 @@ channel_got_revoke,,changed,num_changed*struct changed_htlc # (eg. if we sent another commitment_signed, that would implicitly ack). channel_got_revoke_reply,1122 -# Tell peer that channel is shutting down +# Tell peer to shut down channel. channel_send_shutdown,1023 -channel_send_shutdown,,scriptpubkey_len,u16 -channel_send_shutdown,,scriptpubkey,scriptpubkey_len*u8 # Peer told us that channel is shutting down channel_got_shutdown,1024 diff --git a/lightningd/channel.c b/lightningd/channel.c index b89b63804..775821386 100644 --- a/lightningd/channel.c +++ b/lightningd/channel.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -145,8 +146,7 @@ struct channel *new_channel(struct peer *peer, u64 dbid, const struct channel_info *channel_info, /* NULL or stolen */ u8 *remote_shutdown_scriptpubkey, - /* (-1 if not chosen yet) */ - s64 local_shutdown_idx, + u64 final_key_idx, bool last_was_revoke, /* NULL or stolen */ struct changed_htlc *last_sent_commit, @@ -199,7 +199,7 @@ struct channel *new_channel(struct peer *peer, u64 dbid, channel->channel_info = *channel_info; channel->remote_shutdown_scriptpubkey = tal_steal(channel, remote_shutdown_scriptpubkey); - channel->local_shutdown_idx = local_shutdown_idx; + channel->final_key_idx = 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; @@ -208,6 +208,11 @@ struct channel *new_channel(struct peer *peer, u64 dbid, list_add_tail(&peer->channels, &channel->list); tal_add_destructor(channel, destroy_channel); + /* Make sure we see any spends using this key */ + txfilter_add_scriptpubkey(peer->ld->owned_txfilter, + take(p2wpkh_for_keyidx(NULL, peer->ld, + channel->final_key_idx))); + return channel; } diff --git a/lightningd/channel.h b/lightningd/channel.h index 01ff66f4e..0a46ac2f0 100644 --- a/lightningd/channel.h +++ b/lightningd/channel.h @@ -80,8 +80,8 @@ struct channel { /* Their scriptpubkey if they sent shutdown. */ u8 *remote_shutdown_scriptpubkey; - /* Our key for shutdown (-1 if not chosen yet) */ - s64 local_shutdown_idx; + /* Address for any final outputs */ + u64 final_key_idx; /* Reestablishment stuff: last sent commit and revocation details. */ bool last_was_revoke; @@ -122,8 +122,7 @@ struct channel *new_channel(struct peer *peer, u64 dbid, const struct channel_info *channel_info, /* NULL or stolen */ u8 *remote_shutdown_scriptpubkey, - /* (-1 if not chosen yet) */ - s64 local_shutdown_idx, + u64 final_key_idx, bool last_was_revoke, /* NULL or stolen */ struct changed_htlc *last_sent_commit, diff --git a/lightningd/channel_control.c b/lightningd/channel_control.c index c4f65b7a9..a00946f31 100644 --- a/lightningd/channel_control.c +++ b/lightningd/channel_control.c @@ -76,48 +76,11 @@ static void peer_got_shutdown(struct channel *channel, const u8 *msg) return; } - if (channel->local_shutdown_idx == -1) { - u8 *scriptpubkey; - - channel->local_shutdown_idx = wallet_get_newindex(ld); - if (channel->local_shutdown_idx == -1) { - channel_internal_error(channel, - "Can't get local shutdown index"); - return; - } - + /* If we weren't already shutting down, we are now */ + if (channel->state == CHANNELD_NORMAL) channel_set_state(channel, CHANNELD_NORMAL, CHANNELD_SHUTTING_DOWN); - /* BOLT #2: - * - * A sending node MUST set `scriptpubkey` to one of the - * following forms: - * - * ...3. `OP_0` `20` 20-bytes (version 0 pay to witness pubkey), - */ - scriptpubkey = p2wpkh_for_keyidx(msg, ld, - channel->local_shutdown_idx); - if (!scriptpubkey) { - channel_internal_error(channel, - "Can't get shutdown script %"PRIu64, - channel->local_shutdown_idx); - return; - } - - txfilter_add_scriptpubkey(ld->owned_txfilter, scriptpubkey); - - /* BOLT #2: - * - * A receiving node MUST reply to a `shutdown` message with a - * `shutdown` once there are no outstanding updates on the - * peer, unless it has already sent a `shutdown`. - */ - subd_send_msg(channel->owner, - take(towire_channel_send_shutdown(channel, - scriptpubkey))); - } - /* TODO(cdecker) Selectively save updated fields to DB */ wallet_channel_save(ld->wallet, channel); } @@ -215,7 +178,6 @@ bool peer_start_channeld(struct channel *channel, const struct failed_htlc **failed_htlcs; enum side *failed_sides; struct short_channel_id funding_channel_id; - const u8 *shutdown_scriptpubkey; u64 num_revocations; struct lightningd *ld = channel->peer->ld; const struct config *cfg = &ld->config; @@ -262,13 +224,6 @@ bool peer_start_channeld(struct channel *channel, memset(&funding_channel_id, 0, sizeof(funding_channel_id)); } - if (channel->local_shutdown_idx != -1) { - shutdown_scriptpubkey - = p2wpkh_for_keyidx(tmpctx, ld, - channel->local_shutdown_idx); - } else - shutdown_scriptpubkey = NULL; - num_revocations = revocations_received(&channel->their_shachain.chain); /* Warn once. */ @@ -316,8 +271,10 @@ bool peer_start_channeld(struct channel *channel, channel->remote_funding_locked, &funding_channel_id, reconnected, - shutdown_scriptpubkey, + channel->state == CHANNELD_SHUTTING_DOWN, channel->remote_shutdown_scriptpubkey != NULL, + p2wpkh_for_keyidx(tmpctx, ld, + channel->final_key_idx), channel->channel_flags, funding_signed); diff --git a/lightningd/closing_control.c b/lightningd/closing_control.c index 9ce9ce7f1..30af84f9a 100644 --- a/lightningd/closing_control.c +++ b/lightningd/closing_control.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -140,20 +141,15 @@ void peer_start_closingd(struct channel *channel, bool reconnected) { const tal_t *tmpctx = tal_tmpctx(channel); - u8 *initmsg, *local_scriptpubkey; + u8 *initmsg; u64 minfee, startfee, feelimit; u64 num_revocations; u64 funding_msatoshi, our_msatoshi, their_msatoshi; struct lightningd *ld = channel->peer->ld; - if (channel->local_shutdown_idx == -1 - || !channel->remote_shutdown_scriptpubkey) { + if (!channel->remote_shutdown_scriptpubkey) { channel_internal_error(channel, - "Can't start closing: local %s remote %s", - channel->local_shutdown_idx == -1 - ? "not shutdown" : "shutdown", - channel->remote_shutdown_scriptpubkey - ? "shutdown" : "not shutdown"); + "Can't start closing: no remote info"); tal_free(tmpctx); return; } @@ -174,15 +170,6 @@ void peer_start_closingd(struct channel *channel, return; } - local_scriptpubkey = p2wpkh_for_keyidx(tmpctx, ld, - channel->local_shutdown_idx); - if (!local_scriptpubkey) { - channel_internal_error(channel, - "Can't generate local shutdown scriptpubkey"); - tal_free(tmpctx); - return; - } - /* BOLT #2: * * A sending node MUST set `fee_satoshis` lower than or equal @@ -227,7 +214,8 @@ void peer_start_closingd(struct channel *channel, their_msatoshi / 1000, /* Rounds down */ channel->our_config.dust_limit_satoshis, minfee, feelimit, startfee, - local_scriptpubkey, + p2wpkh_for_keyidx(tmpctx, ld, + channel->final_key_idx), channel->remote_shutdown_scriptpubkey, reconnected, channel->next_index[LOCAL], diff --git a/lightningd/onchain_control.c b/lightningd/onchain_control.c index c2cbad270..30926a316 100644 --- a/lightningd/onchain_control.c +++ b/lightningd/onchain_control.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -343,13 +344,12 @@ enum watch_result funding_spent(struct channel *channel, size_t input_num, const struct block *block) { - u8 *msg, *scriptpubkey; + u8 *msg; struct bitcoin_txid our_last_txid; - s64 keyindex; - struct pubkey ourkey; struct htlc_stub *stubs; const tal_t *tmpctx = tal_tmpctx(channel); struct lightningd *ld = channel->peer->ld; + struct pubkey final_key; channel_fail_permanent(channel, "Funding transaction spent"); @@ -380,35 +380,13 @@ enum watch_result funding_spent(struct channel *channel, return KEEP_WATCHING; } - /* We re-use this key to send other outputs to. */ - if (channel->local_shutdown_idx >= 0) - keyindex = channel->local_shutdown_idx; - else { - keyindex = wallet_get_newindex(ld); - if (keyindex < 0) { - log_broken(channel->log, "Could not get keyindex"); - tal_free(tmpctx); - return KEEP_WATCHING; - } - } - scriptpubkey = p2wpkh_for_keyidx(tmpctx, ld, keyindex); - if (!scriptpubkey) { - channel_internal_error(channel, - "Can't get shutdown script %"PRIu64, - keyindex); + if (!bip32_pubkey(ld->wallet->bip32_base, &final_key, + channel->final_key_idx)) { + log_broken(channel->log, "Could not derive onchain key %"PRIu64, + channel->final_key_idx); tal_free(tmpctx); - return DELETE_WATCH; + return KEEP_WATCHING; } - txfilter_add_scriptpubkey(ld->owned_txfilter, scriptpubkey); - - if (!bip32_pubkey(ld->wallet->bip32_base, &ourkey, keyindex)) { - channel_internal_error(channel, - "Can't get shutdown key %"PRIu64, - keyindex); - tal_free(tmpctx); - return DELETE_WATCH; - } - /* This could be a mutual close, but it doesn't matter. */ bitcoin_txid(channel->last_tx, &our_last_txid); @@ -429,9 +407,10 @@ enum watch_result funding_spent(struct channel *channel, channel->our_config.dust_limit_satoshis, &channel->channel_info.theirbase.revocation, &our_last_txid, - scriptpubkey, + p2wpkh_for_keyidx(tmpctx, ld, + channel->final_key_idx), channel->remote_shutdown_scriptpubkey, - &ourkey, + &final_key, channel->funder, &channel->channel_info.theirbase.payment, &channel->channel_info.theirbase.htlc, diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index 6b9f81d12..79c8af92d 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -146,7 +146,8 @@ void json_add_uncommitted_channel(struct json_result *response, json_object_end(response); } -/* Steals fields from uncommitted_channel */ +/* Steals fields from uncommitted_channel: returns NULL if can't generate a + * key for this channel (shouldn't happen!). */ static struct channel * wallet_commit_channel(struct lightningd *ld, struct uncommitted_channel *uc, @@ -162,6 +163,14 @@ wallet_commit_channel(struct lightningd *ld, { struct channel *channel; u64 our_msatoshi; + s64 final_key_idx; + + /* Get a key to use for closing outputs from this tx */ + final_key_idx = wallet_get_newindex(ld); + if (final_key_idx == -1) { + log_broken(uc->log, "Can't get final key index"); + return NULL; + } if (uc->fc) our_msatoshi = funding_satoshi * 1000 - push_msat; @@ -198,7 +207,7 @@ wallet_commit_channel(struct lightningd *ld, NULL, /* No HTLC sigs yet */ channel_info, NULL, /* No remote_shutdown_scriptpubkey yet */ - -1, false, + final_key_idx, false, NULL, /* No commit sent yet */ uc->first_blocknum); @@ -335,6 +344,10 @@ static void opening_funder_finished(struct subd *openingd, const u8 *resp, fc->channel_flags, &channel_info, feerate); + if (!channel) { + command_fail(fc->cmd, "Key generation failure"); + goto failed; + } /* Get HSM to sign the funding tx. */ log_debug(channel->log, "Getting HSM to sign funding tx"); @@ -450,6 +463,10 @@ static void opening_fundee_finished(struct subd *openingd, channel_flags, &channel_info, feerate); + if (!channel) { + tal_free(uc); + return; + } log_debug(channel->log, "Watching funding tx %s", type_to_string(reply, struct bitcoin_txid, diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 73bb9774a..cefc44994 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -808,28 +808,12 @@ static void json_close(struct command *cmd, /* Normal case. */ if (channel->state == CHANNELD_NORMAL) { - u8 *shutdown_scriptpubkey; - - channel->local_shutdown_idx = wallet_get_newindex(cmd->ld); - if (channel->local_shutdown_idx == -1) { - command_fail(cmd, "Failed to get new key for shutdown"); - return; - } - shutdown_scriptpubkey = p2wpkh_for_keyidx(cmd, cmd->ld, - channel->local_shutdown_idx); - if (!shutdown_scriptpubkey) { - command_fail(cmd, "Failed to get script for shutdown"); - return; - } - - channel_set_state(channel, CHANNELD_NORMAL, CHANNELD_SHUTTING_DOWN); - - txfilter_add_scriptpubkey(cmd->ld->owned_txfilter, shutdown_scriptpubkey); + channel_set_state(channel, + CHANNELD_NORMAL, CHANNELD_SHUTTING_DOWN); if (channel->owner) subd_send_msg(channel->owner, - take(towire_channel_send_shutdown(channel, - shutdown_scriptpubkey))); + take(towire_channel_send_shutdown(channel))); command_success(cmd, null_response(cmd)); } else diff --git a/wallet/test/Makefile b/wallet/test/Makefile index e7d028428..64a88be6f 100644 --- a/wallet/test/Makefile +++ b/wallet/test/Makefile @@ -6,6 +6,7 @@ WALLET_TEST_COMMON_OBJS := \ common/htlc_state.o \ common/type_to_string.o \ common/memleak.o \ + common/key_derive.o \ common/pseudorand.o \ common/timeout.o \ common/utils.o \ diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 3eeef5295..035eec229 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -28,10 +28,6 @@ static void db_log_(struct log *log UNUSED, enum log_level level UNUSED, const c bool deprecated_apis = true; /* AUTOGENERATED MOCKS START */ -/* Generated stub for bip32_pubkey */ -bool bip32_pubkey(const struct ext_key *bip32_base UNNEEDED, - struct pubkey *pubkey UNNEEDED, u32 index UNNEEDED) -{ fprintf(stderr, "bip32_pubkey called!\n"); abort(); } /* Generated stub for bitcoind_gettxout */ void bitcoind_gettxout(struct bitcoind *bitcoind UNNEEDED, const struct bitcoin_txid *txid UNNEEDED, const u32 outnum UNNEEDED, @@ -332,7 +328,7 @@ u8 *towire_channel_funding_announce_depth(const tal_t *ctx UNNEEDED) u8 *towire_channel_funding_locked(const tal_t *ctx UNNEEDED, const struct short_channel_id *short_channel_id UNNEEDED) { fprintf(stderr, "towire_channel_funding_locked called!\n"); abort(); } /* Generated stub for towire_channel_send_shutdown */ -u8 *towire_channel_send_shutdown(const tal_t *ctx UNNEEDED, const u8 *scriptpubkey UNNEEDED) +u8 *towire_channel_send_shutdown(const tal_t *ctx UNNEEDED) { fprintf(stderr, "towire_channel_send_shutdown called!\n"); abort(); } /* Generated stub for towire_error */ u8 *towire_error(const tal_t *ctx UNNEEDED, const struct channel_id *channel_id UNNEEDED, const u8 *data UNNEEDED) @@ -360,9 +356,6 @@ u8 *towire_gossip_disable_channel(const tal_t *ctx UNNEEDED, const struct short_ /* Generated stub for towire_gossip_getpeers_request */ u8 *towire_gossip_getpeers_request(const tal_t *ctx UNNEEDED, const struct pubkey *id UNNEEDED) { fprintf(stderr, "towire_gossip_getpeers_request called!\n"); abort(); } -/* Generated stub for txfilter_add_scriptpubkey */ -void txfilter_add_scriptpubkey(struct txfilter *filter UNNEEDED, const u8 *script TAKES UNNEEDED) -{ fprintf(stderr, "txfilter_add_scriptpubkey called!\n"); abort(); } /* Generated stub for unsupported_features */ bool unsupported_features(const u8 *gfeatures UNNEEDED, const u8 *lfeatures UNNEEDED) { fprintf(stderr, "unsupported_features called!\n"); abort(); } @@ -445,6 +438,12 @@ const char *log_prefix(const struct log *log UNNEEDED) return ""; } +void txfilter_add_scriptpubkey(struct txfilter *filter UNNEEDED, const u8 *script TAKES) +{ + if (taken(script)) + tal_free(script); +} + /** * mempat -- Set the memory to a pattern * @@ -464,6 +463,7 @@ static struct wallet *create_test_wallet(struct lightningd *ld, const tal_t *ctx char filename[] = "/tmp/ldb-XXXXXX"; int fd = mkstemp(filename); struct wallet *w = tal(ctx, struct wallet); + static unsigned char badseed[BIP32_ENTROPY_LEN_128]; CHECK_MSG(fd != -1, "Unable to generate temp filename"); close(fd); @@ -472,6 +472,12 @@ static struct wallet *create_test_wallet(struct lightningd *ld, const tal_t *ctx ltmp = tal_tmpctx(ctx); list_head_init(&w->unstored_payments); w->ld = ld; + ld->wallet = w; + + w->bip32_base = tal(w, struct ext_key); + CHECK(bip32_key_from_seed(badseed, sizeof(badseed), + BIP32_VER_TEST_PRIVATE, 0, + w->bip32_base) == WALLY_OK); CHECK_MSG(w->db, "Failed opening the db"); db_migrate(w->db, w->log); @@ -668,10 +674,11 @@ static bool channelseq(struct channel *c1, struct channel *c2) CHECK(memeq(&c1->last_sig, sizeof(c1->last_sig), &c2->last_sig, sizeof(c2->last_sig))); - if (c1->remote_shutdown_scriptpubkey) { - CHECK(c2->remote_shutdown_scriptpubkey); - CHECK(c1->local_shutdown_idx == c2->local_shutdown_idx); - } + CHECK(c1->final_key_idx == c2->final_key_idx); + CHECK(memeq(c1->remote_shutdown_scriptpubkey, + tal_len(c1->remote_shutdown_scriptpubkey), + c2->remote_shutdown_scriptpubkey, + tal_len(c2->remote_shutdown_scriptpubkey))); CHECK(c1->last_was_revoke == c2->last_was_revoke); @@ -717,6 +724,7 @@ static bool test_channel_crud(struct lightningd *ld, const tal_t *ctx) ci->feerate_per_kw[LOCAL] = ci->feerate_per_kw[REMOTE] = 31337; mempat(scriptpubkey, tal_len(scriptpubkey)); c1.first_blocknum = 1; + c1.final_key_idx = 1337; p = new_peer(ld, 0, &pk, NULL); c1.peer = p; c1.dbid = wallet_get_channel_dbid(w); @@ -782,7 +790,6 @@ static bool test_channel_crud(struct lightningd *ld, const tal_t *ctx) /* Variant 4: update and add remote_shutdown_scriptpubkey */ c1.remote_shutdown_scriptpubkey = scriptpubkey; - c1.local_shutdown_idx = 1337; 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")); @@ -972,6 +979,13 @@ int main(void) ok &= test_htlc_crud(ld, tmpctx); ok &= test_payment_crud(ld, tmpctx); + take_cleanup(); tal_free(tmpctx); + + /* FIXME! https://github.com/ElementsProject/libwally-core/issues/26 */ + { + secp256k1_context *secp_ctx(void); + secp256k1_context_destroy(secp_ctx()); + } return !ok; } diff --git a/wallet/wallet.c b/wallet/wallet.c index 3fe9ccbd5..e98dfcc5a 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -584,6 +585,7 @@ static struct channel *wallet_stmt2channel(const tal_t *ctx, struct wallet *w, s u8 *remote_shutdown_scriptpubkey; struct changed_htlc *last_sent_commit; const tal_t *tmpctx = tal_tmpctx(ctx); + s64 final_key_idx; peer_dbid = sqlite3_column_int64(stmt, 1); peer = find_peer_by_dbid(w->ld, peer_dbid); @@ -640,6 +642,12 @@ static struct channel *wallet_stmt2channel(const tal_t *ctx, struct wallet *w, s return NULL; } + final_key_idx = sqlite3_column_int64(stmt, 29); + if (final_key_idx < 0) { + log_broken(w->log, "%s: Final key < 0", __func__); + tal_free(tmpctx); + return NULL; + } chan = new_channel(peer, sqlite3_column_int64(stmt, 0), &wshachain, sqlite3_column_int(stmt, 5), @@ -665,8 +673,7 @@ static struct channel *wallet_stmt2channel(const tal_t *ctx, struct wallet *w, s sqlite3_column_int64(stmt, 0)), &channel_info, remote_shutdown_scriptpubkey, - remote_shutdown_scriptpubkey - ? sqlite3_column_int64(stmt, 29) : -1, + final_key_idx, sqlite3_column_int(stmt, 34) != 0, last_sent_commit, sqlite3_column_int64(stmt, 35)); @@ -909,7 +916,7 @@ void wallet_channel_save(struct wallet *w, struct channel *chan) tal_len(chan->remote_shutdown_scriptpubkey), SQLITE_TRANSIENT); - sqlite3_bind_int64(stmt, 17, chan->local_shutdown_idx); + sqlite3_bind_int64(stmt, 17, chan->final_key_idx); sqlite3_bind_int64(stmt, 18, chan->our_config.id); sqlite3_bind_tx(stmt, 19, chan->last_tx); sqlite3_bind_signature(stmt, 20, &chan->last_sig);