From 1ac10716be8af4441c1cc9f27cb67b5fc144ab2a Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Tue, 8 Aug 2017 19:09:08 +0200 Subject: [PATCH] wallet: Add closing parameters to channel state I must have missed these before, so adding them now. --- wallet/db.c | 2 ++ wallet/wallet.c | 23 +++++++++++++++++++---- wallet/wallet_tests.c | 16 ++++++++++++++++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/wallet/db.c b/wallet/db.c index a5cb3ed1b..20ee7063b 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -72,6 +72,8 @@ char *dbmigrations[] = { " shutdown_keyidx_local INTEGER," " last_sent_commit_state INTEGER," " last_sent_commit_id INTEGER," + " closing_fee_received INTEGER," + " closing_sig_received BLOB," " PRIMARY KEY (id)" ");", "CREATE TABLE peers (" diff --git a/wallet/wallet.c b/wallet/wallet.c index a298e36fc..221a6476b 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -505,7 +505,17 @@ static bool wallet_stmt2channel(struct wallet *w, sqlite3_stmt *stmt, chan->peer->last_sent_commit = tal_free(chan->peer->last_sent_commit); col += 2; } - assert(col == 32); + + chan->peer->closing_fee_received = sqlite3_column_int64(stmt, col++); + if (sqlite3_column_type(stmt, col) != SQLITE_NULL) { + if (!chan->peer->closing_sig_received) { + chan->peer->closing_sig_received = tal(chan->peer, secp256k1_ecdsa_signature); + } + ok &= sqlite3_column_sig(stmt, col++, chan->peer->closing_sig_received); + } else { + col++; + } + assert(col == 34); return ok; } @@ -529,7 +539,8 @@ bool wallet_channel_load(struct wallet *w, const u64 id, "delayed_payment_basepoint_remote, per_commit_remote, " "old_per_commit_remote, feerate_per_kw, shachain_remote_id, " "shutdown_scriptpubkey_remote, shutdown_keyidx_local, " - "last_sent_commit_state, last_sent_commit_id FROM channels WHERE " + "last_sent_commit_state, last_sent_commit_id, " + "closing_fee_received, closing_sig_received FROM channels WHERE " "id=%" PRIu64 ";"; sqlite3_stmt *stmt = db_query(__func__, w->db, channel_query, id); @@ -547,7 +558,7 @@ bool wallet_channel_load(struct wallet *w, const u64 id, static char* db_serialize_signature(const tal_t *ctx, secp256k1_ecdsa_signature* sig) { u8 buf[64]; - if (secp256k1_ecdsa_signature_serialize_compact(secp256k1_ctx, buf, sig) != 1) + if (!sig || secp256k1_ecdsa_signature_serialize_compact(secp256k1_ctx, buf, sig) != 1) return "null"; return tal_fmt(ctx, "'%s'", tal_hexstr(ctx, buf, sizeof(buf))); } @@ -662,7 +673,9 @@ bool wallet_channel_save(struct wallet *w, struct wallet_channel *chan){ " msatoshi_local=%s," " shutdown_scriptpubkey_remote='%s'," " shutdown_keyidx_local=%"PRIu64"," - " channel_config_local=%"PRIu64 + " channel_config_local=%"PRIu64"," + " closing_fee_received=%"PRIu64"," + " closing_sig_received=%s" " WHERE id=%"PRIu64, p->their_shachain.id, p->scid?tal_fmt(tmpctx,"'%s'", short_channel_id_to_str(tmpctx, p->scid)):"null", @@ -683,6 +696,8 @@ bool wallet_channel_save(struct wallet *w, struct wallet_channel *chan){ p->remote_shutdown_scriptpubkey?tal_hex(tmpctx, p->remote_shutdown_scriptpubkey):"", p->local_shutdown_idx, p->our_config.id, + p->closing_fee_received, + db_serialize_signature(tmpctx, p->closing_sig_received), chan->id); if (chan->peer->channel_info) { diff --git a/wallet/wallet_tests.c b/wallet/wallet_tests.c index 8f4e2e9d6..9f977e2d6 100644 --- a/wallet/wallet_tests.c +++ b/wallet/wallet_tests.c @@ -154,6 +154,14 @@ static bool channelseq(struct wallet_channel *c1, struct wallet_channel *c2) CHECK(lc1->id == lc2->id); } + CHECK((p1->closing_sig_received != NULL) == (p2->closing_sig_received != NULL)); + if(p1->closing_sig_received) { + CHECK(memeq(p1->closing_sig_received, + sizeof(secp256k1_ecdsa_signature), + p2->closing_sig_received, + sizeof(secp256k1_ecdsa_signature))); + } + return true; } @@ -166,6 +174,7 @@ static bool test_channel_crud(const tal_t *ctx) struct sha256_double *hash = tal(w, struct sha256_double); struct pubkey pk; struct changed_htlc last_commit; + secp256k1_ecdsa_signature *sig = tal(w, secp256k1_ecdsa_signature); u64 msat = 12345; @@ -174,6 +183,7 @@ static bool test_channel_crud(const tal_t *ctx) memset(&p, 0, sizeof(p)); memset(&ci, 3, sizeof(ci)); memset(hash, 'B', sizeof(*hash)); + memset(sig, 0, sizeof(*sig)); memset(&last_commit, 0, sizeof(last_commit)); pubkey_from_der(tal_hexdata(w, "02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc", 66), 33, &pk); ci.feerate_per_kw = 31337; @@ -235,6 +245,12 @@ static bool test_channel_crud(const tal_t *ctx) CHECK_MSG(wallet_channel_load(w, c1.id, c2), tal_fmt(w, "Load from DB: %s", w->db->err)); CHECK_MSG(channelseq(&c1, c2), "Compare loaded with saved (v6)"); + /* Variant 7: update with closing_sig */ + p.closing_sig_received = sig; + CHECK_MSG(wallet_channel_save(w, &c1), tal_fmt(w, "Insert into DB: %s", w->db->err)); + CHECK_MSG(wallet_channel_load(w, c1.id, c2), tal_fmt(w, "Load from DB: %s", w->db->err)); + CHECK_MSG(channelseq(&c1, c2), "Compare loaded with saved (v7)"); + tal_free(w); return true; }