Skip to content

Commit 6d252e7

Browse files
committed
add operator_set_id
1 parent 25ae8d5 commit 6d252e7

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@ type Migration struct {
1414
func (m *Migration) Up(db *sql.DB, grm *gorm.DB, cfg *config.Config) error {
1515
queries := []string{
1616
// =============================================================================
17-
// PART 1: Enhance queued_slashing_withdrawals to support withdrawal queue
17+
// PART 1: Withdrawal queue - no schema changes needed
1818
// =============================================================================
19-
20-
// Add completion tracking (timestamps can be derived from blocks table via FK)
21-
`alter table queued_slashing_withdrawals add column if not exists completion_block_number bigint`,
22-
23-
// Add FK constraint for completion block
24-
`alter table queued_slashing_withdrawals add constraint fk_completion_block foreign key (completion_block_number) references blocks(number) on delete set null`,
19+
// Note: Withdrawal queue logic uses withdrawable_date to determine when
20+
// shares should stop earning rewards. The withdrawable_date is calculated as
21+
// queued_date + 14 days. No additional columns needed in queued_slashing_withdrawals.
2522

2623
// =============================================================================
2724
// PART 2: Create operator_allocation_snapshots table for rewards calculation
@@ -35,8 +32,21 @@ func (m *Migration) Up(db *sql.DB, grm *gorm.DB, cfg *config.Config) error {
3532
strategy varchar not null,
3633
operator_set_id bigint not null,
3734
magnitude numeric not null,
38-
snapshot date not null
35+
snapshot date not null,
36+
primary key (operator, avs, strategy, operator_set_id, snapshot)
3937
)`,
38+
39+
// =============================================================================
40+
// PART 3: Update operator_allocations table for allocation/deallocation rounding
41+
// =============================================================================
42+
43+
// Add effective_date column for allocation/deallocation rounding
44+
// This is a computed value based on magnitude changes (round UP for increases, DOWN for decreases)
45+
// block_timestamp can be derived from block_number FK to blocks table
46+
`alter table operator_allocations add column if not exists effective_date date`,
47+
48+
// Create index for effective_date queries (includes operator_set_id for proper partitioning)
49+
`create index if not exists idx_operator_allocations_effective_date on operator_allocations(operator, avs, strategy, operator_set_id, effective_date)`,
4050
}
4151

4252
for _, query := range queries {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,14 @@ func (m *Migration) Up(db *sql.DB, grm *gorm.DB, cfg *config.Config) error {
4747
`create table if not exists deallocation_queue_snapshots (
4848
operator varchar not null,
4949
avs varchar not null,
50+
operator_set_id bigint not null,
5051
strategy varchar not null,
5152
magnitude_decrease numeric not null,
5253
block_date date not null,
5354
effective_date date not null,
5455
snapshot date not null,
55-
primary key (operator, avs, strategy, snapshot),
56-
constraint uniq_deallocation_queue_snapshots unique (operator, avs, strategy, snapshot)
56+
primary key (operator, avs, operator_set_id, strategy, snapshot),
57+
constraint uniq_deallocation_queue_snapshots unique (operator, avs, operator_set_id, strategy, snapshot)
5758
)`,
5859

5960
// Index for querying deallocation queue snapshots by snapshot date

pkg/rewards/deallocationQueueShareSnapshots.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,29 @@ import (
2525
// NOTE: This handles DECREASES (deallocations). Increases (allocations) are simpler
2626
// because they round UP, so they naturally start earning on effective_date.
2727
const deallocationQueueShareSnapshotsQuery = `
28+
insert into deallocation_queue_snapshots(
29+
operator,
30+
avs,
31+
operator_set_id,
32+
strategy,
33+
magnitude_decrease,
34+
block_date,
35+
effective_date,
36+
snapshot
37+
)
2838
with deallocation_adjustments as (
2939
select
3040
oa.operator,
3141
oa.avs,
42+
oa.operator_set_id,
3243
oa.strategy,
3344
oa.magnitude as new_magnitude,
3445
oa.effective_date,
3546
date(b.block_time) as block_date,
3647
oa.block_number,
3748
-- Get previous allocation to calculate the difference
3849
lag(oa.magnitude) over (
39-
partition by oa.operator, oa.avs, oa.strategy
50+
partition by oa.operator, oa.avs, oa.operator_set_id, oa.strategy
4051
order by oa.block_number, oa.log_index
4152
) as prev_magnitude
4253
from operator_allocations oa
@@ -52,6 +63,7 @@ const deallocationQueueShareSnapshotsQuery = `
5263
select
5364
operator,
5465
avs,
66+
operator_set_id,
5567
strategy,
5668
new_magnitude,
5769
prev_magnitude,
@@ -64,18 +76,10 @@ const deallocationQueueShareSnapshotsQuery = `
6476
prev_magnitude is not null
6577
and new_magnitude::numeric < prev_magnitude::numeric
6678
)
67-
insert into deallocation_queue_snapshots(
68-
operator,
69-
avs,
70-
strategy,
71-
magnitude_decrease,
72-
block_date,
73-
effective_date,
74-
snapshot
75-
)
7679
select
7780
operator,
7881
avs,
82+
operator_set_id,
7983
strategy,
8084
magnitude_decrease,
8185
block_date,
@@ -88,6 +92,7 @@ const deallocationQueueShareSnapshotsQuery = `
8892
type DeallocationQueueSnapshot struct {
8993
Operator string `gorm:"column:operator;primaryKey"`
9094
Avs string `gorm:"column:avs;primaryKey"`
95+
OperatorSetId uint64 `gorm:"column:operator_set_id;primaryKey"`
9196
Strategy string `gorm:"column:strategy;primaryKey"`
9297
MagnitudeDecrease string `gorm:"column:magnitude_decrease"`
9398
BlockDate string `gorm:"column:block_date"`

0 commit comments

Comments
 (0)