Skip to content

Commit

Permalink
Update active_stake_cache_update to directly check epoch_stake_progre…
Browse files Browse the repository at this point in the history
…ss in function instead of bash
  • Loading branch information
rdlrt committed Sep 29, 2024
1 parent e2c5f16 commit c20ff68
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
3 changes: 0 additions & 3 deletions files/grest/cron/jobs/active-stake-cache-update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,4 @@ echo "$(date +%F_%H:%M:%S) Running active stake cache update..."
echo "No update needed, exiting..." &&
exit 0

next_epoch_no=$(psql ${DB_NAME} -qbt -c "SELECT MAX(epoch_no) FROM epoch_stake_progress WHERE completed='t'" | tr -cd '[:alnum:]')

psql ${DB_NAME} -qbt -c "SELECT GREST.active_stake_cache_update(${next_epoch_no});" 1>/dev/null
echo "$(date +%F_%H:%M:%S) Job done!"
20 changes: 11 additions & 9 deletions files/grest/rpc/01_cached_tables/active_stake_cache.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,29 @@ CREATE TABLE IF NOT EXISTS grest.epoch_active_stake_cache (
);

CREATE OR REPLACE FUNCTION grest.active_stake_cache_update_check()
RETURNS boolean
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
_current_epoch_no integer;
_latest_epoch_stake integer;
_last_active_stake_validated_epoch text;
BEGIN
-- Get Last Active Stake Validated Epoch
SELECT last_value INTO _last_active_stake_validated_epoch
FROM grest.control_table
WHERE key = 'last_active_stake_validated_epoch';
-- Get Current Epoch
SELECT MAX(no) INTO _current_epoch_no
FROM epoch;
RAISE NOTICE 'Next epoch: %', _current_epoch_no+1;
SELECT MAX(epoch_no) INTO _latest_epoch_stake
FROM epoch_stake_progress
WHERE completed='t';
RAISE NOTICE 'Latest epoch in epoch_stake: %', _latest_epoch_stake;
RAISE NOTICE 'Latest epoch in active stake cache: %', COALESCE(_last_active_stake_validated_epoch::integer, '0');
IF (SELECT MAX(epoch_no) FROM epoch_stake_progress WHERE completed='t')::integer > COALESCE(_last_active_stake_validated_epoch,'0')::integer THEN
RETURN TRUE;
IF _latest_epoch_stake::integer > COALESCE(_last_active_stake_validated_epoch,'0')::integer THEN
RAISE NOTICE 'Running update for epoch: %', _latest_epoch_stake;
PERFORM grest.active_stake_cache_update(_latest_epoch_stake);
ELSE
RAISE NOTICE 'Skipping! Active Stake cache is already up to date with DB!';
END IF;
RAISE NOTICE 'Active Stake cache is up to date with DB!';
RETURN FALSE;
END;
$$;

Expand Down

0 comments on commit c20ff68

Please sign in to comment.