Skip to content

Persistence transaction tracking in the database #1364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 12, 2018
10 changes: 10 additions & 0 deletions wallet/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,16 @@ char *dbmigrations[] = {
" , msatoshi_to_us_max = msatoshi_local"
" ;",
/* -- Min and max msatoshi_to_us ends -- */
/* Transactions we are interested in. Either we sent them ourselves or we
* are watching them. We don't cascade block height deletes so we don't
* forget any of them by accident.*/
"CREATE TABLE transactions ("
" id BLOB"
", blockheight INTEGER REFERENCES blocks(height) ON DELETE SET NULL"
", txindex INTEGER"
", rawtx BLOB"
", PRIMARY KEY (id)"
");",
NULL,
};

Expand Down
43 changes: 43 additions & 0 deletions wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -2158,3 +2158,46 @@ struct outpoint *wallet_outpoint_for_scid(struct wallet *w, tal_t *ctx,

return op;
}

void wallet_transaction_add(struct wallet *w, const struct bitcoin_tx *tx,
const u32 blockheight, const u32 txindex)
{
struct bitcoin_txid txid;
sqlite3_stmt *stmt = db_prepare(w->db, "SELECT blockheight FROM transactions WHERE id=?");
bool known;

bitcoin_txid(tx, &txid);
sqlite3_bind_sha256(stmt, 1, &txid.shad.sha);
known = sqlite3_step(stmt) == SQLITE_ROW;
sqlite3_finalize(stmt);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a debugging check, in which case it'd be better to check that the other fields haven't changed? Plus add a /*FIXME: change to INSERT OR UPDATE */ for later optimization?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I get the question. I could change this to become SELECT count(*) FROM transactions WHERE id=? and then check that I get a 1 as a result, but the effect is the same, either I have a row or I don't, and depending on which it is I need to insert or update.

Or are you saying we should fetch all fields and check that they are identical?


if (!known) {
/* This transaction is still unknown, insert */
stmt = db_prepare(w->db,
"INSERT INTO transactions ("
" id"
", blockheight"
", txindex"
", rawtx) VALUES (?, ?, ?, ?);");
sqlite3_bind_sha256(stmt, 1, &txid.shad.sha);
if (blockheight) {
sqlite3_bind_int(stmt, 2, blockheight);
sqlite3_bind_int(stmt, 3, txindex);
} else {
sqlite3_bind_null(stmt, 2);
sqlite3_bind_null(stmt, 3);
}
sqlite3_bind_tx(stmt, 4, tx);
db_exec_prepared(w->db, stmt);
} else if (blockheight){
/* We know about the transaction, update */
stmt = db_prepare(w->db,
"UPDATE transactions "
"SET blockheight = ?, txindex = ? "
"WHERE id = ?");
sqlite3_bind_int(stmt, 1, blockheight);
sqlite3_bind_int(stmt, 2, txindex);
sqlite3_bind_sha256(stmt, 3, &txid.shad.sha);
db_exec_prepared(w->db, stmt);
}
}
4 changes: 4 additions & 0 deletions wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -810,4 +810,8 @@ void wallet_utxoset_add(struct wallet *w, const struct bitcoin_tx *tx,
const u32 outnum, const u32 blockheight,
const u32 txindex, const u8 *scriptpubkey,
const u64 satoshis);

void wallet_transaction_add(struct wallet *w, const struct bitcoin_tx *tx,
const u32 blockheight, const u32 txindex);

#endif /* LIGHTNING_WALLET_WALLET_H */