Skip to content

Commit

Permalink
feat: latest active root via contract call (#194)
Browse files Browse the repository at this point in the history
* feat: latest active root via contract call

* remove unused code
  • Loading branch information
shrimalmadhur authored Aug 15, 2024
1 parent abc7880 commit 88db313
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 120 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/Layr-Labs/eigenlayer-contracts v0.3.2-mainnet-rewards
github.com/Layr-Labs/eigenlayer-rewards-proofs v0.2.10
github.com/Layr-Labs/eigenpod-proofs-generation v0.0.14-stable.0.20240730152248-5c11a259293e
github.com/Layr-Labs/eigensdk-go v0.1.10-0.20240806213237-f9be5dc855c8
github.com/Layr-Labs/eigensdk-go v0.1.10
github.com/blang/semver/v4 v4.0.0
github.com/consensys/gnark-crypto v0.12.1
github.com/ethereum/go-ethereum v1.14.5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ github.com/Layr-Labs/eigenlayer-rewards-proofs v0.2.10 h1:Uxf0MaBJNiFjEdNGuCmm/U
github.com/Layr-Labs/eigenlayer-rewards-proofs v0.2.10/go.mod h1:OlJd1QjqEW53wfWG/lJyPCGvrXwWVEjPQsP4TV+gttQ=
github.com/Layr-Labs/eigenpod-proofs-generation v0.0.14-stable.0.20240730152248-5c11a259293e h1:DvW0/kWHV9mZsbH2KOjEHKTSIONNPUj6X05FJvUohy4=
github.com/Layr-Labs/eigenpod-proofs-generation v0.0.14-stable.0.20240730152248-5c11a259293e/go.mod h1:T7tYN8bTdca2pkMnz9G2+ZwXYWw5gWqQUIu4KLgC/vM=
github.com/Layr-Labs/eigensdk-go v0.1.10-0.20240806213237-f9be5dc855c8 h1:Yu9Fhlriq0zB5zo9yS8vN5CvLpydhVwHxLWjAuIetP0=
github.com/Layr-Labs/eigensdk-go v0.1.10-0.20240806213237-f9be5dc855c8/go.mod h1:XcLVDtlB1vOPj63D236b451+SC75B8gwgkpNhYHSxNs=
github.com/Layr-Labs/eigensdk-go v0.1.10 h1:VWXSoJ0Vm2ZEfSPgftOgmek3xwXKRmeKrNFXfMJJlko=
github.com/Layr-Labs/eigensdk-go v0.1.10/go.mod h1:XcLVDtlB1vOPj63D236b451+SC75B8gwgkpNhYHSxNs=
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
github.com/Microsoft/hcsshim v0.11.4 h1:68vKo2VN8DE9AdN4tnkWnmdhqdbpUFM8OF3Airm7fz8=
Expand Down
2 changes: 1 addition & 1 deletion pkg/operator/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func RegisterCmd(p utils.Prompter) *cli.Command {
}

if !status {
receipt, err := elWriter.RegisterAsOperator(ctx, operatorCfg.Operator)
receipt, err := elWriter.RegisterAsOperator(ctx, operatorCfg.Operator, true)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/operator/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ This command only updates above details. To update metadata URI, use eigenlayer
return err
}

receipt, err := elWriter.UpdateOperatorDetails(context.Background(), operatorCfg.Operator)
receipt, err := elWriter.UpdateOperatorDetails(context.Background(), operatorCfg.Operator, true)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/operator/update_metadata_uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Requires the same file used for registration as argument
return err
}

receipt, err := elWriter.UpdateMetadataURI(context.Background(), operatorCfg.Operator.MetadataUrl)
receipt, err := elWriter.UpdateMetadataURI(context.Background(), operatorCfg.Operator.MetadataUrl, true)
if err != nil {
fmt.Printf("%s Error while updating operator metadata uri\n", utils.EmojiCrossMark)
return err
Expand Down
36 changes: 9 additions & 27 deletions pkg/rewards/claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
contractrewardscoordinator "github.com/Layr-Labs/eigenlayer-contracts/pkg/bindings/IRewardsCoordinator"

"github.com/Layr-Labs/eigenlayer-rewards-proofs/pkg/claimgen"
"github.com/Layr-Labs/eigenlayer-rewards-proofs/pkg/proofDataFetcher"
"github.com/Layr-Labs/eigenlayer-rewards-proofs/pkg/proofDataFetcher/httpProofDataFetcher"

"github.com/Layr-Labs/eigensdk-go/chainio/clients/elcontracts"
Expand Down Expand Up @@ -187,7 +186,7 @@ func Claim(cCtx *cli.Context, p utils.Prompter) error {
return eigenSdkUtils.WrapError("failed to create new writer from config", err)
}

receipt, err := eLWriter.ProcessClaim(ctx, elClaim, config.RecipientAddress)
receipt, err := eLWriter.ProcessClaim(ctx, elClaim, config.RecipientAddress, true)
if err != nil {
return eigenSdkUtils.WrapError("failed to process claim", err)
}
Expand Down Expand Up @@ -268,51 +267,34 @@ func getClaimDistributionRoot(
return "", 0, eigenSdkUtils.WrapError("failed to get latest submitted timestamp", err)
}
claimDate := time.Unix(int64(latestSubmittedTimestamp), 0).UTC().Format(time.DateOnly)
logger.Debugf("Latest submitted timestamp: %s", claimDate)

rootCount, err := elReader.GetDistributionRootsLength(&bind.CallOpts{})
if err != nil {
return "", 0, eigenSdkUtils.WrapError("failed to get number of published roots", err)
}

rootIndex := uint32(rootCount.Uint64() - 1)
logger.Debugf("Latest active rewards snapshot timestamp: %s, root index: %d", claimDate, rootIndex)
return claimDate, rootIndex, nil
} else if claimTimestamp == "latest_active" {
// Get the latest 10 roots
postedRoots, err := df.FetchPostedRewards(ctx)
latestClaimableRoot, err := elReader.GetCurrentClaimableDistributionRoot(&bind.CallOpts{})
if err != nil {
return "", 0, eigenSdkUtils.WrapError("failed to fetch posted rewards", err)
return "", 0, eigenSdkUtils.WrapError("failed to get latest claimable root", err)
}

ts, rootIndex, err := getLatestActivePostedRoot(postedRoots)
rootIndex, err := elReader.GetRootIndexFromHash(&bind.CallOpts{}, latestClaimableRoot.Root)
if err != nil {
return "", 0, eigenSdkUtils.WrapError("failed to get latest active posted root", err)
return "", 0, eigenSdkUtils.WrapError("failed to get root index from hash", err)
}
logger.Debugf("Latest active posted root timestamp: %s, index: %d", ts, rootIndex)

ts := time.Unix(int64(latestClaimableRoot.RewardsCalculationEndTimestamp), 0).UTC().Format(time.DateOnly)
logger.Debugf("Latest rewards snapshot timestamp: %s, root index: %d", ts, rootIndex)

return ts, rootIndex, nil
}
return "", 0, errors.New("invalid claim timestamp")
}

// getLatestActivePostedRoot returns the latest active posted root by sorting the roots by the latest calculated end
// timestamp in descending order and checking the latest timestamp which activated before the current time
func getLatestActivePostedRoot(postedRoots []*proofDataFetcher.SubmittedRewardRoot) (string, uint32, error) {
// sort by latest calculated end timestamp
sort.Slice(postedRoots, func(i, j int) bool {
return postedRoots[i].CalcEndTimestamp.After(postedRoots[j].CalcEndTimestamp)
})

currTime := time.Now()
for _, postedRoot := range postedRoots {
if postedRoot.ActivatedAt.Before(currTime) {
return postedRoot.GetRewardDate(), postedRoot.RootIndex, nil
}
// There is no else here because on of last 10 root be
}
return "", 0, errors.New("no active posted roots found")
}

func convertClaimTokenLeaves(
claimTokenLeaves []contractrewardscoordinator.IRewardsCoordinatorTokenTreeMerkleLeaf,
) []rewardscoordinator.IRewardsCoordinatorTokenTreeMerkleLeaf {
Expand Down
88 changes: 2 additions & 86 deletions pkg/rewards/claim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ package rewards

import (
"flag"
"os"
"testing"
"time"

"github.com/Layr-Labs/eigenlayer-rewards-proofs/pkg/proofDataFetcher"

"github.com/Layr-Labs/eigenlayer-cli/pkg/internal/common/flags"
"github.com/Layr-Labs/eigenlayer-cli/pkg/internal/testutils"
"os"
"testing"

"github.com/Layr-Labs/eigensdk-go/logging"

Expand Down Expand Up @@ -55,83 +51,3 @@ func TestReadAndValidateConfig_RecipientProvided(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, common.HexToAddress(recipientAddress), config.RecipientAddress)
}

func TestGetLatestActivePostedRoot(t *testing.T) {
now := time.Now().UTC()
var tests = []struct {
name string
postedRoots []*proofDataFetcher.SubmittedRewardRoot
expectedRootIndex uint32
rewardsTimestampString string
}{
{
name: "found an activated root before current time",
postedRoots: []*proofDataFetcher.SubmittedRewardRoot{
{
RootIndex: 1,
ActivatedAt: now,
CalcEndTimestamp: now.Add(-24 * time.Hour),
},
{
RootIndex: 2,
ActivatedAt: now.Add(-2 * time.Hour),
CalcEndTimestamp: now.Add(-48 * time.Hour),
},
{
RootIndex: 3,
ActivatedAt: now.Add(1 * time.Hour),
CalcEndTimestamp: now,
},
},
expectedRootIndex: 1,
rewardsTimestampString: now.Add(-24 * time.Hour).Format(time.DateOnly),
},
{
name: "found no activated root before current time",
postedRoots: []*proofDataFetcher.SubmittedRewardRoot{
{
RootIndex: 3,
ActivatedAt: now.Add(1 * time.Hour),
CalcEndTimestamp: now,
},
{
RootIndex: 4,
ActivatedAt: now.Add(2 * time.Hour),
CalcEndTimestamp: now.Add(3 * time.Hour),
},
},
expectedRootIndex: 0,
},
{
name: "found an activated root before current time 2",
postedRoots: []*proofDataFetcher.SubmittedRewardRoot{
{
RootIndex: 2,
ActivatedAt: now.Add(-2 * time.Hour),
CalcEndTimestamp: now.Add(-24 * time.Hour),
},
{
RootIndex: 3,
ActivatedAt: now.Add(1 * time.Hour),
CalcEndTimestamp: now.Add(-48 * time.Hour),
},
},
expectedRootIndex: 2,
rewardsTimestampString: now.Add(-24 * time.Hour).Format(time.DateOnly),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actualRewardTimeString, actualRootIndex, err := getLatestActivePostedRoot(tt.postedRoots)
if tt.expectedRootIndex == 0 {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.rewardsTimestampString, actualRewardTimeString)
}
assert.Equal(t, tt.expectedRootIndex, actualRootIndex)

})
}
}
2 changes: 1 addition & 1 deletion pkg/rewards/setclaimer.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func SetClaimer(cCtx *cli.Context, p utils.Prompter) error {
return err
}

receipt, err := elWriter.SetClaimerFor(context.Background(), config.ClaimerAddress)
receipt, err := elWriter.SetClaimerFor(context.Background(), config.ClaimerAddress, true)
if err != nil {
return err
}
Expand Down

0 comments on commit 88db313

Please sign in to comment.