Skip to content

Commit

Permalink
refactor: replaced retry implementation inside functions with generic…
Browse files Browse the repository at this point in the history
… retry function (#1252)

* refactor: replaced retry implementation with genernic retry function

* refactor: used generic retry function instead of individual retry implementation

* refactor: reverted nil with exisiting 0 big integer when error occurs in fetchBalance
  • Loading branch information
Yashk767 authored Oct 9, 2024
1 parent bb75173 commit c4e3e3b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 162 deletions.
129 changes: 16 additions & 113 deletions utils/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"sync"
"time"

"github.com/avast/retry-go"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/tidwall/gjson"
Expand All @@ -31,23 +30,11 @@ func (*UtilsStruct) GetCollectionManagerWithOpts(client *ethclient.Client) (*bin
}

func (*UtilsStruct) GetNumCollections(client *ethclient.Client) (uint16, error) {
var (
numCollections uint16
err error
)
err = retry.Do(
func() error {
numCollections, err = AssetManagerInterface.GetNumCollections(client)
if err != nil {
log.Error("Error in fetching numCollections.... Retrying")
return err
}
return nil
}, RetryInterface.RetryAttempts(core.MaxRetries))
returnedValues, err := InvokeFunctionWithRetryAttempts(AssetManagerInterface, "GetNumCollections", client)

Check failure on line 33 in utils/asset.go

View workflow job for this annotation

GitHub Actions / test

cannot use AssetManagerInterface (variable of type AssetManagerUtils) as context.Context value in argument to InvokeFunctionWithRetryAttempts: AssetManagerUtils does not implement context.Context (missing method Deadline)

Check failure on line 33 in utils/asset.go

View workflow job for this annotation

GitHub Actions / test

cannot use client (variable of type *ethclient.Client) as string value in argument to InvokeFunctionWithRetryAttempts
if err != nil {
return 0, err
}
return numCollections, nil
return returnedValues[0].Interface().(uint16), nil
}

func (*UtilsStruct) GetJobs(client *ethclient.Client) ([]bindings.StructsJob, error) {
Expand All @@ -67,23 +54,11 @@ func (*UtilsStruct) GetJobs(client *ethclient.Client) ([]bindings.StructsJob, er
}

func (*UtilsStruct) GetNumActiveCollections(client *ethclient.Client) (uint16, error) {
var (
numActiveAssets uint16
err error
)
err = retry.Do(
func() error {
numActiveAssets, err = AssetManagerInterface.GetNumActiveCollections(client)
if err != nil {
log.Error("Error in fetching active assets.... Retrying")
return err
}
return nil
}, RetryInterface.RetryAttempts(core.MaxRetries))
returnedValues, err := InvokeFunctionWithRetryAttempts(AssetManagerInterface, "GetNumActiveCollections", client)

Check failure on line 57 in utils/asset.go

View workflow job for this annotation

GitHub Actions / test

cannot use AssetManagerInterface (variable of type AssetManagerUtils) as context.Context value in argument to InvokeFunctionWithRetryAttempts: AssetManagerUtils does not implement context.Context (missing method Deadline)

Check failure on line 57 in utils/asset.go

View workflow job for this annotation

GitHub Actions / test

cannot use client (variable of type *ethclient.Client) as string value in argument to InvokeFunctionWithRetryAttempts
if err != nil {
return 0, err
}
return numActiveAssets, nil
return returnedValues[0].Interface().(uint16), nil
}

func (*UtilsStruct) GetAllCollections(client *ethclient.Client) ([]bindings.StructsCollection, error) {
Expand All @@ -103,43 +78,19 @@ func (*UtilsStruct) GetAllCollections(client *ethclient.Client) ([]bindings.Stru
}

func (*UtilsStruct) GetCollection(client *ethclient.Client, collectionId uint16) (bindings.StructsCollection, error) {
var (
collection bindings.StructsCollection
err error
)
err = retry.Do(
func() error {
collection, err = AssetManagerInterface.GetCollection(client, collectionId)
if err != nil {
log.Error("Error in fetching collection.... Retrying")
return err
}
return nil
}, RetryInterface.RetryAttempts(core.MaxRetries))
returnedValues, err := InvokeFunctionWithRetryAttempts(AssetManagerInterface, "GetCollection", client, collectionId)

Check failure on line 81 in utils/asset.go

View workflow job for this annotation

GitHub Actions / test

cannot use AssetManagerInterface (variable of type AssetManagerUtils) as context.Context value in argument to InvokeFunctionWithRetryAttempts: AssetManagerUtils does not implement context.Context (missing method Deadline)

Check failure on line 81 in utils/asset.go

View workflow job for this annotation

GitHub Actions / test

cannot use client (variable of type *ethclient.Client) as string value in argument to InvokeFunctionWithRetryAttempts
if err != nil {
return bindings.StructsCollection{}, err
}
return collection, nil
return returnedValues[0].Interface().(bindings.StructsCollection), nil
}

func (*UtilsStruct) GetActiveCollectionIds(client *ethclient.Client) ([]uint16, error) {
var (
activeCollectionIds []uint16
err error
)
err = retry.Do(
func() error {
activeCollectionIds, err = AssetManagerInterface.GetActiveCollections(client)
if err != nil {
log.Error("Error in fetching active assets.... Retrying")
return err
}
return nil
}, RetryInterface.RetryAttempts(core.MaxRetries))
returnedValues, err := InvokeFunctionWithRetryAttempts(AssetManagerInterface, "GetActiveCollections", client)

Check failure on line 89 in utils/asset.go

View workflow job for this annotation

GitHub Actions / test

cannot use AssetManagerInterface (variable of type AssetManagerUtils) as context.Context value in argument to InvokeFunctionWithRetryAttempts: AssetManagerUtils does not implement context.Context (missing method Deadline)

Check failure on line 89 in utils/asset.go

View workflow job for this annotation

GitHub Actions / test

cannot use client (variable of type *ethclient.Client) as string value in argument to InvokeFunctionWithRetryAttempts
if err != nil {
return nil, err
}
return activeCollectionIds, nil
return returnedValues[0].Interface().([]uint16), nil
}

func (*UtilsStruct) GetAggregatedDataOfCollection(ctx context.Context, client *ethclient.Client, collectionId uint16, epoch uint32, commitParams *types.CommitParams) (*big.Int, error) {
Expand Down Expand Up @@ -224,23 +175,11 @@ func (*UtilsStruct) Aggregate(ctx context.Context, client *ethclient.Client, pre
}

func (*UtilsStruct) GetActiveJob(client *ethclient.Client, jobId uint16) (bindings.StructsJob, error) {
var (
job bindings.StructsJob
err error
)
err = retry.Do(
func() error {
job, err = AssetManagerInterface.Jobs(client, jobId)
if err != nil {
log.Errorf("Error in fetching job %d.... Retrying", jobId)
return err
}
return nil
}, RetryInterface.RetryAttempts(core.MaxRetries))
returnedValues, err := InvokeFunctionWithRetryAttempts(AssetManagerInterface, "Jobs", client, jobId)

Check failure on line 178 in utils/asset.go

View workflow job for this annotation

GitHub Actions / test

cannot use AssetManagerInterface (variable of type AssetManagerUtils) as context.Context value in argument to InvokeFunctionWithRetryAttempts: AssetManagerUtils does not implement context.Context (missing method Deadline)
if err != nil {
return bindings.StructsJob{}, err
}
return job, nil
return returnedValues[0].Interface().(bindings.StructsJob), nil
}

func (*UtilsStruct) GetActiveCollection(collectionsCache *cache.CollectionsCache, collectionId uint16) (bindings.StructsCollection, error) {
Expand Down Expand Up @@ -377,63 +316,27 @@ func (*UtilsStruct) GetAssignedCollections(ctx context.Context, client *ethclien
}

func (*UtilsStruct) GetLeafIdOfACollection(client *ethclient.Client, collectionId uint16) (uint16, error) {
var (
leafId uint16
err error
)
err = retry.Do(
func() error {
leafId, err = AssetManagerInterface.GetLeafIdOfACollection(client, collectionId)
if err != nil {
log.Error("Error in fetching collection id.... Retrying")
return err
}
return nil
}, RetryInterface.RetryAttempts(core.MaxRetries))
returnedValues, err := InvokeFunctionWithRetryAttempts(AssetManagerInterface, "GetLeafIdOfACollection", client, collectionId)
if err != nil {
return 0, err
}
return leafId, nil
return returnedValues[0].Interface().(uint16), nil
}

func (*UtilsStruct) GetCollectionIdFromIndex(client *ethclient.Client, medianIndex uint16) (uint16, error) {
var (
collectionId uint16
err error
)
err = retry.Do(
func() error {
collectionId, err = AssetManagerInterface.GetCollectionIdFromIndex(client, medianIndex)
if err != nil {
log.Error("Error in fetching collection id.... Retrying")
return err
}
return nil
}, RetryInterface.RetryAttempts(core.MaxRetries))
returnedValues, err := InvokeFunctionWithRetryAttempts(AssetManagerInterface, "GetCollectionIdFromIndex", client, medianIndex)
if err != nil {
return 0, err
}
return collectionId, nil
return returnedValues[0].Interface().(uint16), nil
}

func (*UtilsStruct) GetCollectionIdFromLeafId(client *ethclient.Client, leafId uint16) (uint16, error) {
var (
collectionId uint16
err error
)
err = retry.Do(
func() error {
collectionId, err = AssetManagerInterface.GetCollectionIdFromLeafId(client, leafId)
if err != nil {
log.Error("Error in fetching collection id.... Retrying")
return err
}
return nil
}, RetryInterface.RetryAttempts(core.MaxRetries))
returnedValues, err := InvokeFunctionWithRetryAttempts(AssetManagerInterface, "GetCollectionIdFromLeafId", client, leafId)
if err != nil {
return 0, err
}
return collectionId, nil
return returnedValues[0].Interface().(uint16), nil
}

func GetCustomJobsFromJSONFile(collection string, jsonFileData string) []bindings.StructsJob {
Expand Down
34 changes: 4 additions & 30 deletions utils/client_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package utils

import (
"context"
"github.com/avast/retry-go"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"math/big"
"razor/core"
)

func (*ClientStruct) GetNonceAtWithRetry(ctx context.Context, client *ethclient.Client, accountAddress common.Address) (uint64, error) {
Expand All @@ -29,43 +27,19 @@ func (*ClientStruct) GetLatestBlockWithRetry(ctx context.Context, client *ethcli
}

func (*ClientStruct) SuggestGasPriceWithRetry(client *ethclient.Client) (*big.Int, error) {
var (
gasPrice *big.Int
err error
)
err = retry.Do(
func() error {
gasPrice, err = ClientInterface.SuggestGasPrice(client, context.Background())
if err != nil {
log.Error("Error in fetching gas price.... Retrying")
return err
}
return nil
}, RetryInterface.RetryAttempts(core.MaxRetries))
returnedValues, err := InvokeFunctionWithRetryAttempts(ClientInterface, "SuggestGasPrice", client, context.Background())
if err != nil {
return nil, err
}
return gasPrice, nil
return returnedValues[0].Interface().(*big.Int), nil
}

func (*ClientStruct) EstimateGasWithRetry(client *ethclient.Client, message ethereum.CallMsg) (uint64, error) {
var (
gasLimit uint64
err error
)
err = retry.Do(
func() error {
gasLimit, err = ClientInterface.EstimateGas(client, context.Background(), message)
if err != nil {
log.Error("Error in estimating gas limit.... Retrying")
return err
}
return nil
}, RetryInterface.RetryAttempts(core.MaxRetries))
returnedValues, err := InvokeFunctionWithRetryAttempts(ClientInterface, "EstimateGas", client, context.Background(), message)
if err != nil {
return 0, err
}
return gasLimit, nil
return returnedValues[0].Interface().(uint64), nil
}

func (*ClientStruct) FilterLogsWithRetry(ctx context.Context, client *ethclient.Client, query ethereum.FilterQuery) ([]types.Log, error) {
Expand Down
25 changes: 6 additions & 19 deletions utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"razor/logger"
"time"

"github.com/avast/retry-go"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
solsha3 "github.com/miguelmota/go-solidity-sha3"
Expand All @@ -31,26 +29,15 @@ func (*UtilsStruct) ConnectToClient(provider string) *ethclient.Client {
}

func (*UtilsStruct) FetchBalance(client *ethclient.Client, accountAddress string) (*big.Int, error) {
var (
balance *big.Int
err error
)
err = retry.Do(
func() error {
address := common.HexToAddress(accountAddress)
erc20Contract := UtilsInterface.GetTokenManager(client)
opts := UtilsInterface.GetOptions()
balance, err = CoinInterface.BalanceOf(erc20Contract, &opts, address)
if err != nil {
log.Error("Error in fetching balance....Retrying")
return err
}
return nil
}, RetryInterface.RetryAttempts(core.MaxRetries))
address := common.HexToAddress(accountAddress)
erc20Contract := UtilsInterface.GetTokenManager(client)
opts := UtilsInterface.GetOptions()

returnedValues, err := InvokeFunctionWithRetryAttempts(CoinInterface, "BalanceOf", erc20Contract, &opts, address)
if err != nil {
return big.NewInt(0), err
}
return balance, nil
return returnedValues[0].Interface().(*big.Int), nil
}

func (*UtilsStruct) GetBufferedState(header *Types.Header, stateBuffer uint64, buffer int32) (int64, error) {
Expand Down

0 comments on commit c4e3e3b

Please sign in to comment.