From fb4ad7087e1ff9a3b5711c560bf8efb27b68d680 Mon Sep 17 00:00:00 2001 From: rdlrt <3169068+rdlrt@users.noreply.github.com> Date: Thu, 14 Mar 2024 09:54:10 +1100 Subject: [PATCH] Minor fixes in cache --- .../rpc/01_cached_tables/pool_history_cache.sql | 15 ++++++++------- .../01_cached_tables/stake_distribution_cache.sql | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/files/grest/rpc/01_cached_tables/pool_history_cache.sql b/files/grest/rpc/01_cached_tables/pool_history_cache.sql index 58cea776..db3267bf 100644 --- a/files/grest/rpc/01_cached_tables/pool_history_cache.sql +++ b/files/grest/rpc/01_cached_tables/pool_history_cache.sql @@ -44,16 +44,16 @@ BEGIN RAISE EXCEPTION 'Active stake cache not yet populated! Exiting...'; END IF; + SELECT COALESCE(MAX(epoch_no), 0) INTO _latest_epoch_no_in_cache FROM grest.pool_history_cache; + -- Split into 100 epochs at a time to avoid hours spent on a single query (which can be risky if that query is killed) + SELECT LEAST( 500 , (MAX(no) - _latest_epoch_no_in_cache) ) + _latest_epoch_no_in_cache INTO _curr_epoch FROM epoch; + IF _epoch_no_to_insert_from IS NULL THEN - SELECT COALESCE(MAX(epoch_no), 0) INTO _latest_epoch_no_in_cache - FROM grest.pool_history_cache; IF _latest_epoch_no_in_cache = 0 THEN RAISE NOTICE 'Pool history cache table is empty, starting initial population...'; PERFORM grest.pool_history_cache_update (0); RETURN; END IF; - SELECT MAX(no) INTO _curr_epoch - FROM epoch; -- no-op IF we already have data up until second most recent epoch IF _latest_epoch_no_in_cache >= (_curr_epoch - 1) THEN INSERT INTO grest.control_table (key, last_value) @@ -68,6 +68,7 @@ BEGIN -- purge the data for the given epoch range, in theory should do nothing IF invoked only at start of new epoch DELETE FROM grest.pool_history_cache WHERE epoch_no >= _epoch_no_to_insert_from; + INSERT INTO grest.pool_history_cache ( WITH blockcounts AS ( @@ -75,8 +76,7 @@ BEGIN sl.pool_hash_id, b.epoch_no, COUNT(*) AS block_cnt - FROM - block AS b, + FROM block AS b, slot_leader AS sl WHERE b.slot_leader_id = sl.id AND b.epoch_no >= _epoch_no_to_insert_from @@ -170,6 +170,7 @@ BEGIN COUNT(1) AS delegator_cnt FROM epoch_stake AS es WHERE es.epoch_no >= _epoch_no_to_insert_from + AND es.epoch_no <= _curr_epoch GROUP BY es.pool_id, es.epoch_no @@ -239,7 +240,7 @@ BEGIN END END AS epoch_ros FROM pool_hash AS ph - INNER JOIN activeandfees AS actf ON actf.pool_id = ph."view" + INNER JOIN activeandfees AS actf ON actf.pool_id = ph.view LEFT JOIN blockcounts AS b ON ph.id = b.pool_hash_id AND actf.epoch_no = b.epoch_no LEFT JOIN leadertotals AS l ON ph.id = l.pool_id diff --git a/files/grest/rpc/01_cached_tables/stake_distribution_cache.sql b/files/grest/rpc/01_cached_tables/stake_distribution_cache.sql index 892c11f2..3b9768c8 100644 --- a/files/grest/rpc/01_cached_tables/stake_distribution_cache.sql +++ b/files/grest/rpc/01_cached_tables/stake_distribution_cache.sql @@ -120,7 +120,7 @@ BEGIN account_delta_instant_rewards AS ( SELECT awdp.stake_address_id, - COALESCE(SUM(ir.amount), 0) AS rewards + COALESCE(SUM(ir.amount), 0) AS amount FROM instant_reward AS ir INNER JOIN accounts_with_delegated_pools AS awdp ON awdp.stake_address_id = ir.addr_id WHERE ir.spendable_epoch >= (_active_stake_epoch + 2)