From 8c4ef7f05972751b5dd4741a4f304790c9762234 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 15 Mar 2019 13:19:18 +1030 Subject: [PATCH] db: avoid the only failing db_exec. Replace with an explicit test for existence. This simplifies our db interface. Signed-off-by: Rusty Russell --- wallet/db.c | 15 --------------- wallet/db.h | 9 --------- wallet/wallet.c | 19 +++++++++++++++---- 3 files changed, 15 insertions(+), 28 deletions(-) diff --git a/wallet/db.c b/wallet/db.c index e3fc671f3..94a300e72 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -496,21 +496,6 @@ static void PRINTF_FMT(3, 4) tal_free(cmd); } -bool db_exec_prepared_mayfail_(const char *caller UNUSED, struct db *db, sqlite3_stmt *stmt) -{ - assert(db->in_transaction); - - if (sqlite3_step(stmt) != SQLITE_DONE) { - goto fail; - } - - db_stmt_done(stmt); - return true; -fail: - db_stmt_done(stmt); - return false; -} - sqlite3_stmt *PRINTF_FMT(3, 4) db_query_(const char *location, struct db *db, const char *fmt, ...) { diff --git a/wallet/db.h b/wallet/db.h index 7e513aec9..2286fefb7 100644 --- a/wallet/db.h +++ b/wallet/db.h @@ -109,15 +109,6 @@ sqlite3_stmt *db_prepare_(const char *location, struct db *db, const char *query #define db_exec_prepared(db,stmt) db_exec_prepared_(__func__,db,stmt) void db_exec_prepared_(const char *caller, struct db *db, sqlite3_stmt *stmt); -/** - * db_exec_prepared_mayfail - db_exec_prepared, but don't fatal() it fails. - */ -#define db_exec_prepared_mayfail(db,stmt) \ - db_exec_prepared_mayfail_(__func__,db,stmt) -bool db_exec_prepared_mayfail_(const char *caller, - struct db *db, - sqlite3_stmt *stmt); - /* Wrapper around sqlite3_finalize(), for tracking statements. */ void db_stmt_done(sqlite3_stmt *stmt); diff --git a/wallet/wallet.c b/wallet/wallet.c index ad7e88f1c..e6af28b2a 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -67,12 +67,24 @@ struct wallet *wallet_new(struct lightningd *ld, "channel_id, peer_id, commitment_point, confirmation_height, " \ "spend_height, scriptpubkey" -/* We actually use the db constraints to uniquify, so OK if this fails. */ +/* This can fail if we've already seen UTXO. */ bool wallet_add_utxo(struct wallet *w, struct utxo *utxo, enum wallet_output_type type) { sqlite3_stmt *stmt; + stmt = db_prepare(w->db, + "SELECT * from outputs WHERE prev_out_tx=? AND prev_out_index=?"); + sqlite3_bind_blob(stmt, 1, &utxo->txid, sizeof(utxo->txid), SQLITE_TRANSIENT); + sqlite3_bind_int(stmt, 2, utxo->outnum); + + /* If we get a result, that means a clash. */ + if (sqlite3_step(stmt) == SQLITE_ROW) { + db_stmt_done(stmt); + return false; + } + db_stmt_done(stmt); + stmt = db_prepare(w->db, "INSERT INTO outputs (" UTXO_FIELDS ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"); @@ -109,9 +121,8 @@ bool wallet_add_utxo(struct wallet *w, struct utxo *utxo, else sqlite3_bind_null(stmt, 12); - /* May fail if we already know about the tx, e.g., because - * it's change or some internal tx. */ - return db_exec_prepared_mayfail(w->db, stmt); + db_exec_prepared(w->db, stmt); + return true; } /**