Browse Source

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>
ppa-0.6.1
Rusty Russell 7 years ago
committed by Christian Decker
parent
commit
3282768302
  1. 29
      wallet/db.c
  2. 4
      wallet/db_tests.c
  3. 3
      wallet/wallet.c
  4. 30
      wallet/wallet_tests.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;
}

4
wallet/db_tests.c

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

3
wallet/wallet.c

@ -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;
}

30
wallet/wallet_tests.c

@ -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…
Cancel
Save