mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-02-22 06:41:44 +01:00
wallet: don't fail, assume db ops will call fatal.
And override fatal() in wallet_tests to be sure. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
1eb7e92a30
commit
3282768302
4 changed files with 34 additions and 32 deletions
29
wallet/db.c
29
wallet/db.c
|
@ -363,47 +363,30 @@ static int db_migration_count(void)
|
|||
/**
|
||||
* db_migrate - Apply all remaining migrations from the current version
|
||||
*/
|
||||
static bool db_migrate(struct db *db)
|
||||
static void db_migrate(struct db *db)
|
||||
{
|
||||
/* Attempt to read the version from the database */
|
||||
int current, available;
|
||||
|
||||
if (!db_begin_transaction(db)) {
|
||||
/* No need to rollback, we didn't even start... */
|
||||
return false;
|
||||
}
|
||||
db_begin_transaction(db);
|
||||
|
||||
current = db_get_version(db);
|
||||
available = db_migration_count();
|
||||
|
||||
while (++current <= available) {
|
||||
if (!db_exec(__func__, db, "%s", dbmigrations[current]))
|
||||
goto fail;
|
||||
}
|
||||
while (++current <= available)
|
||||
db_exec(__func__, db, "%s", dbmigrations[current]);
|
||||
|
||||
/* Finally update the version number in the version table */
|
||||
db_exec(__func__, db, "UPDATE version SET version=%d;", available);
|
||||
|
||||
if (!db_commit_transaction(db)) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
return true;
|
||||
fail:
|
||||
db_rollback_transaction(db);
|
||||
return false;
|
||||
db_commit_transaction(db);
|
||||
}
|
||||
|
||||
struct db *db_setup(const tal_t *ctx)
|
||||
{
|
||||
struct db *db = db_open(ctx, DB_FILE);
|
||||
if (!db) {
|
||||
return db;
|
||||
}
|
||||
|
||||
if (!db_migrate(db)) {
|
||||
return tal_free(db);
|
||||
}
|
||||
db_migrate(db);
|
||||
return db;
|
||||
}
|
||||
|
||||
|
|
|
@ -24,7 +24,7 @@ static bool test_empty_db_migrate(void)
|
|||
struct db *db = create_test_db(__func__);
|
||||
CHECK(db);
|
||||
CHECK(db_get_version(db) == -1);
|
||||
CHECK(db_migrate(db));
|
||||
db_migrate(db);
|
||||
CHECK(db_get_version(db) == db_migration_count());
|
||||
|
||||
tal_free(db);
|
||||
|
@ -55,7 +55,7 @@ static bool test_vars(void)
|
|||
struct db *db = create_test_db(__func__);
|
||||
char *varname = "testvar";
|
||||
CHECK(db);
|
||||
CHECK(db_migrate(db));
|
||||
db_migrate(db);
|
||||
|
||||
/* Check default behavior */
|
||||
CHECK(db_get_intvar(db, varname, 42) == 42);
|
||||
|
|
|
@ -18,9 +18,6 @@ struct wallet *wallet_new(const tal_t *ctx, struct log *log)
|
|||
wallet->db = db_setup(wallet);
|
||||
wallet->log = log;
|
||||
wallet->bip32_base = NULL;
|
||||
if (!wallet->db) {
|
||||
fatal("Unable to setup the wallet database");
|
||||
}
|
||||
return wallet;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,13 +1,32 @@
|
|||
#include <lightningd/log.h>
|
||||
|
||||
static void wallet_fatal(const char *fmt, ...);
|
||||
#define fatal wallet_fatal
|
||||
|
||||
#include "wallet.c"
|
||||
|
||||
#include "db.c"
|
||||
|
||||
#include <ccan/mem/mem.h>
|
||||
#include <lightningd/log.h>
|
||||
#include <ccan/tal/str/str.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <wallet/test_utils.h>
|
||||
|
||||
static char *wallet_err;
|
||||
static void wallet_fatal(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
/* Fail hard if we're complaining about not being in transaction */
|
||||
assert(!strstarts(fmt, "No longer in transaction"));
|
||||
|
||||
va_start(ap, fmt);
|
||||
wallet_err = tal_vfmt(NULL, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void invoice_add(struct invoices *invs,
|
||||
struct invoice *inv){}
|
||||
|
||||
|
@ -37,7 +56,8 @@ static struct wallet *create_test_wallet(const tal_t *ctx)
|
|||
w->db = db_open(w, filename);
|
||||
|
||||
CHECK_MSG(w->db, "Failed opening the db");
|
||||
CHECK_MSG(db_migrate(w->db), "DB migration failed");
|
||||
db_migrate(w->db);
|
||||
CHECK_MSG(!wallet_err, "DB migration failed");
|
||||
|
||||
ltmp = tal_tmpctx(ctx);
|
||||
log_book = new_log_book(w, 20*1024*1024, LOG_DBG);
|
||||
|
@ -57,7 +77,8 @@ static bool test_wallet_outputs(void)
|
|||
|
||||
w->db = db_open(w, filename);
|
||||
CHECK_MSG(w->db, "Failed opening the db");
|
||||
CHECK_MSG(db_migrate(w->db), "DB migration failed");
|
||||
db_migrate(w->db);
|
||||
CHECK_MSG(!wallet_err, "DB migration failed");
|
||||
|
||||
memset(&u, 0, sizeof(u));
|
||||
|
||||
|
@ -108,7 +129,8 @@ static bool test_shachain_crud(void)
|
|||
|
||||
w->db = db_open(w, filename);
|
||||
CHECK_MSG(w->db, "Failed opening the db");
|
||||
CHECK_MSG(db_migrate(w->db), "DB migration failed");
|
||||
db_migrate(w->db);
|
||||
CHECK_MSG(!wallet_err, "DB migration failed");
|
||||
|
||||
CHECK_MSG(fd != -1, "Unable to generate temp filename");
|
||||
close(fd);
|
||||
|
|
Loading…
Add table
Reference in a new issue