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: wrap error type #203

Merged
merged 7 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
77 changes: 48 additions & 29 deletions chainio/clients/avsregistry/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package avsregistry

import (
"context"
"errors"
"math"
"math/big"

"github.com/Layr-Labs/eigensdk-go/chainio/clients/eth"
"github.com/Layr-Labs/eigensdk-go/crypto/bls"
"github.com/Layr-Labs/eigensdk-go/logging"
"github.com/Layr-Labs/eigensdk-go/types"
"github.com/Layr-Labs/eigensdk-go/utils"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -123,26 +123,26 @@ func BuildAvsRegistryChainReader(
) (*AvsRegistryChainReader, error) {
contractRegistryCoordinator, err := regcoord.NewContractRegistryCoordinator(registryCoordinatorAddr, ethClient)
if err != nil {
return nil, types.WrapError(errors.New("Failed to create contractRegistryCoordinator"), err)
return nil, utils.WrapError("Failed to create contractRegistryCoordinator", err)
}
blsApkRegistryAddr, err := contractRegistryCoordinator.BlsApkRegistry(&bind.CallOpts{})
if err != nil {
return nil, types.WrapError(errors.New("Failed to get blsApkRegistryAddr"), err)
return nil, utils.WrapError("Failed to get blsApkRegistryAddr", err)
}
stakeRegistryAddr, err := contractRegistryCoordinator.StakeRegistry(&bind.CallOpts{})
if err != nil {
return nil, types.WrapError(errors.New("Failed to get stakeRegistryAddr"), err)
return nil, utils.WrapError("Failed to get stakeRegistryAddr", err)
}
contractStakeRegistry, err := stakeregistry.NewContractStakeRegistry(stakeRegistryAddr, ethClient)
if err != nil {
return nil, types.WrapError(errors.New("Failed to create contractStakeRegistry"), err)
return nil, utils.WrapError("Failed to create contractStakeRegistry", err)
}
contractOperatorStateRetriever, err := opstateretriever.NewContractOperatorStateRetriever(
operatorStateRetrieverAddr,
ethClient,
)
if err != nil {
return nil, types.WrapError(errors.New("Failed to create contractOperatorStateRetriever"), err)
return nil, utils.WrapError("Failed to create contractOperatorStateRetriever", err)
}
return NewAvsRegistryChainReader(
registryCoordinatorAddr,
Expand All @@ -168,10 +168,10 @@ func (r *AvsRegistryChainReader) GetOperatorsStakeInQuorumsAtCurrentBlock(
}
curBlock, err := r.ethClient.BlockNumber(opts.Context)
if err != nil {
return nil, types.WrapError(errors.New("Cannot get current block number"), err)
return nil, utils.WrapError("Cannot get current block number", err)
}
if curBlock > math.MaxUint32 {
return nil, types.WrapError(errors.New("Current block number is too large to be converted to uint32"), err)
return nil, utils.WrapError("Current block number is too large to be converted to uint32", err)
}
return r.GetOperatorsStakeInQuorumsAtBlock(opts, quorumNumbers, uint32(curBlock))
}
Expand All @@ -189,7 +189,7 @@ func (r *AvsRegistryChainReader) GetOperatorsStakeInQuorumsAtBlock(
quorumNumbers.UnderlyingType(),
blockNumber)
if err != nil {
return nil, types.WrapError(errors.New("Failed to get operators state"), err)
return nil, utils.WrapError("Failed to get operators state", err)
}
return operatorStakes, nil
}
Expand All @@ -203,10 +203,10 @@ func (r *AvsRegistryChainReader) GetOperatorAddrsInQuorumsAtCurrentBlock(
}
curBlock, err := r.ethClient.BlockNumber(opts.Context)
if err != nil {
return nil, types.WrapError(errors.New("Failed to get current block number"), err)
return nil, utils.WrapError("Failed to get current block number", err)
}
if curBlock > math.MaxUint32 {
return nil, types.WrapError(errors.New("Current block number is too large to be converted to uint32"), err)
return nil, utils.WrapError("Current block number is too large to be converted to uint32", err)
}
operatorStakes, err := r.operatorStateRetriever.GetOperatorState(
opts,
Expand All @@ -215,7 +215,7 @@ func (r *AvsRegistryChainReader) GetOperatorAddrsInQuorumsAtCurrentBlock(
uint32(curBlock),
)
if err != nil {
return nil, types.WrapError(errors.New("Failed to get operators state"), err)
return nil, utils.WrapError("Failed to get operators state", err)
}
var quorumOperatorAddrs [][]common.Address
for _, quorum := range operatorStakes {
Expand All @@ -240,7 +240,7 @@ func (r *AvsRegistryChainReader) GetOperatorsStakeInQuorumsOfOperatorAtBlock(
operatorId,
blockNumber)
if err != nil {
return nil, nil, types.WrapError(errors.New("Failed to get operators state"), err)
return nil, nil, utils.WrapError("Failed to get operators state", err)
}
quorums := types.BitmapToQuorumIds(quorumBitmap)
return quorums, operatorStakes, nil
Expand All @@ -257,10 +257,10 @@ func (r *AvsRegistryChainReader) GetOperatorsStakeInQuorumsOfOperatorAtCurrentBl
}
curBlock, err := r.ethClient.BlockNumber(opts.Context)
if err != nil {
return nil, nil, types.WrapError(errors.New("Failed to get current block number"), err)
return nil, nil, utils.WrapError("Failed to get current block number", err)
}
if curBlock > math.MaxUint32 {
return nil, nil, types.WrapError(errors.New("Current block number is too large to be converted to uint32"), err)
return nil, nil, utils.WrapError("Current block number is too large to be converted to uint32", err)
}
opts.BlockNumber = big.NewInt(int64(curBlock))
return r.GetOperatorsStakeInQuorumsOfOperatorAtBlock(opts, operatorId, uint32(curBlock))
Expand All @@ -275,7 +275,7 @@ func (r *AvsRegistryChainReader) GetOperatorStakeInQuorumsOfOperatorAtCurrentBlo
) (map[types.QuorumNum]types.StakeAmount, error) {
quorumBitmap, err := r.registryCoordinator.GetCurrentQuorumBitmap(opts, operatorId)
if err != nil {
return nil, types.WrapError(errors.New("Failed to get operator quorums"), err)
return nil, utils.WrapError("Failed to get operator quorums", err)
}
quorums := types.BitmapToQuorumIds(quorumBitmap)
quorumStakes := make(map[types.QuorumNum]types.StakeAmount)
Expand All @@ -286,7 +286,7 @@ func (r *AvsRegistryChainReader) GetOperatorStakeInQuorumsOfOperatorAtCurrentBlo
uint8(quorum),
)
if err != nil {
return nil, types.WrapError(errors.New("Failed to get operator stake"), err)
return nil, utils.WrapError("Failed to get operator stake", err)
}
quorumStakes[quorum] = stake
}
Expand All @@ -311,7 +311,10 @@ func (r *AvsRegistryChainReader) GetCheckSignaturesIndices(
nonSignerOperatorIdsBytes,
)
if err != nil {
return opstateretriever.OperatorStateRetrieverCheckSignaturesIndices{}, types.WrapError(errors.New("Failed to get check signatures indices"), err)
return opstateretriever.OperatorStateRetrieverCheckSignaturesIndices{}, utils.WrapError(
"Failed to get check signatures indices",
err,
)
}
return checkSignatureIndices, nil
}
Expand All @@ -325,7 +328,7 @@ func (r *AvsRegistryChainReader) GetOperatorId(
operatorAddress,
)
if err != nil {
return [32]byte{}, types.WrapError(errors.New("Failed to get operator id"), err)
return [32]byte{}, utils.WrapError("Failed to get operator id", err)
}
return operatorId, nil
}
Expand All @@ -339,7 +342,7 @@ func (r *AvsRegistryChainReader) GetOperatorFromId(
operatorId,
)
if err != nil {
return gethcommon.Address{}, types.WrapError(errors.New("Failed to get operator address"), err)
return gethcommon.Address{}, utils.WrapError("Failed to get operator address", err)
}
return operatorAddress, nil
}
Expand All @@ -350,7 +353,7 @@ func (r *AvsRegistryChainReader) IsOperatorRegistered(
) (bool, error) {
operatorStatus, err := r.registryCoordinator.GetOperatorStatus(opts, operatorAddress)
if err != nil {
return false, types.WrapError(errors.New("Failed to get operator status"), err)
return false, utils.WrapError("Failed to get operator status", err)
}

// 0 = NEVER_REGISTERED, 1 = REGISTERED, 2 = DEREGISTERED
Expand All @@ -366,7 +369,7 @@ func (r *AvsRegistryChainReader) QueryExistingRegisteredOperatorPubKeys(

blsApkRegistryAbi, err := apkreg.ContractBLSApkRegistryMetaData.GetAbi()
if err != nil {
return nil, nil, types.WrapError(errors.New("Cannot get Abi"), err)
return nil, nil, utils.WrapError("Cannot get Abi", err)
}

if startBlock == nil {
Expand All @@ -375,7 +378,7 @@ func (r *AvsRegistryChainReader) QueryExistingRegisteredOperatorPubKeys(
if stopBlock == nil {
curBlockNum, err := r.ethClient.BlockNumber(ctx)
if err != nil {
return nil, nil, types.WrapError(errors.New("Cannot get current block number"), err)
return nil, nil, utils.WrapError("Cannot get current block number", err)
}
stopBlock = big.NewInt(int64(curBlockNum))
}
Expand All @@ -400,9 +403,17 @@ func (r *AvsRegistryChainReader) QueryExistingRegisteredOperatorPubKeys(

logs, err := r.ethClient.FilterLogs(ctx, query)
if err != nil {
return nil, nil, types.WrapError(errors.New("Cannot filter logs"), err)
return nil, nil, utils.WrapError("Cannot filter logs", err)
}
r.logger.Debug("avsRegistryChainReader.QueryExistingRegisteredOperatorPubKeys", "numTransactionLogs", len(logs), "fromBlock", i, "toBlock", toBlock)
r.logger.Debug(
"avsRegistryChainReader.QueryExistingRegisteredOperatorPubKeys",
"numTransactionLogs",
len(logs),
"fromBlock",
i,
"toBlock",
toBlock,
)

for _, vLog := range logs {

Expand All @@ -412,7 +423,7 @@ func (r *AvsRegistryChainReader) QueryExistingRegisteredOperatorPubKeys(

event, err := blsApkRegistryAbi.Unpack("NewPubkeyRegistration", vLog.Data)
if err != nil {
return nil, nil, types.WrapError(errors.New("Cannot unpack event data"), err)
return nil, nil, utils.WrapError("Cannot unpack event data", err)
}

G1Pubkey := event[0].(struct {
Expand Down Expand Up @@ -456,7 +467,7 @@ func (r *AvsRegistryChainReader) QueryExistingRegisteredOperatorSockets(
if stopBlock == nil {
curBlockNum, err := r.ethClient.BlockNumber(ctx)
if err != nil {
return nil, types.WrapError(errors.New("Cannot get current block number"), err)
return nil, utils.WrapError("Cannot get current block number", err)
}
stopBlock = big.NewInt(int64(curBlockNum))
}
Expand All @@ -478,15 +489,23 @@ func (r *AvsRegistryChainReader) QueryExistingRegisteredOperatorSockets(
}
socketUpdates, err := r.registryCoordinator.FilterOperatorSocketUpdate(filterOpts, nil)
if err != nil {
return nil, types.WrapError(errors.New("Cannot filter operator socket updates"), err)
return nil, utils.WrapError("Cannot filter operator socket updates", err)
}

numSocketUpdates := 0
for socketUpdates.Next() {
operatorIdToSocketMap[socketUpdates.Event.OperatorId] = types.Socket(socketUpdates.Event.Socket)
numSocketUpdates++
}
r.logger.Debug("avsRegistryChainReader.QueryExistingRegisteredOperatorSockets", "numTransactionLogs", numSocketUpdates, "fromBlock", i, "toBlock", toBlock)
r.logger.Debug(
"avsRegistryChainReader.QueryExistingRegisteredOperatorSockets",
"numTransactionLogs",
numSocketUpdates,
"fromBlock",
i,
"toBlock",
toBlock,
)
}
return operatorIdToSocketMap, nil
}
14 changes: 6 additions & 8 deletions chainio/clients/avsregistry/subscriber.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package avsregistry

import (
"errors"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/event"
Expand All @@ -11,7 +9,7 @@ import (
blsapkreg "github.com/Layr-Labs/eigensdk-go/contracts/bindings/BLSApkRegistry"
regcoord "github.com/Layr-Labs/eigensdk-go/contracts/bindings/RegistryCoordinator"
"github.com/Layr-Labs/eigensdk-go/logging"
"github.com/Layr-Labs/eigensdk-go/types"
"github.com/Layr-Labs/eigensdk-go/utils"
)

type AvsRegistrySubscriber interface {
Expand Down Expand Up @@ -47,15 +45,15 @@ func BuildAvsRegistryChainSubscriber(
) (*AvsRegistryChainSubscriber, error) {
regCoord, err := regcoord.NewContractRegistryCoordinator(regCoordAddr, ethWsClient)
if err != nil {
return nil, types.WrapError(errors.New("Failed to create RegistryCoordinator contract"), err)
return nil, utils.WrapError("Failed to create RegistryCoordinator contract", err)
}
blsApkRegAddr, err := regCoord.BlsApkRegistry(&bind.CallOpts{})
if err != nil {
return nil, types.WrapError(errors.New("Failed to get BLSApkRegistry address from RegistryCoordinator"), err)
return nil, utils.WrapError("Failed to get BLSApkRegistry address from RegistryCoordinator", err)
}
blsApkReg, err := blsapkreg.NewContractBLSApkRegistry(blsApkRegAddr, ethWsClient)
if err != nil {
return nil, types.WrapError(errors.New("Failed to create BLSApkRegistry contract"), err)
return nil, utils.WrapError("Failed to create BLSApkRegistry contract", err)
}
return NewAvsRegistryChainSubscriber(logger, regCoord, blsApkReg)
}
Expand All @@ -66,7 +64,7 @@ func (s *AvsRegistryChainSubscriber) SubscribeToNewPubkeyRegistrations() (chan *
&bind.WatchOpts{}, newPubkeyRegistrationChan, nil,
)
if err != nil {
return nil, nil, types.WrapError(errors.New("Failed to subscribe to NewPubkeyRegistration events"), err)
return nil, nil, utils.WrapError("Failed to subscribe to NewPubkeyRegistration events", err)
}
return newPubkeyRegistrationChan, sub, nil
}
Expand All @@ -77,7 +75,7 @@ func (s *AvsRegistryChainSubscriber) SubscribeToOperatorSocketUpdates() (chan *r
&bind.WatchOpts{}, operatorSocketUpdateChan, nil,
)
if err != nil {
return nil, nil, types.WrapError(errors.New("Failed to subscribe to OperatorSocketUpdate events"), err)
return nil, nil, utils.WrapError("Failed to subscribe to OperatorSocketUpdate events", err)
}
return operatorSocketUpdateChan, sub, nil
}
Loading
Loading