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: used a common http client for all the requests #1201

26 changes: 26 additions & 0 deletions client/httpClient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package client

import (
"net/http"
"razor/core/types"
"time"
)

type HttpClient struct {
client *http.Client
}

func NewHttpClient(config types.HttpClientConfig) *HttpClient {
client := &http.Client{
Timeout: time.Duration(config.Timeout) * time.Second,
Transport: &http.Transport{
MaxIdleConns: config.MaxIdleConnections,
MaxIdleConnsPerHost: config.MaxIdleConnectionsPerHost,
},
}
return &HttpClient{client}
}

func (hc *HttpClient) Do(request *http.Request) (*http.Response, error) {
return hc.client.Do(request)
}
5 changes: 3 additions & 2 deletions cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"math/big"
"razor/cache"
"razor/client"
"razor/core"
"razor/core/types"
"razor/pkg/bindings"
Expand Down Expand Up @@ -56,7 +57,7 @@ func (*UtilsStruct) GetSalt(client *ethclient.Client, epoch uint32) ([32]byte, e
HandleCommitState fetches the collections assigned to the staker and creates the leaves required for the merkle tree generation.
Values for only the collections assigned to the staker is fetched for others, 0 is added to the leaves of tree.
*/
func (*UtilsStruct) HandleCommitState(client *ethclient.Client, epoch uint32, seed []byte, rogueData types.Rogue) (types.CommitData, error) {
func (*UtilsStruct) HandleCommitState(client *ethclient.Client, epoch uint32, seed []byte, httpClient *client.HttpClient, rogueData types.Rogue) (types.CommitData, error) {
numActiveCollections, err := razorUtils.GetNumActiveCollections(client)
if err != nil {
return types.CommitData{}, err
Expand Down Expand Up @@ -95,7 +96,7 @@ func (*UtilsStruct) HandleCommitState(client *ethclient.Client, epoch uint32, se
errChan <- err
return
}
collectionData, err := razorUtils.GetAggregatedDataOfCollection(client, collectionId, epoch, localCache)
collectionData, err := razorUtils.GetAggregatedDataOfCollection(client, collectionId, epoch, localCache, httpClient)
if err != nil {
log.Error("Error in getting aggregated data of collection: ", err)
errChan <- err
Expand Down
9 changes: 5 additions & 4 deletions cmd/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/ethereum/go-ethereum/ethclient"
"github.com/stretchr/testify/mock"
"math/big"
clientPkg "razor/client"
"razor/core"
"razor/core/types"
"razor/pkg/bindings"
Expand Down Expand Up @@ -228,11 +229,11 @@ func TestHandleCommitState(t *testing.T) {
utilsMock.On("GetNumActiveCollections", mock.AnythingOfType("*ethclient.Client")).Return(tt.args.numActiveCollections, tt.args.numActiveCollectionsErr)
utilsMock.On("GetAssignedCollections", mock.AnythingOfType("*ethclient.Client"), mock.Anything, mock.Anything).Return(tt.args.assignedCollections, tt.args.seqAllottedCollections, tt.args.assignedCollectionsErr)
utilsMock.On("GetCollectionIdFromIndex", mock.AnythingOfType("*ethclient.Client"), mock.Anything).Return(tt.args.collectionId, tt.args.collectionIdErr)
utilsMock.On("GetAggregatedDataOfCollection", mock.AnythingOfType("*ethclient.Client"), mock.Anything, mock.Anything, mock.Anything).Return(tt.args.collectionData, tt.args.collectionDataErr)
utilsMock.On("GetAggregatedDataOfCollection", mock.AnythingOfType("*ethclient.Client"), mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.collectionData, tt.args.collectionDataErr)
utilsMock.On("GetRogueRandomValue", mock.Anything).Return(rogueValue)

utils := &UtilsStruct{}
got, err := utils.HandleCommitState(client, epoch, seed, tt.args.rogueData)
got, err := utils.HandleCommitState(client, epoch, seed, &clientPkg.HttpClient{}, tt.args.rogueData)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Data from HandleCommitState function, got = %v, want = %v", got, tt.want)
}
Expand Down Expand Up @@ -393,11 +394,11 @@ func BenchmarkHandleCommitState(b *testing.B) {
utilsMock.On("GetNumActiveCollections", mock.AnythingOfType("*ethclient.Client")).Return(v.numActiveCollections, nil)
utilsMock.On("GetAssignedCollections", mock.AnythingOfType("*ethclient.Client"), mock.Anything, mock.Anything).Return(v.assignedCollections, nil, nil)
utilsMock.On("GetCollectionIdFromIndex", mock.AnythingOfType("*ethclient.Client"), mock.Anything).Return(uint16(1), nil)
utilsMock.On("GetAggregatedDataOfCollection", mock.AnythingOfType("*ethclient.Client"), mock.Anything, mock.Anything, mock.Anything).Return(big.NewInt(1000), nil)
utilsMock.On("GetAggregatedDataOfCollection", mock.AnythingOfType("*ethclient.Client"), mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(big.NewInt(1000), nil)
utilsMock.On("GetRogueRandomValue", mock.Anything).Return(rogueValue)

ut := &UtilsStruct{}
_, err := ut.HandleCommitState(client, epoch, seed, types.Rogue{IsRogue: false})
_, err := ut.HandleCommitState(client, epoch, seed, &clientPkg.HttpClient{}, types.Rogue{IsRogue: false})
if err != nil {
log.Fatal(err)
}
Expand Down
1 change: 0 additions & 1 deletion cmd/config-utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ func (*UtilsStruct) GetConfigData() (types.Configurations, error) {
config.RPCTimeout = rpcTimeout
utils.RPCTimeout = rpcTimeout
config.HTTPTimeout = httpTimeout
utils.HTTPTimeout = httpTimeout
config.LogFileMaxSize = logFileMaxSize
config.LogFileMaxBackups = logFileMaxBackups
config.LogFileMaxAge = logFileMaxAge
Expand Down
9 changes: 5 additions & 4 deletions cmd/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"crypto/ecdsa"
"math/big"
"razor/client"
"razor/core/types"
"razor/path"
"razor/pkg/bindings"
Expand Down Expand Up @@ -164,7 +165,7 @@ type UtilsCmdInterface interface {
ClaimBounty(config types.Configurations, client *ethclient.Client, redeemBountyInput types.RedeemBountyInput) (common.Hash, error)
ClaimBlockReward(options types.TransactionOptions) (common.Hash, error)
GetSalt(client *ethclient.Client, epoch uint32) ([32]byte, error)
HandleCommitState(client *ethclient.Client, epoch uint32, seed []byte, rogueData types.Rogue) (types.CommitData, error)
HandleCommitState(client *ethclient.Client, epoch uint32, seed []byte, httpClient *client.HttpClient, rogueData types.Rogue) (types.CommitData, error)
Commit(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, seed []byte, values []*big.Int) (common.Hash, error)
ListAccounts() ([]accounts.Account, error)
AssignAmountInWei(flagSet *pflag.FlagSet) (*big.Int, error)
Expand Down Expand Up @@ -231,14 +232,14 @@ type UtilsCmdInterface interface {
GetSmallestStakeAndId(client *ethclient.Client, epoch uint32) (*big.Int, uint32, error)
StakeCoins(txnArgs types.TransactionOptions) (common.Hash, error)
CalculateSecret(account types.Account, epoch uint32, keystorePath string, chainId *big.Int) ([]byte, []byte, error)
HandleBlock(client *ethclient.Client, account types.Account, blockNumber *big.Int, config types.Configurations, rogueData types.Rogue, backupNodeActionsToIgnore []string)
HandleBlock(client *ethclient.Client, account types.Account, blockNumber *big.Int, config types.Configurations, httpClient *client.HttpClient, rogueData types.Rogue, backupNodeActionsToIgnore []string)
ExecuteVote(flagSet *pflag.FlagSet)
Vote(ctx context.Context, config types.Configurations, client *ethclient.Client, rogueData types.Rogue, account types.Account, backupNodeActionsToIgnore []string) error
Vote(ctx context.Context, config types.Configurations, client *ethclient.Client, account types.Account, httpClient *client.HttpClient, rogueData types.Rogue, backupNodeActionsToIgnore []string) error
HandleExit()
ExecuteListAccounts(flagSet *pflag.FlagSet)
ClaimCommission(flagSet *pflag.FlagSet)
ExecuteStake(flagSet *pflag.FlagSet)
InitiateCommit(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, stakerId uint32, rogueData types.Rogue) error
InitiateCommit(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, stakerId uint32, httpClient *client.HttpClient, rogueData types.Rogue) error
InitiateReveal(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, staker bindings.StructsStaker, rogueData types.Rogue) error
InitiatePropose(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, staker bindings.StructsStaker, blockNumber *big.Int, rogueData types.Rogue) error
GetBountyIdFromEvents(client *ethclient.Client, blockNumber *big.Int, bountyHunter string) (uint32, error)
Expand Down
Loading
Loading