Skip to content

Commit d310df6

Browse files
committed
fix
1 parent 354b9d3 commit d310df6

File tree

12 files changed

+982
-24
lines changed

12 files changed

+982
-24
lines changed

internal/config/config.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -862,14 +862,11 @@ func (c *Config) IsRewardsV2_1EnabledForCutoffDate(cutoffDate string) (bool, err
862862
}
863863

864864
func (c *Config) IsRewardsV2_2EnabledForCutoffDate(cutoffDate string) (bool, error) {
865-
<<<<<<< HEAD
866-
=======
867865
// Check global flag first - if disabled, return false regardless of date
868866
if !c.Rewards.RewardsV2_2Enabled {
869867
return false, nil
870868
}
871869

872-
>>>>>>> e7a0eb58 (feat: implement Operator Set Rewards with Unique & Total Stake)
873870
forks, err := c.GetRewardsSqlForkDates()
874871
if err != nil {
875872
return false, err
@@ -878,10 +875,6 @@ func (c *Config) IsRewardsV2_2EnabledForCutoffDate(cutoffDate string) (bool, err
878875
if err != nil {
879876
return false, errors.Join(fmt.Errorf("failed to parse cutoff date %s", cutoffDate), err)
880877
}
881-
<<<<<<< HEAD
882-
// TODO: Need to change to fork sabine
883-
=======
884-
>>>>>>> e7a0eb58 (feat: implement Operator Set Rewards with Unique & Total Stake)
885878
pecosForkDateTime, err := time.Parse(time.DateOnly, forks[RewardsFork_Pecos].Date)
886879
if err != nil {
887880
return false, errors.Join(fmt.Errorf("failed to parse Pecos fork date %s", forks[RewardsFork_Pecos].Date), err)

pkg/postgres/migrations/202511141700_withdrawalQueueAndAllocationRounding/up.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,14 @@ func (m *Migration) Up(db *sql.DB, grm *gorm.DB, cfg *config.Config) error {
3232
strategy varchar not null,
3333
operator_set_id bigint not null,
3434
magnitude numeric not null,
35+
max_magnitude numeric not null,
3536
snapshot date not null,
3637
primary key (operator, avs, strategy, operator_set_id, snapshot)
3738
)`,
3839

40+
// Add max_magnitude column if table already exists without it
41+
`ALTER TABLE operator_allocation_snapshots ADD COLUMN IF NOT EXISTS max_magnitude numeric NOT NULL DEFAULT '0'`,
42+
3943
// =============================================================================
4044
// PART 3: Update operator_allocations table for allocation/deallocation rounding
4145
// =============================================================================

pkg/rewards/15_goldOperatorOperatorSetUniqueStakeRewards.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func (rc *RewardsCalculator) GenerateGold15OperatorOperatorSetUniqueStakeRewards
136136
query, err := rewardsUtils.RenderQueryTemplate(_15_goldOperatorOperatorSetUniqueStakeRewardsQuery, map[string]interface{}{
137137
"destTableName": destTableName,
138138
"activeODRewardsTable": allTableNames[rewardsUtils.Table_11_ActiveODOperatorSetRewards],
139-
"operatorAllocationSnapshotsTable": allTableNames[rewardsUtils.Table_OperatorAllocationSnapshots],
139+
"operatorAllocationSnapshotsTable": "operator_allocation_snapshots",
140140
"operatorShareSnapshotsTable": "operator_share_snapshots",
141141
})
142142
if err != nil {

pkg/rewards/17_goldAvsOperatorSetUniqueStakeRewards.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func (rc *RewardsCalculator) GenerateGold17AvsOperatorSetUniqueStakeRewardsTable
159159
query, err := rewardsUtils.RenderQueryTemplate(_17_goldAvsOperatorSetUniqueStakeRewardsQuery, map[string]interface{}{
160160
"destTableName": destTableName,
161161
"activeODRewardsTable": allTableNames[rewardsUtils.Table_11_ActiveODOperatorSetRewards],
162-
"operatorAllocationSnapshotsTable": allTableNames[rewardsUtils.Table_OperatorAllocationSnapshots],
162+
"operatorAllocationSnapshotsTable": "operator_allocation_snapshots",
163163
})
164164
if err != nil {
165165
rc.logger.Sugar().Errorw("Failed to render query template", "error", err)

pkg/rewards/operatorAllocationSnapshots.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
const operatorAllocationSnapshotsQuery = `
10-
insert into operator_allocation_snapshots(operator, avs, strategy, operator_set_id, magnitude, snapshot)
10+
insert into operator_allocation_snapshots(operator, avs, strategy, operator_set_id, magnitude, max_magnitude, snapshot)
1111
WITH ranked_allocation_records as (
1212
SELECT *,
1313
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
@@ -86,16 +86,48 @@ const operatorAllocationSnapshotsQuery = `
8686
cleaned_records as (
8787
SELECT * FROM allocation_windows
8888
WHERE start_time < end_time
89+
),
90+
-- Get the latest max_magnitude for each (operator, strategy) as of the cutoff date
91+
latest_max_magnitudes as (
92+
SELECT
93+
omm.operator,
94+
omm.strategy,
95+
omm.max_magnitude,
96+
ROW_NUMBER() OVER (
97+
PARTITION BY omm.operator, omm.strategy
98+
ORDER BY omm.block_number DESC, omm.log_index DESC
99+
) AS rn
100+
FROM operator_max_magnitudes omm
101+
INNER JOIN blocks b ON omm.block_number = b.number
102+
WHERE b.block_time < TIMESTAMP '{{.cutoffDate}}'
103+
),
104+
-- Join allocations with max_magnitudes
105+
allocations_with_max as (
106+
SELECT
107+
cr.operator,
108+
cr.avs,
109+
cr.strategy,
110+
cr.operator_set_id,
111+
cr.magnitude,
112+
COALESCE(lmm.max_magnitude, '0') as max_magnitude,
113+
cr.start_time,
114+
cr.end_time
115+
FROM cleaned_records cr
116+
LEFT JOIN latest_max_magnitudes lmm
117+
ON cr.operator = lmm.operator
118+
AND cr.strategy = lmm.strategy
119+
AND lmm.rn = 1
89120
)
90121
SELECT
91122
operator,
92123
avs,
93124
strategy,
94125
operator_set_id,
95126
magnitude,
127+
max_magnitude,
96128
cast(day AS DATE) AS snapshot
97129
FROM
98-
cleaned_records
130+
allocations_with_max
99131
CROSS JOIN
100132
generate_series(DATE(start_time), DATE(end_time) - interval '1' day, interval '1' day) AS day
101133
on conflict do nothing;

pkg/rewards/operatorShareSnapshots.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ combined_snapshots as (
6666
SELECT
6767
coalesce(base.operator, alloc.operator) as operator,
6868
coalesce(base.strategy, alloc.strategy) as strategy,
69-
coalesce(alloc.total_magnitude::text, base.shares) as shares,
69+
coalesce(alloc.total_magnitude, base.shares::numeric) as shares,
7070
coalesce(base.snapshot, alloc.snapshot) as snapshot
7171
FROM base_snapshots base
7272
FULL OUTER JOIN allocation_adjustments alloc

pkg/rewards/rewardsV2_2_integration_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package rewards
33
import (
44
"fmt"
55
"math/big"
6-
"strings"
76
"testing"
87
"time"
98

@@ -268,14 +267,13 @@ func Test_RewardsV2_2_WithWithdrawalQueue_Integration(t *testing.T) {
268267
err := rc.generateSnapshotData(snapshotDate)
269268
require.NoError(t, err)
270269

271-
// Check operator allocation snapshots
270+
// Check operator allocation snapshots (persistent table, not per-date)
272271
var allocatedStake string
273-
tableName := fmt.Sprintf("operator_allocation_snapshots_%s", strings.ReplaceAll(snapshotDate, "-", "_"))
274-
query := fmt.Sprintf(`
272+
query := `
275273
SELECT COALESCE(SUM(magnitude::numeric), 0)::text
276-
FROM %s
274+
FROM operator_allocation_snapshots
277275
WHERE snapshot = $1 AND operator = $2
278-
`, tableName)
276+
`
279277
err = grm.Raw(query, snapshotDate, "operator1").Scan(&allocatedStake).Error
280278
require.NoError(t, err)
281279

pkg/rewards/rewardsV2_2_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,8 @@ func Test_RewardsV2_2(t *testing.T) {
301301
assert.Nil(t, err)
302302

303303
// V2.2 specific validation - ensure operator allocations snapshot was created
304-
operatorAllocSnapshotTable := goldTableNames[rewardsUtils.Table_OperatorAllocationSnapshots]
304+
// Note: operator_allocation_snapshots is a persistent table without date suffix
305+
operatorAllocSnapshotTable := "operator_allocation_snapshots"
305306
rows, err = getRowCountForTable(grm, operatorAllocSnapshotTable)
306307
assert.Nil(t, err)
307308
t.Logf("Operator allocation snapshots: %v", rows)

pkg/rewards/stakerShareSnapshots.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const stakerShareSnapshotsQuery = `
6868
SELECT
6969
coalesce(base.staker, wq.staker) as staker,
7070
coalesce(base.strategy, wq.strategy) as strategy,
71-
(coalesce(base.shares::numeric, 0) + coalesce(wq.shares::numeric, 0))::text as shares,
71+
(coalesce(base.shares::numeric, 0) + coalesce(wq.shares::numeric, 0)) as shares,
7272
coalesce(base.snapshot, wq.snapshot) as snapshot
7373
FROM base_snapshots base
7474
FULL OUTER JOIN withdrawal_queue_adjustments wq

pkg/rewards/tables.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,5 +160,6 @@ type OperatorAllocationSnapshot struct {
160160
Strategy string
161161
OperatorSetId uint64
162162
Magnitude string
163+
MaxMagnitude string
163164
Snapshot time.Time
164165
}

0 commit comments

Comments
 (0)