From c4e84bcbe2c2011b29e53687f8ea31ec1b61259c Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 21 Jul 2023 09:45:19 +0930 Subject: [PATCH] db: add runes tables and accessors. Signed-off-by: Rusty Russell --- wallet/db.c | 2 ++ wallet/wallet.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ wallet/wallet.h | 30 ++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) diff --git a/wallet/db.c b/wallet/db.c index 986f9d58d..ecc982321 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -956,6 +956,8 @@ static struct migration dbmigrations[] = { {NULL, migrate_fill_in_channel_type}, {SQL("ALTER TABLE peers ADD feature_bits BLOB DEFAULT NULL;"), NULL}, {NULL, migrate_normalize_invstr}, + {SQL("CREATE TABLE runes (id BIGSERIAL, rune TEXT, PRIMARY KEY (id));"), NULL}, + {SQL("CREATE TABLE runes_blacklist (start_index BIGINT, end_index BIGINT);"), NULL}, }; /** diff --git a/wallet/wallet.c b/wallet/wallet.c index 09ee27f77..6e7e1f946 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -5465,3 +5465,55 @@ struct wallet_htlc_iter *wallet_htlcs_next(struct wallet *w, *hstate = db_col_int(iter->stmt, "h.hstate"); return iter; } + +struct rune_blacklist *wallet_get_runes_blacklist(const tal_t *ctx, struct wallet *wallet) +{ + struct db_stmt *stmt; + struct rune_blacklist *blist = tal_arr(ctx, struct rune_blacklist, 0); + + stmt = db_prepare_v2(wallet->db, SQL("SELECT start_index, end_index FROM runes_blacklist")); + db_query_prepared(stmt); + + while (db_step(stmt)) { + struct rune_blacklist b; + b.start = db_col_u64(stmt, "start_index"); + b.end = db_col_u64(stmt, "end_index"); + tal_arr_expand(&blist, b); + } + tal_free(stmt); + return blist; +} + +const char *wallet_get_rune(const tal_t *ctx, struct wallet *wallet, u64 unique_id) +{ + struct db_stmt *stmt; + const char *runestr; + + stmt = db_prepare_v2(wallet->db, SQL("SELECT rune FROM runes WHERE id = ?")); + db_bind_u64(stmt, unique_id); + db_query_prepared(stmt); + + if (db_step(stmt)) + runestr = db_col_strdup(ctx, stmt, "rune"); + else + runestr = NULL; + tal_free(stmt); + return runestr; +} + +const char **wallet_get_runes(const tal_t *ctx, struct wallet *wallet) +{ + struct db_stmt *stmt; + const char **strs = tal_arr(ctx, const char *, 0); + + stmt = db_prepare_v2(wallet->db, SQL("SELECT rune FROM runes")); + db_query_prepared(stmt); + + while (db_step(stmt)) { + const char *str = db_col_strdup(strs, stmt, "rune"); + tal_arr_expand(&strs, str); + } + tal_free(stmt); + return strs; +} + diff --git a/wallet/wallet.h b/wallet/wallet.h index 63f43d3ba..70fcf0350 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -1524,4 +1524,34 @@ struct wally_psbt *psbt_using_utxos(const tal_t *ctx, u32 nlocktime, u32 nsequence, struct wally_psbt *base); + +/** + * Get a particular runestring from the db + * @ctx: tal ctx for return to be tallocated from + * @wallet: the wallet + * @unique_id: the id of the rune. + * + * Returns NULL if it's not found. + */ +const char *wallet_get_rune(const tal_t *ctx, struct wallet *wallet, u64 unique_id); + +/** + * Get every runestring from the db + * @ctx: tal ctx for return to be tallocated from + * @wallet: the wallet + */ +const char **wallet_get_runes(const tal_t *ctx, struct wallet *wallet); + +/* Load the runes blacklist */ +struct rune_blacklist { + u64 start, end; +}; + +/** + * Load the blacklist from the db. + * @ctx: tal ctx for return to be tallocated from + * @wallet: the wallet + */ +struct rune_blacklist *wallet_get_runes_blacklist(const tal_t *ctx, struct wallet *wallet); + #endif /* LIGHTNING_WALLET_WALLET_H */