Skip to content

Commit

Permalink
imp(rpc,server): concurrent gRPC queries (#1352)
Browse files Browse the repository at this point in the history
* Problem: grpc query is not run concurrently

Solution:
- Initiate the GRPCClient introduced in [sdk 0.46](cosmos/cosmos-sdk#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>
  • Loading branch information
mjtechworks and fedekunze committed Sep 20, 2022
1 parent 85b2b6c commit 0f98931
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
49 changes: 46 additions & 3 deletions server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"net"
"net/http"
"os"
"path/filepath"
Expand All @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion x/evm/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 0f98931

Please sign in to comment.