Skip to content

Commit

Permalink
lightningd/runes: update last_used timestamp when a rune successfully…
Browse files Browse the repository at this point in the history
… used
  • Loading branch information
ShahanaFarooqui authored and rustyrussell committed Sep 21, 2023
1 parent eacf0b5 commit db55dea
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lightningd/runes.c
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,12 @@ static const char *check_condition(const tal_t *ctx,
ptok->end - ptok->start);
}

static void update_rune_usage_time(struct runes *runes,
struct rune *rune)
{
wallet_rune_update_last_used(runes->ld->wallet, rune, time_now());
}

static struct command_result *json_checkrune(struct command *cmd,
const char *buffer,
const jsmntok_t *obj UNNEEDED,
Expand Down Expand Up @@ -835,6 +841,7 @@ static struct command_result *json_checkrune(struct command *cmd,
/* If it succeeded, *now* we increment any associated usage counter. */
if (cinfo.usage)
cinfo.usage->counter++;
update_rune_usage_time(cmd->ld->runes, ras->rune);

js = json_stream_success(cmd);
json_add_bool(js, "valid", true);
Expand Down
17 changes: 16 additions & 1 deletion wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -5753,6 +5753,20 @@ void wallet_rune_insert(struct wallet *wallet, const struct rune *rune)
db_rune_insert(wallet->db, rune);
}

void wallet_rune_update_last_used(struct wallet *wallet, const struct rune *rune, struct timeabs last_used)
{
struct db_stmt *stmt;
struct timerel t;

t.ts = last_used.ts;
stmt = db_prepare_v2(wallet->db,
SQL("UPDATE runes SET last_used_nsec = ? WHERE id = ?;"));
db_bind_u64(stmt, time_to_nsec(t));
db_bind_u64(stmt, rune_unique_id(rune));
db_exec_prepared_v2(stmt);
tal_free(stmt);
}

static void db_insert_blacklist(struct db *db,
const struct rune_blacklist *entry)
{
Expand Down Expand Up @@ -5852,7 +5866,8 @@ void migrate_datastore_commando_runes(struct lightningd *ld, struct db *db)
void migrate_runes_idfix(struct lightningd *ld, struct db *db)
{
/* ID fields were wrong. Pull them all out and put them back */
const char **runes = db_get_runes(tmpctx, db, NULL);
struct timeabs *last_used;
const char **runes = db_get_runes(tmpctx, db, &last_used);
struct db_stmt *stmt;

stmt = db_prepare_v2(db, SQL("DELETE FROM runes;"));
Expand Down
9 changes: 9 additions & 0 deletions wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -1607,6 +1607,15 @@ const char **wallet_get_runes(const tal_t *ctx, struct wallet *wallet, struct ti
*/
void wallet_rune_insert(struct wallet *wallet, const struct rune *rune);

/**
* wallet_rune_update_last_used -- Update the timestamp on an existing rune
*
* @wallet: the wallet to save into
* @rune: the instance to store
* @last_used: now
*/
void wallet_rune_update_last_used(struct wallet *wallet, const struct rune *rune, struct timeabs last_used);

/* Load the runes blacklist */
struct rune_blacklist {
u64 start, end;
Expand Down

0 comments on commit db55dea

Please sign in to comment.