Skip to content

Commit ef95a04

Browse files
committed
fix
1 parent c639c54 commit ef95a04

File tree

4 files changed

+53
-87
lines changed

4 files changed

+53
-87
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@ func (m *Migration) Up(db *sql.DB, grm *gorm.DB, cfg *config.Config) error {
1515
queries := []string{
1616

1717
// =============================================================================
18-
// PART 4: Create withdrawal_queue_share_snapshots table for rewards calculation
18+
// PART 1: Create withdrawal_queue_share_snapshots table for rewards calculation
1919
// =============================================================================
2020

2121
// Table to track shares in withdrawal queue that should still earn rewards
2222
// Stakers continue earning while in 14-day queue because they're still taking slashing risk
23-
// Follows naming convention: *_snapshots suffix for snapshot tables (consistent with staker_share_snapshots, etc.)
2423
`create table if not exists withdrawal_queue_share_snapshots (
2524
staker varchar not null,
2625
strategy varchar not null,
@@ -39,7 +38,7 @@ func (m *Migration) Up(db *sql.DB, grm *gorm.DB, cfg *config.Config) error {
3938
`create index if not exists idx_withdrawal_queue_share_snapshots_staker on withdrawal_queue_share_snapshots(staker, snapshot)`,
4039

4140
// =============================================================================
42-
// PART 5: Create deallocation_queue_snapshots table for operator allocation tracking
41+
// PART 2: Create deallocation_queue_snapshots table for operator allocation tracking
4342
// =============================================================================
4443

4544
// Table to track operator allocation decreases that haven't reached effective_date yet

pkg/rewards/deallocationQueueShareSnapshots.go

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const deallocationQueueShareSnapshotsQuery = `
4242
from operator_allocations oa
4343
inner join blocks b on oa.block_number = b.number
4444
where
45-
oa.effective_date is not null -- Only Sabine fork records
45+
oa.effective_date is not null
4646
-- Allocation already recorded before or on snapshot
4747
and date(b.block_time) <= '{{.snapshotDate}}'
4848
-- Effective date is in the future relative to snapshot
@@ -85,8 +85,6 @@ const deallocationQueueShareSnapshotsQuery = `
8585
on conflict on constraint uniq_deallocation_queue_snapshots do nothing;
8686
`
8787

88-
// DeallocationQueueSnapshot represents operator allocation decreases that haven't
89-
// reached their effective_date yet, and should still be counted for rewards.
9088
type DeallocationQueueSnapshot struct {
9189
Operator string `gorm:"column:operator;primaryKey"`
9290
Avs string `gorm:"column:avs;primaryKey"`
@@ -101,13 +99,7 @@ func (DeallocationQueueSnapshot) TableName() string {
10199
return "deallocation_queue_snapshots"
102100
}
103101

104-
// GenerateAndInsertDeallocationQueueSnapshots calculates and inserts operator allocation
105-
// decreases that should still be counted for rewards because their effective_date hasn't
106-
// been reached yet.
107-
//
108-
// This feature is only active after the Sabine fork (when effective_date is populated).
109102
func (r *RewardsCalculator) GenerateAndInsertDeallocationQueueSnapshots(snapshotDate string) error {
110-
// Check if we're past the Sabine fork
111103
forks, err := r.globalConfig.GetRewardsSqlForkDates()
112104
if err != nil {
113105
r.logger.Sugar().Errorw("Failed to get rewards fork dates", "error", err)
@@ -120,7 +112,6 @@ func (r *RewardsCalculator) GenerateAndInsertDeallocationQueueSnapshots(snapshot
120112
return nil
121113
}
122114

123-
// Get the block number for the snapshot date to compare with fork block
124115
var maxBlock uint64
125116
res := r.grm.Raw(`
126117
SELECT COALESCE(MAX(number), 0) as max_block
@@ -133,7 +124,6 @@ func (r *RewardsCalculator) GenerateAndInsertDeallocationQueueSnapshots(snapshot
133124
return res.Error
134125
}
135126

136-
// Only apply deallocation queue logic if we're past the Sabine fork
137127
if maxBlock < sabineFork.BlockNumber {
138128
r.logger.Sugar().Debugw("Snapshot date is before Sabine fork, skipping deallocation queue logic",
139129
zap.String("snapshotDate", snapshotDate),
@@ -168,38 +158,41 @@ func (r *RewardsCalculator) GenerateAndInsertDeallocationQueueSnapshots(snapshot
168158
return nil
169159
}
170160

171-
// ListDeallocationQueueSnapshots returns all deallocation queue snapshots for debugging
172-
func (r *RewardsCalculator) ListDeallocationQueueSnapshots() ([]*DeallocationQueueSnapshot, error) {
173-
var snapshots []*DeallocationQueueSnapshot
174-
res := r.grm.Model(&DeallocationQueueSnapshot{}).Find(&snapshots)
175-
if res.Error != nil {
176-
r.logger.Sugar().Errorw("Failed to list deallocation queue snapshots", "error", res.Error)
177-
return nil, res.Error
161+
func (r *RewardsCalculator) AdjustOperatorShareSnapshotsForDeallocationQueue(snapshotDate string) error {
162+
adjustQuery := `
163+
insert into operator_share_snapshots(operator, strategy, shares, snapshot)
164+
select
165+
dqs.operator,
166+
dqs.strategy,
167+
sum(dqs.magnitude_decrease::numeric)::text as shares,
168+
dqs.snapshot as snapshot
169+
from deallocation_queue_snapshots dqs
170+
where dqs.snapshot = '{{.snapshotDate}}'
171+
group by dqs.operator, dqs.strategy, dqs.snapshot
172+
on conflict on constraint uniq_operator_share_snapshots
173+
do update set shares = operator_share_snapshots.shares + EXCLUDED.shares;`
174+
175+
query, err := rewardsUtils.RenderQueryTemplate(adjustQuery, map[string]interface{}{
176+
"snapshotDate": snapshotDate,
177+
})
178+
if err != nil {
179+
r.logger.Sugar().Errorw("Failed to render deallocation queue adjustment query template", "error", err)
180+
return err
178181
}
179-
return snapshots, nil
180-
}
181182

182-
// adjustOperatorAllocationSnapshotsForDeallocationQueueQuery adds deallocation queue
183-
// allocations back to operator allocation snapshots.
184-
//
185-
// This is similar to withdrawal queue adjustment but for operator allocations.
186-
// We add back the magnitude_decrease to the current allocation to get the pre-deallocation value.
187-
const adjustOperatorAllocationSnapshotsForDeallocationQueueQuery = `
188-
-- NOT YET IMPLEMENTED
189-
-- This would need to integrate with operator allocation snapshots used in rewards v2.2
190-
-- For now, this is a placeholder for future operator set rewards calculation
191-
-- TODO: Integrate with operator set rewards calculation
192-
`
183+
res := r.grm.Debug().Exec(query)
184+
if res.Error != nil {
185+
r.logger.Sugar().Errorw("Failed to adjust operator_share_snapshots for deallocation queue",
186+
zap.String("snapshotDate", snapshotDate),
187+
zap.Error(res.Error),
188+
)
189+
return res.Error
190+
}
193191

194-
// AdjustOperatorAllocationSnapshotsForDeallocationQueue adds deallocation queue
195-
// allocations back to operator allocation snapshots.
196-
//
197-
// NOTE: This is a placeholder for integration with operator set rewards (v2.2).
198-
// The actual adjustment logic will depend on how operator allocations are used in rewards.
199-
func (r *RewardsCalculator) AdjustOperatorAllocationSnapshotsForDeallocationQueue(snapshotDate string) error {
200-
// TODO: Implement adjustment logic once operator set rewards calculation structure is finalized
201-
r.logger.Sugar().Debugw("Deallocation queue adjustment not yet implemented",
192+
r.logger.Sugar().Infow("Adjusted operator share snapshots for deallocation queue",
202193
zap.String("snapshotDate", snapshotDate),
194+
zap.Int64("rowsAffected", res.RowsAffected),
203195
)
196+
204197
return nil
205198
}

pkg/rewards/rewards.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,20 +674,24 @@ func (rc *RewardsCalculator) generateSnapshotData(snapshotDate string) error {
674674
}
675675
rc.logger.Sugar().Debugw("Generated operator share snapshots")
676676

677-
// Generate deallocation queue snapshots - operators continue earning on old allocation until effective_date
678677
if err = rc.GenerateAndInsertDeallocationQueueSnapshots(snapshotDate); err != nil {
679678
rc.logger.Sugar().Errorw("Failed to generate deallocation queue snapshots", "error", err)
680679
return err
681680
}
682681
rc.logger.Sugar().Debugw("Generated deallocation queue snapshots")
683682

683+
if err = rc.AdjustOperatorShareSnapshotsForDeallocationQueue(snapshotDate); err != nil {
684+
rc.logger.Sugar().Errorw("Failed to adjust operator share snapshots for deallocation queue", "error", err)
685+
return err
686+
}
687+
rc.logger.Sugar().Debugw("Adjusted operator share snapshots for deallocation queue")
688+
684689
if err = rc.GenerateAndInsertStakerShareSnapshots(snapshotDate); err != nil {
685690
rc.logger.Sugar().Errorw("Failed to generate staker share snapshots", "error", err)
686691
return err
687692
}
688693
rc.logger.Sugar().Debugw("Generated staker share snapshots")
689694

690-
// Adjust staker share snapshots to include withdrawal queue shares
691695
if err = rc.AdjustStakerShareSnapshotsForWithdrawalQueue(snapshotDate); err != nil {
692696
rc.logger.Sugar().Errorw("Failed to adjust staker share snapshots for withdrawal queue", "error", err)
693697
return err

pkg/rewards/withdrawalQueueShareSnapshots.go

Lines changed: 13 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,6 @@ func (WithdrawalQueueShareSnapshot) TableName() string {
7373
return "withdrawal_queue_share_snapshots"
7474
}
7575

76-
// GenerateAndInsertWithdrawalQueueShares calculates and inserts shares in withdrawal queue
77-
// that should still earn rewards for the given snapshot date.
78-
//
79-
// This feature is only active after the Sabine fork.
8076
func (r *RewardsCalculator) GenerateAndInsertWithdrawalQueueShares(snapshotDate string) error {
8177
forks, err := r.globalConfig.GetRewardsSqlForkDates()
8278
if err != nil {
@@ -90,7 +86,6 @@ func (r *RewardsCalculator) GenerateAndInsertWithdrawalQueueShares(snapshotDate
9086
return nil
9187
}
9288

93-
// Get the block number for the snapshot date to compare with fork block
9489
var maxBlock uint64
9590
res := r.grm.Raw(`
9691
SELECT COALESCE(MAX(number), 0) as max_block
@@ -136,45 +131,20 @@ func (r *RewardsCalculator) GenerateAndInsertWithdrawalQueueShares(snapshotDate
136131
return nil
137132
}
138133

139-
// ListWithdrawalQueueShareSnapshots returns all withdrawal queue share snapshots for debugging
140-
func (r *RewardsCalculator) ListWithdrawalQueueShareSnapshots() ([]*WithdrawalQueueShareSnapshot, error) {
141-
var snapshots []*WithdrawalQueueShareSnapshot
142-
res := r.grm.Model(&WithdrawalQueueShareSnapshot{}).Find(&snapshots)
143-
if res.Error != nil {
144-
r.logger.Sugar().Errorw("Failed to list withdrawal queue share snapshots", "error", res.Error)
145-
return nil, res.Error
146-
}
147-
return snapshots, nil
148-
}
149-
150-
// adjustStakerShareSnapshotsForWithdrawalQueueQuery adds withdrawal queue shares
151-
// to existing staker_share_snapshots for the given snapshot date.
152-
//
153-
// This is executed AFTER GenerateAndInsertStakerShareSnapshots to add back shares
154-
// that are in the withdrawal queue and should still earn rewards.
155-
const adjustStakerShareSnapshotsForWithdrawalQueueQuery = `
156-
-- Add withdrawal queue shares to existing staker share snapshots
157-
-- If a staker/strategy already exists, add to their shares
158-
-- Otherwise, insert a new row
159-
insert into staker_share_snapshots(staker, strategy, shares, snapshot)
160-
select
161-
wqss.staker,
162-
wqss.strategy,
163-
wqss.shares,
164-
wqss.snapshot as snapshot
165-
from withdrawal_queue_share_snapshots wqss
166-
where wqss.snapshot = '{{.snapshotDate}}'
167-
on conflict on constraint uniq_staker_share_snapshots
168-
do update set
169-
-- Add withdrawal queue shares to existing snapshot shares
170-
shares = staker_share_snapshots.shares + EXCLUDED.shares;
171-
`
172-
173-
// AdjustStakerShareSnapshotsForWithdrawalQueue adds withdrawal queue shares to
174-
// staker share snapshots. This ensures stakers continue earning rewards while
175-
// their withdrawals are in the 14-day queue.
176134
func (r *RewardsCalculator) AdjustStakerShareSnapshotsForWithdrawalQueue(snapshotDate string) error {
177-
query, err := rewardsUtils.RenderQueryTemplate(adjustStakerShareSnapshotsForWithdrawalQueueQuery, map[string]interface{}{
135+
adjustQuery := `
136+
insert into staker_share_snapshots(staker, strategy, shares, snapshot)
137+
select
138+
wqss.staker,
139+
wqss.strategy,
140+
wqss.shares,
141+
wqss.snapshot as snapshot
142+
from withdrawal_queue_share_snapshots wqss
143+
where wqss.snapshot = '{{.snapshotDate}}'
144+
on conflict on constraint uniq_staker_share_snapshots
145+
do update set shares = staker_share_snapshots.shares + EXCLUDED.shares;`
146+
147+
query, err := rewardsUtils.RenderQueryTemplate(adjustQuery, map[string]interface{}{
178148
"snapshotDate": snapshotDate,
179149
})
180150
if err != nil {

0 commit comments

Comments
 (0)