Skip to content

Commit

Permalink
lightningd: tweak db remote channel_announcement sigs API.
Browse files Browse the repository at this point in the history
Don't return false on db errors (we always fail on those), but return
false if they don't exist.

Also, add routine to clear them.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell committed Jan 31, 2024
1 parent cff166b commit e84baf7
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 39 deletions.
17 changes: 9 additions & 8 deletions lightningd/channel_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -1557,14 +1557,15 @@ bool peer_start_channeld(struct channel *channel,
if (channel->ignore_fee_limits || ld->config.ignore_fee_limits)
log_unusual(channel->log, "Ignoring fee limits!");

if (!wallet_remote_ann_sigs_load(tmpctx, channel->peer->ld->wallet,
channel->dbid,
&remote_ann_node_sig,
&remote_ann_bitcoin_sig)) {
channel_internal_error(channel,
"Could not load remote announcement"
" signatures");
return false;
remote_ann_node_sig = tal(tmpctx, secp256k1_ecdsa_signature);
remote_ann_bitcoin_sig = tal(tmpctx, secp256k1_ecdsa_signature);

if (!wallet_remote_ann_sigs_load(channel->peer->ld->wallet,
channel,
remote_ann_node_sig,
remote_ann_bitcoin_sig)) {
remote_ann_node_sig = tal_free(remote_ann_node_sig);
remote_ann_bitcoin_sig = tal_free(remote_ann_bitcoin_sig);
}

pbases = wallet_penalty_base_load_for_channel(
Expand Down
6 changes: 5 additions & 1 deletion wallet/test/run-wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -1818,12 +1818,16 @@ static bool test_channel_crud(struct lightningd *ld, const tal_t *ctx)
wallet_announcement_save(w, c1.dbid, node_sig1, bitcoin_sig1);
CHECK_MSG(!wallet_err,
tal_fmt(w, "Insert ann sigs into DB: %s", wallet_err));
CHECK_MSG(load = wallet_remote_ann_sigs_load(w, w, c1.dbid, &node_sig2, &bitcoin_sig2), tal_fmt(w, "Load ann sigs from DB"));
node_sig2 = tal(tmpctx, secp256k1_ecdsa_signature);
bitcoin_sig2 = tal(tmpctx, secp256k1_ecdsa_signature);
CHECK_MSG(load = wallet_remote_ann_sigs_load(w, &c1, node_sig2, bitcoin_sig2), tal_fmt(w, "Load ann sigs from DB"));
CHECK_MSG(!wallet_err,
tal_fmt(w, "Load ann sigs from DB: %s", wallet_err));
CHECK(load == true);
CHECK_MSG(!memcmp(node_sig1, node_sig2, sizeof(*node_sig1)), "Compare ann sigs loaded with saved (v5)");
CHECK_MSG(!memcmp(bitcoin_sig1, bitcoin_sig2, sizeof(*node_sig1)), "Compare ann sigs loaded with saved (v5)");
wallet_remote_ann_sigs_clear(w, &c1);
CHECK(!wallet_remote_ann_sigs_load(w, &c1, node_sig2, bitcoin_sig2));

db_commit_transaction(w->db);
CHECK(!wallet_err);
Expand Down
55 changes: 30 additions & 25 deletions wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -1075,16 +1075,17 @@ 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)
bool wallet_remote_ann_sigs_load(struct wallet *w,
const struct channel *chan,
secp256k1_ecdsa_signature *remote_ann_node_sig,
secp256k1_ecdsa_signature *remote_ann_bitcoin_sig)
{
struct db_stmt *stmt;
bool res;
stmt = db_prepare_v2(
w->db, SQL("SELECT remote_ann_node_sig, remote_ann_bitcoin_sig"
" FROM channels WHERE id = ?"));
db_bind_u64(stmt, id);
db_bind_u64(stmt, chan->dbid);
db_query_prepared(stmt);

res = db_step(stmt);
Expand All @@ -1096,29 +1097,31 @@ bool wallet_remote_ann_sigs_load(const tal_t *ctx, struct wallet *w, u64 id,
if (db_col_is_null(stmt, "remote_ann_node_sig")
|| db_col_is_null(stmt, "remote_ann_bitcoin_sig")) {
db_col_ignore(stmt, "remote_ann_bitcoin_sig");
*remote_ann_node_sig = *remote_ann_bitcoin_sig = NULL;
tal_free(stmt);
return true;
return false;
}

/* 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 (!db_col_signature(stmt, "remote_ann_node_sig", remote_ann_node_sig))
db_fatal(w->db, "Failed to decode remote_ann_node_sig for id %"PRIu64, chan->dbid);

if (!db_col_signature(stmt, "remote_ann_node_sig", *remote_ann_node_sig))
goto fail;

if (!db_col_signature(stmt, "remote_ann_bitcoin_sig", *remote_ann_bitcoin_sig))
goto fail;
if (!db_col_signature(stmt, "remote_ann_bitcoin_sig", remote_ann_bitcoin_sig))
db_fatal(w->db, "Failed to decode remote_ann_bitcoin_sig for id %"PRIu64, chan->dbid);

tal_free(stmt);
return true;
}

fail:
*remote_ann_node_sig = tal_free(*remote_ann_node_sig);
*remote_ann_bitcoin_sig = tal_free(*remote_ann_bitcoin_sig);
tal_free(stmt);
return false;
void wallet_remote_ann_sigs_clear(struct wallet *w, const struct channel *chan)
{
struct db_stmt *stmt;
stmt = db_prepare_v2(w->db,
SQL("UPDATE channels"
" SET remote_ann_node_sig=?, remote_ann_bitcoin_sig=?"
" WHERE id = ?"));
db_bind_null(stmt);
db_bind_null(stmt);
db_bind_u64(stmt, chan->dbid);
db_exec_prepared_v2(take(stmt));
}

static struct fee_states *wallet_channel_fee_states_load(struct wallet *w,
Expand Down Expand Up @@ -2215,6 +2218,7 @@ void wallet_channel_save(struct wallet *w, struct channel *chan)
{
struct db_stmt *stmt;
u8 *last_sent_commit;
const struct peer_update *peer_update;
assert(chan->first_blocknum);

wallet_channel_config_save(w, &chan->our_config);
Expand Down Expand Up @@ -2353,12 +2357,13 @@ void wallet_channel_save(struct wallet *w, struct channel *chan)
db_bind_null(stmt);

db_bind_int(stmt, chan->ignore_fee_limits);
if (chan->peer_update) {
db_bind_int(stmt, chan->peer_update->fee_base);
db_bind_int(stmt, chan->peer_update->fee_ppm);
db_bind_int(stmt, chan->peer_update->cltv_delta);
db_bind_amount_msat(stmt, &chan->peer_update->htlc_minimum_msat);
db_bind_amount_msat(stmt, &chan->peer_update->htlc_maximum_msat);
peer_update = chan->peer_update;
if (peer_update) {
db_bind_int(stmt, peer_update->fee_base);
db_bind_int(stmt, peer_update->fee_ppm);
db_bind_int(stmt, peer_update->cltv_delta);
db_bind_amount_msat(stmt, &peer_update->htlc_minimum_msat);
db_bind_amount_msat(stmt, &peer_update->htlc_maximum_msat);
} else {
db_bind_null(stmt);
db_bind_null(stmt);
Expand Down
20 changes: 15 additions & 5 deletions wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -1262,15 +1262,25 @@ bool wallet_forward_delete(struct wallet *w,
/**
* 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
* @chan: channel (must be in db)
* @remote_ann_node_sig: location to load remote_ann_node_sig to
* @remote_ann_bitcoin_sig: location to load remote_ann_bitcoin_sig to
*
* Returns false if the signatures were null.
*/
bool wallet_remote_ann_sigs_load(struct wallet *w,
const struct channel *chan,
secp256k1_ecdsa_signature *remote_ann_node_sig,
secp256k1_ecdsa_signature *remote_ann_bitcoin_sig);

/**
* Null out remote_ann_node_sig and remote_ann_bitcoin_sig
*
* @w: wallet containing the channel
* @id: channel database id
*/
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);
void wallet_remote_ann_sigs_clear(struct wallet *w, const struct channel *chan);

/**
* Get a list of transactions that we track in the wallet.
Expand Down

0 comments on commit e84baf7

Please sign in to comment.