Skip to content

Commit

Permalink
chore: return successful queries headers (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Apr 25, 2024
1 parent 261a601 commit 219a63d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
3 changes: 3 additions & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package constants

const HeaderBlockHeight = "Grpc-Metadata-X-Cosmos-Block-Height"
10 changes: 9 additions & 1 deletion pkg/fetchers/cosmos/proposals_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,21 @@ func (rpc *RPC) GetAllV1Proposals() ([]types.Proposal, *types.QueryError) {
)

var batchProposals responses.V1ProposalsRPCResponse
if errs := rpc.Client.Get(url, &batchProposals); len(errs) > 0 {
errs, header := rpc.Client.GetWithPredicate(url, &batchProposals, types.HTTPPredicateAlwaysPass())
if len(errs) > 0 {
return nil, &types.QueryError{
QueryError: nil,
NodeErrors: errs,
}
}

_, err := utils.GetBlockHeightFromHeader(header)
if err != nil {
return nil, &types.QueryError{
QueryError: errors.New("got error when parsing proposal height"),
}
}

if batchProposals.Message != "" {
return nil, &types.QueryError{
QueryError: errors.New(batchProposals.Message),
Expand Down
11 changes: 6 additions & 5 deletions pkg/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,29 @@ func (client *Client) Get(
url string,
target interface{},
) []types.NodeError {
return client.GetWithPredicate(url, target, types.HTTPPredicateAlwaysPass())
errs, _ := client.GetWithPredicate(url, target, types.HTTPPredicateAlwaysPass())
return errs
}

func (client *Client) GetWithPredicate(
url string,
target interface{},
predicate types.HTTPPredicate,
) []types.NodeError {
) ([]types.NodeError, http.Header) {
nodeErrors := make([]types.NodeError, len(client.Hosts))

for index, lcd := range client.Hosts {
fullURL := lcd + url
client.Logger.Trace().Str("url", fullURL).Msg("Trying making request to LCD")

_, err := client.GetFull(
header, err := client.GetFull(
fullURL,
target,
predicate,
)

if err == nil {
return nil
return nil, header
}

client.Logger.Warn().Str("url", fullURL).Err(err).Msg("LCD request failed")
Expand All @@ -60,7 +61,7 @@ func (client *Client) GetWithPredicate(
}

client.Logger.Warn().Str("url", url).Msg("All LCD requests failed")
return nodeErrors
return nodeErrors, nil
}

func (client *Client) GetFull(
Expand Down
11 changes: 2 additions & 9 deletions pkg/types/http_predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package types

import (
"fmt"
"main/pkg/utils"
"net/http"
"strconv"
)

type HTTPPredicate func(response *http.Response) error
Expand All @@ -16,14 +16,7 @@ func HTTPPredicateAlwaysPass() HTTPPredicate {

func HTTPPredicateCheckHeightAfter(prevHeight int64) HTTPPredicate {
return func(response *http.Response) error {
currentHeightHeader := response.Header.Get("Grpc-Metadata-X-Cosmos-Block-Height")

// not returned height is ok
if currentHeightHeader == "" {
return nil
}

currentHeight, err := strconv.ParseInt(currentHeightHeader, 10, 64)
currentHeight, err := utils.GetBlockHeightFromHeader(response.Header)
if err != nil {
return err
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package utils

import (
"fmt"
"main/pkg/constants"
"math"
"net/http"
"strconv"
"strings"
"time"
)
Expand Down Expand Up @@ -68,3 +71,17 @@ func FormatDuration(duration time.Duration) string {

return strings.Join(parts, " ")
}

func GetBlockHeightFromHeader(header http.Header) (int64, error) {
valueStr := header.Get(constants.HeaderBlockHeight)
if valueStr == "" {
return 0, nil
}

value, err := strconv.ParseInt(valueStr, 10, 64)
if err != nil {
return 0, err
}

return value, nil
}

0 comments on commit 219a63d

Please sign in to comment.