wallet: make static, not dynamic decision to insert for everything.

Since we create new entries from wallet_channel_insert(), there's no
need for the branches.  And indeed, many wallet functions can be
static.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2018-02-18 23:24:46 +10:30
parent 72108f0cb9
commit 3c0be71d37
3 changed files with 29 additions and 25 deletions

View File

@ -999,10 +999,11 @@ static bool test_channel_config_crud(struct lightningd *ld, const tal_t *ctx)
cc1->to_self_delay = 5;
cc1->max_accepted_htlcs = 6;
CHECK(transaction_wrap(w->db, wallet_channel_config_save(w, cc1)));
CHECK(transaction_wrap(w->db, wallet_channel_config_insert(w, cc1)));
CHECK_MSG(
cc1->id == 1,
tal_fmt(ctx, "channel_config->id != 1; got %" PRIu64, cc1->id));
CHECK(transaction_wrap(w->db, wallet_channel_config_save(w, cc1)));
CHECK(transaction_wrap(w->db, wallet_channel_config_load(w, cc1->id, cc2)));
CHECK(memeq(cc1, sizeof(*cc1), cc2, sizeof(*cc2)));

View File

@ -316,9 +316,13 @@ s64 wallet_get_newindex(struct lightningd *ld)
return newidx;
}
void wallet_shachain_init(struct wallet *wallet, struct wallet_shachain *chain)
static void wallet_shachain_init(struct wallet *wallet,
struct wallet_shachain *chain)
{
sqlite3_stmt *stmt;
assert(chain->id == 0);
/* Create shachain */
shachain_init(&chain->chain);
stmt = db_prepare(wallet->db, "INSERT INTO shachains (min_index, num_valid) VALUES (?, 0);");
@ -730,17 +734,24 @@ u32 wallet_first_blocknum(struct wallet *w, u32 first_possible)
return first_channel;
}
void wallet_channel_config_save(struct wallet *w, struct channel_config *cc)
static void wallet_channel_config_insert(struct wallet *w,
struct channel_config *cc)
{
sqlite3_stmt *stmt;
/* Is this an update? If not insert a stub first */
if (!cc->id) {
stmt = db_prepare(
w->db,"INSERT INTO channel_configs DEFAULT VALUES;");
db_exec_prepared(w->db, stmt);
cc->id = sqlite3_last_insert_rowid(w->db->sql);
}
assert(cc->id == 0);
stmt = db_prepare(w->db, "INSERT INTO channel_configs DEFAULT VALUES;");
db_exec_prepared(w->db, stmt);
cc->id = sqlite3_last_insert_rowid(w->db->sql);
}
static void wallet_channel_config_save(struct wallet *w,
const struct channel_config *cc)
{
sqlite3_stmt *stmt;
assert(cc->id != 0);
stmt = db_prepare(w->db, "UPDATE channel_configs SET"
" dust_limit_satoshis=?,"
" max_htlc_value_in_flight_msat=?,"
@ -796,11 +807,6 @@ void wallet_channel_save(struct wallet *w, struct channel *chan)
sqlite3_stmt *stmt;
assert(chan->first_blocknum);
/* Need to initialize the shachain first so we get an id */
if (chan->their_shachain.id == 0) {
wallet_shachain_init(w, &chan->their_shachain);
}
wallet_channel_config_save(w, &chan->our_config);
stmt = db_prepare(w->db, "UPDATE channels SET"
@ -864,6 +870,8 @@ void wallet_channel_save(struct wallet *w, struct channel *chan)
db_exec_prepared(w->db, stmt);
if (chan->channel_info) {
if (chan->channel_info->their_config.id == 0)
wallet_channel_config_insert(w, &chan->channel_info->their_config);
wallet_channel_config_save(w, &chan->channel_info->their_config);
stmt = db_prepare(w->db, "UPDATE channels SET"
" fundingkey_remote=?,"
@ -934,6 +942,11 @@ void wallet_channel_insert(struct wallet *w, struct channel *chan)
sqlite3_bind_int(stmt, 3, chan->dbid);
db_exec_prepared(w->db, stmt);
wallet_channel_config_insert(w, &chan->our_config);
if (chan->channel_info)
wallet_channel_config_insert(w, &chan->channel_info->their_config);
wallet_shachain_init(w, &chan->their_shachain);
/* Now save path as normal */
wallet_channel_save(w, chan);
tal_free(tmpctx);

View File

@ -183,11 +183,6 @@ bool wallet_can_spend(struct wallet *w, const u8 *script,
*/
s64 wallet_get_newindex(struct lightningd *ld);
/**
* wallet_shachain_init -- wallet wrapper around shachain_init
*/
void wallet_shachain_init(struct wallet *wallet, struct wallet_shachain *chain);
/**
* wallet_shachain_add_hash -- wallet wrapper around shachain_add_hash
*/
@ -241,11 +236,6 @@ void wallet_channel_delete(struct wallet *w, u64 wallet_id);
*/
void wallet_peer_delete(struct wallet *w, u64 peer_dbid);
/**
* wallet_channel_config_save -- Upsert a channel_config into the database
*/
void wallet_channel_config_save(struct wallet *w, struct channel_config *cc);
/**
* wallet_channel_config_load -- Load channel_config from database into cc
*/