Skip to content

Commit

Permalink
DB: Store the remote channel announcement signatures into DB
Browse files Browse the repository at this point in the history
1. Add the fields of remote channel announcement signatures into TABLE channels
2. Add the related function
  • Loading branch information
trueptolemy authored and cdecker committed May 29, 2019
1 parent 22600fa commit b41d529
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
3 changes: 3 additions & 0 deletions wallet/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
62 changes: 62 additions & 0 deletions wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
Expand Down
28 changes: 28 additions & 0 deletions wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) /!\ */
Expand Down Expand Up @@ -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 */

0 comments on commit b41d529

Please sign in to comment.