wallet: Helper to store/retrieve persistent vars to/from DB

Not the nicest code, but it allows us to store the bip32_max_index so
that we don't forget our addresses upon restart. We could have done
the same by retrieving the max index from our index, but then we'd
forget addresses that don't have an associated output. Conversion
to/from string is so that we can store arbitrary one off values in the
DB in the future, independent of type.
This commit is contained in:
Christian Decker 2017-06-01 16:10:40 +02:00 committed by Rusty Russell
parent bdb6b1002f
commit 257ecf6222
5 changed files with 81 additions and 0 deletions

View File

@ -49,6 +49,7 @@ static void json_newaddr(struct command *cmd,
ripemd160(&p2sh, h.u.u8, sizeof(h));
ld->bip32_max_index++;
db_set_intvar(ld->wallet->db, "bip32_max_index", ld->bip32_max_index);
json_object_start(response, NULL);
json_add_string(response, "address",

View File

@ -257,6 +257,8 @@ int main(int argc, char *argv[])
/* Initialize wallet, now that we are in the correct directory */
ld->wallet = wallet_new(ld, ld->log);
ld->bip32_max_index = db_get_intvar(ld->wallet->db, "bip32_max_index", 0);
/* Mark ourselves live. */
log_info(ld->log, "Hello world from %s!", version());

View File

@ -2,8 +2,10 @@
#include "daemon/log.h"
#include "lightningd/lightningd.h"
#include <ccan/tal/str/str.h>
#include <ccan/tal/tal.h>
#include <inttypes.h>
#define DB_FILE "lightningd.sqlite3"
@ -230,3 +232,40 @@ struct db *db_setup(const tal_t *ctx)
}
return db;
}
s64 db_get_intvar(struct db *db, char *varname, s64 defval)
{
int err;
s64 res = defval;
const unsigned char *stringvar;
sqlite3_stmt *stmt =
db_query(__func__, db,
"SELECT val FROM vars WHERE name='%s' LIMIT 1", varname);
if (!stmt)
return defval;
err = sqlite3_step(stmt);
if (err == SQLITE_ROW) {
stringvar = sqlite3_column_text(stmt, 0);
res = atol((const char *)stringvar);
}
sqlite3_finalize(stmt);
return res;
}
bool db_set_intvar(struct db *db, char *varname, s64 val)
{
/* Attempt to update */
db_exec(__func__, db,
"UPDATE vars SET val='%" PRId64 "' WHERE name='%s';", val,
varname);
if (sqlite3_changes(db->sql) > 0)
return true;
else
return db_exec(
__func__, db,
"INSERT INTO vars (name, val) VALUES ('%s', '%" PRId64
"');",
varname, val);
}

View File

@ -57,4 +57,20 @@ bool db_commit_transaction(struct db *db);
*/
bool db_rollback_transaction(struct db *db);
/**
* db_set_intvar - Set an integer variable in the database
*
* Utility function to store generic integer values in the
* database.
*/
bool db_set_intvar(struct db *db, char *varname, s64 val);
/**
* db_get_intvar - Retrieve an integer variable from the database
*
* Either returns the value in the database, or @defval if
* the query failed or no such variable exists.
*/
s64 db_get_intvar(struct db *db, char *varname, s64 defval);
#endif /* WALLET_DB_H */

View File

@ -31,11 +31,34 @@ static bool test_empty_db_migrate(void)
return true;
}
static bool test_vars(void)
{
struct db *db = create_test_db(__func__);
char *varname = "testvar";
CHECK(db);
CHECK(db_migrate(db));
/* Check default behavior */
CHECK(db_get_intvar(db, varname, 42) == 42);
/* Check setting and getting */
CHECK(db_set_intvar(db, varname, 1));
CHECK(db_get_intvar(db, varname, 42) == 1);
/* Check updating */
CHECK(db_set_intvar(db, varname, 2));
CHECK(db_get_intvar(db, varname, 42) == 2);
tal_free(db);
return true;
}
int main(void)
{
bool ok = true;
ok &= test_empty_db_migrate();
ok &= test_vars();
return !ok;
}