Skip to content

Commit

Permalink
1.2.1 (#302)
Browse files Browse the repository at this point in the history
## Description
Various patches for 1.2.0

- [x] Fix for when both key and script was registered with same hash
- [x] Fix for drep_script format according to CIP-105, implementation
was in conflict with pre-defined roles
- [x] Pool stat fix, rollback to previous code until DBSync gets a fix
- [x] Replace usage of `view` column with own bech32 utility functions
  - [x] Utility function for stake_address encoding/decoding
  - [x] stake_address.view references
  - [x] Cache data that rely on bech32 format for stake account
  - [x] Any cache references that rely on stake account in bech32 format
  - [x] pool_hash.view references
  - [x] Cache data including bech32 format for pool IDs
  - [x] Any cache references that rely on pool ID in bech32 format

### Cancelled:
- [ ] DRep view references - cancelled due to presence of pre-created
roles in dbsync
- [ ] multi_asset > fingerprint references - requires addition of
blake2b extension, TBC - if/when dbsync retires bech32
- [ ] Truncate (delete from) multi_asset.fingerprint, stake_address.view
column and corresponding indexes to save on disk space and background
maintenance on Postgres DB => In absence of tx_out.address, the primary
benefactor of this would be multi_asset.fingerprint, but that's
currently not being handled in this release - Hence, skipped this item
accordingly

---------

Co-authored-by: rdlrt <3169068+rdlrt@users.noreply.github.com>
Co-authored-by: Greg Beresnev <greg.beresnev@yarris.com>
  • Loading branch information
3 people authored Sep 17, 2024
1 parent 613ff7b commit 7c6a243
Show file tree
Hide file tree
Showing 63 changed files with 531 additions and 406 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
-- Binary format
-- 1 byte variable length
-- 1 byte variable length
-- <------> <------------------->
-- ┌────────┬─────────────────────┐
-- │ header │ key │
-- │ header │ key
-- └────────┴─────────────────────┘
-- 🔎
-- ╎ 7 6 5 4 3 2 1 0
Expand Down Expand Up @@ -34,11 +34,25 @@ BEGIN
END;
$$;

CREATE OR REPLACE FUNCTION grest.cip129_cc_hot_has_script(_cc_hot text)
RETURNS boolean
LANGUAGE plpgsql STABLE
AS $$
BEGIN
IF LENGTH(_cc_hot) = 60 THEN
RETURN SUBSTRING(b32_decode(_cc_hot) from 2 for 1) = '3';
ELSE
RETURN STARTS_WITH(_cc_hot, 'cc_hot_script');
END IF;
END;
$$;

CREATE OR REPLACE FUNCTION grest.cip129_hex_to_cc_hot(_raw bytea, _is_script boolean)
RETURNS text
LANGUAGE plpgsql STABLE
AS $$
BEGIN
IF _raw IS NULL THEN RETURN NULL; END IF;
IF _is_script THEN
RETURN b32_encode('cc_hot', ('\x03'::bytea || _raw)::text);
ELSE
Expand All @@ -60,11 +74,25 @@ BEGIN
END;
$$;

CREATE OR REPLACE FUNCTION grest.cip129_cc_cold_has_script(_cc_cold text)
RETURNS boolean
LANGUAGE plpgsql STABLE
AS $$
BEGIN
IF LENGTH(_cc_cold) = 61 THEN
RETURN SUBSTRING(b32_decode(_cc_cold) from 2 for 1) = '3';
ELSE
RETURN STARTS_WITH(_cc_cold, 'cc_cold_script');
END IF;
END;
$$;

CREATE OR REPLACE FUNCTION grest.cip129_hex_to_cc_cold(_raw bytea, _is_script boolean)
RETURNS text
LANGUAGE plpgsql STABLE
AS $$
BEGIN
IF _raw IS NULL THEN RETURN NULL; END IF;
IF _is_script THEN
RETURN b32_encode('cc_cold', ('\x13'::bytea || _raw)::text);
ELSE
Expand All @@ -86,11 +114,25 @@ BEGIN
END;
$$;

CREATE OR REPLACE FUNCTION grest.cip129_drep_id_has_script(_drep_id text)
RETURNS boolean
LANGUAGE plpgsql STABLE
AS $$
BEGIN
IF LENGTH(_drep_id) = 58 THEN
RETURN SUBSTRING(b32_decode(_drep_id) from 2 for 1) = '3';
ELSE
RETURN STARTS_WITH(_drep_id, 'drep_script');
END IF;
END;
$$;

CREATE OR REPLACE FUNCTION grest.cip129_hex_to_drep_id(_raw bytea, _is_script boolean)
RETURNS text
LANGUAGE plpgsql STABLE
AS $$
BEGIN
IF _raw IS NULL THEN RETURN NULL; END IF;
IF _is_script THEN
RETURN b32_encode('drep', ('\x23'::bytea || _raw)::text);
ELSE
Expand Down Expand Up @@ -121,10 +163,13 @@ END;
$$;

COMMENT ON FUNCTION grest.cip129_cc_hot_to_hex IS 'Returns binary hex from Constitutional Committee Hot Credential ID in old or new (CIP-129) format'; -- noqa: LT01
COMMENT ON FUNCTION grest.cip129_cc_hot_has_script IS 'Returns true if Constitutional Committee Hot Credential ID is of type script'; -- noqa: LT01
COMMENT ON FUNCTION grest.cip129_hex_to_cc_hot IS 'Returns Constitutional Committee Hot Credential ID in CIP-129 format from raw binary hex'; -- noqa: LT01
COMMENT ON FUNCTION grest.cip129_cc_cold_to_hex IS 'Returns binary hex from Constitutional Committee Cold Credential ID in old or new (CIP-129) format'; -- noqa: LT01
COMMENT ON FUNCTION grest.cip129_cc_cold_has_script IS 'Returns true if Constitutional Committee Cold Credential ID is of type script'; -- noqa: LT01
COMMENT ON FUNCTION grest.cip129_hex_to_cc_cold IS 'Returns Constitutional Committee Cold Credential ID in CIP-129 format from raw binary hex'; -- noqa: LT01
COMMENT ON FUNCTION grest.cip129_drep_id_to_hex IS 'Returns binary hex from DRep Credential ID in old or new (CIP-129) format'; -- noqa: LT01
COMMENT ON FUNCTION grest.cip129_drep_id_has_script IS 'Returns true if DRep Credential ID is of type script'; -- noqa: LT01
COMMENT ON FUNCTION grest.cip129_hex_to_drep_id IS 'Returns DRep Credential ID in CIP-129 format from raw binary hex'; -- noqa: LT01
COMMENT ON FUNCTION grest.cip129_from_gov_action_id IS 'Returns string array containing transaction hash and certificate index from Governance Action Proposal ID in CIP-129 format'; -- noqa: LT01
COMMENT ON FUNCTION grest.cip129_to_gov_action_id IS 'Returns Governance Action Proposal ID in CIP-129 format from transaction hash appended by index of certificate within the transaction'; -- noqa: LT01
19 changes: 19 additions & 0 deletions files/grest/rpc/000_utilities/cip5.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- CIP References
-- 0005: Common bech32 prefixes https://cips.cardano.org/cip/CIP-0005
-- 0019: Cardano Addresses https://cips.cardano.org/cip/CIP-0019

CREATE OR REPLACE FUNCTION grest.cip5_hex_to_stake_addr(_raw bytea)
RETURNS text
LANGUAGE plpgsql STABLE
AS $$
BEGIN
IF _raw IS NULL THEN
RETURN NULL;
END IF;
IF SUBSTRING(ENCODE(_raw, 'hex') from 2 for 1) = '0' THEN
RETURN b32_encode('stake_test', _raw::text);
ELSE
RETURN b32_encode('stake', _raw::text);
END IF;
END;
$$;
File renamed without changes.
2 changes: 1 addition & 1 deletion files/grest/rpc/00_blockchain/reserve_withdrawals.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ AS $$
ENCODE(b.hash,'hex'),
b.block_no,
r.amount::text,
sa.view,
grest.cip5_hex_to_stake_addr(sa.hash_raw) AS stake_address,
earned_epoch,
spendable_epoch
FROM reserve AS r
Expand Down
2 changes: 1 addition & 1 deletion files/grest/rpc/00_blockchain/treasury_withdrawals.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ AS $$
ENCODE(b.hash,'hex'),
b.block_no,
t.amount::text,
sa.view,
grest.cip5_hex_to_stake_addr(sa.hash_raw) AS stake_address,
earned_epoch,
spendable_epoch
FROM treasury AS t
Expand Down
7 changes: 3 additions & 4 deletions files/grest/rpc/01_cached_tables/active_stake_cache.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CREATE TABLE IF NOT EXISTS grest.pool_active_stake_cache (
pool_id varchar NOT NULL,
pool_id bigint NOT NULL,
epoch_no bigint NOT NULL,
amount lovelace NOT NULL,
PRIMARY KEY (pool_id, epoch_no)
Expand Down Expand Up @@ -64,15 +64,14 @@ BEGIN
-- POOL ACTIVE STAKE CACHE
INSERT INTO grest.pool_active_stake_cache
SELECT
pool_hash.view AS pool_id,
epoch_stake.pool_id AS pool_id,
epoch_stake.epoch_no,
SUM(epoch_stake.amount) AS amount
FROM public.epoch_stake
INNER JOIN public.pool_hash ON pool_hash.id = epoch_stake.pool_id
WHERE epoch_stake.epoch_no >= _last_active_stake_validated_epoch
AND epoch_stake.epoch_no <= _epoch_no
GROUP BY
pool_hash.view,
epoch_stake.pool_id,
epoch_stake.epoch_no
ON CONFLICT (
pool_id,
Expand Down
Loading

0 comments on commit 7c6a243

Please sign in to comment.