Skip to content

Commit c6ae65a

Browse files
committed
create operator_allocation_snapshots table for rounding logic
1 parent 68e1b83 commit c6ae65a

File tree

7 files changed

+323
-405
lines changed

7 files changed

+323
-405
lines changed

pkg/eigenState/operatorAllocations/operatorAllocations.go

Lines changed: 0 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"slices"
88
"sort"
99
"strings"
10-
"time"
1110

1211
"github.com/Layr-Labs/sidecar/internal/config"
1312
"github.com/Layr-Labs/sidecar/pkg/eigenState/base"
@@ -28,7 +27,6 @@ type OperatorAllocation struct {
2827
BlockNumber uint64
2928
TransactionHash string
3029
LogIndex uint64
31-
EffectiveDate string // Rounded date when allocation takes effect (YYYY-MM-DD)
3230
}
3331

3432
type OperatorAllocationModel struct {
@@ -121,27 +119,6 @@ func (oa *OperatorAllocationModel) handleOperatorAllocationCreatedEvent(log *sto
121119
LogIndex: log.LogIndex,
122120
}
123121

124-
// Sabine fork: Apply rounding logic and populate date fields
125-
isSabineForkActive, err := oa.IsActiveForSabineForkBlockHeight(log.BlockNumber)
126-
if err != nil {
127-
return nil, err
128-
}
129-
if isSabineForkActive {
130-
effectiveDateStr, err := oa.calculateAllocationDates(
131-
log.BlockNumber,
132-
magnitude,
133-
strings.ToLower(outputData.Operator),
134-
strings.ToLower(outputData.OperatorSet.Avs),
135-
strings.ToLower(outputData.Strategy),
136-
outputData.OperatorSet.Id,
137-
)
138-
if err != nil {
139-
return nil, err
140-
}
141-
142-
split.EffectiveDate = effectiveDateStr
143-
}
144-
145122
return split, nil
146123
}
147124

@@ -363,123 +340,3 @@ func (oa *OperatorAllocationModel) IsActiveForBlockHeight(blockHeight uint64) (b
363340

364341
return blockHeight >= forks[config.RewardsFork_Brazos].BlockNumber, nil
365342
}
366-
367-
func (oa *OperatorAllocationModel) IsActiveForSabineForkBlockHeight(blockHeight uint64) (bool, error) {
368-
forks, err := oa.globalConfig.GetRewardsSqlForkDates()
369-
if err != nil {
370-
oa.logger.Sugar().Errorw("Failed to get rewards sql fork dates", zap.Error(err))
371-
return false, err
372-
}
373-
374-
return blockHeight >= forks[config.RewardsFork_Sabine].BlockNumber, nil
375-
}
376-
377-
// calculateAllocationDates calculates the effective date for an allocation
378-
// This encapsulates the logic for querying block data and applying rounding rules
379-
// Block timestamp can be derived from block_number FK to blocks table
380-
func (oa *OperatorAllocationModel) calculateAllocationDates(
381-
blockNumber uint64,
382-
magnitude *big.Int,
383-
operator string,
384-
avs string,
385-
strategy string,
386-
operatorSetId uint64,
387-
) (effectiveDateStr string, err error) {
388-
// 1. Get block timestamp from blocks table
389-
var block storage.Block
390-
result := oa.DB.Where("number = ?", blockNumber).First(&block)
391-
if result.Error != nil {
392-
oa.logger.Sugar().Errorw("Failed to query block timestamp",
393-
zap.Error(result.Error),
394-
zap.Uint64("blockNumber", blockNumber),
395-
)
396-
return "", fmt.Errorf("failed to query block timestamp: %w", result.Error)
397-
}
398-
399-
// 2. Get previous allocation magnitude for comparison
400-
previousMagnitude, err := oa.getPreviousAllocationMagnitude(
401-
operator,
402-
avs,
403-
strategy,
404-
operatorSetId,
405-
blockNumber,
406-
)
407-
if err != nil {
408-
oa.logger.Sugar().Errorw("Failed to get previous allocation magnitude",
409-
zap.Error(err),
410-
zap.Uint64("blockNumber", blockNumber),
411-
zap.String("operator", operator),
412-
zap.String("avs", avs),
413-
zap.String("strategy", strategy),
414-
zap.Uint64("operatorSetId", operatorSetId),
415-
)
416-
return "", fmt.Errorf("failed to get previous allocation magnitude: %w", err)
417-
}
418-
419-
// 3. Determine effective date using rounding rules
420-
effectiveDate := oa.determineEffectiveDate(block.BlockTime, magnitude, previousMagnitude)
421-
422-
// 4. Format and return date string
423-
effectiveDateStr = effectiveDate.Format("2006-01-02")
424-
425-
return effectiveDateStr, nil
426-
}
427-
428-
// getPreviousAllocationMagnitude retrieves the most recent allocation magnitude
429-
// for the given operator-avs-strategy combination before the specified block number
430-
func (oa *OperatorAllocationModel) getPreviousAllocationMagnitude(
431-
operator string,
432-
avs string,
433-
strategy string,
434-
operatorSetId uint64,
435-
currentBlockNumber uint64,
436-
) (*big.Int, error) {
437-
var previousAllocation OperatorAllocation
438-
439-
result := oa.DB.
440-
Where("operator = ?", operator).
441-
Where("avs = ?", avs).
442-
Where("strategy = ?", strategy).
443-
Where("operator_set_id = ?", operatorSetId).
444-
Where("block_number < ?", currentBlockNumber).
445-
Order("block_number DESC, log_index DESC").
446-
Limit(1).
447-
First(&previousAllocation)
448-
449-
if result.Error != nil {
450-
if result.Error == gorm.ErrRecordNotFound {
451-
// No previous allocation found, return 0
452-
return big.NewInt(0), nil
453-
}
454-
return nil, result.Error
455-
}
456-
457-
magnitude, success := new(big.Int).SetString(previousAllocation.Magnitude, 10)
458-
if !success {
459-
return nil, fmt.Errorf("failed to parse previous magnitude: %s", previousAllocation.Magnitude)
460-
}
461-
462-
return magnitude, nil
463-
}
464-
465-
// determineEffectiveDate determines the effective date for an allocation based on rounding rules
466-
// - Allocation (increase): Round UP to next day
467-
// - Deallocation (decrease): Round DOWN to current day
468-
func (oa *OperatorAllocationModel) determineEffectiveDate(
469-
blockTimestamp time.Time,
470-
newMagnitude *big.Int,
471-
previousMagnitude *big.Int,
472-
) time.Time {
473-
blockTimestamp = blockTimestamp.UTC()
474-
comparison := newMagnitude.Cmp(previousMagnitude)
475-
476-
year, month, day := blockTimestamp.Date()
477-
midnightUTC := time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
478-
479-
if comparison > 0 {
480-
// Allocation (increase) - always round up to next day
481-
return midnightUTC.Add(24 * time.Hour)
482-
}
483-
// Deallocation (decrease or no change) - round down to current day
484-
return midnightUTC
485-
}

0 commit comments

Comments
 (0)