Skip to content

Commit cc6120e

Browse files
committed
materialized + backward compatibility
1 parent efef872 commit cc6120e

14 files changed

+85
-91
lines changed

pkg/postgres/migrations/202512021500_stakerSharesView/down.go

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

pkg/postgres/migrations/202512021500_stakerSharesView/up.go

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

pkg/postgres/migrations/migrator.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ import (
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"
8686
_202511171438_withdrawalAndDeallocationQueues "github.com/Layr-Labs/sidecar/pkg/postgres/migrations/202511171438_withdrawalAndDeallocationQueues"
87-
_202512021500_stakerSharesView "github.com/Layr-Labs/sidecar/pkg/postgres/migrations/202512021500_stakerSharesView"
8887
)
8988

9089
// Migration interface defines the contract for database migrations.
@@ -231,7 +230,6 @@ func (m *Migrator) MigrateAll() error {
231230
&_202511051502_keyRotationScheduled.Migration{},
232231
&_202511141700_withdrawalQueueAndAllocationRounding.Migration{},
233232
&_202511171438_withdrawalAndDeallocationQueues.Migration{},
234-
&_202512021500_stakerSharesView.Migration{},
235233
}
236234

237235
for _, migration := range migrations {

pkg/rewards/13_goldStakerODOperatorSetRewardAmounts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ staker_strategy_shares AS (
7373
sdo.*,
7474
sss.shares
7575
FROM staker_delegated_operators sdo
76-
JOIN staker_share_snapshots_final sss
76+
JOIN staker_share_snapshots sss
7777
ON sdo.staker = sss.staker
7878
AND sdo.snapshot = sss.snapshot
7979
AND sdo.strategy = sss.strategy

pkg/rewards/2_goldStakerRewardAmounts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ staker_avs_strategy_shares AS (
5656
sdo.*,
5757
sss.shares
5858
FROM staker_delegated_operators sdo
59-
JOIN staker_share_snapshots_final sss
59+
JOIN staker_share_snapshots sss
6060
ON
6161
sdo.staker = sss.staker AND
6262
sdo.snapshot = sss.snapshot AND

pkg/rewards/4_goldRewardsForAll.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ WITH reward_snapshot_stakers AS (
2020
sss.staker,
2121
sss.shares
2222
FROM {{.activeRewardsTable}} ap
23-
JOIN staker_share_snapshots_final as sss
23+
JOIN staker_share_snapshots as sss
2424
ON ap.strategy = sss.strategy and ap.snapshot = sss.snapshot
2525
WHERE ap.reward_type = 'all_stakers'
2626
-- Parse out negative shares and zero multiplier so there is no division by zero case

pkg/rewards/5_goldRfaeStakers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ staker_strategy_shares AS (
6666
sdo.*,
6767
sss.shares
6868
FROM staker_delegated_operators sdo
69-
JOIN staker_share_snapshots_final sss
69+
JOIN staker_share_snapshots sss
7070
ON
7171
sdo.staker = sss.staker AND
7272
sdo.snapshot = sss.snapshot AND

pkg/rewards/9_goldStakerODRewardAmounts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ staker_avs_strategy_shares AS (
6565
sdo.*,
6666
sss.shares
6767
FROM staker_delegated_operators sdo
68-
JOIN staker_share_snapshots_final sss
68+
JOIN staker_share_snapshots sss
6969
ON sdo.staker = sss.staker
7070
AND sdo.snapshot = sss.snapshot
7171
AND sdo.strategy = sss.strategy

pkg/rewards/operatorAllocationSnapshots.go

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package rewards
22

33
import (
4+
"github.com/Layr-Labs/sidecar/internal/config"
45
"github.com/Layr-Labs/sidecar/pkg/rewardsUtils"
56
"go.uber.org/zap"
67
)
@@ -11,7 +12,8 @@ const operatorAllocationSnapshotsQuery = `
1112
SELECT *,
1213
ROW_NUMBER() OVER (PARTITION BY operator, avs, strategy, operator_set_id, cast(block_time AS DATE) ORDER BY block_time DESC, log_index DESC) AS rn
1314
FROM operator_allocations oa
14-
INNER JOIN blocks b ON oa.effective_block = b.number
15+
-- Backward compatibility: use effective_block if available, fall back to block_number for old records
16+
INNER JOIN blocks b ON COALESCE(oa.effective_block, oa.block_number) = b.number
1517
WHERE b.block_time < TIMESTAMP '{{.cutoffDate}}'
1618
),
1719
-- Get the latest record for each day
@@ -38,9 +40,12 @@ const operatorAllocationSnapshotsQuery = `
3840
magnitude,
3941
block_time,
4042
LAG(magnitude) OVER (PARTITION BY operator, avs, strategy, operator_set_id ORDER BY block_time, block_number, log_index) as previous_magnitude,
41-
-- Allocation (increase): Round UP to next day
42-
-- Deallocation (decrease or no change): Round DOWN to current day
43+
-- Backward compatibility: Apply new rounding logic only after Sabine fork
44+
-- Pre-Sabine: Always round down to current day (old behavior)
45+
-- Post-Sabine: Allocation (increase) rounds UP, deallocation rounds DOWN
4346
CASE
47+
{{ if .useSabineRounding }}
48+
-- Post-Sabine fork rounding logic
4449
WHEN LAG(magnitude) OVER (PARTITION BY operator, avs, strategy, operator_set_id ORDER BY block_time, block_number, log_index) IS NULL THEN
4550
-- First allocation: round up to next day
4651
date_trunc('day', block_time) + INTERVAL '1' day
@@ -50,6 +55,15 @@ const operatorAllocationSnapshotsQuery = `
5055
ELSE
5156
-- Decrease or no change: round down to current day
5257
date_trunc('day', block_time)
58+
{{ else }}
59+
-- Pre-Sabine fork rounding logic (backward compatibility)
60+
WHEN LAG(magnitude) OVER (PARTITION BY operator, avs, strategy, operator_set_id ORDER BY block_time, block_number, log_index) IS NULL THEN
61+
-- First allocation: round down to current day (old behavior)
62+
date_trunc('day', block_time)
63+
ELSE
64+
-- All other cases: round down to current day
65+
date_trunc('day', block_time)
66+
{{ end }}
5367
END AS snapshot_time
5468
FROM daily_records
5569
),
@@ -88,14 +102,33 @@ const operatorAllocationSnapshotsQuery = `
88102
`
89103

90104
func (r *RewardsCalculator) GenerateAndInsertOperatorAllocationSnapshots(snapshotDate string) error {
105+
// Determine if we should use Sabine fork rounding logic based on snapshot date
106+
forks, err := r.globalConfig.GetRewardsSqlForkDates()
107+
if err != nil {
108+
r.logger.Sugar().Errorw("Failed to get rewards fork dates", "error", err)
109+
return err
110+
}
111+
112+
useSabineRounding := false
113+
if sabineFork, exists := forks[config.RewardsFork_Sabine]; exists {
114+
// Use new rounding logic if snapshot date is on or after Sabine fork
115+
useSabineRounding = snapshotDate >= sabineFork.Date
116+
}
117+
91118
query, err := rewardsUtils.RenderQueryTemplate(operatorAllocationSnapshotsQuery, map[string]interface{}{
92-
"cutoffDate": snapshotDate,
119+
"cutoffDate": snapshotDate,
120+
"useSabineRounding": useSabineRounding,
93121
})
94122
if err != nil {
95123
r.logger.Sugar().Errorw("Failed to render query template", "error", err)
96124
return err
97125
}
98126

127+
r.logger.Sugar().Debugw("Generating operator allocation snapshots",
128+
zap.String("snapshotDate", snapshotDate),
129+
zap.Bool("useSabineRounding", useSabineRounding),
130+
)
131+
99132
res := r.grm.Exec(query)
100133
if res.Error != nil {
101134
r.logger.Sugar().Errorw("Failed to generate operator_allocation_snapshots",

pkg/rewards/stakerOperators/1_stakerStrategyPayouts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ staker_avs_strategy_shares AS (
5656
sdo.*,
5757
sss.shares
5858
FROM staker_delegated_operators sdo
59-
JOIN staker_share_snapshots_final sss
59+
JOIN staker_share_snapshots sss
6060
ON
6161
sdo.staker = sss.staker AND
6262
sdo.snapshot = sss.snapshot AND

0 commit comments

Comments
 (0)