Skip to content

Commit

Permalink
feat: check for previous height when querying
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed May 2, 2024
1 parent b5b6f57 commit 253f95f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
5 changes: 5 additions & 0 deletions pkg/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func NewClient(logger *zerolog.Logger, chain string, tracer trace.Tracer) *Clien
func (c *Client) Get(
url string,
target interface{},
predicate types.HTTPPredicate,
ctx context.Context,
) (types.QueryInfo, http.Header, error) {
childCtx, span := c.tracer.Start(ctx, "HTTP request")
Expand Down Expand Up @@ -71,6 +72,10 @@ func (c *Client) Get(

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

if predicateErr := predicate(res); predicateErr != nil {
return queryInfo, res.Header, predicateErr
}

err = json.NewDecoder(res.Body).Decode(target)
queryInfo.Success = err == nil

Expand Down
2 changes: 1 addition & 1 deletion pkg/price_fetchers/coingecko/coingecko.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (c *Coingecko) FetchPrices(
url := fmt.Sprintf("https://api.coingecko.com/api/v3/simple/price?ids=%s&vs_currencies=usd", ids)

var response Response
queryInfo, _, err := c.Client.Get(url, &response, childCtx)
queryInfo, _, err := c.Client.Get(url, &response, types.HTTPPredicateAlwaysPass(), childCtx)

if err != nil {
c.Logger.Error().Err(err).Msg("Could not get rate")
Expand Down
15 changes: 14 additions & 1 deletion pkg/tendermint/tendermint.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,20 @@ func (rpc *RPC) Get(
target interface{},
ctx context.Context,
) (types.QueryInfo, error) {
info, header, err := rpc.Client.Get(url, target, ctx)
rpc.Mutex.Lock()
previousHeight, found := rpc.LastHeight[url]
if !found {
previousHeight = 0
}
rpc.Mutex.Unlock()

info, header, err := rpc.Client.Get(
url,
target,
types.HTTPPredicateCheckHeightAfter(previousHeight),
ctx,
)

if err != nil {
return info, err
}
Expand Down
34 changes: 34 additions & 0 deletions pkg/types/http_predicate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package types

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

type HTTPPredicate func(response *http.Response) error

func HTTPPredicateAlwaysPass() HTTPPredicate {
return func(response *http.Response) error {
return nil
}
}

func HTTPPredicateCheckHeightAfter(prevHeight int64) HTTPPredicate {
return func(response *http.Response) error {
currentHeight, err := utils.GetBlockHeightFromHeader(response.Header)
if err != nil {
return err
}

if prevHeight > currentHeight {
return fmt.Errorf(
"previous height (%d) is bigger than the current height (%d)",
prevHeight,
currentHeight,
)
}

return nil
}
}

0 comments on commit 253f95f

Please sign in to comment.