Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(server/v2/cometbft): use only protov1 and backport #21084 #21681

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions server/v2/cometbft/client/rpc/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
19 changes: 9 additions & 10 deletions server/v2/cometbft/client/rpc/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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
}
Expand Down
49 changes: 31 additions & 18 deletions server/v2/cometbft/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ 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"
"github.com/cometbft/cometbft/p2p"
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"

Expand Down Expand Up @@ -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
}
Expand All @@ -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} <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(`
Expand All @@ -231,32 +232,45 @@ $ %s query block --%s=%s <hash>
`,
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
}
Expand All @@ -271,7 +285,6 @@ $ %s query block --%s=%s <hash>
}

return printOutput(cmd, bz)

case TypeHash:

if args[0] == "" {
Expand Down
2 changes: 1 addition & 1 deletion server/v2/cometbft/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ replace (
)

require (
buf.build/gen/go/cometbft/cometbft/protocolbuffers/go v1.34.2-20240701160653-fedbb9acfd2f.2
cosmossdk.io/api v0.7.5
cosmossdk.io/core v1.0.0-alpha.2
cosmossdk.io/errors v1.0.1
Expand All @@ -45,6 +44,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.0 // indirect
cosmossdk.io/core/testing v0.0.0-00010101000000-000000000000 // indirect
Expand Down
26 changes: 12 additions & 14 deletions server/v2/cometbft/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,20 @@ 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"
"cosmossdk.io/core/server"
"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) {
Expand Down Expand Up @@ -148,47 +146,47 @@ 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,
}
}
}

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,
MsgResponses: msgResponses,
},
}

return protojson.Marshal(res)
return gogoproto.Marshal(res)
}

// ToSDKEvidence takes comet evidence and returns sdk evidence
Expand Down
Loading
Loading