From e4d59199f5240b224007d0ac28728fbaf1988844 Mon Sep 17 00:00:00 2001 From: Rohan Shrothrium Date: Wed, 6 Dec 2023 18:32:14 +0530 Subject: [PATCH] #42: refactor types and utils Signed-off-by: Rohan Shrothrium --- .../clients/avs_registry_contracts_client.go | 16 +++++- types/common.go | 34 ++++++++++++ types/constants.go | 5 +- types/operator.go | 25 ++------- types/operator_metadata.go | 23 -------- utils/utils.go | 53 +------------------ 6 files changed, 58 insertions(+), 98 deletions(-) create mode 100644 types/common.go diff --git a/chainio/clients/avs_registry_contracts_client.go b/chainio/clients/avs_registry_contracts_client.go index 11071d64..fad84e29 100644 --- a/chainio/clients/avs_registry_contracts_client.go +++ b/chainio/clients/avs_registry_contracts_client.go @@ -7,6 +7,7 @@ import ( regcoord "github.com/Layr-Labs/eigensdk-go/contracts/bindings/BLSRegistryCoordinatorWithIndices" "github.com/Layr-Labs/eigensdk-go/logging" "github.com/Layr-Labs/eigensdk-go/types" + "math/big" "github.com/ethereum/go-ethereum/accounts/abi/bind" gethcommon "github.com/ethereum/go-ethereum/common" @@ -159,7 +160,7 @@ func (a *AvsRegistryContractsChainClient) GetOperatorQuorumsAtCurrentBlock( if err != nil { return nil, err } - quorums := types.BitmapToQuorumIds(quorumBitmap) + quorums := bitmapToQuorumIds(quorumBitmap) return quorums, nil } @@ -177,7 +178,7 @@ func (a *AvsRegistryContractsChainClient) GetOperatorsStakeInQuorumsOfOperatorAt if err != nil { return nil, nil, err } - quorums := types.BitmapToQuorumIds(quorumBitmap) + quorums := bitmapToQuorumIds(quorumBitmap) return quorums, blsOperatorStateRetrieverOperator, nil } @@ -217,3 +218,14 @@ func (a *AvsRegistryContractsChainClient) DeregisterOperator( quorumNumbers, pubkey) } + +func bitmapToQuorumIds(bitmap *big.Int) []types.QuorumNum { + // loop through each index in the bitmap to construct the array + quorumIds := make([]types.QuorumNum, 0, types.MaxNumberOfQuorums) + for i := 0; i < types.MaxNumberOfQuorums; i++ { + if bitmap.Bit(i) == 1 { + quorumIds = append(quorumIds, types.QuorumNum(i)) + } + } + return quorumIds +} diff --git a/types/common.go b/types/common.go new file mode 100644 index 00000000..290bdfb3 --- /dev/null +++ b/types/common.go @@ -0,0 +1,34 @@ +package types + +import ( + "errors" + "net/url" + "regexp" +) + +func isValidEthereumAddress(address string) bool { + re := regexp.MustCompile("^0x[0-9a-fA-F]{40}$") + return re.MatchString(address) +} + +func checkIfUrlIsValid(rawUrl string) error { + // Regular expression to validate URLs + urlPattern := regexp.MustCompile(`^(https?|ftp)://[^\s/$.?#].[^\s]*$`) + + // Check if the URL matches the regular expression + if !urlPattern.MatchString(rawUrl) { + return errors.New("invalid url") + } + + parsedURL, err := url.Parse(rawUrl) + if err != nil { + return err + } + + // Check if the URL is valid + if parsedURL.Scheme != "" && parsedURL.Host != "" { + return nil + } else { + return errors.New("invalid url") + } +} diff --git a/types/constants.go b/types/constants.go index d4321b33..e2a03ddf 100644 --- a/types/constants.go +++ b/types/constants.go @@ -1,3 +1,6 @@ package types -const EigenPromNamespace = "eigen" +const ( + EigenPromNamespace = "eigen" + MaxNumberOfQuorums = 192 +) diff --git a/types/operator.go b/types/operator.go index 173ebdcc..06d80b11 100644 --- a/types/operator.go +++ b/types/operator.go @@ -8,9 +8,9 @@ import ( "math/big" "net/http" - "github.com/Layr-Labs/eigensdk-go/crypto/bls" - "github.com/Layr-Labs/eigensdk-go/utils" "github.com/ethereum/go-ethereum/common" + + "github.com/Layr-Labs/eigensdk-go/crypto/bls" ) const ( @@ -32,15 +32,15 @@ type Operator struct { } func (o Operator) Validate() error { - if !utils.IsValidEthereumAddress(o.Address) { + if !isValidEthereumAddress(o.Address) { return errors.New("invalid operator address") } - if !utils.IsValidEthereumAddress(o.EarningsReceiverAddress) { + if !isValidEthereumAddress(o.EarningsReceiverAddress) { return errors.New("invalid EarningsReceiverAddress address") } - if o.DelegationApproverAddress != ZeroAddress && !utils.IsValidEthereumAddress(o.DelegationApproverAddress) { + if o.DelegationApproverAddress != ZeroAddress && !isValidEthereumAddress(o.DelegationApproverAddress) { return fmt.Errorf( "invalid DelegationApproverAddress address, it should be either %s or a valid non zero ethereum address", ZeroAddress, @@ -108,21 +108,6 @@ type OperatorAvsState struct { BlockNumber BlockNum } -var ( - maxNumberOfQuorums = 192 -) - -func BitmapToQuorumIds(bitmap *big.Int) []QuorumNum { - // loop through each index in the bitmap to construct the array - quorumIds := make([]QuorumNum, 0, maxNumberOfQuorums) - for i := 0; i < maxNumberOfQuorums; i++ { - if bitmap.Bit(i) == 1 { - quorumIds = append(quorumIds, QuorumNum(i)) - } - } - return quorumIds -} - type QuorumAvsState struct { QuorumNumber QuorumNum TotalStake StakeAmount diff --git a/types/operator_metadata.go b/types/operator_metadata.go index b8015c6a..5565d518 100644 --- a/types/operator_metadata.go +++ b/types/operator_metadata.go @@ -5,7 +5,6 @@ import ( "fmt" "net/url" "path/filepath" - "regexp" "strings" ) @@ -75,28 +74,6 @@ func (om *OperatorMetadata) Validate() error { return nil } -func checkIfUrlIsValid(rawUrl string) error { - // Regular expression to validate URLs - urlPattern := regexp.MustCompile(`^(https?|ftp)://[^\s/$.?#].[^\s]*$`) - - // Check if the URL matches the regular expression - if !urlPattern.MatchString(rawUrl) { - return errors.New("invalid url") - } - - parsedURL, err := url.Parse(rawUrl) - if err != nil { - return err - } - - // Check if the URL is valid - if parsedURL.Scheme != "" && parsedURL.Host != "" { - return nil - } else { - return errors.New("invalid url") - } -} - func isImageURL(urlString string) bool { // Parse the URL parsedURL, err := url.Parse(urlString) diff --git a/utils/utils.go b/utils/utils.go index e14247d7..9793ed0b 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -2,58 +2,12 @@ package utils import ( "crypto/ecdsa" - "encoding/json" "errors" - "log" - "math/big" - "os" - "path/filepath" - "regexp" - gethcommon "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" - "gopkg.in/yaml.v3" + "math/big" ) -func ReadFile(path string) ([]byte, error) { - b, err := os.ReadFile(filepath.Clean(path)) - if err != nil { - return nil, err - } - return b, nil -} - -func ReadYamlConfig(path string, o interface{}) error { - if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) { - log.Fatal("Path ", path, " does not exist") - } - b, err := ReadFile(path) - if err != nil { - return err - } - - err = yaml.Unmarshal(b, o) - if err != nil { - log.Fatalf("unable to parse file with error %#v", err) - } - - return nil -} - -func ReadJsonConfig(path string, o interface{}) error { - b, err := ReadFile(path) - if err != nil { - return err - } - - err = json.Unmarshal(b, o) - if err != nil { - log.Fatalf("unable to parse file with error %#v", err) - } - - return nil -} - func EcdsaPrivateKeyToAddress(privateKey *ecdsa.PrivateKey) (gethcommon.Address, error) { publicKey := privateKey.Public() publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey) @@ -71,8 +25,3 @@ func RoundUpDivideBig(a, b *big.Int) *big.Int { res.Div(a, b) return res } - -func IsValidEthereumAddress(address string) bool { - re := regexp.MustCompile("^0x[0-9a-fA-F]{40}$") - return re.MatchString(address) -}