Skip to content

Commit

Permalink
Add SQL Format updates as per SQLFluff guidelines (#226)
Browse files Browse the repository at this point in the history
1. Fix Asset Info Cache (include mint/burn tx rather than sum for meta
consideration)
  2. Update SQLs as per SQLFluff linting guidelines:
  - [x] 00_blockchain
  - [x] 01_cached_tables
  - [x] 02_indexes
  - [x] account
  - [x] address
  - [x] assets
  - [x] blocks
  - [x] epoch
  - [x] pool
  - [x] script
  - [x] transactions
  - [x] views
3. Fix `_last_active_stake_validated_epoch` in active_stake_cache (#222)
4. Typo for `pool_history_cache.sql` as well as add a check to ensure
epoch_info_cache has run at least once prior to pool_history_cache
(#223)
5. Move control_table entry in cache tables to the end (instead of
start).

  ## Where should the reviewer start?

Not really sure, too big to drill down 🙁 , we shouldnt have added any
functional changes as part of linting. I expect us to cover review as
part of tests itself

  ## Motivation and context

Across months, different folks added SQL updates using different
patterns, making it difficult to align/scan/automate linting

  ## How has this been tested?

  Was tested on guildnet as per below:
  - [x] Run `./setup-grest.sh -r -b lint`
  - [x] Wait for cache to be re-populated
  - [x] Run schemathesis tests
  • Loading branch information
rdlrt committed Aug 2, 2023
1 parent 6d8a8be commit ee63dfd
Show file tree
Hide file tree
Showing 72 changed files with 2,982 additions and 3,156 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
tests/**/__pycache__
tests/**/.*
.vscode
.swp
18 changes: 18 additions & 0 deletions files/grest/.sqlfluff
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[sqlfluff]
dialect = postgres
exclude_rules = structure.column_order, references.keywords
max_line_length=260
recurse = 0
capitalisation_policy = upper
extended_capitalisation_policy = upper
idented_joins = True
indented_using_on = False
tab_space_size = 2
large_file_skip_byte_limit=35000

[sqlfluff:indentation]
tab_space_size = 2
allow_implicit_indents = True

[sqlfluff:rules:convention.count_rows]
prefer_count_1 = True
60 changes: 30 additions & 30 deletions files/grest/rpc/00_blockchain/genesis.sql
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
CREATE FUNCTION grest.genesis ()
RETURNS TABLE (
NETWORKMAGIC varchar,
NETWORKID varchar,
ACTIVESLOTCOEFF varchar,
UPDATEQUORUM varchar,
MAXLOVELACESUPPLY varchar,
EPOCHLENGTH varchar,
SYSTEMSTART integer,
SLOTSPERKESPERIOD varchar,
SLOTLENGTH varchar,
MAXKESREVOLUTIONS varchar,
SECURITYPARAM varchar,
ALONZOGENESIS varchar
)
LANGUAGE PLPGSQL
AS $$
CREATE OR REPLACE FUNCTION grest.genesis()
RETURNS TABLE (
networkmagic varchar,
networkid varchar,
activeslotcoeff varchar,
updatequorum varchar,
maxlovelacesupply varchar,
epochlength varchar,
systemstart integer,
slotsperkesperiod varchar,
slotlength varchar,
maxkesrevolutions varchar,
securityparam varchar,
alonzogenesis varchar
)
LANGUAGE plpgsql
AS $$
BEGIN
RETURN QUERY
SELECT
g.NETWORKMAGIC,
g.NETWORKID,
g.ACTIVESLOTCOEFF,
g.UPDATEQUORUM,
g.MAXLOVELACESUPPLY,
g.EPOCHLENGTH,
EXTRACT(epoch from g.SYSTEMSTART::timestamp)::integer,
g.SLOTSPERKESPERIOD,
g.SLOTLENGTH,
g.MAXKESREVOLUTIONS,
g.SECURITYPARAM,
g.ALONZOGENESIS
g.networkmagic,
g.networkid,
g.activeslotcoeff,
g.updatequorum,
g.maxlovelacesupply,
g.epochlength,
EXTRACT(EPOCH FROM g.systemstart::timestamp)::integer,
g.slotsperkesperiod,
g.slotlength,
g.maxkesrevolutions,
g.securityparam,
g.alonzogenesis
FROM
grest.genesis g;
grest.genesis AS g;
END;
$$;
38 changes: 18 additions & 20 deletions files/grest/rpc/00_blockchain/param_updates.sql
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
CREATE OR REPLACE FUNCTION grest.param_updates ()
RETURNS TABLE (
tx_hash text,
block_height word31type,
block_time integer,
epoch_no word31type,
data jsonb
)
LANGUAGE PLPGSQL
AS $$

CREATE OR REPLACE FUNCTION grest.param_updates()
RETURNS TABLE (
tx_hash text,
block_height word31type,
block_time integer,
epoch_no word31type,
data jsonb
)
LANGUAGE plpgsql
AS $$
BEGIN
RETURN QUERY
SELECT DISTINCT ON (pp.registered_tx_id)
ENCODE(t.hash,'hex') as tx_hash,
ENCODE(t.hash,'hex') AS tx_hash,
b.block_no AS block_height,
EXTRACT(epoch from b.time)::integer as block_time,
EXTRACT(EPOCH FROM b.time)::integer AS block_time,
b.epoch_no,
JSONB_STRIP_NULLS(JSONB_BUILD_OBJECT(
'min_fee_a', pp.min_fee_a,
Expand Down Expand Up @@ -47,13 +46,12 @@ BEGIN
'max_collateral_inputs', pp.max_collateral_inputs,
'coins_per_utxo_size', pp.coins_per_utxo_size
)) AS data
FROM
public.param_proposal pp
INNER JOIN tx t ON t.id = pp.registered_tx_id
INNER JOIN block b ON t.block_id = b.id
LEFT JOIN cost_model CM ON CM.id = pp.cost_model_id
;
FROM
public.param_proposal pp
INNER JOIN tx t ON t.id = pp.registered_tx_id
INNER JOIN block b ON t.block_id = b.id
LEFT JOIN cost_model CM ON CM.id = pp.cost_model_id;
END;
$$;

COMMENT ON FUNCTION grest.param_updates IS 'Parameter updates applied to the network';
COMMENT ON FUNCTION grest.param_updates IS 'Parameter updates applied to the network'; -- noqa: LT01
41 changes: 20 additions & 21 deletions files/grest/rpc/00_blockchain/tip.sql
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
CREATE FUNCTION grest.tip ()
RETURNS TABLE (
hash text,
epoch_no word31type,
abs_slot word63type,
epoch_slot word31type,
block_no word31type,
block_time integer
)
LANGUAGE PLPGSQL
AS $$
CREATE OR REPLACE FUNCTION grest.tip()
RETURNS TABLE (
hash text,
epoch_no word31type,
abs_slot word63type,
epoch_slot word31type,
block_no word31type,
block_time integer
)
LANGUAGE plpgsql
AS $$
BEGIN
RETURN QUERY
SELECT
ENCODE(B.HASH::bytea, 'hex') AS BLOCK_HASH,
b.EPOCH_NO AS EPOCH_NO,
b.SLOT_NO AS ABS_SLOT,
b.EPOCH_SLOT_NO AS EPOCH_SLOT,
b.BLOCK_NO,
EXTRACT(EPOCH from b.TIME)::integer
ENCODE(b.hash::bytea, 'hex') AS block_hash,
b.epoch_no AS epoch_no,
b.slot_no AS abs_slot,
b.epoch_slot_no AS epoch_slot,
b.block_no,
EXTRACT(EPOCH FROM b.time)::integer
FROM
BLOCK B
block b
ORDER BY
B.ID DESC
b.id DESC
LIMIT 1;
END;
$$;

COMMENT ON FUNCTION grest.tip IS 'Get the tip info about the latest block seen by chain';

COMMENT ON FUNCTION grest.tip IS 'Get the tip info about the latest block seen by chain'; -- noqa: LT01
58 changes: 33 additions & 25 deletions files/grest/rpc/00_blockchain/totals.sql
Original file line number Diff line number Diff line change
@@ -1,36 +1,44 @@
CREATE FUNCTION grest.totals (_epoch_no numeric DEFAULT NULL)
RETURNS TABLE (
epoch_no word31type,
circulation text,
treasury text,
reward text,
supply text,
reserves text
)
LANGUAGE PLPGSQL
AS $$
CREATE OR REPLACE FUNCTION grest.totals(_epoch_no numeric DEFAULT NULL)
RETURNS TABLE (
epoch_no word31type,
circulation text,
treasury text,
reward text,
supply text,
reserves text
)
LANGUAGE plpgsql
AS $$
BEGIN
IF _epoch_no IS NULL THEN
RETURN QUERY (
SELECT
ap.epoch_no, ap.utxo::text, ap.treasury::text, ap.rewards::text, (ap.treasury + ap.rewards + ap.utxo + ap.deposits + ap.fees)::text as supply, ap.reserves::text
FROM
public.ada_pots as ap
ORDER BY
ap.epoch_no DESC) ;
ap.epoch_no,
ap.utxo::text,
ap.treasury::text,
ap.rewards::text,
(ap.treasury + ap.rewards + ap.utxo + ap.deposits + ap.fees)::text AS supply,
ap.reserves::text
FROM
public.ada_pots AS ap
ORDER BY
ap.epoch_no DESC);
ELSE
RETURN QUERY (
SELECT
ap.epoch_no, ap.utxo::text, ap.treasury::text, ap.rewards::text, (ap.treasury + ap.rewards + ap.utxo + ap.deposits + ap.fees)::text as supply, ap.reserves::text
FROM
public.ada_pots as ap
WHERE
ap.epoch_no = _epoch_no
ORDER BY
ap.epoch_no DESC);
ap.epoch_no, ap.utxo::text,
ap.treasury::text,
ap.rewards::text,
(ap.treasury + ap.rewards + ap.utxo + ap.deposits + ap.fees)::text AS supply,
ap.reserves::text
FROM
public.ada_pots AS ap
WHERE
ap.epoch_no = _epoch_no
ORDER BY
ap.epoch_no DESC);
END IF;
END;
$$;

COMMENT ON FUNCTION grest.totals IS 'Get the circulating utxo, treasury, rewards, supply and reserves in lovelace for specified epoch, all epochs if empty';

COMMENT ON FUNCTION grest.totals IS 'Get the circulating utxo, treasury, rewards, supply and reserves in lovelace for specified epoch, all epochs if empty'; -- noqa: LT01
Loading

0 comments on commit ee63dfd

Please sign in to comment.