Skip to content

Commit

Permalink
fix(server/v2/comebft): wire missing services + fix simulation (backp…
Browse files Browse the repository at this point in the history
…ort #21964) (#22555)

Co-authored-by: Julien Robert <[email protected]>
  • Loading branch information
mergify[bot] and julienrbrt authored Nov 19, 2024
1 parent 0083431 commit 3fcdcae
Show file tree
Hide file tree
Showing 21 changed files with 413 additions and 240 deletions.
6 changes: 3 additions & 3 deletions client/grpc/cmtservice/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ type (

// NewQueryServer creates a new CometBFT query server.
func NewQueryServer(
clientCtx CometRPC,
cometRPC CometRPC,
queryFn abciQueryFn,
consensusAddressCodec address.Codec,
) ServiceServer {
return queryServer{
rpc: clientCtx,
rpc: cometRPC,
queryFn: queryFn,
consensusCodec: consensusAddressCodec,
}
Expand Down Expand Up @@ -284,7 +284,7 @@ func (s queryServer) ABCIQuery(ctx context.Context, req *ABCIQueryRequest) (*ABC
func RegisterTendermintService(
clientCtx client.Context,
server gogogrpc.Server,
iRegistry codectypes.InterfaceRegistry,
_ codectypes.InterfaceRegistry,
queryFn abciQueryFn,
) {
node, err := clientCtx.GetNode()
Expand Down
22 changes: 22 additions & 0 deletions proto/cosmos/base/node/v2/query.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
syntax = "proto3";
package cosmos.base.node.v2;

import "google/api/annotations.proto";

option go_package = "cosmossdk.io/server/v2/api/grpc/nodeservice";

// Service defines the gRPC querier service for node related queries.
service Service {
// Config queries for the operator configuration.
rpc Config(ConfigRequest) returns (ConfigResponse) {
option (google.api.http).get = "/cosmos/base/node/v2/config";
}
}

// ConfigRequest defines the request structure for the Config gRPC query.
message ConfigRequest {}

// ConfigResponse defines the response structure for the Config gRPC query.
message ConfigResponse {
string minimum_gas_price = 1;
}
68 changes: 58 additions & 10 deletions server/v2/cometbft/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (

abci "github.com/cometbft/cometbft/abci/types"
abciproto "github.com/cometbft/cometbft/api/cometbft/abci/v1"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
gogoproto "github.com/cosmos/gogoproto/proto"
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
Expand All @@ -27,7 +30,6 @@ import (
"cosmossdk.io/log"
"cosmossdk.io/schema/appdata"
"cosmossdk.io/server/v2/appmanager"
"cosmossdk.io/server/v2/cometbft/client/grpc/cmtservice"
"cosmossdk.io/server/v2/cometbft/handlers"
"cosmossdk.io/server/v2/cometbft/mempool"
"cosmossdk.io/server/v2/cometbft/types"
Expand All @@ -37,13 +39,18 @@ import (
consensustypes "cosmossdk.io/x/consensus/types"
)

const (
QueryPathApp = "app"
QueryPathP2P = "p2p"
QueryPathStore = "store"
)

var _ abci.Application = (*Consensus[transaction.Tx])(nil)

type Consensus[T transaction.Tx] struct {
logger log.Logger
appName, version string
app appmanager.AppManager[T]
appCloser func() error
txCodec transaction.Codec[T]
store types.Store
streaming streaming.Manager
Expand Down Expand Up @@ -78,7 +85,6 @@ func NewConsensus[T transaction.Tx](
logger log.Logger,
appName string,
app appmanager.AppManager[T],
appCloser func() error,
mp mempool.Mempool[T],
indexedEvents map[string]struct{},
queryHandlersMap map[string]appmodulev2.Handler,
Expand All @@ -91,7 +97,6 @@ func NewConsensus[T transaction.Tx](
appName: appName,
version: getCometBFTServerVersion(),
app: app,
appCloser: appCloser,
cfg: cfg,
store: store,
logger: logger,
Expand Down Expand Up @@ -221,17 +226,17 @@ func (c *Consensus[T]) Query(ctx context.Context, req *abciproto.QueryRequest) (
}

switch path[0] {
case cmtservice.QueryPathApp:
case QueryPathApp:
resp, err = c.handlerQueryApp(ctx, path, req)

case cmtservice.QueryPathStore:
resp, err = c.handleQueryStore(path, c.store, req)
case QueryPathStore:
resp, err = c.handleQueryStore(path, req)

case cmtservice.QueryPathP2P:
case QueryPathP2P:
resp, err = c.handleQueryP2P(path)

default:
resp = QueryResult(errorsmod.Wrap(cometerrors.ErrUnknownRequest, "unknown query path"), c.cfg.AppTomlConfig.Trace)
resp = QueryResult(errorsmod.Wrapf(cometerrors.ErrUnknownRequest, "unknown query path %s", req.Path), c.cfg.AppTomlConfig.Trace)
}

if err != nil {
Expand Down Expand Up @@ -267,6 +272,50 @@ func (c *Consensus[T]) maybeRunGRPCQuery(ctx context.Context, req *abci.QueryReq
handlerFullName = string(md.Input().FullName())
}

// special case for simulation as it is an external gRPC registered on the grpc server component
// and not on the app itself, so it won't pass the router afterwards.
if req.Path == "/cosmos.tx.v1beta1.Service/Simulate" {
simulateRequest := &txtypes.SimulateRequest{}
err = gogoproto.Unmarshal(req.Data, simulateRequest)
if err != nil {
return nil, true, fmt.Errorf("unable to decode gRPC request with path %s from ABCI.Query: %w", req.Path, err)
}

tx, err := c.txCodec.Decode(simulateRequest.TxBytes)
if err != nil {
return nil, true, fmt.Errorf("failed to decode tx: %w", err)
}

txResult, _, err := c.app.Simulate(ctx, tx)
if err != nil {
return nil, true, fmt.Errorf("%v with gas used: '%d'", err, txResult.GasUsed)
}

msgResponses := make([]*codectypes.Any, 0, len(txResult.Resp))
// pack the messages into Any
for _, msg := range txResult.Resp {
anyMsg, err := codectypes.NewAnyWithValue(msg)
if err != nil {
return nil, true, fmt.Errorf("failed to pack message response: %w", err)
}

msgResponses = append(msgResponses, anyMsg)
}

resp := &txtypes.SimulateResponse{
GasInfo: &sdk.GasInfo{
GasUsed: txResult.GasUsed,
GasWanted: txResult.GasWanted,
},
Result: &sdk.Result{
MsgResponses: msgResponses,
},
}

res, err := queryResponse(resp, req.Height)
return res, true, err
}

handler, found := c.queryHandlersMap[handlerFullName]
if !found {
return nil, true, fmt.Errorf("no query handler found for %s", req.Path)
Expand All @@ -281,7 +330,6 @@ func (c *Consensus[T]) maybeRunGRPCQuery(ctx context.Context, req *abci.QueryReq
resp := QueryResult(err, c.cfg.AppTomlConfig.Trace)
resp.Height = req.Height
return resp, true, err

}

resp, err = queryResponse(res, req.Height)
Expand Down
2 changes: 1 addition & 1 deletion server/v2/cometbft/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ func setUpConsensus(t *testing.T, gasLimit uint64, mempool mempool.Mempool[mock.
nil,
)

return NewConsensus[mock.Tx](log.NewNopLogger(), "testing-app", am, func() error { return nil },
return NewConsensus[mock.Tx](log.NewNopLogger(), "testing-app", am,
mempool, map[string]struct{}{}, nil, mockStore,
Config{AppTomlConfig: DefaultAppTomlConfig()}, mock.TxCodec{}, "test")
}
Expand Down
71 changes: 0 additions & 71 deletions server/v2/cometbft/client/grpc/cmtservice/autocli.go

This file was deleted.

56 changes: 0 additions & 56 deletions server/v2/cometbft/client/grpc/cmtservice/service.go

This file was deleted.

16 changes: 11 additions & 5 deletions server/v2/cometbft/client/rpc/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,15 @@ func QueryBlocks(ctx context.Context, rpcClient CometRPC, page, limit int, query
return nil, err
}

result := NewSearchBlocksResult(int64(resBlocks.TotalCount), int64(len(blocks)), int64(page), int64(limit), blocks)

return result, nil
totalPages := calcTotalPages(int64(resBlocks.TotalCount), int64(limit))
return &sdk.SearchBlocksResult{
TotalCount: int64(resBlocks.TotalCount),
Count: int64(len(blocks)),
PageNumber: int64(page),
PageTotal: totalPages,
Limit: int64(limit),
Blocks: blocks,
}, nil
}

// GetBlockByHeight gets block by height
Expand All @@ -65,7 +71,7 @@ func GetBlockByHeight(ctx context.Context, rpcClient CometRPC, height *int64) (*
return nil, err
}

out, err := NewResponseResultBlock(resBlock)
out, err := responseResultBlock(resBlock)
if err != nil {
return nil, err
}
Expand All @@ -90,7 +96,7 @@ func GetBlockByHash(ctx context.Context, rpcClient CometRPC, hashHexString strin
} else if resBlock.Block == nil {
return nil, fmt.Errorf("block not found with hash: %s", hashHexString)
}
out, err := NewResponseResultBlock(resBlock)
out, err := responseResultBlock(resBlock)
if err != nil {
return nil, err
}
Expand Down
20 changes: 3 additions & 17 deletions server/v2/cometbft/client/rpc/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
cmttypes "github.com/cometbft/cometbft/api/cometbft/types/v1"
coretypes "github.com/cometbft/cometbft/rpc/core/types"
gogoproto "github.com/cosmos/gogoproto/proto"

sdk "github.com/cosmos/cosmos-sdk/types"
)

// formatBlockResults parses the indexed blocks into a slice of BlockResponse objects.
Expand All @@ -17,7 +15,7 @@ func formatBlockResults(resBlocks []*coretypes.ResultBlock) ([]*cmttypes.Block,
out = make([]*cmttypes.Block, len(resBlocks))
)
for i := range resBlocks {
out[i], err = NewResponseResultBlock(resBlocks[i])
out[i], err = responseResultBlock(resBlocks[i])
if err != nil {
return nil, fmt.Errorf("unable to create response block from comet result block: %v: %w", resBlocks[i], err)
}
Expand All @@ -29,20 +27,8 @@ func formatBlockResults(resBlocks []*coretypes.ResultBlock) ([]*cmttypes.Block,
return out, nil
}

func NewSearchBlocksResult(totalCount, count, page, limit int64, blocks []*cmttypes.Block) *sdk.SearchBlocksResult {
totalPages := calcTotalPages(totalCount, limit)
return &sdk.SearchBlocksResult{
TotalCount: totalCount,
Count: count,
PageNumber: page,
PageTotal: totalPages,
Limit: limit,
Blocks: blocks,
}
}

// NewResponseResultBlock returns a BlockResponse given a ResultBlock from CometBFT
func NewResponseResultBlock(res *coretypes.ResultBlock) (*cmttypes.Block, error) {
// responseResultBlock returns a BlockResponse given a ResultBlock from CometBFT
func responseResultBlock(res *coretypes.ResultBlock) (*cmttypes.Block, error) {
blkProto, err := res.Block.ToProto()
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 3fcdcae

Please sign in to comment.