mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-19 07:04:22 +01:00
Node ids are pubkeys, but we only use them as pubkeys for routing and checking gossip messages. So we're packing and unpacking them constantly, and wasting some space and time. This introduces a new type, explicitly the SEC1 compressed encoding (33 bytes). We ensure its validity when we load from the db, or get it from JSON. We still use 'struct pubkey' for peer messages, which checks validity. Results from 5 runs, min-max(mean +/- stddev): store_load_msec,vsz_kb,store_rewrite_sec,listnodes_sec,listchannels_sec,routing_sec,peer_write_all_sec 39475-39572(39518+/-36),2880732,41.150000-41.390000(41.298+/-0.085),2.260000-2.550000(2.336+/-0.11),44.390000-65.150000(58.648+/-7.5),32.740000-33.020000(32.89+/-0.093),44.130000-45.090000(44.566+/-0.32) Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
141 lines
3.1 KiB
C
141 lines
3.1 KiB
C
#include <lightningd/log.h>
|
|
|
|
static void db_test_fatal(const char *fmt, ...);
|
|
#define db_fatal db_test_fatal
|
|
|
|
static void db_log_(struct log *log UNUSED, enum log_level level UNUSED, const char *fmt UNUSED, ...)
|
|
{
|
|
}
|
|
#define log_ db_log_
|
|
|
|
#include "wallet/db.c"
|
|
|
|
#include "test_utils.h"
|
|
|
|
#include <common/amount.h>
|
|
#include <common/memleak.h>
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
|
|
/* AUTOGENERATED MOCKS START */
|
|
/* Generated stub for json_escaped_string_ */
|
|
struct json_escaped *json_escaped_string_(const tal_t *ctx UNNEEDED,
|
|
const void *bytes UNNEEDED, size_t len UNNEEDED)
|
|
{ fprintf(stderr, "json_escaped_string_ called!\n"); abort(); }
|
|
/* Generated stub for node_id_valid */
|
|
bool node_id_valid(const struct node_id *id UNNEEDED)
|
|
{ fprintf(stderr, "node_id_valid called!\n"); abort(); }
|
|
/* AUTOGENERATED MOCKS END */
|
|
|
|
static char *db_err;
|
|
static void db_test_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);
|
|
db_err = tal_vfmt(NULL, fmt, ap);
|
|
va_end(ap);
|
|
}
|
|
|
|
void plugin_hook_db_sync(struct db *db UNNEEDED, const char **changes UNNEEDED, const char *final UNNEEDED)
|
|
{
|
|
}
|
|
|
|
static struct db *create_test_db(void)
|
|
{
|
|
struct db *db;
|
|
char filename[] = "/tmp/ldb-XXXXXX";
|
|
|
|
int fd = mkstemp(filename);
|
|
if (fd == -1)
|
|
return NULL;
|
|
close(fd);
|
|
|
|
db = db_open(NULL, filename);
|
|
return db;
|
|
}
|
|
|
|
static bool test_empty_db_migrate(struct lightningd *ld)
|
|
{
|
|
struct db *db = create_test_db();
|
|
CHECK(db);
|
|
db_begin_transaction(db);
|
|
CHECK(db_get_version(db) == -1);
|
|
db_commit_transaction(db);
|
|
db_migrate(ld, db, NULL);
|
|
db_begin_transaction(db);
|
|
CHECK(db_get_version(db) == ARRAY_SIZE(dbmigrations) - 1);
|
|
db_commit_transaction(db);
|
|
|
|
tal_free(db);
|
|
return true;
|
|
}
|
|
|
|
static bool test_primitives(void)
|
|
{
|
|
struct db *db = create_test_db();
|
|
db_begin_transaction(db);
|
|
CHECK(db->in_transaction);
|
|
db_commit_transaction(db);
|
|
CHECK(!db->in_transaction);
|
|
db_begin_transaction(db);
|
|
db_commit_transaction(db);
|
|
|
|
db_begin_transaction(db);
|
|
db_exec(__func__, db, "SELECT name FROM sqlite_master WHERE type='table';");
|
|
CHECK_MSG(!db_err, "Simple correct SQL command");
|
|
|
|
db_exec(__func__, db, "not a valid SQL statement");
|
|
CHECK_MSG(db_err, "Failing SQL command");
|
|
db_err = tal_free(db_err);
|
|
db_commit_transaction(db);
|
|
CHECK(!db->in_transaction);
|
|
tal_free(db);
|
|
|
|
return true;
|
|
}
|
|
|
|
static bool test_vars(struct lightningd *ld)
|
|
{
|
|
struct db *db = create_test_db();
|
|
char *varname = "testvar";
|
|
CHECK(db);
|
|
db_migrate(ld, db, NULL);
|
|
|
|
db_begin_transaction(db);
|
|
/* Check default behavior */
|
|
CHECK(db_get_intvar(db, varname, 42) == 42);
|
|
|
|
/* Check setting and getting */
|
|
db_set_intvar(db, varname, 1);
|
|
CHECK(db_get_intvar(db, varname, 42) == 1);
|
|
|
|
/* Check updating */
|
|
db_set_intvar(db, varname, 2);
|
|
CHECK(db_get_intvar(db, varname, 42) == 2);
|
|
db_commit_transaction(db);
|
|
|
|
tal_free(db);
|
|
return true;
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
setup_locale();
|
|
|
|
bool ok = true;
|
|
/* Dummy for migration hooks */
|
|
struct lightningd *ld = tal(NULL, struct lightningd);
|
|
ld->config = test_config;
|
|
|
|
ok &= test_empty_db_migrate(ld);
|
|
ok &= test_vars(ld);
|
|
ok &= test_primitives();
|
|
|
|
tal_free(ld);
|
|
return !ok;
|
|
}
|