Skip to content

Commit

Permalink
Add a backfill all function
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmcgary committed Dec 4, 2024
1 parent 291041a commit 8f32a89
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/DataDog/datadog-go/v5 v5.5.0
github.com/Layr-Labs/eigenlayer-contracts v0.4.1-holesky-pepe.0.20240813143901-00fc4b95e9c1
github.com/Layr-Labs/eigenlayer-rewards-proofs v0.2.13
github.com/Layr-Labs/protocol-apis v1.0.0-rc.1
github.com/Layr-Labs/protocol-apis v1.0.0-rc.1.0.20241204030420-83d31161930e
github.com/ethereum/go-ethereum v1.14.9
github.com/gocarina/gocsv v0.0.0-20240520201108-78e41c74b4b1
github.com/google/uuid v1.6.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ github.com/Layr-Labs/protocol-apis v0.1.0-beta.3.0.20241203214053-44b580f4ea84 h
github.com/Layr-Labs/protocol-apis v0.1.0-beta.3.0.20241203214053-44b580f4ea84/go.mod h1:prNA2/mLO5vpMZ2q78Nsn0m97wm28uiRnwO+/yOxigk=
github.com/Layr-Labs/protocol-apis v1.0.0-rc.1 h1:N3OAsdZ5V/QVAjsJbJa1kruocoi50jfLquyVk4bL9HQ=
github.com/Layr-Labs/protocol-apis v1.0.0-rc.1/go.mod h1:prNA2/mLO5vpMZ2q78Nsn0m97wm28uiRnwO+/yOxigk=
github.com/Layr-Labs/protocol-apis v1.0.0-rc.1.0.20241203225729-619c724a75e3 h1:kc3jPZzgTsXWTPDzEi9iB9DAc2+soPHsGd41KTVhC04=
github.com/Layr-Labs/protocol-apis v1.0.0-rc.1.0.20241203225729-619c724a75e3/go.mod h1:prNA2/mLO5vpMZ2q78Nsn0m97wm28uiRnwO+/yOxigk=
github.com/Layr-Labs/protocol-apis v1.0.0-rc.1.0.20241204030420-83d31161930e h1:h6ptdsDTKiTldAyel+XXfbusrarpF2+arhPlaFUeq7M=
github.com/Layr-Labs/protocol-apis v1.0.0-rc.1.0.20241204030420-83d31161930e/go.mod h1:prNA2/mLO5vpMZ2q78Nsn0m97wm28uiRnwO+/yOxigk=
github.com/Microsoft/go-winio v0.5.0/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84=
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
Expand Down
35 changes: 35 additions & 0 deletions pkg/rewards/rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,41 @@ func (rc *RewardsCalculator) GetMaxSnapshotDateForCutoffDate(cutoffDate string)
return maxSnapshotStr, nil
}

func (rc *RewardsCalculator) BackfillAllStakerOperators() error {
var generatedSnapshots []storage.GeneratedRewardsSnapshots
query := `select * from generated_rewards_snapshots order by snapshot_date asc`
res := rc.grm.Raw(query).Scan(&generatedSnapshots)
if res.Error != nil {
rc.logger.Sugar().Errorw("Failed to get generated snapshots", "error", res.Error)
return res.Error
}

// First acquire a lock. If we cant, return and let the caller retry.
if rc.GetIsGenerating() {
err := &ErrRewardsCalculationInProgress{}
rc.logger.Sugar().Infow(err.Error())
return err
}
rc.acquireGenerationLock()
defer rc.releaseGenerationLock()

// take the largest snapshot date and generate the snapshot tables, which will be all-inclusive
latestSnapshotDate := generatedSnapshots[len(generatedSnapshots)-1].SnapshotDate
if err := rc.generateSnapshotData(latestSnapshotDate); err != nil {
rc.logger.Sugar().Errorw("Failed to generate snapshot data", "error", err)
return err
}

// iterate over each snapshot and generate the staker operators table data for each
for _, snapshot := range generatedSnapshots {
if err := rc.sog.GenerateStakerOperatorsTable(snapshot.SnapshotDate); err != nil {
rc.logger.Sugar().Errorw("Failed to generate staker operators table", "error", err)
return err
}
}
return nil
}

// GenerateStakerOperatorsTableForPastSnapshot generates the staker operators table for a past snapshot date, OR
// generates the rewards and the related staker-operator table data if the snapshot is greater than the latest snapshot.
func (rc *RewardsCalculator) GenerateStakerOperatorsTableForPastSnapshot(cutoffDate string) error {
Expand Down
11 changes: 11 additions & 0 deletions pkg/rpcServer/rewardsHandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ func (rpc *RpcServer) GenerateStakerOperators(ctx context.Context, req *sidecarV
return &sidecarV1.GenerateStakerOperatorsResponse{}, nil
}

func (rpc *RpcServer) BackfillStakerOperators(ctx context.Context, req *sidecarV1.BackfillStakerOperatorsRequest) (*sidecarV1.BackfillStakerOperatorsResponse, error) {
err := rpc.rewardsCalculator.BackfillAllStakerOperators()
if err != nil {
if errors.Is(err, &rewards.ErrRewardsCalculationInProgress{}) {
return nil, status.Error(codes.FailedPrecondition, err.Error())
}
return nil, status.Error(codes.Internal, err.Error())
}
return &sidecarV1.BackfillStakerOperatorsResponse{}, nil
}

func (rpc *RpcServer) GetRewardsForSnapshot(ctx context.Context, req *sidecarV1.GetRewardsForSnapshotRequest) (*sidecarV1.GetRewardsForSnapshotResponse, error) {
return nil, status.Error(codes.Unimplemented, "method GetRewardsForSnapshot not implemented")
}
Expand Down

0 comments on commit 8f32a89

Please sign in to comment.