Skip to content

Commit fe4de17

Browse files
committed
add deallocation to rewards
1 parent cc6120e commit fe4de17

File tree

6 files changed

+48
-190
lines changed

6 files changed

+48
-190
lines changed

pkg/postgres/migrations/202511171438_withdrawalAndDeallocationQueues/up.go

Lines changed: 0 additions & 45 deletions
This file was deleted.

pkg/postgres/migrations/migrator.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ import (
8383
_202507301421_crossChainRegistryTables "github.com/Layr-Labs/sidecar/pkg/postgres/migrations/202507301421_crossChainRegistryTables"
8484
_202511051502_keyRotationScheduled "github.com/Layr-Labs/sidecar/pkg/postgres/migrations/202511051502_keyRotationScheduled"
8585
_202511141700_withdrawalQueueAndAllocationRounding "github.com/Layr-Labs/sidecar/pkg/postgres/migrations/202511141700_withdrawalQueueAndAllocationRounding"
86-
_202511171438_withdrawalAndDeallocationQueues "github.com/Layr-Labs/sidecar/pkg/postgres/migrations/202511171438_withdrawalAndDeallocationQueues"
8786
)
8887

8988
// Migration interface defines the contract for database migrations.
@@ -229,7 +228,6 @@ func (m *Migrator) MigrateAll() error {
229228
&_202507301346_taskMailboxTables.Migration{},
230229
&_202511051502_keyRotationScheduled.Migration{},
231230
&_202511141700_withdrawalQueueAndAllocationRounding.Migration{},
232-
&_202511171438_withdrawalAndDeallocationQueues.Migration{},
233231
}
234232

235233
for _, migration := range migrations {

pkg/rewards/operatorShareSnapshots.go

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,49 @@ operator_share_windows as (
3939
cleaned_records as (
4040
SELECT * FROM operator_share_windows
4141
WHERE start_time < end_time
42+
),
43+
base_snapshots as (
44+
SELECT
45+
operator,
46+
strategy,
47+
shares,
48+
cast(day AS DATE) AS snapshot
49+
FROM
50+
cleaned_records
51+
CROSS JOIN
52+
generate_series(DATE(start_time), DATE(end_time) - interval '1' day, interval '1' day) AS day
53+
),
54+
-- Add operator allocations (deallocation delay handled via effective_block)
55+
allocation_adjustments as (
56+
SELECT
57+
oas.operator,
58+
oas.strategy,
59+
SUM(oas.magnitude) as total_magnitude,
60+
oas.snapshot
61+
FROM operator_allocation_snapshots oas
62+
WHERE oas.snapshot = DATE '{{.snapshotDate}}'
63+
GROUP BY oas.operator, oas.strategy, oas.snapshot
64+
),
65+
combined_snapshots as (
66+
SELECT
67+
coalesce(base.operator, alloc.operator) as operator,
68+
coalesce(base.strategy, alloc.strategy) as strategy,
69+
coalesce(alloc.total_magnitude::text, base.shares) as shares,
70+
coalesce(base.snapshot, alloc.snapshot) as snapshot
71+
FROM base_snapshots base
72+
FULL OUTER JOIN allocation_adjustments alloc
73+
ON base.operator = alloc.operator
74+
AND base.strategy = alloc.strategy
75+
AND base.snapshot = alloc.snapshot
4276
)
43-
SELECT
44-
operator,
45-
strategy,
46-
shares,
47-
cast(day AS DATE) AS snapshot
48-
FROM
49-
cleaned_records
50-
CROSS JOIN
51-
generate_series(DATE(start_time), DATE(end_time) - interval '1' day, interval '1' day) AS day
77+
SELECT * FROM combined_snapshots
5278
on conflict on constraint uniq_operator_share_snapshots do nothing;
5379
`
5480

5581
func (r *RewardsCalculator) GenerateAndInsertOperatorShareSnapshots(snapshotDate string) error {
5682
query, err := rewardsUtils.RenderQueryTemplate(operatorShareSnapshotsQuery, map[string]interface{}{
57-
"cutoffDate": snapshotDate,
83+
"cutoffDate": snapshotDate,
84+
"snapshotDate": snapshotDate,
5885
})
5986
if err != nil {
6087
r.logger.Sugar().Errorw("Failed to render operator share snapshots query", "error", err)

pkg/rewards/rewards.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -644,12 +644,7 @@ func (rc *RewardsCalculator) generateSnapshotData(snapshotDate string) error {
644644
}
645645
rc.logger.Sugar().Debugw("Generated staker shares")
646646

647-
// Generate withdrawal queue shares - stakers continue earning while in 14-day queue
648-
if err = rc.GenerateAndInsertWithdrawalQueueShares(snapshotDate); err != nil {
649-
rc.logger.Sugar().Errorw("Failed to generate withdrawal queue shares", "error", err)
650-
return err
651-
}
652-
rc.logger.Sugar().Debugw("Generated withdrawal queue shares")
647+
// Withdrawal queue adjustments queried directly in staker_share_snapshots generation
653648

654649
if err = rc.GenerateAndInsertOperatorShares(snapshotDate); err != nil {
655650
rc.logger.Sugar().Errorw("Failed to generate operator shares", "error", err)

pkg/rewards/stakerShareSnapshots.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,18 @@ const stakerShareSnapshotsQuery = `
5151
CROSS JOIN
5252
generate_series(DATE(start_time), DATE(end_time) - interval '1' day, interval '1' day) AS day
5353
),
54-
-- Materialize withdrawal queue adjustments into final snapshots
55-
-- Per Sean's feedback: "Why not adjust the query that populates the staker_share_snapshots table to account for this case?"
56-
-- This materializes the combined result (base + withdrawal queue) into staker_share_snapshots
54+
-- Add shares in withdrawal queue (still earning during 14-day period)
5755
withdrawal_queue_adjustments as (
5856
SELECT
59-
wqs.staker,
60-
wqs.strategy,
61-
wqs.shares,
62-
wqs.snapshot
63-
FROM withdrawal_queue_share_snapshots wqs
64-
WHERE wqs.snapshot = DATE '{{.snapshotDate}}'
57+
qsw.staker,
58+
qsw.strategy,
59+
qsw.shares_to_withdraw as shares,
60+
DATE '{{.snapshotDate}}' as snapshot
61+
FROM queued_slashing_withdrawals qsw
62+
INNER JOIN blocks b_queued ON qsw.block_number = b_queued.number
63+
WHERE
64+
DATE(b_queued.block_time) <= '{{.snapshotDate}}'
65+
AND DATE(b_queued.block_time) + INTERVAL '14 days' > '{{.snapshotDate}}'
6566
),
6667
combined_snapshots as (
6768
SELECT

pkg/rewards/withdrawalQueueShareSnapshots.go

Lines changed: 0 additions & 118 deletions
This file was deleted.

0 commit comments

Comments
 (0)