Skip to content

Commit

Permalink
Merge pull request #711 from oasisprotocol/ptrus/fix/clean-zero-deleg…
Browse files Browse the repository at this point in the history
…ations

delegations: remove delegations with 0 shares
  • Loading branch information
ptrus authored Jun 19, 2024
2 parents 038415b + 78b8d75 commit 61400ef
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
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,
// 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 = `
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;

0 comments on commit 61400ef

Please sign in to comment.