wallet: Implement channel_config persistence

This commit is contained in:
Christian Decker 2017-08-05 14:45:42 +02:00 committed by Rusty Russell
parent 6f441d3cd2
commit cc5af13e05
4 changed files with 76 additions and 0 deletions

View File

@ -32,6 +32,8 @@
* * [`2`:`max_accepted_htlcs`]
*/
struct channel_config {
/* Database ID */
u64 id;
u64 dust_limit_satoshis;
u64 max_htlc_value_in_flight_msat;
u64 channel_reserve_satoshis;

View File

@ -80,6 +80,16 @@ char *dbmigrations[] = {
" address TEXT,"
" PRIMARY KEY (id)"
");",
"CREATE TABLE channel_configs ("
" id INTEGER,"
" dust_limit_satoshis INTEGER,"
" max_htlc_value_in_flight_msat INTEGER,"
" channel_reserve_satoshis INTEGER,"
" htlc_minimum_msat INTEGER,"
" to_self_delay INTEGER,"
" max_accepted_htlcs INTEGER,"
" PRIMARY KEY (id)"
");",
NULL,
};

View File

@ -558,6 +558,58 @@ static char* db_serialize_pubkey(const tal_t *ctx, struct pubkey *pk)
return tal_hex(ctx, der);
}
bool wallet_channel_config_save(struct wallet *w, struct channel_config *cc)
{
bool ok = true;
/* Is this an update? If not insert a stub first */
if (!cc->id) {
ok &= db_exec(__func__, w->db,
"INSERT INTO channel_configs DEFAULT VALUES;");
cc->id = sqlite3_last_insert_rowid(w->db->sql);
}
ok &= db_exec(
__func__, w->db, "UPDATE channel_configs SET"
" dust_limit_satoshis=%" PRIu64 ","
" max_htlc_value_in_flight_msat=%" PRIu64 ","
" channel_reserve_satoshis=%" PRIu64 ","
" htlc_minimum_msat=%" PRIu64 ","
" to_self_delay=%d,"
" max_accepted_htlcs=%d"
" WHERE id=%" PRIu64 ";",
cc->dust_limit_satoshis, cc->max_htlc_value_in_flight_msat,
cc->channel_reserve_satoshis, cc->htlc_minimum_msat,
cc->to_self_delay, cc->max_accepted_htlcs, cc->id);
return ok;
}
bool wallet_channel_config_load(struct wallet *w, const u64 id,
struct channel_config *cc)
{
bool ok = true;
int col = 1;
const char *query =
"SELECT id, dust_limit_satoshis, max_htlc_value_in_flight_msat, "
"channel_reserve_satoshis, htlc_minimum_msat, to_self_delay, "
"max_accepted_htlcs FROM channel_configs WHERE id=%" PRIu64 ";";
sqlite3_stmt *stmt = db_query(__func__, w->db, query, id);
if (!stmt || sqlite3_step(stmt) != SQLITE_ROW) {
sqlite3_finalize(stmt);
return false;
}
cc->id = id;
cc->dust_limit_satoshis = sqlite3_column_int64(stmt, col++);
cc->max_htlc_value_in_flight_msat = sqlite3_column_int64(stmt, col++);
cc->channel_reserve_satoshis = sqlite3_column_int64(stmt, col++);
cc->htlc_minimum_msat = sqlite3_column_int64(stmt, col++);
cc->to_self_delay = sqlite3_column_int(stmt, col++);
cc->max_accepted_htlcs = sqlite3_column_int(stmt, col++);
assert(col == 7);
sqlite3_finalize(stmt);
return ok;
}
bool wallet_channel_save(struct wallet *w, struct wallet_channel *chan){
bool ok = true;
struct peer *p = chan->peer;

View File

@ -5,6 +5,7 @@
#include "db.h"
#include <ccan/crypto/shachain/shachain.h>
#include <ccan/tal/tal.h>
#include <lightningd/channel_config.h>
#include <lightningd/utxo.h>
#include <wally_bip32.h>
@ -173,4 +174,15 @@ bool wallet_channel_load(struct wallet *w, const u64 id,
* insert)
*/
bool wallet_channel_save(struct wallet *w, struct wallet_channel *chan);
/**
* wallet_channel_config_save -- Upsert a channel_config into the database
*/
bool wallet_channel_config_save(struct wallet *w, struct channel_config *cc);
/**
* wallet_channel_config_load -- Load channel_config from database into cc
*/
bool wallet_channel_config_load(struct wallet *w, const u64 id,
struct channel_config *cc);
#endif /* WALLET_WALLET_H */