Skip to content

Commit

Permalink
refactor: use root_index rather than snapshot as a request argument
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmcgary committed Jan 10, 2025
1 parent 0b8d30f commit 7261d60
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 26 deletions.
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.1.0
github.com/Layr-Labs/protocol-apis v1.1.1-0.20250110201843-c2f2cf37e910
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 @@ -43,6 +43,10 @@ github.com/Layr-Labs/eigenlayer-rewards-proofs v0.2.13 h1:Blb4AE+jC/vddV71w4/MQA
github.com/Layr-Labs/eigenlayer-rewards-proofs v0.2.13/go.mod h1:PD/HoyzZjxDw1tAcZw3yD0yGddo+yhmwQAi+lk298r4=
github.com/Layr-Labs/protocol-apis v1.1.0 h1:PO6x+Y9ORiac2dkaWJayRFqhyzcvMbvRQkDIpLTNtVc=
github.com/Layr-Labs/protocol-apis v1.1.0/go.mod h1:prNA2/mLO5vpMZ2q78Nsn0m97wm28uiRnwO+/yOxigk=
github.com/Layr-Labs/protocol-apis v1.1.1-0.20250110201222-e8670ac00c32 h1:nRHAH0dn5qkQXUjdrPlGThtKLt154UKAHfzCEyMqfr0=
github.com/Layr-Labs/protocol-apis v1.1.1-0.20250110201222-e8670ac00c32/go.mod h1:prNA2/mLO5vpMZ2q78Nsn0m97wm28uiRnwO+/yOxigk=
github.com/Layr-Labs/protocol-apis v1.1.1-0.20250110201843-c2f2cf37e910 h1:X3t1mr1kAOGDJ3paPS/lzHJchK4y+oWSJ+D/7+MgOmY=
github.com/Layr-Labs/protocol-apis v1.1.1-0.20250110201843-c2f2cf37e910/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
21 changes: 9 additions & 12 deletions pkg/proofs/rewardsProofs.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,21 @@ func (rps *RewardsProofsStore) getRewardsDataForSnapshot(snapshot string) (*Proo
return data, nil
}

func (rps *RewardsProofsStore) GenerateRewardsClaimProof(earnerAddress string, tokenAddresses []string, snapshotDate string) (
func (rps *RewardsProofsStore) GenerateRewardsClaimProof(earnerAddress string, tokenAddresses []string, rootIndex int64) (
[]byte,
*rewardsCoordinator.IRewardsCoordinatorRewardsMerkleClaim,
error,
) {
if snapshotDate == "" {
snapshotDate = "latest"
}

distributionRoot, err := rps.rewardsCalculator.FindClaimableDistributionRoot(snapshotDate)
distributionRoot, err := rps.rewardsCalculator.FindClaimableDistributionRoot(rootIndex)
if err != nil {
rps.logger.Sugar().Errorf("Failed to find most claimable distribution root", zap.Error(err))
rps.logger.Sugar().Errorf("Failed to find claimable distribution root for root_index",
zap.Int64("rootIndex", rootIndex),
zap.Error(err),
)
return nil, nil, err
}
if snapshotDate == "latest" {
snapshotDate = distributionRoot.GetSnapshotDate()
}

snapshotDate := distributionRoot.GetSnapshotDate()

// Make sure rewards have been generated for this snapshot.
// Any snapshot that is >= the provided date is valid since we'll select only data up
Expand Down Expand Up @@ -102,11 +100,10 @@ func (rps *RewardsProofsStore) GenerateRewardsClaimProof(earnerAddress string, t
return gethcommon.HexToAddress(addr)
})
earner := gethcommon.HexToAddress(earnerAddress)
rootIndex := distributionRoot.RootIndex

claim, err := claimgen.GetProofForEarner(
proofData.Distribution,
uint32(rootIndex),
uint32(distributionRoot.RootIndex),
proofData.AccountTree,
proofData.TokenTree,
earner,
Expand Down
20 changes: 9 additions & 11 deletions pkg/rewards/rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,26 +675,25 @@ func (rc *RewardsCalculator) generateAndInsertFromQuery(
)
}

func (rc *RewardsCalculator) FindClaimableDistributionRoot(snapshotDate string) (*types.SubmittedDistributionRoot, error) {
if snapshotDate == "" {
snapshotDate = "latest"
}
func (rc *RewardsCalculator) FindClaimableDistributionRoot(rootIndex int64) (*types.SubmittedDistributionRoot, error) {
query := `
select
*
from submitted_distribution_roots as sdr
left join disabled_distribution_roots as ddr on (sdr.root_index = ddr.root_index)
where
ddr.root_index is null
{{ if eq .snapshotDate "latest" }}
{{ if eq .rootIndex "-1" }}
and activated_at >= now()
{{ else }}
and activated_at >= '{{.snapshotDate}}'::timestamp(6)
and sdr.root_index = {{.rootIndex}}
{{ end }}
order by root_index desc
limit 1
`
renderedQuery, err := rewardsUtils.RenderQueryTemplate(query, map[string]string{"snapshotDate": snapshotDate})
renderedQuery, err := rewardsUtils.RenderQueryTemplate(query, map[string]string{
"rootIndex": fmt.Sprintf("%d", rootIndex),
})
if err != nil {
rc.logger.Sugar().Errorw("Failed to render query template", "error", err)
return nil, err
Expand All @@ -704,8 +703,8 @@ func (rc *RewardsCalculator) FindClaimableDistributionRoot(snapshotDate string)
res := rc.grm.Raw(renderedQuery).Scan(&submittedDistributionRoot)
if res.Error != nil {
if errors.Is(res.Error, gorm.ErrRecordNotFound) {
rc.logger.Sugar().Errorw("No active distribution root found for snapshot",
zap.String("snapshotDate", snapshotDate),
rc.logger.Sugar().Errorw("No active distribution root found by root_index",
zap.Int64("rootIndex", rootIndex),
zap.Error(res.Error),
)
return nil, res.Error
Expand All @@ -724,9 +723,8 @@ func (rc *RewardsCalculator) GetGeneratedRewardsForSnapshotDate(snapshotDate str
from generated_rewards_snapshots as grs
where
status = 'complete'
{{if ne .snapshotDate "latest"}}
and grs.snapshot_date::timestamp(6) >= '{{.snapshotDate}}'::timestamp(6)
{{end}}
order by grs.snapshot_date asc
limit 1
`, map[string]string{"snapshotDate": snapshotDate})

Expand Down
11 changes: 9 additions & 2 deletions pkg/rpcServer/proofsHandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,16 @@ func convertClaimProofToRPCResponse(solidityProof *claimgen.IRewardsCoordinatorR
func (rpc *RpcServer) GenerateClaimProof(ctx context.Context, req *sidecarV1.GenerateClaimProofRequest) (*sidecarV1.GenerateClaimProofResponse, error) {
earner := req.GetEarnerAddress()
tokens := req.GetTokens()
snapshotDate := req.GetSnapshot()
rootIndex := req.GetRootIndex()

root, claim, err := rpc.rewardsProofs.GenerateRewardsClaimProof(earner, tokens, snapshotDate)
var rootIndexVal int64
if rootIndex == nil {
rootIndexVal = -1
} else {
rootIndexVal = rootIndex.GetValue()
}

root, claim, err := rpc.rewardsProofs.GenerateRewardsClaimProof(earner, tokens, rootIndexVal)
if err != nil {
return nil, status.Errorf(codes.Internal, "Failed to generate claim proof %s", err.Error())
}
Expand Down

0 comments on commit 7261d60

Please sign in to comment.