From 7edbb2dae7616991449f8e81940a938f5300c5bc Mon Sep 17 00:00:00 2001 From: Calvin Wang Date: Tue, 12 Sep 2023 00:03:22 +1000 Subject: [PATCH 1/2] update cosmos client connection --- pkg/cosmos/client/client.go | 62 ++++++------------------------------- 1 file changed, 10 insertions(+), 52 deletions(-) diff --git a/pkg/cosmos/client/client.go b/pkg/cosmos/client/client.go index 6f9ab843..870ebf5d 100644 --- a/pkg/cosmos/client/client.go +++ b/pkg/cosmos/client/client.go @@ -1,13 +1,9 @@ package client import ( - "bytes" "context" - "encoding/json" "fmt" - "io" "math" - "net/http" "regexp" "strconv" "time" @@ -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" @@ -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 @@ -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, 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 } @@ -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). WithChainID(chainID) cosmosServiceClient := txtypes.NewServiceClient(clientCtx) From 97012db056c45a30944b9af1c4d8fa8ab7f2a8b6 Mon Sep 17 00:00:00 2001 From: Calvin Wang Date: Tue, 12 Sep 2023 00:39:26 +1000 Subject: [PATCH 2/2] revert renaming --- pkg/cosmos/client/client.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/cosmos/client/client.go b/pkg/cosmos/client/client.go index 870ebf5d..ece69f28 100644 --- a/pkg/cosmos/client/client.go +++ b/pkg/cosmos/client/client.go @@ -95,7 +95,7 @@ type Client struct { // NewClient creates a new cosmos client func NewClient(chainID string, - clientURL string, + tendermintURL string, requestTimeout time.Duration, lggr logger.Logger, ) (*Client, error) { @@ -103,12 +103,12 @@ func NewClient(chainID string, requestTimeout = DefaultTimeout } - httpClient, err := libclient.DefaultHTTPClient(clientURL) + httpClient, err := libclient.DefaultHTTPClient(tendermintURL) if err != nil { return nil, err } httpClient.Timeout = requestTimeout - rpcClient, err := rpchttp.NewWithClient(clientURL, "/websocket", httpClient) + tmClient, err := rpchttp.NewWithClient(tendermintURL, "/websocket", httpClient) if err != nil { return nil, err } @@ -118,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(rpcClient). + WithClient(tmClient). WithChainID(chainID) cosmosServiceClient := txtypes.NewServiceClient(clientCtx)