diff --git a/lightningd/channel_config.h b/lightningd/channel_config.h index 5b2ad9d6b..f28db980e 100644 --- a/lightningd/channel_config.h +++ b/lightningd/channel_config.h @@ -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; diff --git a/wallet/db.c b/wallet/db.c index 9e5540407..a5cb3ed1b 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -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, }; diff --git a/wallet/wallet.c b/wallet/wallet.c index 3ba3ed0f1..26e0676d7 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -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; diff --git a/wallet/wallet.h b/wallet/wallet.h index 50e5edbcb..00db5745a 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -5,6 +5,7 @@ #include "db.h" #include #include +#include #include #include @@ -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 */