Skip to content

Commit

Permalink
Refactor/wrap error type (#203)
Browse files Browse the repository at this point in the history
* refactor: move WrapError

* fix: after rebasing

* chore: go fmt

* refactor: simplify returned value for WrapError

* refactor: move WrapError to errors.go

* fix: wrap error

* fix: use w% as placeholder for error
  • Loading branch information
renlulu authored Apr 29, 2024
1 parent 55e3fae commit 46fd7b1
Show file tree
Hide file tree
Showing 24 changed files with 321 additions and 185 deletions.
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

0 comments on commit 46fd7b1

Please sign in to comment.