From b41d529b2836379b5d2e58d3802cb9b89c1690e5 Mon Sep 17 00:00:00 2001 From: trueptolemy <823220586@qq.com> Date: Wed, 15 May 2019 23:21:26 +0800 Subject: [PATCH] DB: Store the remote channel announcement signatures into DB 1. Add the fields of remote channel announcement signatures into TABLE channels 2. Add the related function --- wallet/db.c | 3 +++ wallet/wallet.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ wallet/wallet.h | 28 ++++++++++++++++++++++ 3 files changed, 93 insertions(+) diff --git a/wallet/db.c b/wallet/db.c index a6fe7d2e9..b4679b623 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -382,6 +382,9 @@ static struct migration dbmigrations[] = { { "ALTER TABLE channels ADD remote_upfront_shutdown_script BLOB;", NULL }, /* PR #2524: Add failcode into forward_payment */ { "ALTER TABLE forwarded_payments ADD failcode INTEGER;", NULL }, + /* remote signatures for channel announcement */ + { "ALTER TABLE channels ADD remote_ann_node_sig BLOB;", NULL }, + { "ALTER TABLE channels ADD remote_ann_bitcoin_sig BLOB;", NULL }, }; /* Leak tracking. */ diff --git a/wallet/wallet.c b/wallet/wallet.c index fb10c51ed..738b721d3 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -595,6 +595,50 @@ wallet_htlc_sigs_load(const tal_t *ctx, struct wallet *w, u64 channelid) return htlc_sigs; } +bool wallet_remote_ann_sigs_load(const tal_t *ctx, struct wallet *w, u64 id, + secp256k1_ecdsa_signature **remote_ann_node_sig, + secp256k1_ecdsa_signature **remote_ann_bitcoin_sig) +{ + sqlite3_stmt *stmt; + int res; + stmt = db_select_prepare(w->db, + "remote_ann_node_sig, remote_ann_bitcoin_sig" + " FROM channels WHERE id = ?"); + sqlite3_bind_int64(stmt, 1, id); + + res = sqlite3_step(stmt); + + /* This must succeed, since we know the channel exists */ + assert(res == SQLITE_ROW); + + /* if only one sig exists, forget the sig and hope peer send new ones*/ + if(sqlite3_column_type(stmt, 0) == SQLITE_NULL || + sqlite3_column_type(stmt, 1) == SQLITE_NULL) { + *remote_ann_node_sig = *remote_ann_bitcoin_sig = NULL; + db_stmt_done(stmt); + return true; + } + + /* the case left over is both sigs exist */ + *remote_ann_node_sig = tal(ctx, secp256k1_ecdsa_signature); + *remote_ann_bitcoin_sig = tal(ctx, secp256k1_ecdsa_signature); + + if (!sqlite3_column_signature(stmt, 0, *remote_ann_node_sig)) + goto fail; + + if (!sqlite3_column_signature(stmt, 1, *remote_ann_bitcoin_sig)) + goto fail; + + db_stmt_done(stmt); + return true; + +fail: + *remote_ann_node_sig = tal_free(*remote_ann_node_sig); + *remote_ann_bitcoin_sig = tal_free(*remote_ann_bitcoin_sig); + db_stmt_done(stmt); + return false; +} + /** * wallet_stmt2channel - Helper to populate a wallet_channel from a sqlite3_stmt */ @@ -951,6 +995,24 @@ u64 wallet_get_channel_dbid(struct wallet *wallet) return ++wallet->max_channel_dbid; } +/* When we receive the remote announcement message, we will also call this function */ +void wallet_announcement_save(struct wallet *w, u64 id, + secp256k1_ecdsa_signature *remote_ann_node_sig, + secp256k1_ecdsa_signature *remote_ann_bitcoin_sig) +{ + sqlite3_stmt *stmt; + + stmt = db_prepare(w->db, "UPDATE channels SET" + " remote_ann_node_sig=?," + " remote_ann_bitcoin_sig=?" + " WHERE id=?"); + + sqlite3_bind_signature(stmt, 1, remote_ann_node_sig); + sqlite3_bind_signature(stmt, 2, remote_ann_bitcoin_sig); + sqlite3_bind_int64(stmt, 3, id); + db_exec_prepared(w->db, stmt); +} + void wallet_channel_save(struct wallet *w, struct channel *chan) { sqlite3_stmt *stmt; diff --git a/wallet/wallet.h b/wallet/wallet.h index 5f8651c9d..af2301df4 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -555,6 +555,21 @@ bool wallet_htlcs_load_for_channel(struct wallet *wallet, struct htlc_in_map *htlcs_in, struct htlc_out_map *htlcs_out); +/** + * wallet_announcement_save - Save remote announcement information with channel. + * + * @wallet: wallet to load from + * @id: channel database id + * @remote_ann_node_sig: location to load remote_ann_node_sig to + * @remote_ann_bitcoin_sig: location to load remote_ann_bitcoin_sig to + * + * This function is only used to save REMOTE announcement information into DB + * when the channel has set the announce_channel bit and don't send the shutdown + * message(BOLT#7). + */ +void wallet_announcement_save(struct wallet *wallet, u64 id, + secp256k1_ecdsa_signature *remote_ann_node_sig, + secp256k1_ecdsa_signature *remote_ann_bitcoin_sig); /* /!\ This is a DB ENUM, please do not change the numbering of any * already defined elements (adding is ok) /!\ */ @@ -1052,4 +1067,17 @@ struct amount_msat wallet_total_forward_fees(struct wallet *w); */ const struct forwarding *wallet_forwarded_payments_get(struct wallet *w, const tal_t *ctx); + +/** + * Load remote_ann_node_sig and remote_ann_bitcoin_sig + * + * @ctx: allocation context for the return value + * @w: wallet containing the channel + * @id: channel database id + * @remote_ann_node_sig: location to load remote_ann_node_sig to + * @remote_ann_bitcoin_sig: location to load remote_ann_bitcoin_sig to + */ +bool wallet_remote_ann_sigs_load(const tal_t *ctx, struct wallet *w, u64 id, + secp256k1_ecdsa_signature **remote_ann_node_sig, + secp256k1_ecdsa_signature **remote_ann_bitcoin_sig); #endif /* LIGHTNING_WALLET_WALLET_H */