Browse Source

wallet: Migrate output tracking to native sqlite3 binding

Signed-off-by: Christian Decker <decker.christian@gmail.com>
ppa-0.6.1
Christian Decker 7 years ago
committed by Rusty Russell
parent
commit
8f198f3746
  1. 62
      wallet/wallet.c

62
wallet/wallet.c

@ -28,16 +28,16 @@ struct wallet *wallet_new(const tal_t *ctx, struct log *log)
bool wallet_add_utxo(struct wallet *w, struct utxo *utxo, bool wallet_add_utxo(struct wallet *w, struct utxo *utxo,
enum wallet_output_type type) enum wallet_output_type type)
{ {
tal_t *tmpctx = tal_tmpctx(w); sqlite3_stmt *stmt;
char *hextxid = tal_hexstr(tmpctx, &utxo->txid, 32);
bool result = db_exec( stmt = db_prepare(w->db, "INSERT INTO outputs (prev_out_tx, prev_out_index, value, type, status, keyindex) VALUES (?, ?, ?, ?, ?, ?);");
__func__, w->db, sqlite3_bind_blob(stmt, 1, &utxo->txid, sizeof(utxo->txid), SQLITE_TRANSIENT);
"INSERT INTO outputs (prev_out_tx, prev_out_index, value, type, " sqlite3_bind_int(stmt, 2, utxo->outnum);
"status, keyindex) VALUES ('%s', %d, %"PRIu64", %d, %d, %d);", sqlite3_bind_int64(stmt, 3, utxo->amount);
hextxid, utxo->outnum, utxo->amount, type, output_state_available, sqlite3_bind_int(stmt, 4, type);
utxo->keyindex); sqlite3_bind_int(stmt, 5, output_state_available);
tal_free(tmpctx); sqlite3_bind_int(stmt, 6, utxo->keyindex);
return result; return db_exec_prepared(w->db, stmt);
} }
/** /**
@ -47,8 +47,7 @@ bool wallet_add_utxo(struct wallet *w, struct utxo *utxo,
*/ */
static bool wallet_stmt2output(sqlite3_stmt *stmt, struct utxo *utxo) static bool wallet_stmt2output(sqlite3_stmt *stmt, struct utxo *utxo)
{ {
const unsigned char *hextxid = sqlite3_column_text(stmt, 0); memcpy(&utxo->txid, sqlite3_column_blob(stmt, 0), sqlite3_column_bytes(stmt, 0));
hex_decode((const char*)hextxid, sizeof(utxo->txid) * 2, &utxo->txid, sizeof(utxo->txid));
utxo->outnum = sqlite3_column_int(stmt, 1); utxo->outnum = sqlite3_column_int(stmt, 1);
utxo->amount = sqlite3_column_int(stmt, 2); utxo->amount = sqlite3_column_int(stmt, 2);
utxo->is_p2sh = sqlite3_column_int(stmt, 3) == p2sh_wpkh; utxo->is_p2sh = sqlite3_column_int(stmt, 3) == p2sh_wpkh;
@ -62,22 +61,22 @@ bool wallet_update_output_status(struct wallet *w,
const u32 outnum, enum output_status oldstatus, const u32 outnum, enum output_status oldstatus,
enum output_status newstatus) enum output_status newstatus)
{ {
tal_t *tmpctx = tal_tmpctx(w); sqlite3_stmt *stmt;
char *hextxid = tal_hexstr(tmpctx, txid, sizeof(*txid));
if (oldstatus != output_state_any) { if (oldstatus != output_state_any) {
db_exec(__func__, w->db, stmt = db_prepare(
"UPDATE outputs SET status=%d WHERE status=%d " w->db, "UPDATE outputs SET status=? WHERE status=? AND prev_out_tx=? AND prev_out_index=?");
"AND prev_out_tx = '%s' AND prev_out_index = " sqlite3_bind_int(stmt, 1, newstatus);
"%d;", sqlite3_bind_int(stmt, 2, oldstatus);
newstatus, oldstatus, hextxid, outnum); sqlite3_bind_blob(stmt, 3, txid, sizeof(*txid), SQLITE_TRANSIENT);
sqlite3_bind_int(stmt, 4, outnum);
} else { } else {
db_exec(__func__, w->db, stmt = db_prepare(
"UPDATE outputs SET status=%d WHERE " w->db, "UPDATE outputs SET status=? WHERE prev_out_tx=? AND prev_out_index=?");
"prev_out_tx = '%s' AND prev_out_index = " sqlite3_bind_int(stmt, 1, newstatus);
"%d;", sqlite3_bind_blob(stmt, 2, txid, sizeof(*txid), SQLITE_TRANSIENT);
newstatus, hextxid, outnum); sqlite3_bind_int(stmt, 3, outnum);
} }
tal_free(tmpctx); db_exec_prepared(w->db, stmt);
return sqlite3_changes(w->db->sql) > 0; return sqlite3_changes(w->db->sql) > 0;
} }
@ -85,16 +84,13 @@ struct utxo **wallet_get_utxos(const tal_t *ctx, struct wallet *w, const enum ou
{ {
struct utxo **results; struct utxo **results;
int i; int i;
sqlite3_stmt *stmt =
db_query(__func__, w->db, "SELECT prev_out_tx, prev_out_index, "
"value, type, status, keyindex FROM "
"outputs WHERE status=%d OR %d=255",
state, state);
if (!stmt) sqlite3_stmt *stmt = db_prepare(
return NULL; w->db, "SELECT prev_out_tx, prev_out_index, value, type, status, keyindex "
"FROM outputs WHERE status=?1 OR ?1=255");
sqlite3_bind_int(stmt, 1, state);
results = tal_arr(ctx, struct utxo*, 0); results = tal_arr(ctx, struct utxo*, 0);
for (i=0; sqlite3_step(stmt) == SQLITE_ROW; i++) { for (i=0; sqlite3_step(stmt) == SQLITE_ROW; i++) {
tal_resize(&results, i+1); tal_resize(&results, i+1);
results[i] = tal(results, struct utxo); results[i] = tal(results, struct utxo);

Loading…
Cancel
Save