From 6adbf417dfeaf94c96aefc0f021acd7a47347f40 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Thu, 12 Sep 2024 12:40:30 +0200 Subject: [PATCH] refactor(server/v2/cometbft): use only protov1 and backport #21084 (#21681) (cherry picked from commit 766117c5fffb78514363d4f7291e5f53b4db750c) # Conflicts: # server/v2/cometbft/go.mod # simapp/gomod2nix.toml --- server/v2/cometbft/client/rpc/block.go | 10 +++--- server/v2/cometbft/client/rpc/utils.go | 19 +++++----- server/v2/cometbft/commands.go | 49 ++++++++++++++++---------- server/v2/cometbft/go.mod | 6 ++++ server/v2/cometbft/utils.go | 26 +++++++------- 5 files changed, 63 insertions(+), 47 deletions(-) diff --git a/server/v2/cometbft/client/rpc/block.go b/server/v2/cometbft/client/rpc/block.go index 47ff2a081c28..1342f69f52ff 100644 --- a/server/v2/cometbft/client/rpc/block.go +++ b/server/v2/cometbft/client/rpc/block.go @@ -5,9 +5,9 @@ import ( "encoding/hex" "fmt" - v11 "buf.build/gen/go/cometbft/cometbft/protocolbuffers/go/cometbft/types/v1" + cmttypes "github.com/cometbft/cometbft/api/cometbft/types/v1" - abciv1beta1 "cosmossdk.io/api/cosmos/base/abci/v1beta1" + sdk "github.com/cosmos/cosmos-sdk/types" ) // GetChainHeight returns the current blockchain height. @@ -39,7 +39,7 @@ func GetChainHeight(ctx context.Context, rpcClient CometRPC) (int64, error) { // tx.height = 5 # all txs of the fifth block // // For more information, see the /subscribe CometBFT RPC endpoint documentation -func QueryBlocks(ctx context.Context, rpcClient CometRPC, page, limit int, query, orderBy string) (*abciv1beta1.SearchBlocksResult, error) { +func QueryBlocks(ctx context.Context, rpcClient CometRPC, page, limit int, query, orderBy string) (*sdk.SearchBlocksResult, error) { resBlocks, err := rpcClient.BlockSearch(ctx, query, &page, &limit, orderBy) if err != nil { return nil, err @@ -56,7 +56,7 @@ func QueryBlocks(ctx context.Context, rpcClient CometRPC, page, limit int, query } // GetBlockByHeight gets block by height -func GetBlockByHeight(ctx context.Context, rpcClient CometRPC, height *int64) (*v11.Block, error) { +func GetBlockByHeight(ctx context.Context, rpcClient CometRPC, height *int64) (*cmttypes.Block, error) { // header -> BlockchainInfo // header, tx -> Block // results -> BlockResults @@ -77,7 +77,7 @@ func GetBlockByHeight(ctx context.Context, rpcClient CometRPC, height *int64) (* } // GetBlockByHash gets block by hash -func GetBlockByHash(ctx context.Context, rpcClient CometRPC, hashHexString string) (*v11.Block, error) { +func GetBlockByHash(ctx context.Context, rpcClient CometRPC, hashHexString string) (*cmttypes.Block, error) { hash, err := hex.DecodeString(hashHexString) if err != nil { return nil, err diff --git a/server/v2/cometbft/client/rpc/utils.go b/server/v2/cometbft/client/rpc/utils.go index 6dfac989b9a1..0ac0b23259e6 100644 --- a/server/v2/cometbft/client/rpc/utils.go +++ b/server/v2/cometbft/client/rpc/utils.go @@ -3,19 +3,18 @@ package rpc import ( "fmt" - v11 "buf.build/gen/go/cometbft/cometbft/protocolbuffers/go/cometbft/types/v1" + cmttypes "github.com/cometbft/cometbft/api/cometbft/types/v1" coretypes "github.com/cometbft/cometbft/rpc/core/types" gogoproto "github.com/cosmos/gogoproto/proto" - protov2 "google.golang.org/protobuf/proto" - abciv1beta1 "cosmossdk.io/api/cosmos/base/abci/v1beta1" + sdk "github.com/cosmos/cosmos-sdk/types" ) // formatBlockResults parses the indexed blocks into a slice of BlockResponse objects. -func formatBlockResults(resBlocks []*coretypes.ResultBlock) ([]*v11.Block, error) { +func formatBlockResults(resBlocks []*coretypes.ResultBlock) ([]*cmttypes.Block, error) { var ( err error - out = make([]*v11.Block, len(resBlocks)) + out = make([]*cmttypes.Block, len(resBlocks)) ) for i := range resBlocks { out[i], err = NewResponseResultBlock(resBlocks[i]) @@ -30,9 +29,9 @@ func formatBlockResults(resBlocks []*coretypes.ResultBlock) ([]*v11.Block, error return out, nil } -func NewSearchBlocksResult(totalCount, count, page, limit int64, blocks []*v11.Block) *abciv1beta1.SearchBlocksResult { +func NewSearchBlocksResult(totalCount, count, page, limit int64, blocks []*cmttypes.Block) *sdk.SearchBlocksResult { totalPages := calcTotalPages(totalCount, limit) - return &abciv1beta1.SearchBlocksResult{ + return &sdk.SearchBlocksResult{ TotalCount: totalCount, Count: count, PageNumber: page, @@ -43,7 +42,7 @@ func NewSearchBlocksResult(totalCount, count, page, limit int64, blocks []*v11.B } // NewResponseResultBlock returns a BlockResponse given a ResultBlock from CometBFT -func NewResponseResultBlock(res *coretypes.ResultBlock) (*v11.Block, error) { +func NewResponseResultBlock(res *coretypes.ResultBlock) (*cmttypes.Block, error) { blkProto, err := res.Block.ToProto() if err != nil { return nil, err @@ -53,8 +52,8 @@ func NewResponseResultBlock(res *coretypes.ResultBlock) (*v11.Block, error) { return nil, err } - blk := &v11.Block{} - err = protov2.Unmarshal(blkBz, blk) + blk := &cmttypes.Block{} + err = gogoproto.Unmarshal(blkBz, blk) if err != nil { return nil, err } diff --git a/server/v2/cometbft/commands.go b/server/v2/cometbft/commands.go index 49be1c969adc..6e2afedfbb9d 100644 --- a/server/v2/cometbft/commands.go +++ b/server/v2/cometbft/commands.go @@ -7,6 +7,9 @@ import ( "strconv" "strings" + "github.com/spf13/cobra" + "sigs.k8s.io/yaml" + cmtcfg "github.com/cometbft/cometbft/config" cmtjson "github.com/cometbft/cometbft/libs/json" "github.com/cometbft/cometbft/node" @@ -14,9 +17,7 @@ import ( pvm "github.com/cometbft/cometbft/privval" rpchttp "github.com/cometbft/cometbft/rpc/client/http" cmtversion "github.com/cometbft/cometbft/version" - "github.com/spf13/cobra" - "google.golang.org/protobuf/encoding/protojson" - "sigs.k8s.io/yaml" + gogoproto "github.com/cosmos/gogoproto/proto" "cosmossdk.io/server/v2/cometbft/client/rpc" @@ -200,7 +201,7 @@ for. Each module documents its respective events under 'xx_events.md'. return err } - bz, err := protojson.Marshal(blocks) + bz, err := gogoproto.Marshal(blocks) if err != nil { return err } @@ -222,7 +223,7 @@ for. Each module documents its respective events under 'xx_events.md'. // QueryBlockCmd implements the default command for a Block query. func QueryBlockCmd() *cobra.Command { cmd := &cobra.Command{ - Use: "block --type={height|hash} ", + Use: "block --type={height|hash} [height|hash]", Short: "Query for a committed block by height, hash, or event(s)", Long: "Query for a specific committed block using the CometBFT RPC `block` and `block_by_hash` method", Example: strings.TrimSpace(fmt.Sprintf(` @@ -231,32 +232,45 @@ $ %s query block --%s=%s `, version.AppName, FlagType, TypeHeight, version.AppName, FlagType, TypeHash)), - Args: cobra.ExactArgs(1), + Args: cobra.MaximumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - typ, _ := cmd.Flags().GetString(FlagType) - rpcclient, err := rpcClient(cmd) - fmt.Println("rpcclient", rpcclient, err) if err != nil { return err } + typ, _ := cmd.Flags().GetString(FlagType) + if len(args) == 0 { + // do not break default v0.50 behavior of block hash + // if no args are provided, set the type to height + typ = TypeHeight + } + switch typ { case TypeHeight: - if args[0] == "" { - return errors.New("argument should be a block height") + var ( + err error + height int64 + ) + heightStr := "" + if len(args) > 0 { + heightStr = args[0] } - // optional height - var height *int64 - if len(args) > 0 { - height, err = parseOptionalHeight(args[0]) + if heightStr == "" { + cmd.Println("Falling back to latest block height:") + height, err = rpc.GetChainHeight(cmd.Context(), rpcclient) + if err != nil { + return fmt.Errorf("failed to get chain height: %w", err) + } + } else { + height, err = strconv.ParseInt(heightStr, 10, 64) if err != nil { - return err + return fmt.Errorf("failed to parse block height: %w", err) } } - output, err := rpc.GetBlockByHeight(cmd.Context(), rpcclient, height) + output, err := rpc.GetBlockByHeight(cmd.Context(), rpcclient, &height) if err != nil { return err } @@ -271,7 +285,6 @@ $ %s query block --%s=%s } return printOutput(cmd, bz) - case TypeHash: if args[0] == "" { diff --git a/server/v2/cometbft/go.mod b/server/v2/cometbft/go.mod index f1ab051d4380..088998ff28f4 100644 --- a/server/v2/cometbft/go.mod +++ b/server/v2/cometbft/go.mod @@ -16,9 +16,14 @@ replace ( ) require ( +<<<<<<< HEAD buf.build/gen/go/cometbft/cometbft/protocolbuffers/go v1.34.2-20240701160653-fedbb9acfd2f.2 cosmossdk.io/api v0.8.0 cosmossdk.io/core v1.0.0 // main +======= + cosmossdk.io/api v0.7.5 + cosmossdk.io/core v1.0.0-alpha.2 +>>>>>>> 766117c5f (refactor(server/v2/cometbft): use only protov1 and backport #21084 (#21681)) cosmossdk.io/errors v1.0.1 cosmossdk.io/log v1.4.1 cosmossdk.io/server/v2 v2.0.0-20240912070812-0fc06f14104b // main @@ -43,6 +48,7 @@ require ( ) require ( + buf.build/gen/go/cometbft/cometbft/protocolbuffers/go v1.34.2-20240701160653-fedbb9acfd2f.2 // indirect buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.34.2-20240130113600-88ef6483f90f.2 // indirect cosmossdk.io/collections v0.4.1-0.20240802064046-23fac2f1b8ab // indirect cosmossdk.io/core/testing v0.0.0-20240909133312-50288938d1b6 // indirect diff --git a/server/v2/cometbft/utils.go b/server/v2/cometbft/utils.go index b8470c79e76a..6aaf1b5401d6 100644 --- a/server/v2/cometbft/utils.go +++ b/server/v2/cometbft/utils.go @@ -8,15 +8,11 @@ import ( "strings" "time" - abciv1 "buf.build/gen/go/cometbft/cometbft/protocolbuffers/go/cometbft/abci/v1" abci "github.com/cometbft/cometbft/api/cometbft/abci/v1" cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1" gogoproto "github.com/cosmos/gogoproto/proto" gogoany "github.com/cosmos/gogoproto/types/any" - "google.golang.org/protobuf/encoding/protojson" - "google.golang.org/protobuf/types/known/anypb" - v1beta1 "cosmossdk.io/api/cosmos/base/abci/v1beta1" appmodulev2 "cosmossdk.io/core/appmodule/v2" "cosmossdk.io/core/comet" "cosmossdk.io/core/event" @@ -24,6 +20,8 @@ import ( "cosmossdk.io/core/transaction" errorsmod "cosmossdk.io/errors" consensus "cosmossdk.io/x/consensus/types" + + sdk "github.com/cosmos/cosmos-sdk/types" ) func queryResponse(res transaction.Msg, height int64) (*abci.QueryResponse, error) { @@ -148,16 +146,16 @@ func intoABCIEvents(events []event.Event, indexSet map[string]struct{}) []abci.E func intoABCISimulationResponse(txRes server.TxResult, indexSet map[string]struct{}) ([]byte, error) { indexAll := len(indexSet) == 0 - abciEvents := make([]*abciv1.Event, len(txRes.Events)) + abciEvents := make([]abci.Event, len(txRes.Events)) for i, e := range txRes.Events { - abciEvents[i] = &abciv1.Event{ + abciEvents[i] = abci.Event{ Type: e.Type, - Attributes: make([]*abciv1.EventAttribute, len(e.Attributes)), + Attributes: make([]abci.EventAttribute, len(e.Attributes)), } for j, attr := range e.Attributes { _, index := indexSet[fmt.Sprintf("%s.%s", e.Type, attr.Key)] - abciEvents[i].Attributes[j] = &abciv1.EventAttribute{ + abciEvents[i].Attributes[j] = abci.EventAttribute{ Key: attr.Key, Value: attr.Value, Index: index || indexAll, @@ -165,22 +163,22 @@ func intoABCISimulationResponse(txRes server.TxResult, indexSet map[string]struc } } - msgResponses := make([]*anypb.Any, len(txRes.Resp)) + msgResponses := make([]*gogoany.Any, len(txRes.Resp)) for i, resp := range txRes.Resp { // use this hack to maintain the protov2 API here for now anyMsg, err := gogoany.NewAnyWithCacheWithValue(resp) if err != nil { return nil, err } - msgResponses[i] = &anypb.Any{TypeUrl: anyMsg.TypeUrl, Value: anyMsg.Value} + msgResponses[i] = anyMsg } - res := &v1beta1.SimulationResponse{ - GasInfo: &v1beta1.GasInfo{ + res := &sdk.SimulationResponse{ + GasInfo: sdk.GasInfo{ GasWanted: txRes.GasWanted, GasUsed: txRes.GasUsed, }, - Result: &v1beta1.Result{ + Result: &sdk.Result{ Data: []byte{}, Log: txRes.Error.Error(), Events: abciEvents, @@ -188,7 +186,7 @@ func intoABCISimulationResponse(txRes server.TxResult, indexSet map[string]struc }, } - return protojson.Marshal(res) + return gogoproto.Marshal(res) } // ToSDKEvidence takes comet evidence and returns sdk evidence