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

BCI-979: Update Cosmos client connection #372

Merged
merged 2 commits into from
Sep 12, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 10 additions & 52 deletions pkg/cosmos/client/client.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package client

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"math"
"net/http"
"regexp"
"strconv"
"time"
Expand All @@ -19,6 +15,7 @@ import (

wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
rpchttp "github.com/cometbft/cometbft/rpc/client/http"
libclient "github.com/cometbft/cometbft/rpc/jsonrpc/client"
cosmosclient "github.com/cosmos/cosmos-sdk/client"
tmtypes "github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
"github.com/cosmos/cosmos-sdk/client/tx"
Expand All @@ -32,8 +29,6 @@ import (
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)

const httpResponseLimit = 10_000_000 // 10MB

//go:generate mockery --name ReaderWriter --output ./mocks/
type ReaderWriter interface {
Writer
Expand Down Expand Up @@ -98,59 +93,22 @@ type Client struct {
log logger.Logger
}

// responseRoundTripper is a http.RoundTripper which calls respFn with each response body.
type responseRoundTripper struct {
original http.RoundTripper
respFn func([]byte)
}

func (rt *responseRoundTripper) RoundTrip(r *http.Request) (resp *http.Response, err error) {
resp, err = rt.original.RoundTrip(r)
if err != nil {
return
}
source := http.MaxBytesReader(nil, resp.Body, httpResponseLimit)
b, err := io.ReadAll(resp.Body)
source.Close()
if err != nil {
return nil, fmt.Errorf("failed to read response: %w", err)
}
go rt.respFn(b)
resp.Body = io.NopCloser(bytes.NewReader(b))
return
}

// NewClient creates a new cosmos client
func NewClient(chainID string,
tendermintURL string,
clientURL string,
calvwang9 marked this conversation as resolved.
Show resolved Hide resolved
requestTimeout time.Duration,
lggr logger.Logger,
) (*Client, error) {
if requestTimeout <= 0 {
requestTimeout = DefaultTimeout
}
// Note rpchttp.New or rpchttp.NewWithTimeout use a (buggy) custom transport
// which results in new connections being created per request.
// Pass our own client here which uses a default transport and caches connections properly.
tmClient, err := rpchttp.NewWithClient(tendermintURL, "/websocket", &http.Client{
Timeout: requestTimeout,
Transport: &responseRoundTripper{original: http.DefaultTransport,
// Log any response that is missing the JSONRPC 'id' field, because the tendermint/rpc/jsonrpc/client rejects them.
respFn: func(b []byte) {
jsonRPC := struct {
ID json.RawMessage `json:"id"`
}{}
if err := json.Unmarshal(b, &jsonRPC); err != nil {
lggr.Warnf("Response is not a JSON object: %s: %v", string(b), err)
return
}
if len(jsonRPC.ID) == 0 || string(jsonRPC.ID) == "null" {
lggr.Warnf("Response is missing JSONRPC ID: %s", string(b))
return
}
},
},
})

httpClient, err := libclient.DefaultHTTPClient(clientURL)
if err != nil {
return nil, err
}
httpClient.Timeout = requestTimeout
rpcClient, err := rpchttp.NewWithClient(clientURL, "/websocket", httpClient)
if err != nil {
return nil, err
}
Expand All @@ -160,7 +118,7 @@ func NewClient(chainID string,
// If so then we would start putting timeouts on the ctx we pass in to the generate grpc client calls.
clientCtx := params.NewClientContext().
WithAccountRetriever(authtypes.AccountRetriever{}).
WithClient(tmClient).
WithClient(rpcClient).
calvwang9 marked this conversation as resolved.
Show resolved Hide resolved
WithChainID(chainID)

cosmosServiceClient := txtypes.NewServiceClient(clientCtx)
Expand Down
Loading