From 65740193f9b23ddfbc9348309eef4b0cfc268086 Mon Sep 17 00:00:00 2001 From: yihuang Date: Tue, 20 Sep 2022 21:00:36 +0800 Subject: [PATCH] imp(rpc,server): concurrent gRPC queries (#1352) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Problem: grpc query is not run concurrently Solution: - Initiate the GRPCClient introduced in [sdk 0.46](https://github.com/cosmos/cosmos-sdk/pull/11234). * Update CHANGELOG.md * Update CHANGELOG.md * Update server/start.go Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> * wrap error * Update server/start.go * more complete copy * fix grpc crash Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> --- CHANGELOG.md | 1 + server/start.go | 49 +++++++++++++++++++++++++++++++++++--- x/evm/keeper/grpc_query.go | 2 +- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d6d06345..95d8fa9e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -59,6 +59,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (test) [#1311](https://github.com/evmos/ethermint/pull/1311) Add integration test for the `rollback` cmd * (ledger) [#1277](https://github.com/evmos/ethermint/pull/1277) Add Ledger preprocessing transaction hook for EIP-712-signed Cosmos payloads. * (rpc) [#1296](https://github.com/evmos/ethermint/pull/1296) Add RPC Backend unit tests. +* (rpc) [#1352](https://github.com/evmos/ethermint/pull/1352) Make the grpc queries run concurrently, don't block the consensus state machine. ### Bug Fixes diff --git a/server/start.go b/server/start.go index 2843aba4..499737df 100644 --- a/server/start.go +++ b/server/start.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "io" + "net" "net/http" "os" "path/filepath" @@ -17,6 +18,7 @@ import ( "github.com/spf13/cobra" "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" abciserver "github.com/tendermint/tendermint/abci/server" tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands" @@ -39,6 +41,7 @@ import ( serverconfig "github.com/cosmos/cosmos-sdk/server/config" servergrpc "github.com/cosmos/cosmos-sdk/server/grpc" "github.com/cosmos/cosmos-sdk/server/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/evmos/ethermint/indexer" ethdebug "github.com/evmos/ethermint/rpc/namespaces/ethereum/debug" @@ -363,17 +366,57 @@ func startInProcess(ctx *server.Context, clientCtx client.Context, appCreator ty } } - var apiSrv *api.Server - if config.API.Enable { + if config.API.Enable || config.JSONRPC.Enable { genDoc, err := genDocProvider() if err != nil { return err } - clientCtx := clientCtx. + clientCtx = clientCtx. WithHomeDir(home). WithChainID(genDoc.ChainID) + // Set `GRPCClient` to `clientCtx` to enjoy concurrent grpc query. + // only use it if gRPC server is enabled. + if config.GRPC.Enable { + _, port, err := net.SplitHostPort(config.GRPC.Address) + if err != nil { + return sdkerrors.Wrapf(err, "invalid grpc address %s", config.GRPC.Address) + } + + maxSendMsgSize := config.GRPC.MaxSendMsgSize + if maxSendMsgSize == 0 { + maxSendMsgSize = serverconfig.DefaultGRPCMaxSendMsgSize + } + + maxRecvMsgSize := config.GRPC.MaxRecvMsgSize + if maxRecvMsgSize == 0 { + maxRecvMsgSize = serverconfig.DefaultGRPCMaxRecvMsgSize + } + + grpcAddress := fmt.Sprintf("127.0.0.1:%s", port) + + // If grpc is enabled, configure grpc client for grpc gateway and json-rpc. + grpcClient, err := grpc.Dial( + grpcAddress, + grpc.WithTransportCredentials(insecure.NewCredentials()), + grpc.WithDefaultCallOptions( + grpc.ForceCodec(codec.NewProtoCodec(clientCtx.InterfaceRegistry).GRPCCodec()), + grpc.MaxCallRecvMsgSize(maxRecvMsgSize), + grpc.MaxCallSendMsgSize(maxSendMsgSize), + ), + ) + if err != nil { + return err + } + + clientCtx = clientCtx.WithGRPCClient(grpcClient) + ctx.Logger.Debug("gRPC client assigned to client context", "address", grpcAddress) + } + } + + var apiSrv *api.Server + if config.API.Enable { apiSrv = api.New(clientCtx, ctx.Logger.With("server", "api")) app.RegisterAPIRoutes(apiSrv, config.API) errCh := make(chan error) diff --git a/x/evm/keeper/grpc_query.go b/x/evm/keeper/grpc_query.go index 9255a6c6..e2f4c5f7 100644 --- a/x/evm/keeper/grpc_query.go +++ b/x/evm/keeper/grpc_query.go @@ -106,7 +106,7 @@ func (k Keeper) ValidatorAccount(c context.Context, req *types.QueryValidatorAcc validator, found := k.stakingKeeper.GetValidatorByConsAddr(ctx, consAddr) if !found { - return nil, nil + return nil, fmt.Errorf("validator not found for %s", consAddr.String()) } accAddr := sdk.AccAddress(validator.GetOperator())