From 09247d4f957e325ad2d66a44e3ba131c80e4b27e Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Wed, 18 Dec 2019 19:26:23 +0100 Subject: [PATCH] db: Add tracking of whether the current transaction is dirty --- wallet/db.c | 13 +++++++++++++ wallet/db_common.h | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/wallet/db.c b/wallet/db.c index 49951effe..811fd29fe 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -763,6 +763,11 @@ static void db_report_changes(struct db *db, const char *final, size_t min) assert(db->changes); assert(tal_count(db->changes) >= min); + /* Having changes implies that we have a dirty TX. The opposite is + * currently not true, e.g., the postgres driver doesn't record + * changes yet. */ + assert(!tal_count(db->changes) || db->dirty); + if (tal_count(db->changes) > min) plugin_hook_db_sync(db); db->changes = tal_free(db->changes); @@ -785,6 +790,9 @@ void db_begin_transaction_(struct db *db, const char *location) if (db->in_transaction) db_fatal("Already in transaction from %s", db->in_transaction); + /* No writes yet. */ + db->dirty = false; + db_prepare_for_changes(db); ok = db->config->begin_tx_fn(db); if (!ok) @@ -805,6 +813,7 @@ void db_commit_transaction(struct db *db) db_fatal("Failed to commit DB transaction: %s", db->error); db->in_transaction = NULL; + db->dirty = false; } static struct db_config *db_config_find(const char *dsn) @@ -1357,6 +1366,10 @@ void db_column_txid(struct db_stmt *stmt, int pos, struct bitcoin_txid *t) bool db_exec_prepared_v2(struct db_stmt *stmt TAKES) { bool ret = stmt->db->config->exec_fn(stmt); + + /* If this was a write we need to bump the data_version upon commit. */ + stmt->db->dirty = stmt->db->dirty || !stmt->query->readonly; + stmt->executed = true; list_del_from(&stmt->db->pending_statements, &stmt->list); diff --git a/wallet/db_common.h b/wallet/db_common.h index 0fa1a09ed..3d56d6b73 100644 --- a/wallet/db_common.h +++ b/wallet/db_common.h @@ -30,6 +30,10 @@ struct db { char *error; struct log *log; + + /* Were there any modifying statements in the current transaction? + * Used to bump the data_version in the DB.*/ + bool dirty; }; struct db_query {