Skip to content

Commit

Permalink
debonding_delegations: remove id from the table
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrus committed Jun 27, 2024
1 parent 1cf5545 commit 8d75a47
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 13 deletions.
1 change: 1 addition & 0 deletions .changelog/712.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix handling of multiple debonding-delegations ending in the same epoch
2 changes: 1 addition & 1 deletion analyzer/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@ func (m *processor) queueEscrows(batch *storage.QueryBatch, data *stakingData) e
e.Escrow.String(),
e.Owner.String(),
)
batch.Queue(queries.ConsensusDebondingStartDebondingDelegationsInsert,
batch.Queue(queries.ConsensusDebondingStartDebondingDelegationsUpsert,
e.Escrow.String(),
e.Owner.String(),
e.DebondingShares.String(),
Expand Down
2 changes: 1 addition & 1 deletion analyzer/consensus/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func (mg *GenesisProcessor) addStakingBackendMigrations(batch *storage.QueryBatc
for _, delegator := range sortedAddressKeys(escrows) {
debondingDelegations := escrows[delegator]
for _, debondingDelegation := range debondingDelegations {
batch.Queue(queries.ConsensusDebondingStartDebondingDelegationsInsert,
batch.Queue(queries.ConsensusDebondingStartDebondingDelegationsUpsert,
delegatee.String(),
delegator.String(),
debondingDelegation.Shares.ToBigInt(),
Expand Down
20 changes: 10 additions & 10 deletions analyzer/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,11 @@ var (
DELETE FROM chain.delegations
WHERE delegatee = $1 AND delegator = $2 AND shares = 0`

ConsensusDebondingStartDebondingDelegationsInsert = `
ConsensusDebondingStartDebondingDelegationsUpsert = `
INSERT INTO chain.debonding_delegations (delegatee, delegator, shares, debond_end)
VALUES ($1, $2, $3, $4)`
VALUES ($1, $2, $3, $4)
ON CONFLICT (delegatee, delegator, debond_end) DO
UPDATE SET shares = chain.debonding_delegations.shares + $3`

ConsensusReclaimEscrowBalanceUpdate = `
UPDATE chain.accounts
Expand All @@ -388,16 +390,14 @@ var (
escrow_total_shares_debonding = escrow_total_shares_debonding - $3
WHERE address = $1`

// Network upgrades delays debonding by 1 epoch.
// debond_end IN ($4::bigint, $4::bigint - 1, 0) is used because:
// - Network upgrades delays debonding by 1 epoch.
// - Some very old events might not have the debond_end set, so we have 0 in the Db.
// This should not be problematic in practice since nowadays we have fast-sync where
// we skip inserting debonding delegations for old epochs, so we should not encounter this.
ConsensusDeleteDebondingDelegations = `
DELETE FROM chain.debonding_delegations
WHERE id = (
SELECT id
FROM chain.debonding_delegations
WHERE
delegator = $1 AND delegatee = $2 AND shares = $3 AND debond_end IN ($4::bigint, $4::bigint - 1)
LIMIT 1
)`
WHERE delegator = $1 AND delegatee = $2 AND shares = $3 AND debond_end IN ($4::bigint, $4::bigint - 1, 0)`

ConsensusAllowanceChangeDelete = `
DELETE FROM chain.allowances
Expand Down
2 changes: 1 addition & 1 deletion storage/migrations/00_consensus.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ CREATE INDEX ix_delegations_delegator ON chain.delegations(delegator);

CREATE TABLE chain.debonding_delegations
(
id BIGSERIAL PRIMARY KEY, -- index-internal ID
id BIGSERIAL PRIMARY KEY, -- index-internal ID -- removed in 20_consensus_debonding_delegations_id_remove.up.sql.
delegatee oasis_addr NOT NULL REFERENCES chain.accounts(address) DEFERRABLE INITIALLY DEFERRED,
delegator oasis_addr NOT NULL REFERENCES chain.accounts(address) DEFERRABLE INITIALLY DEFERRED,
shares UINT_NUMERIC NOT NULL,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
BEGIN;

-- Create the new debonding delegations table.
CREATE TABLE chain.debonding_delegations_new
(
delegatee oasis_addr NOT NULL REFERENCES chain.accounts(address) DEFERRABLE INITIALLY DEFERRED,
delegator oasis_addr NOT NULL REFERENCES chain.accounts(address) DEFERRABLE INITIALLY DEFERRED,
debond_end UINT63 NOT NULL,
shares UINT_NUMERIC NOT NULL,
PRIMARY KEY (delegatee, delegator, debond_end)
);

-- Insert data from the old table into the new table, merging shares on conflict.
-- Skip debonding delegations that already ended (old debonding delegations table was not pruned correctly in all cases).
DO $$
DECLARE
current_epoch UINT63;
BEGIN
SELECT id INTO current_epoch
FROM chain.epochs
ORDER BY id DESC
LIMIT 1;

-- Step 3: Insert data from the old table into the new table, merging shares on conflict
INSERT INTO chain.debonding_delegations_new (delegatee, delegator, debond_end, shares)
SELECT delegatee,
delegator,
debond_end,
SUM(shares) as shares
FROM chain.debonding_delegations
WHERE debond_end > current_epoch
GROUP BY delegatee, delegator, debond_end
ON CONFLICT (delegatee, delegator, debond_end) DO UPDATE
SET shares = chain.debonding_delegations_new.shares + EXCLUDED.shares;
END $$;

-- Drop the old table and rename the new table.
DROP TABLE chain.debonding_delegations;
ALTER TABLE chain.debonding_delegations_new RENAME TO debonding_delegations;

-- Grant others read-only use.
-- (We granted already in 00_consensus.up.sql, but the grant does not apply to new tables.)
GRANT SELECT ON ALL TABLES IN SCHEMA chain TO PUBLIC;
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA chain TO PUBLIC;
GRANT SELECT ON ALL TABLES IN SCHEMA analysis TO PUBLIC;
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA analysis TO PUBLIC;

COMMIT;

0 comments on commit 8d75a47

Please sign in to comment.