Skip to content

Commit

Permalink
add index, include resigned in ranking order
Browse files Browse the repository at this point in the history
  • Loading branch information
oXtxNt9U committed Oct 17, 2024
1 parent e9bafd0 commit 282a6ea
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export class CreateIndexes1697617471901 implements MigrationInterface {
CREATE INDEX wallets_balance ON wallets(balance);
CREATE INDEX wallets_attributes ON wallets using GIN(attributes);
CREATE INDEX wallets_validators ON wallets ((attributes->>'validatorPublicKey'))
WHERE (attributes ? 'validatorPublicKey');
`);
}

Expand Down Expand Up @@ -98,6 +100,7 @@ export class CreateIndexes1697617471901 implements MigrationInterface {
DROP INDEX wallets_balance;
DROP INDEX wallets_attributes;
DROP INDEX wallets_validators;
`);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@ export class CreateUpdateValidatorRankingFunction1729064427168 implements Migrat
RETURNS VOID AS $$
BEGIN
WITH all_validators AS (
SELECT address, (attributes->>'validatorVoteBalance')::numeric AS vote_balance
SELECT
address,
(attributes->>'validatorVoteBalance')::numeric AS vote_balance,
COALESCE((attributes->>'validatorResigned')::boolean, FALSE) AS is_resigned
FROM wallets
WHERE attributes ? 'validatorPublicKey' AND COALESCE((attributes->>'validatorResigned')::boolean, FALSE) IS NOT TRUE
WHERE attributes ? 'validatorPublicKey'
),
ranking AS (
SELECT
address,
vote_balance,
ROW_NUMBER() OVER (ORDER BY vote_balance DESC, address ASC) AS rank
ROW_NUMBER() OVER (ORDER BY is_resigned ASC, vote_balance DESC, address ASC) AS rank
FROM all_validators
ORDER BY 2 DESC, 1 ASC
ORDER BY is_resigned ASC, vote_balance DESC, address ASC
)
UPDATE wallets
SET
Expand Down

0 comments on commit 282a6ea

Please sign in to comment.