Browse Source

db: avoid the only failing db_exec.

Replace with an explicit test for existence.  This simplifies our db interface.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
pr-2587
Rusty Russell 6 years ago
parent
commit
8c4ef7f059
  1. 15
      wallet/db.c
  2. 9
      wallet/db.h
  3. 19
      wallet/wallet.c

15
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, ...)
{

9
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);

19
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;
}
/**

Loading…
Cancel
Save