From 78b8d751ef8dfa3f4fd8c286ed6150f8ddcf76a5 Mon Sep 17 00:00:00 2001 From: ptrus Date: Tue, 18 Jun 2024 18:39:23 +0200 Subject: [PATCH] delegations: remove delegations with 0 shares --- .changelog/711.feature.md | 1 + analyzer/consensus/consensus.go | 27 +++++++++++++++++++ analyzer/queries/queries.go | 13 +++------ .../17_delegations_zero_shares.up.sql | 10 +++++++ 4 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 .changelog/711.feature.md create mode 100644 storage/migrations/17_delegations_zero_shares.up.sql diff --git a/.changelog/711.feature.md b/.changelog/711.feature.md new file mode 100644 index 000000000..57449548d --- /dev/null +++ b/.changelog/711.feature.md @@ -0,0 +1 @@ +delegations: delete delegations with 0 shares diff --git a/analyzer/consensus/consensus.go b/analyzer/consensus/consensus.go index d64ade041..6461f3928 100644 --- a/analyzer/consensus/consensus.go +++ b/analyzer/consensus/consensus.go @@ -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(), diff --git a/analyzer/queries/queries.go b/analyzer/queries/queries.go index e08ce06bc..6c05eaabd 100644 --- a/analyzer/queries/queries.go +++ b/analyzer/queries/queries.go @@ -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) @@ -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)` diff --git a/storage/migrations/17_delegations_zero_shares.up.sql b/storage/migrations/17_delegations_zero_shares.up.sql new file mode 100644 index 000000000..187d598ab --- /dev/null +++ b/storage/migrations/17_delegations_zero_shares.up.sql @@ -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;