From 3a2191fcfe60e54f2c017029889bd9578acbf5a4 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 26 Aug 2020 06:50:52 +0930 Subject: [PATCH] wallet: db_bind_talarr and db_column_talarr helpers. Signed-off-by: Rusty Russell --- wallet/db.c | 23 ++++++++++++++++++++--- wallet/db.h | 2 ++ wallet/invoices.c | 2 +- wallet/wallet.c | 45 ++++++++++++--------------------------------- 4 files changed, 35 insertions(+), 37 deletions(-) diff --git a/wallet/db.c b/wallet/db.c index f082a9918..30940600c 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -1430,7 +1430,7 @@ void db_bind_short_channel_id_arr(struct db_stmt *stmt, int col, for (size_t i = 0; i < num; ++i) towire_short_channel_id(&ser, &id[i]); - db_bind_blob(stmt, col, ser, tal_count(ser)); + db_bind_talarr(stmt, col, ser); } void db_bind_signature(struct db_stmt *stmt, int col, @@ -1453,7 +1453,7 @@ void db_bind_tx(struct db_stmt *stmt, int col, const struct wally_tx *tx) { u8 *ser = linearize_wtx(stmt, tx); assert(ser); - db_bind_blob(stmt, col, ser, tal_count(ser)); + db_bind_talarr(stmt, col, ser); } void db_bind_psbt(struct db_stmt *stmt, int col, const struct wally_psbt *psbt) @@ -1484,7 +1484,15 @@ void db_bind_json_escape(struct db_stmt *stmt, int pos, void db_bind_onionreply(struct db_stmt *stmt, int pos, const struct onionreply *r) { - db_bind_blob(stmt, pos, r->contents, tal_bytelen(r->contents)); + db_bind_talarr(stmt, pos, r->contents); +} + +void db_bind_talarr(struct db_stmt *stmt, int col, const u8 *arr) +{ + if (!arr) + db_bind_null(stmt, col); + else + db_bind_blob(stmt, col, arr, tal_bytelen(arr)); } void db_column_preimage(struct db_stmt *stmt, int col, @@ -1686,6 +1694,15 @@ struct onionreply *db_column_onionreply(const tal_t *ctx, return r; } +u8 *db_column_talarr(const tal_t *ctx, struct db_stmt *stmt, int col) +{ + if (db_column_is_null(stmt, col)) + return NULL; + return tal_dup_arr(ctx, u8, + db_column_blob(stmt, col), + db_column_bytes(stmt, col), 0); +} + bool db_exec_prepared_v2(struct db_stmt *stmt TAKES) { bool ret = stmt->db->config->exec_fn(stmt); diff --git a/wallet/db.h b/wallet/db.h index 3ef2c7783..624e1e9a6 100644 --- a/wallet/db.h +++ b/wallet/db.h @@ -128,6 +128,7 @@ void db_bind_json_escape(struct db_stmt *stmt, int pos, const struct json_escape *esc); void db_bind_onionreply(struct db_stmt *stmt, int col, const struct onionreply *r); +void db_bind_talarr(struct db_stmt *stmt, int col, const u8 *arr); bool db_step(struct db_stmt *stmt); u64 db_column_u64(struct db_stmt *stmt, int col); @@ -162,6 +163,7 @@ struct bitcoin_tx *db_column_psbt_to_tx(const tal_t *ctx, struct db_stmt *stmt, struct onionreply *db_column_onionreply(const tal_t *ctx, struct db_stmt *stmt, int col); +u8 *db_column_talarr(const tal_t *ctx, struct db_stmt *stmt, int col); #define db_column_arr(ctx, stmt, col, type) \ ((type *)db_column_arr_((ctx), (stmt), (col), \ diff --git a/wallet/invoices.c b/wallet/invoices.c index 253ac758b..d465b4bfd 100644 --- a/wallet/invoices.c +++ b/wallet/invoices.c @@ -300,7 +300,7 @@ bool invoices_create(struct invoices *invoices, db_bind_u64(stmt, 5, expiry_time); db_bind_text(stmt, 6, b11enc); db_bind_text(stmt, 7, description); - db_bind_blob(stmt, 8, features, tal_bytelen(features)); + db_bind_talarr(stmt, 8, features); db_exec_prepared_v2(stmt); diff --git a/wallet/wallet.c b/wallet/wallet.c index 71e102068..15594ad82 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -1674,11 +1674,7 @@ void wallet_channel_save(struct wallet *w, struct channel *chan) db_bind_amount_msat(stmt, 14, &chan->push); db_bind_amount_msat(stmt, 15, &chan->our_msat); - if (chan->shutdown_scriptpubkey[REMOTE]) - db_bind_blob(stmt, 16, chan->shutdown_scriptpubkey[REMOTE], - tal_count(chan->shutdown_scriptpubkey[REMOTE])); - else - db_bind_null(stmt, 16); + db_bind_talarr(stmt, 16, chan->shutdown_scriptpubkey[REMOTE]); db_bind_u64(stmt, 17, chan->final_key_idx); db_bind_u64(stmt, 18, chan->our_config.id); @@ -1691,17 +1687,10 @@ void wallet_channel_save(struct wallet *w, struct channel *chan) db_bind_amount_msat(stmt, 25, &chan->msat_to_us_max); db_bind_int(stmt, 26, chan->feerate_base); db_bind_int(stmt, 27, chan->feerate_ppm); - if (chan->remote_upfront_shutdown_script) - db_bind_blob( - stmt, 28, chan->remote_upfront_shutdown_script, - tal_count(chan->remote_upfront_shutdown_script)); - else - db_bind_null(stmt, 28); - + db_bind_talarr(stmt, 28, chan->remote_upfront_shutdown_script); db_bind_int(stmt, 29, chan->option_static_remotekey); db_bind_int(stmt, 30, chan->option_anchor_outputs); - db_bind_blob(stmt, 31, chan->shutdown_scriptpubkey[LOCAL], - tal_count(chan->shutdown_scriptpubkey[LOCAL])); + db_bind_talarr(stmt, 31, chan->shutdown_scriptpubkey[LOCAL]); db_bind_u64(stmt, 32, chan->dbid); db_exec_prepared_v2(take(stmt)); @@ -1756,15 +1745,14 @@ void wallet_channel_save(struct wallet *w, struct channel *chan) for (size_t i = 0; i < tal_count(chan->last_sent_commit); i++) towire_changed_htlc(&last_sent_commit, &chan->last_sent_commit[i]); + /* Make it null in db if it's empty */ + if (tal_count(last_sent_commit) == 0) + last_sent_commit = tal_free(last_sent_commit); stmt = db_prepare_v2(w->db, SQL("UPDATE channels SET" " last_sent_commit=?" " WHERE id=?")); - if (tal_count(last_sent_commit)) - db_bind_blob(stmt, 0, last_sent_commit, - tal_count(last_sent_commit)); - else - db_bind_null(stmt, 0); + db_bind_talarr(stmt, 0, last_sent_commit); db_bind_u64(stmt, 1, chan->dbid); db_exec_prepared_v2(stmt); tal_free(stmt); @@ -2138,10 +2126,7 @@ void wallet_htlc_update(struct wallet *wallet, const u64 htlc_dbid, else db_bind_null(stmt, 3); - if (failmsg) - db_bind_blob(stmt, 4, failmsg, tal_bytelen(failmsg)); - else - db_bind_null(stmt, 4); + db_bind_talarr(stmt, 4, failmsg); if (we_filled) db_bind_int(stmt, 5, *we_filled); @@ -2980,11 +2965,9 @@ void wallet_payment_set_failinfo(struct wallet *wallet, " WHERE payment_hash=?" " AND partid=?;")); if (failonionreply) - db_bind_blob(stmt, 0, failonionreply->contents, - tal_count(failonionreply->contents)); + db_bind_talarr(stmt, 0, failonionreply->contents); else db_bind_null(stmt, 0); - db_bind_int(stmt, 1, faildestperm ? 1 : 0); db_bind_int(stmt, 2, failindex); db_bind_int(stmt, 3, (int) failcode); @@ -3002,10 +2985,7 @@ void wallet_payment_set_failinfo(struct wallet *wallet, db_bind_null(stmt, 8); } - if (failupdate) - db_bind_blob(stmt, 6, failupdate, tal_count(failupdate)); - else - db_bind_null(stmt, 6); + db_bind_talarr(stmt, 6, failupdate); if (faildetail != NULL) db_bind_text(stmt, 7, faildetail); @@ -3308,7 +3288,7 @@ void wallet_utxoset_add(struct wallet *w, const struct bitcoin_tx *tx, db_bind_int(stmt, 2, blockheight); db_bind_null(stmt, 3); db_bind_int(stmt, 4, txindex); - db_bind_blob(stmt, 5, scriptpubkey, tal_count(scriptpubkey)); + db_bind_talarr(stmt, 5, scriptpubkey); db_bind_amount_sat(stmt, 6, &sat); db_exec_prepared_v2(take(stmt)); @@ -3346,8 +3326,7 @@ void wallet_filteredblock_add(struct wallet *w, const struct filteredblock *fb) db_bind_int(stmt, 2, fb->height); db_bind_null(stmt, 3); db_bind_int(stmt, 4, o->txindex); - db_bind_blob(stmt, 5, o->scriptPubKey, - tal_count(o->scriptPubKey)); + db_bind_talarr(stmt, 5, o->scriptPubKey); db_bind_amount_sat(stmt, 6, &o->amount); db_exec_prepared_v2(take(stmt));