Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: elcontracts update more reader interfaces #497

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions chainio/clients/avsregistry/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,8 @@ import (
type eLReader interface {
CalculateOperatorAVSRegistrationDigestHash(
ctx context.Context,
operatorAddr gethcommon.Address,
serviceManagerAddr gethcommon.Address,
operatorToAvsRegistrationSigSalt [32]byte,
operatorToAvsRegistrationSigExpiry *big.Int,
) ([32]byte, error)
request elcontracts.AVSRegistrationDigestHashRequest,
) (elcontracts.DigestHashResponse, error)
}

type ChainWriter struct {
Expand Down Expand Up @@ -161,17 +158,19 @@ func (w *ChainWriter) RegisterOperatorInQuorumWithAVSRegistryCoordinator(
}

// params to register operator in delegation manager's operator-avs mapping
msgToSign, err := w.elReader.CalculateOperatorAVSRegistrationDigestHash(
response, err := w.elReader.CalculateOperatorAVSRegistrationDigestHash(
ctx,
operatorAddr,
w.serviceManagerAddr,
operatorToAvsRegistrationSigSalt,
operatorToAvsRegistrationSigExpiry,
elcontracts.AVSRegistrationDigestHashRequest{
OperatorAddress: operatorAddr,
AVSAddress: w.serviceManagerAddr,
Salt: operatorToAvsRegistrationSigSalt,
Expiry: operatorToAvsRegistrationSigExpiry,
},
)
if err != nil {
return nil, err
}
operatorSignature, err := crypto.Sign(msgToSign[:], operatorEcdsaPrivateKey)
operatorSignature, err := crypto.Sign(response.DigestHash[:], operatorEcdsaPrivateKey)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -283,17 +282,19 @@ func (w *ChainWriter) RegisterOperator(
).Add(new(big.Int).SetUint64(curBlock.Time()), big.NewInt(sigValidForSeconds))

// params to register operator in delegation manager's operator-avs mapping
msgToSign, err := w.elReader.CalculateOperatorAVSRegistrationDigestHash(
response, err := w.elReader.CalculateOperatorAVSRegistrationDigestHash(
ctx,
operatorAddr,
w.serviceManagerAddr,
operatorToAvsRegistrationSigSalt,
operatorToAvsRegistrationSigExpiry,
elcontracts.AVSRegistrationDigestHashRequest{
OperatorAddress: operatorAddr,
AVSAddress: w.serviceManagerAddr,
Salt: operatorToAvsRegistrationSigSalt,
Expiry: operatorToAvsRegistrationSigExpiry,
},
)
if err != nil {
return nil, err
}
operatorSignature, err := crypto.Sign(msgToSign[:], operatorEcdsaPrivateKey)
operatorSignature, err := crypto.Sign(response.DigestHash[:], operatorEcdsaPrivateKey)
if err != nil {
return nil, err
}
Expand Down
161 changes: 113 additions & 48 deletions chainio/clients/elcontracts/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,108 +224,173 @@ func (r *ChainReader) GetOperatorSharesInStrategy(
)
}

// CalculateDelegationApprovalDigestHash calculates the digest hash that must be signed by the operator’s delegation
// approver.
func (r *ChainReader) CalculateDelegationApprovalDigestHash(
ctx context.Context,
staker gethcommon.Address,
operator gethcommon.Address,
delegationApprover gethcommon.Address,
approverSalt [32]byte,
expiry *big.Int,
) ([32]byte, error) {
request ApprovalDigestHashRequest,
) (DigestHashResponse, error) {
if r.delegationManager == nil {
return [32]byte{}, errors.New("DelegationManager contract not provided")
return DigestHashResponse{}, errors.New("DelegationManager contract not provided")
}

return r.delegationManager.CalculateDelegationApprovalDigestHash(
&bind.CallOpts{Context: ctx},
staker,
operator,
delegationApprover,
approverSalt,
expiry,
hash, err := r.delegationManager.CalculateDelegationApprovalDigestHash(
&bind.CallOpts{Context: ctx, BlockNumber: request.BlockNumber},
request.StakerAddress,
request.OperatorAddress,
request.DelegationAddress,
request.ApproverSalt,
request.Expiry,
)
if err != nil {
return DigestHashResponse{}, utils.WrapError("failed to calculate delegation approval digest hash", err)
}

return DigestHashResponse{DigestHash: hash}, nil
}

// CalculateOperatorAVSRegistrationDigestHash calculates the digest hash to be signed by an operator to register with an
// AVS
func (r *ChainReader) CalculateOperatorAVSRegistrationDigestHash(
ctx context.Context,
operator gethcommon.Address,
avs gethcommon.Address,
salt [32]byte,
expiry *big.Int,
) ([32]byte, error) {
request AVSRegistrationDigestHashRequest,
) (DigestHashResponse, error) {
if r.avsDirectory == nil {
return [32]byte{}, errors.New("AVSDirectory contract not provided")
return DigestHashResponse{}, errors.New("AVSDirectory contract not provided")
}

return r.avsDirectory.CalculateOperatorAVSRegistrationDigestHash(
&bind.CallOpts{Context: ctx},
operator,
avs,
salt,
expiry,
hash, err := r.avsDirectory.CalculateOperatorAVSRegistrationDigestHash(
&bind.CallOpts{Context: ctx, BlockNumber: request.BlockNumber},
request.OperatorAddress,
request.AVSAddress,
request.Salt,
request.Expiry,
)
if err != nil {
return DigestHashResponse{}, utils.WrapError("failed to calculate operator AVS registration digest hash", err)
}

return DigestHashResponse{DigestHash: hash}, nil
}

func (r *ChainReader) GetDistributionRootsLength(ctx context.Context) (*big.Int, error) {
// GetOperatorAVSRegistrationStatus returns the length of distribution roots posted
func (r *ChainReader) GetDistributionRootsLength(ctx context.Context, request RootRequest) (RootLengthResponse, error) {
if r.rewardsCoordinator == nil {
return nil, errors.New("RewardsCoordinator contract not provided")
return RootLengthResponse{}, errors.New("RewardsCoordinator contract not provided")
}

return r.rewardsCoordinator.GetDistributionRootsLength(&bind.CallOpts{Context: ctx})
lengt, err := r.rewardsCoordinator.GetDistributionRootsLength(
&bind.CallOpts{Context: ctx, BlockNumber: request.BlockNumer},
)
if err != nil {
return RootLengthResponse{}, utils.WrapError("failed to get distribution roots length", err)
}

return RootLengthResponse{Length: lengt}, nil
}

func (r *ChainReader) CurrRewardsCalculationEndTimestamp(ctx context.Context) (uint32, error) {
// CurrRewardsCalculationEndTimestamp fetches the end timestamp until which RewardsSubmissions have been calculated
func (r *ChainReader) CurrRewardsCalculationEndTimestamp(
ctx context.Context,
request RewardsEndTimestampRequest,
) (EndTimestampResponse, error) {
if r.rewardsCoordinator == nil {
return 0, errors.New("RewardsCoordinator contract not provided")
return EndTimestampResponse{}, errors.New("RewardsCoordinator contract not provided")
}

endTimestamp, err := r.rewardsCoordinator.CurrRewardsCalculationEndTimestamp(
&bind.CallOpts{Context: ctx, BlockNumber: request.BlockNumber},
)
if err != nil {
return EndTimestampResponse{}, utils.WrapError("failed to get rewards calculation end timestamp", err)
}

return r.rewardsCoordinator.CurrRewardsCalculationEndTimestamp(&bind.CallOpts{Context: ctx})
return EndTimestampResponse{EndTimestamp: endTimestamp}, nil
}

// GetCurrentClaimableDistributionRoot returns the latest claimable root that is not disabled and activated
func (r *ChainReader) GetCurrentClaimableDistributionRoot(
ctx context.Context,
) (rewardscoordinator.IRewardsCoordinatorTypesDistributionRoot, error) {
request RootRequest,
) (ClaimableDistributionRootResponse, error) {
if r.rewardsCoordinator == nil {
return rewardscoordinator.IRewardsCoordinatorTypesDistributionRoot{}, errors.New(
return ClaimableDistributionRootResponse{}, errors.New(
"RewardsCoordinator contract not provided",
)
}

return r.rewardsCoordinator.GetCurrentClaimableDistributionRoot(&bind.CallOpts{Context: ctx})
root, err := r.rewardsCoordinator.GetCurrentClaimableDistributionRoot(
&bind.CallOpts{Context: ctx, BlockNumber: request.BlockNumer},
)
if err != nil {
return ClaimableDistributionRootResponse{}, utils.WrapError(
"failed to get current claimable distribution root",
err,
)
}

return ClaimableDistributionRootResponse{DistributionRoot: root}, nil
}

// GetRootIndexFromHash returns the index of a root hash
func (r *ChainReader) GetRootIndexFromHash(
ctx context.Context,
rootHash [32]byte,
) (uint32, error) {
request RootHashRequest,
) (RootIndexResponse, error) {
if r.rewardsCoordinator == nil {
return 0, errors.New("RewardsCoordinator contract not provided")
return RootIndexResponse{}, errors.New("RewardsCoordinator contract not provided")
}

index, err := r.rewardsCoordinator.GetRootIndexFromHash(
&bind.CallOpts{Context: ctx, BlockNumber: request.BlockNumber},
request.RootHash,
)
if err != nil {
return RootIndexResponse{}, utils.WrapError("failed to get root index from hash", err)
}

return r.rewardsCoordinator.GetRootIndexFromHash(&bind.CallOpts{Context: ctx}, rootHash)
return RootIndexResponse{Index: index}, nil
}

// GetCumulativeClaimed fetches the total amount claimed by an earner for a specific token.
func (r *ChainReader) GetCumulativeClaimed(
ctx context.Context,
earner gethcommon.Address,
token gethcommon.Address,
) (*big.Int, error) {
request CumulativeClaimedRequest,
) (CumulativeClaimedResponse, error) {
if r.rewardsCoordinator == nil {
return nil, errors.New("RewardsCoordinator contract not provided")
return CumulativeClaimedResponse{}, errors.New("RewardsCoordinator contract not provided")
}

claimed, err := r.rewardsCoordinator.CumulativeClaimed(
&bind.CallOpts{Context: ctx, BlockNumber: request.BlockNumber},
request.EarnerAddress,
request.TokenAddress,
)
if err != nil {
return CumulativeClaimedResponse{}, utils.WrapError("failed to get cumulative claimed", err)
}

return r.rewardsCoordinator.CumulativeClaimed(&bind.CallOpts{Context: ctx}, earner, token)
return CumulativeClaimedResponse{CumulativeClaimed: claimed}, nil
}

// CheckClaim checks if the claim would currently pass the check in processClaim
func (r *ChainReader) CheckClaim(
ctx context.Context,
claim rewardscoordinator.IRewardsCoordinatorTypesRewardsMerkleClaim,
) (bool, error) {
request ClaimRequest,
) (ClaimResponse, error) {
if r.rewardsCoordinator == nil {
return false, errors.New("RewardsCoordinator contract not provided")
return ClaimResponse{}, errors.New("RewardsCoordinator contract not provided")
}

claim, err := r.rewardsCoordinator.CheckClaim(
&bind.CallOpts{Context: ctx, BlockNumber: request.BlockNumber},
request.Claim,
)
if err != nil {
return ClaimResponse{}, utils.WrapError("failed to check claim", err)
}

return r.rewardsCoordinator.CheckClaim(&bind.CallOpts{Context: ctx}, claim)
return ClaimResponse{CheckClaim: claim}, nil
}

func (r *ChainReader) GetOperatorAVSSplit(
Expand Down
Loading