-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Naming tree,
isVerified
tag for Validators
* Added naming tree API * Added `isVerified` to validator data responses * Added new ENV for `explorer.verified-addresses` as a comma-deliniated list of trusted addresses closes: #134 closes: #343
- Loading branch information
Showing
21 changed files
with
689 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
database/src/main/resources/db/migration/V1_64__Add_naming_tree.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
SELECT 'Adding name table' AS comment; | ||
|
||
CREATE TABLE IF NOT EXISTS name | ||
( | ||
id SERIAL PRIMARY KEY, | ||
parent VARCHAR(550), | ||
child VARCHAR(40), | ||
full_name VARCHAR(600), | ||
owner VARCHAR(128), | ||
restricted BOOLEAN DEFAULT FALSE, | ||
height_added INT | ||
); | ||
|
||
DROP INDEX IF EXISTS name_unique_idx; | ||
CREATE UNIQUE INDEX IF NOT EXISTS name_unique_idx ON name (full_name, owner); | ||
|
||
SELECT 'Inserting bind_names' AS comment; | ||
WITH base AS ( | ||
SELECT tc.height, | ||
tc.id, | ||
CASE WHEN attr.key = 'name' THEN attr.value::text END AS full_name, | ||
CASE WHEN attr.key = 'address' THEN attr.value::text END AS owner | ||
FROM tx_cache tc | ||
JOIN tx_message tm ON tm.tx_hash_id = tc.id | ||
JOIN tx_message_type tmt on tm.tx_message_type_id = tmt.id, | ||
jsonb_array_elements(tc.tx_v2 -> 'tx_response' -> 'logs') with ordinality logs("events", idx), | ||
jsonb_to_recordset(logs.events -> 'events') event("type" text, "attributes" jsonb), | ||
jsonb_to_recordset(event.attributes) attr("key" text, "value" text) | ||
WHERE tc.error_code IS NULL | ||
AND tmt.proto_type IN ('/provenance.name.v1.MsgBindNameRequest', | ||
'/cosmwasm.wasm.v1beta1.MsgInstantiateContract', | ||
'/cosmwasm.wasm.v1.MsgInstantiateContract', | ||
'/cosmwasm.wasm.v1.MsgExecuteContract', | ||
'/cosmwasm.wasm.v1beta1.MsgExecuteContract') | ||
AND logs.idx - 1 = tm.msg_idx | ||
AND event.type IN ('provenance.name.v1.EventNameBound') | ||
ORDER BY tc.height | ||
), | ||
name_agg AS ( | ||
SELECT base.id, | ||
array_agg(base.full_name) AS name_agg | ||
FROM base | ||
WHERE base.full_name IS NOT NULL | ||
GROUP BY base.id | ||
), | ||
owner_agg AS ( | ||
SELECT base.id, | ||
array_agg(base.owner) AS owner_agg | ||
FROM base | ||
WHERE base.owner IS NOT NULL | ||
GROUP BY base.id | ||
), | ||
smooshed AS ( | ||
SELECT base.height, | ||
base.id AS tx_id, | ||
REPLACE(unnest(na.name_agg), '"', '') AS full_name, | ||
REPLACE(unnest(oa.owner_agg), '"', '') AS owner | ||
FROM base | ||
JOIN name_agg na ON base.id = na.id | ||
JOIN owner_agg oa ON base.id = oa.id | ||
), | ||
regexed AS ( | ||
SELECT sm.height, | ||
sm.owner, | ||
sm.full_name, | ||
regexp_matches(sm.full_name, '([a-zA-Z0-9-]*)(\.[a-zA-Z0-9.-]*)') AS reg | ||
FROM smooshed sm | ||
) | ||
INSERT | ||
INTO name (parent, child, full_name, owner, height_added) | ||
SELECT substr(reg[2], 2) AS parent, | ||
reg[1] AS child, | ||
r.full_name, | ||
r.owner, | ||
MAX(r.height) | ||
FROM regexed r | ||
GROUP BY parent, child, full_name, owner | ||
ON CONFLICT (full_name, owner) DO UPDATE | ||
SET height_added = CASE | ||
WHEN excluded.height_added > name.height_added THEN excluded.height_added | ||
ELSE name.height_added END; | ||
|
||
|
||
SELECT 'Removing unbind_names' AS comment; | ||
DELETE | ||
FROM name | ||
WHERE id IN ( | ||
WITH base AS (SELECT tc.height, | ||
tc.id, | ||
CASE WHEN attr.key = 'name' THEN attr.value::text END AS full_name, | ||
CASE WHEN attr.key = 'address' THEN attr.value::text END AS owner | ||
FROM tx_cache tc | ||
JOIN tx_message tm ON tm.tx_hash_id = tc.id | ||
JOIN tx_message_type tmt on tm.tx_message_type_id = tmt.id, | ||
jsonb_array_elements(tc.tx_v2 -> 'tx_response' -> 'logs') with ordinality logs("events", idx), | ||
jsonb_to_recordset(logs.events -> 'events') event("type" text, "attributes" jsonb), | ||
jsonb_to_recordset(event.attributes) attr("key" text, "value" text) | ||
WHERE tc.error_code IS NULL | ||
AND tmt.proto_type IN ('/provenance.name.v1.MsgDeleteNameRequest', | ||
'/cosmwasm.wasm.v1beta1.MsgInstantiateContract', | ||
'/cosmwasm.wasm.v1.MsgInstantiateContract', | ||
'/cosmwasm.wasm.v1.MsgExecuteContract', | ||
'/cosmwasm.wasm.v1beta1.MsgExecuteContract') | ||
AND logs.idx - 1 = msg_idx | ||
AND event.type IN ('provenance.name.v1.EventNameUnbound') | ||
ORDER BY tc.height | ||
), | ||
smooshed AS ( | ||
SELECT base.height, | ||
base.id AS tx_id, | ||
REPLACE(MAX(full_name), '"', '') AS full_name, | ||
REPLACE(MAX(owner), '"', '') AS owner | ||
FROM base | ||
GROUP BY base.height, base.id | ||
) | ||
SELECT name.id | ||
FROM smooshed sm | ||
JOIN name ON sm.full_name = name.full_name AND sm.owner = name.owner | ||
WHERE sm.height > name.height_added | ||
); | ||
|
||
DROP INDEX IF EXISTS name_unique_idx; | ||
CREATE UNIQUE INDEX IF NOT EXISTS name_unique_idx ON name (full_name); |
134 changes: 134 additions & 0 deletions
134
database/src/main/resources/db/migration/V1_65__Add_verified_to_validators.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
SELECT 'Adding validator verified to validator_state' AS comment; | ||
|
||
|
||
ALTER TABLE validator_state | ||
ADD COLUMN IF NOT EXISTS verified BOOLEAN NOT NULL DEFAULT FALSE; | ||
|
||
SELECT 'Modify `current_validator_state` view' AS comment; | ||
DROP MATERIALIZED VIEW IF EXISTS current_validator_state; | ||
CREATE MATERIALIZED VIEW IF NOT EXISTS current_validator_state AS | ||
SELECT DISTINCT ON (vs.operator_addr_id) vs.operator_addr_id, | ||
vs.operator_address, | ||
vs.block_height, | ||
vs.moniker, | ||
vs.status, | ||
vs.jailed, | ||
vs.token_count, | ||
vs.json, | ||
svc.account_address, | ||
svc.consensus_address, | ||
svc.consensus_pubkey, | ||
vs.commission_rate, | ||
vs.verified | ||
FROM validator_state vs | ||
JOIN staking_validator_cache svc on vs.operator_addr_id = svc.id | ||
ORDER BY vs.operator_addr_id, vs.block_height desc | ||
WITH DATA; | ||
|
||
|
||
SELECT 'Modify `get_validator_list` function' AS comment; | ||
DROP FUNCTION IF EXISTS get_validator_list(integer, varchar, text, integer, integer, text[]); | ||
CREATE OR REPLACE FUNCTION get_validator_list(active_set integer, active_status character varying, search_state text, search_limit integer, search_offset integer, consensus_set text[] DEFAULT NULL, search_verified boolean DEFAULT NULL) | ||
returns TABLE(operator_addr_id integer, operator_address character varying, block_height integer, moniker character varying, status character varying, jailed boolean, token_count numeric, json jsonb, account_address character varying, consensus_address character varying, consensus_pubkey character varying, commission_rate numeric, verified boolean, validator_state text) | ||
language plpgsql | ||
as | ||
$$ | ||
BEGIN | ||
|
||
RETURN QUERY | ||
WITH active AS ( | ||
SELECT cvs.* | ||
FROM current_validator_state cvs | ||
WHERE cvs.status = active_status | ||
AND cvs.jailed = false | ||
ORDER BY cvs.token_count DESC | ||
LIMIT active_set | ||
), | ||
jailed AS ( | ||
SELECT cvs.* | ||
FROM current_validator_state cvs | ||
WHERE cvs.jailed = true | ||
), | ||
candidate AS ( | ||
SELECT cvs.* | ||
FROM current_validator_state cvs | ||
LEFT JOIN active a ON cvs.operator_address = a.operator_address | ||
WHERE cvs.jailed = false | ||
AND a.operator_address IS NULL | ||
), | ||
state AS ( | ||
SELECT cvs.operator_address, | ||
CASE | ||
WHEN a.operator_address IS NOT NULL THEN 'active' | ||
WHEN j.operator_address IS NOT NULL THEN 'jailed' | ||
WHEN c.operator_address IS NOT NULL THEN 'candidate' | ||
END validator_state | ||
FROM current_validator_state cvs | ||
LEFT JOIN active a ON cvs.operator_address = a.operator_address | ||
LEFT JOIN jailed j ON cvs.operator_address = j.operator_address | ||
LEFT JOIN candidate c ON cvs.operator_address = c.operator_address | ||
) | ||
SELECT cvs.*, | ||
s.validator_state | ||
FROM current_validator_state cvs | ||
LEFT JOIN state s ON cvs.operator_address = s.operator_address | ||
WHERE s.validator_state = search_state | ||
AND (consensus_set IS NULL OR cvs.consensus_address = ANY (consensus_set)) | ||
AND (search_verified IS NULL OR cvs.verified = search_verified) | ||
ORDER BY s.validator_state, cvs.token_count DESC | ||
LIMIT search_limit OFFSET search_offset; | ||
END | ||
$$; | ||
|
||
|
||
SELECT 'Modify `get_all_validator_state` function' AS comment; | ||
DROP FUNCTION IF EXISTS get_all_validator_state(integer, varchar, text[]); | ||
CREATE OR REPLACE FUNCTION get_all_validator_state(active_set integer, active_status character varying, consensus_set text[] DEFAULT NULL, search_verified boolean DEFAULT NULL) | ||
returns TABLE(operator_addr_id integer, operator_address character varying, block_height integer, moniker character varying, status character varying, jailed boolean, token_count numeric, json jsonb, account_address character varying, consensus_address character varying, consensus_pubkey character varying, commission_rate numeric, verified boolean, validator_state text) | ||
language plpgsql | ||
as | ||
$$ | ||
BEGIN | ||
|
||
RETURN QUERY | ||
WITH active AS ( | ||
SELECT cvs.* | ||
FROM current_validator_state cvs | ||
WHERE cvs.status = active_status | ||
AND cvs.jailed = false | ||
ORDER BY cvs.token_count DESC | ||
LIMIT active_set | ||
), | ||
jailed AS ( | ||
SELECT cvs.* | ||
FROM current_validator_state cvs | ||
WHERE cvs.jailed = true | ||
), | ||
candidate AS ( | ||
SELECT cvs.* | ||
FROM current_validator_state cvs | ||
LEFT JOIN active a ON cvs.operator_address = a.operator_address | ||
WHERE cvs.jailed = false | ||
AND a.operator_address IS NULL | ||
), | ||
state AS ( | ||
SELECT cvs.operator_address, | ||
CASE | ||
WHEN a.operator_address IS NOT NULL THEN 'active' | ||
WHEN j.operator_address IS NOT NULL THEN 'jailed' | ||
WHEN c.operator_address IS NOT NULL THEN 'candidate' | ||
END validator_state | ||
FROM current_validator_state cvs | ||
LEFT JOIN active a ON cvs.operator_address = a.operator_address | ||
LEFT JOIN jailed j ON cvs.operator_address = j.operator_address | ||
LEFT JOIN candidate c ON cvs.operator_address = c.operator_address | ||
) | ||
SELECT cvs.*, | ||
s.validator_state | ||
FROM current_validator_state cvs | ||
LEFT JOIN state s ON cvs.operator_address = s.operator_address | ||
WHERE (consensus_set IS NULL OR cvs.consensus_address = ANY (consensus_set)) | ||
AND (search_verified IS NULL OR cvs.verified = search_verified) | ||
ORDER BY s.validator_state, cvs.token_count DESC; | ||
END | ||
$$; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.