Skip to content

Commit

Permalink
db: add runes tables and accessors.
Browse files Browse the repository at this point in the history
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell committed Jul 21, 2023
1 parent 975046a commit c4e84bc
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
2 changes: 2 additions & 0 deletions wallet/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};

/**
Expand Down
52 changes: 52 additions & 0 deletions wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

30 changes: 30 additions & 0 deletions wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

0 comments on commit c4e84bc

Please sign in to comment.