From 3fdb055606425656828138c66a166592844b4960 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Tue, 6 Feb 2018 12:15:26 +0100 Subject: [PATCH] wallet: Make first_blocknum a field of wallet_channel We were sideloading it, which is awkward, now it's a field that we can actually use in the code. Signed-off-by: Christian Decker --- lightningd/peer_control.c | 11 +++++------ lightningd/peer_htlcs.c | 8 ++++---- wallet/test/run-wallet.c | 18 ++++++++++-------- wallet/wallet.c | 13 +++++++------ wallet/wallet.h | 9 +++++---- 5 files changed, 31 insertions(+), 28 deletions(-) diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index bc00103e2..56ce14d6a 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -293,7 +293,7 @@ void peer_set_condition(struct peer *peer, enum peer_state old_state, if (peer_persists(peer)) { assert(peer->channel != NULL); /* TODO(cdecker) Selectively save updated fields to DB */ - wallet_channel_save(peer->ld->wallet, peer->channel, 0); + wallet_channel_save(peer->ld->wallet, peer->channel); } } @@ -416,9 +416,8 @@ static struct wallet_channel *peer_channel_new(struct wallet *w, wallet_peer_by_nodeid(w, &peer->id, peer); wc->id = 0; - - wallet_channel_save(w, wc, get_block_height(peer->ld->topology)); - + wc->first_blocknum = get_block_height(peer->ld->topology); + wallet_channel_save(w, wc); return wc; } @@ -1721,7 +1720,7 @@ static void peer_got_shutdown(struct peer *peer, const u8 *msg) } /* TODO(cdecker) Selectively save updated fields to DB */ - wallet_channel_save(peer->ld->wallet, peer->channel, 0); + wallet_channel_save(peer->ld->wallet, peer->channel); } void peer_last_tx(struct peer *peer, struct bitcoin_tx *tx, @@ -1800,7 +1799,7 @@ static void peer_received_closing_signature(struct peer *peer, const u8 *msg) if (better_closing_fee(peer, tx)) { peer_last_tx(peer, tx, &sig); /* TODO(cdecker) Selectively save updated fields to DB */ - wallet_channel_save(peer->ld->wallet, peer->channel, 0); + wallet_channel_save(peer->ld->wallet, peer->channel); } /* OK, you can continue now. */ diff --git a/lightningd/peer_htlcs.c b/lightningd/peer_htlcs.c index b1fb0bfd0..08ead4aee 100644 --- a/lightningd/peer_htlcs.c +++ b/lightningd/peer_htlcs.c @@ -927,7 +927,7 @@ static bool peer_save_commitsig_sent(struct peer *peer, u64 commitnum) peer->next_index[REMOTE]++; /* FIXME: Save to database, with sig and HTLCs. */ - wallet_channel_save(peer->ld->wallet, peer->channel, 0); + wallet_channel_save(peer->ld->wallet, peer->channel); return true; } @@ -988,7 +988,7 @@ void peer_sending_commitsig(struct peer *peer, const u8 *msg) peer->last_was_revoke = false; tal_free(peer->last_sent_commit); peer->last_sent_commit = tal_steal(peer, changed_htlcs); - wallet_channel_save(peer->ld->wallet, peer->channel, 0); + wallet_channel_save(peer->ld->wallet, peer->channel); /* Tell it we've got it, and to go ahead with commitment_signed. */ subd_send_msg(peer->owner, @@ -1145,7 +1145,7 @@ void peer_got_commitsig(struct peer *peer, const u8 *msg) if (!peer_save_commitsig_received(peer, commitnum, tx, &commit_sig)) return; - wallet_channel_save(peer->ld->wallet, peer->channel, 0); + wallet_channel_save(peer->ld->wallet, peer->channel); /* FIXME: Put these straight in the db! */ tal_free(peer->last_htlc_sigs); @@ -1253,7 +1253,7 @@ void peer_got_revoke(struct peer *peer, const u8 *msg) hin = find_htlc_in(&peer->ld->htlcs_in, peer, changed[i].id); local_fail_htlc(hin, failcodes[i], NULL); } - wallet_channel_save(peer->ld->wallet, peer->channel, 0); + wallet_channel_save(peer->ld->wallet, peer->channel); } static void *tal_arr_append_(void **p, size_t size) diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 9f214ae57..9a54690b4 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -278,6 +278,7 @@ static bool channelseq(struct wallet_channel *c1, struct wallet_channel *c2) struct channel_info *ci1 = p1->channel_info, *ci2 = p2->channel_info; struct changed_htlc *lc1 = p1->last_sent_commit, *lc2 = p2->last_sent_commit; CHECK(c1->id == c2->id); + CHECK(c1->first_blocknum == c2->first_blocknum); CHECK(c1->peer->dbid == c2->peer->dbid); CHECK(p1->their_shachain.id == p2->their_shachain.id); CHECK_MSG(pubkey_eq(&p1->id, &p2->id), "NodeIDs do not match"); @@ -374,6 +375,7 @@ static bool test_channel_crud(const tal_t *ctx) ci.feerate_per_kw[LOCAL] = ci.feerate_per_kw[REMOTE] = 31337; mempat(&p.id, sizeof(p.id)); mempat(scriptpubkey, tal_len(scriptpubkey)); + c1.first_blocknum = 1; c1.peer = &p; p.id = pk; p.our_msatoshi = NULL; @@ -392,7 +394,7 @@ static bool test_channel_crud(const tal_t *ctx) CHECK(!wallet_err); /* Variant 1: insert with null for scid, funding_tx_id, channel_info, last_tx */ - wallet_channel_save(w, &c1, 1); + 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.id), tal_fmt(w, "Load from DB")); @@ -407,7 +409,7 @@ static bool test_channel_crud(const tal_t *ctx) /* Variant 2: update with scid set */ c1.peer->scid = talz(w, struct short_channel_id); - wallet_channel_save(w, &c1, 0); + 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.id), tal_fmt(w, "Load from DB")); @@ -424,7 +426,7 @@ static bool test_channel_crud(const tal_t *ctx) c1.peer->our_msatoshi = &msat; c1.peer->last_was_revoke = !c1.peer->last_was_revoke; - wallet_channel_save(w, &c1, 0); + 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.id), tal_fmt(w, "Load from DB")); CHECK_MSG(!wallet_err, @@ -433,7 +435,7 @@ static bool test_channel_crud(const tal_t *ctx) /* Variant 4: update with funding_tx_id */ c1.peer->funding_txid = hash; - wallet_channel_save(w, &c1, 0); + 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.id), tal_fmt(w, "Load from DB")); CHECK_MSG(!wallet_err, @@ -442,7 +444,7 @@ static bool test_channel_crud(const tal_t *ctx) /* Variant 5: update with channel_info */ p.channel_info = &ci; - wallet_channel_save(w, &c1, 0); + 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.id), tal_fmt(w, "Load from DB")); CHECK_MSG(!wallet_err, @@ -451,7 +453,7 @@ static bool test_channel_crud(const tal_t *ctx) /* Variant 6: update with last_commit_sent */ p.last_sent_commit = &last_commit; - wallet_channel_save(w, &c1, 0); + 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.id), tal_fmt(w, "Load from DB")); CHECK_MSG(!wallet_err, @@ -461,7 +463,7 @@ static bool test_channel_crud(const tal_t *ctx) /* Variant 7: update with last_tx (taken from BOLT #3) */ p.last_tx = bitcoin_tx_from_hex(w, "02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8003a00f0000000000002200208c48d15160397c9731df9bc3b236656efb6665fbfe92b4a6878e88a499f741c4c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de843110ae8f6a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e040047304402206a2679efa3c7aaffd2a447fd0df7aba8792858b589750f6a1203f9259173198a022008d52a0e77a99ab533c36206cb15ad7aeb2aa72b93d4b571e728cb5ec2f6fe260147304402206d6cb93969d39177a09d5d45b583f34966195b77c7e585cf47ac5cce0c90cefb022031d71ae4e33a4e80df7f981d696fbdee517337806a3c7138b7491e2cbb077a0e01475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220", strlen("02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8003a00f0000000000002200208c48d15160397c9731df9bc3b236656efb6665fbfe92b4a6878e88a499f741c4c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de843110ae8f6a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e040047304402206a2679efa3c7aaffd2a447fd0df7aba8792858b589750f6a1203f9259173198a022008d52a0e77a99ab533c36206cb15ad7aeb2aa72b93d4b571e728cb5ec2f6fe260147304402206d6cb93969d39177a09d5d45b583f34966195b77c7e585cf47ac5cce0c90cefb022031d71ae4e33a4e80df7f981d696fbdee517337806a3c7138b7491e2cbb077a0e01475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220")); p.last_sig = sig; - wallet_channel_save(w, &c1, 0); + 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.id), tal_fmt(w, "Load from DB")); CHECK_MSG(!wallet_err, @@ -471,7 +473,7 @@ static bool test_channel_crud(const tal_t *ctx) /* Variant 8: update and add remote_shutdown_scriptpubkey */ p.remote_shutdown_scriptpubkey = scriptpubkey; p.local_shutdown_idx = 1337; - wallet_channel_save(w, &c1, 0); + 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.id), tal_fmt(w, "Load from DB")); CHECK_MSG(!wallet_err, diff --git a/wallet/wallet.c b/wallet/wallet.c index 4d5065a44..db89a5d49 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -577,6 +577,7 @@ static bool wallet_stmt2channel(const tal_t *ctx, struct wallet *w, sqlite3_stmt } chan->peer->last_was_revoke = sqlite3_column_int(stmt, 34) != 0; + chan->first_blocknum = sqlite3_column_int64(stmt, 35); chan->peer->channel = chan; @@ -598,7 +599,7 @@ static const char *channel_fields = "old_per_commit_remote, local_feerate_per_kw, remote_feerate_per_kw, shachain_remote_id, " "shutdown_scriptpubkey_remote, shutdown_keyidx_local, " "last_sent_commit_state, last_sent_commit_id, " - "last_tx, last_sig, last_was_revoke"; + "last_tx, last_sig, last_was_revoke, first_blocknum"; bool wallet_channels_load_active(const tal_t *ctx, struct wallet *w, struct list_head *peers) { @@ -696,12 +697,12 @@ bool wallet_channel_config_load(struct wallet *w, const u64 id, return ok; } -void wallet_channel_save(struct wallet *w, struct wallet_channel *chan, - u32 current_block_height) +void wallet_channel_save(struct wallet *w, struct wallet_channel *chan) { struct peer *p = chan->peer; tal_t *tmpctx = tal_tmpctx(w); sqlite3_stmt *stmt; + assert(chan->first_blocknum); if (p->dbid == 0) { /* Need to store the peer first */ @@ -716,10 +717,10 @@ void wallet_channel_save(struct wallet *w, struct wallet_channel *chan, /* Insert a stub, that we can update, unifies INSERT and UPDATE paths */ if (chan->id == 0) { - assert(current_block_height); - stmt = db_prepare(w->db, "INSERT INTO channels (peer_id,first_blocknum) VALUES (?,?);"); + stmt = db_prepare(w->db, "INSERT INTO channels (" + "peer_id, first_blocknum) VALUES (?, ?);"); sqlite3_bind_int64(stmt, 1, p->dbid); - sqlite3_bind_int(stmt, 2, current_block_height); + sqlite3_bind_int(stmt, 2, chan->first_blocknum); db_exec_prepared(w->db, stmt); chan->id = sqlite3_last_insert_rowid(w->db->sql); } diff --git a/wallet/wallet.h b/wallet/wallet.h index 01ee62895..1dd767fb7 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -66,6 +66,10 @@ struct wallet_shachain { struct wallet_channel { u64 id; struct peer *peer; + + /* Blockheight at creation, scans for funding confirmations + * will start here */ + u64 first_blocknum; }; /* Possible states for a wallet_payment. Payments start in @@ -215,11 +219,8 @@ bool wallet_shachain_load(struct wallet *wallet, u64 id, * @wallet: the wallet to save into * @chan: the instance to store (not const so we can update the unique_id upon * insert) - * @current_block_height: current height, minimum block this funding tx could - * be in (only used on initial insert). */ -void wallet_channel_save(struct wallet *w, struct wallet_channel *chan, - u32 current_block_height); +void wallet_channel_save(struct wallet *w, struct wallet_channel *chan); /** * wallet_channel_delete -- After resolving a channel, forget about it