Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delegations: remove delegations with 0 shares #711

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changelog/711.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
delegations: delete delegations with 0 shares
27 changes: 27 additions & 0 deletions analyzer/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,33 @@ func (m *processor) queueEscrows(batch *storage.QueryBatch, data *stakingData) e
e.Owner.String(),
e.ActiveShares.String(),
)
// Ideally this would be merged with the ConsensusDebondingStartDelegationsUpdate query above,
Copy link
Member Author

@ptrus ptrus Jun 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mitjat note that the previous version didn't actually work, i had to refactor the CTE into two separate queries.

There are probably other workarounds, but just issuing two queries seemed the least complex.

// something like:
//
// ```
// WITH updated AS (
// UPDATE chain.delegations
// SET shares = shares - $3
// WHERE delegatee = $1 AND delegator = $2
// RETURNING delegatee, delegator, shares
// )
//
// -- Delete the delegation if the shares are now 0.
// DELETE FROM chain.delegations
// USING updated
// WHERE chain.delegations.delegatee = updated.delegatee
// AND chain.delegations.delegator = updated.delegator
// AND updated.shares = 0`
// ```
//
// but it is not possible since CTE's cannot handle updating the same row twice:
// https://www.postgresql.org/docs/current/queries-with.html#QUERIES-WITH-MODIFYING
//
// So we need to issue a separate query to delete if zero.
batch.Queue(queries.ConsensusDelegationDeleteIfZeroShares,
e.Escrow.String(),
e.Owner.String(),
)
batch.Queue(queries.ConsensusDebondingStartDebondingDelegationsInsert,
e.Escrow.String(),
e.Owner.String(),
Expand Down
13 changes: 4 additions & 9 deletions analyzer/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,15 +338,6 @@ var (
escrow_balance_active = chain.accounts.escrow_balance_active + $2,
escrow_total_shares_active = chain.accounts.escrow_total_shares_active + $3`

ConsensusAddDelegationsUpsertReplace = `
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was unused.

INSERT INTO chain.delegations
(delegatee, delegator, shares)
VALUES
($1, $2, $3)
ON CONFLICT (delegatee, delegator) DO UPDATE
SET
shares = excluded.shares`

ConsensusAddDelegationsUpsert = `
INSERT INTO chain.delegations (delegatee, delegator, shares)
VALUES ($1, $2, $3)
Expand Down Expand Up @@ -381,6 +372,10 @@ var (
SET shares = shares - $3
WHERE delegatee = $1 AND delegator = $2`

ConsensusDelegationDeleteIfZeroShares = `
DELETE FROM chain.delegations
WHERE delegatee = $1 AND delegator = $2 AND shares = 0`

ConsensusDebondingStartDebondingDelegationsInsert = `
INSERT INTO chain.debonding_delegations (delegatee, delegator, shares, debond_end)
VALUES ($1, $2, $3, $4)`
Expand Down
10 changes: 10 additions & 0 deletions storage/migrations/17_delegations_zero_shares.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
BEGIN;

-- Starting with this commit, the delegations with zero shares will get cleaned up
-- at the debond start time (on reclaim escrow transaction). Here we clean up the
-- delegations that were already debonded and have 0 shares at the time of this
-- deployment.
DELETE FROM chain.delegations
WHERE shares = 0;

COMMIT;
Loading