From 8f32a89c775f73c6ad43ddf385dcd333b7768dd9 Mon Sep 17 00:00:00 2001 From: Sean McGary Date: Tue, 3 Dec 2024 21:13:54 -0600 Subject: [PATCH] Add a backfill all function --- go.mod | 2 +- go.sum | 4 ++++ pkg/rewards/rewards.go | 35 ++++++++++++++++++++++++++++++++ pkg/rpcServer/rewardsHandlers.go | 11 ++++++++++ 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 31fc7222..aeafb764 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 05507133..b75358d3 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/rewards/rewards.go b/pkg/rewards/rewards.go index 212b419a..b71d9c34 100644 --- a/pkg/rewards/rewards.go +++ b/pkg/rewards/rewards.go @@ -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 { diff --git a/pkg/rpcServer/rewardsHandlers.go b/pkg/rpcServer/rewardsHandlers.go index c1c3d0df..8bd406ae 100644 --- a/pkg/rpcServer/rewardsHandlers.go +++ b/pkg/rpcServer/rewardsHandlers.go @@ -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") }