Skip to content

Commit

Permalink
feat: return QueryInfo on queries
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Oct 5, 2023
1 parent 0462416 commit 832985d
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 63 deletions.
23 changes: 13 additions & 10 deletions pkg/data_fetcher/data_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@ import (
)

type DataFetcher struct {
Logger zerolog.Logger
Cache *cache.Cache
Chain *configTypes.Chain
PriceFetchers map[string]priceFetchers.PriceFetcher
AliasManager *alias_manager.AliasManager
Logger zerolog.Logger
Cache *cache.Cache
Chain *configTypes.Chain
PriceFetchers map[string]priceFetchers.PriceFetcher
AliasManager *alias_manager.AliasManager
// MetricsManager *metrics.Manager
TendermintApiClients []*api.TendermintApiClient
}

func NewDataFetcher(
logger *zerolog.Logger,
chain *configTypes.Chain,
aliasManager *alias_manager.AliasManager,
// metricsManager *metrics.Manager,
) *DataFetcher {
tendermintApiClients := make([]*api.TendermintApiClient, len(chain.APINodes))
for index, node := range chain.APINodes {
Expand All @@ -44,6 +46,7 @@ func NewDataFetcher(
Chain: chain,
TendermintApiClients: tendermintApiClients,
AliasManager: aliasManager,
//MetricsManager: metricsManager,
}
}

Expand Down Expand Up @@ -116,7 +119,7 @@ func (f *DataFetcher) GetValidator(address string) (*responses.Validator, bool)
}

for _, node := range f.TendermintApiClients {
notCachedValidator, err := node.GetValidator(address)
notCachedValidator, err, _ := node.GetValidator(address)
if err != nil {
f.Logger.Error().Msg("Error fetching validator")
continue
Expand Down Expand Up @@ -147,7 +150,7 @@ func (f *DataFetcher) GetRewardsAtBlock(
}

for _, node := range f.TendermintApiClients {
notCachedValidator, err := node.GetDelegatorsRewardsAtBlock(delegator, validator, block-1)
notCachedValidator, err, _ := node.GetDelegatorsRewardsAtBlock(delegator, validator, block-1)
if err != nil {
f.Logger.Error().Err(err).Msg("Error fetching rewards")
continue
Expand Down Expand Up @@ -177,7 +180,7 @@ func (f *DataFetcher) GetCommissionAtBlock(
}

for _, node := range f.TendermintApiClients {
notCachedEntry, err := node.GetValidatorCommissionAtBlock(validator, block-1)
notCachedEntry, err, _ := node.GetValidatorCommissionAtBlock(validator, block-1)
if err != nil {
f.Logger.Error().Err(err).Msg("Error fetching commission")
continue
Expand All @@ -204,7 +207,7 @@ func (f *DataFetcher) GetProposal(id string) (*responses.Proposal, bool) {
}

for _, node := range f.TendermintApiClients {
notCachedEntry, err := node.GetProposal(id)
notCachedEntry, err, _ := node.GetProposal(id)
if err != nil {
f.Logger.Error().Err(err).Msg("Error fetching proposal")
continue
Expand All @@ -231,7 +234,7 @@ func (f *DataFetcher) GetStakingParams() (*responses.StakingParams, bool) {
}

for _, node := range f.TendermintApiClients {
notCachedEntry, err := node.GetStakingParams()
notCachedEntry, err, _ := node.GetStakingParams()
if err != nil {
f.Logger.Error().Err(err).Msg("Error fetching staking params")
continue
Expand Down
2 changes: 1 addition & 1 deletion pkg/messages/msg_grant.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package messages

import (
"main/pkg/types"
"main/pkg/types/amount"
"time"

configTypes "main/pkg/config/types"
dataFetcher "main/pkg/data_fetcher"
"main/pkg/types"
"main/pkg/types/event"
"main/pkg/utils"

Expand Down
2 changes: 1 addition & 1 deletion pkg/messages/msg_undelegate.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package messages

import (
"main/pkg/types"
"main/pkg/types/amount"
"time"

codecTypes "github.com/cosmos/cosmos-sdk/codec/types"

configTypes "main/pkg/config/types"
dataFetcher "main/pkg/data_fetcher"
"main/pkg/types"
"main/pkg/types/event"

cosmosTypes "github.com/cosmos/cosmos-sdk/types"
Expand Down
2 changes: 1 addition & 1 deletion pkg/messages/msg_vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package messages

import (
"fmt"
"main/pkg/types"
"strconv"

codecTypes "github.com/cosmos/cosmos-sdk/codec/types"

configTypes "main/pkg/config/types"
dataFetcher "main/pkg/data_fetcher"
"main/pkg/types"
"main/pkg/types/event"
"main/pkg/types/responses"

Expand Down
2 changes: 1 addition & 1 deletion pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (m *Manager) SetDefaultMetrics(chain *configTypes.Chain) {
Add(0)
}

//for _, node := range chain.APINodes {
// for _, node := range chain.APINodes {
// m.successfulQueriesCollector.
// With(prometheus.Labels{"chain": chain.Name, "node": node}).
// Add(0)
Expand Down
2 changes: 1 addition & 1 deletion pkg/reporters/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"html"
"html/template"
"main/pkg/types"
"main/pkg/types/amount"
"time"

Expand All @@ -15,7 +16,6 @@ import (
"main/pkg/config"
configTypes "main/pkg/config/types"
nodesManager "main/pkg/nodes_manager"
"main/pkg/types"
"main/pkg/utils"
"main/templates"

Expand Down
102 changes: 55 additions & 47 deletions pkg/tendermint/api/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package api
import (
"encoding/json"
"fmt"
"main/pkg/types/query_info"
"net/http"
"strconv"
"time"

"main/pkg/config/types"
configTypes "main/pkg/config/types"
"main/pkg/types/responses"

"github.com/rs/zerolog"
Expand All @@ -19,7 +20,7 @@ type TendermintApiClient struct {
Timeout time.Duration
}

func NewTendermintApiClient(logger *zerolog.Logger, url string, chain *types.Chain) *TendermintApiClient {
func NewTendermintApiClient(logger *zerolog.Logger, url string, chain *configTypes.Chain) *TendermintApiClient {
return &TendermintApiClient{
Logger: logger.With().
Str("component", "tendermint_api_client").
Expand All @@ -31,29 +32,25 @@ func NewTendermintApiClient(logger *zerolog.Logger, url string, chain *types.Cha
}
}

func (c *TendermintApiClient) GetValidator(address string) (*responses.Validator, error) {
url := fmt.Sprintf(
"%s/cosmos/staking/v1beta1/validators/%s",
c.URL,
address,
)
func (c *TendermintApiClient) GetValidator(address string) (*responses.Validator, error, query_info.QueryInfo) {
url := fmt.Sprintf("/cosmos/staking/v1beta1/validators/%s", address)

var response *responses.ValidatorResponse
if err := c.Get(url, &response); err != nil {
return nil, err
err, queryInfo := c.Get(url, &response)
if err != nil {
return nil, err, queryInfo
}

return &response.Validator, nil
return &response.Validator, nil, queryInfo
}

func (c *TendermintApiClient) GetDelegatorsRewardsAtBlock(
delegator string,
validator string,
block int64,
) ([]responses.Reward, error) {
) ([]responses.Reward, error, query_info.QueryInfo) {
url := fmt.Sprintf(
"%s/cosmos/distribution/v1beta1/delegators/%s/rewards/%s",
c.URL,
"/cosmos/distribution/v1beta1/delegators/%s/rewards/%s",
delegator,
validator,
)
Expand All @@ -63,20 +60,20 @@ func (c *TendermintApiClient) GetDelegatorsRewardsAtBlock(
}

var response *responses.RewardsResponse
if err := c.GetWithHeaders(url, &response, headers); err != nil || response == nil {
return nil, err
err, queryInfo := c.GetWithHeaders(url, &response, headers)
if err != nil || response == nil {
return nil, err, queryInfo
}

return response.Rewards, nil
return response.Rewards, nil, queryInfo
}

func (c *TendermintApiClient) GetValidatorCommissionAtBlock(
validator string,
block int64,
) ([]responses.Commission, error) {
) ([]responses.Commission, error, query_info.QueryInfo) {
url := fmt.Sprintf(
"%s/cosmos/distribution/v1beta1/validators/%s/commission",
c.URL,
"/cosmos/distribution/v1beta1/validators/%s/commission",
validator,
)

Expand All @@ -85,56 +82,61 @@ func (c *TendermintApiClient) GetValidatorCommissionAtBlock(
}

var response *responses.CommissionResponse
if err := c.GetWithHeaders(url, &response, headers); err != nil || response == nil {
return nil, err
err, queryInfo := c.GetWithHeaders(url, &response, headers)
if err != nil || response == nil {
return nil, err, queryInfo
}

return response.Commission.Commission, nil
return response.Commission.Commission, nil, queryInfo
}

func (c *TendermintApiClient) GetProposal(id string) (*responses.Proposal, error) {
url := fmt.Sprintf(
"%s/cosmos/gov/v1beta1/proposals/%s",
c.URL,
id,
)
func (c *TendermintApiClient) GetProposal(id string) (*responses.Proposal, error, query_info.QueryInfo) {
url := fmt.Sprintf("/cosmos/gov/v1beta1/proposals/%s", id)

var response *responses.ProposalResponse
if err := c.Get(url, &response); err != nil {
return nil, err
err, queryInfo := c.Get(url, &response)
if err != nil {
return nil, err, queryInfo
}

return &response.Proposal, nil
return &response.Proposal, nil, queryInfo
}

func (c *TendermintApiClient) GetStakingParams() (*responses.StakingParams, error) {
url := fmt.Sprintf("%s/cosmos/staking/v1beta1/params", c.URL)
func (c *TendermintApiClient) GetStakingParams() (*responses.StakingParams, error, query_info.QueryInfo) {
url := fmt.Sprintf("/cosmos/staking/v1beta1/params")

var response *responses.StakingParamsResponse
if err := c.Get(url, &response); err != nil {
return nil, err
err, queryInfo := c.Get(url, &response)
if err != nil {
return nil, err, queryInfo
}

return &response.Params, nil
return &response.Params, nil, queryInfo
}

func (c *TendermintApiClient) Get(url string, target interface{}) error {
func (c *TendermintApiClient) Get(url string, target interface{}) (error, query_info.QueryInfo) {
return c.GetWithHeaders(url, target, map[string]string{})
}

func (c *TendermintApiClient) GetWithHeaders(
url string,
relativeURL string,
target interface{},
headers map[string]string,
) error {
client := &http.Client{
Timeout: c.Timeout,
}
) (error, query_info.QueryInfo) {
url := fmt.Sprintf("%s%s", c.URL, relativeURL)

client := &http.Client{Timeout: c.Timeout}
start := time.Now()
queryInfo := query_info.QueryInfo{
Success: false,
Node: c.URL,
URL: relativeURL,
Time: 0,
}

req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return err
return err, queryInfo
}

req.Header.Set("User-Agent", "cosmos-transactions-bot")
Expand All @@ -146,9 +148,10 @@ func (c *TendermintApiClient) GetWithHeaders(
c.Logger.Trace().Str("url", url).Msg("Doing a query...")

res, err := client.Do(req)
queryInfo.Time = time.Since(start)
if err != nil {
c.Logger.Warn().Str("url", url).Err(err).Msg("Query failed")
return err
return err, queryInfo
}
defer res.Body.Close()

Expand All @@ -158,10 +161,15 @@ func (c *TendermintApiClient) GetWithHeaders(
Err(err).
Int("status", res.StatusCode).
Msg("Query returned bad HTTP code")
return fmt.Errorf("bad HTTP code: %d", res.StatusCode)
return fmt.Errorf("bad HTTP code: %d", res.StatusCode), queryInfo
}

c.Logger.Debug().Str("url", url).Dur("duration", time.Since(start)).Msg("Query is finished")

return json.NewDecoder(res.Body).Decode(target)
if err := json.NewDecoder(res.Body).Decode(target); err != nil {
return err, queryInfo
}

queryInfo.Success = true
return nil, queryInfo
}
10 changes: 10 additions & 0 deletions pkg/types/query_info/query_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package query_info

import "time"

type QueryInfo struct {
Success bool
Time time.Duration
URL string
Node string
}
4 changes: 3 additions & 1 deletion pkg/types/reportable.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import "main/pkg/data_fetcher"
import (
"main/pkg/data_fetcher"
)

type Reportable interface {
Type() string
Expand Down

0 comments on commit 832985d

Please sign in to comment.