Skip to content

Commit 766117c

Browse files
authored
refactor(server/v2/cometbft): use only protov1 and backport #21084 (#21681)
1 parent 5475903 commit 766117c

6 files changed

Lines changed: 58 additions & 633 deletions

File tree

server/v2/cometbft/client/rpc/block.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
"encoding/hex"
66
"fmt"
77

8-
v11 "buf.build/gen/go/cometbft/cometbft/protocolbuffers/go/cometbft/types/v1"
8+
cmttypes "github.com/cometbft/cometbft/api/cometbft/types/v1"
99

10-
abciv1beta1 "cosmossdk.io/api/cosmos/base/abci/v1beta1"
10+
sdk "github.com/cosmos/cosmos-sdk/types"
1111
)
1212

1313
// GetChainHeight returns the current blockchain height.
@@ -39,7 +39,7 @@ func GetChainHeight(ctx context.Context, rpcClient CometRPC) (int64, error) {
3939
// tx.height = 5 # all txs of the fifth block
4040
//
4141
// For more information, see the /subscribe CometBFT RPC endpoint documentation
42-
func QueryBlocks(ctx context.Context, rpcClient CometRPC, page, limit int, query, orderBy string) (*abciv1beta1.SearchBlocksResult, error) {
42+
func QueryBlocks(ctx context.Context, rpcClient CometRPC, page, limit int, query, orderBy string) (*sdk.SearchBlocksResult, error) {
4343
resBlocks, err := rpcClient.BlockSearch(ctx, query, &page, &limit, orderBy)
4444
if err != nil {
4545
return nil, err
@@ -56,7 +56,7 @@ func QueryBlocks(ctx context.Context, rpcClient CometRPC, page, limit int, query
5656
}
5757

5858
// GetBlockByHeight gets block by height
59-
func GetBlockByHeight(ctx context.Context, rpcClient CometRPC, height *int64) (*v11.Block, error) {
59+
func GetBlockByHeight(ctx context.Context, rpcClient CometRPC, height *int64) (*cmttypes.Block, error) {
6060
// header -> BlockchainInfo
6161
// header, tx -> Block
6262
// results -> BlockResults
@@ -77,7 +77,7 @@ func GetBlockByHeight(ctx context.Context, rpcClient CometRPC, height *int64) (*
7777
}
7878

7979
// GetBlockByHash gets block by hash
80-
func GetBlockByHash(ctx context.Context, rpcClient CometRPC, hashHexString string) (*v11.Block, error) {
80+
func GetBlockByHash(ctx context.Context, rpcClient CometRPC, hashHexString string) (*cmttypes.Block, error) {
8181
hash, err := hex.DecodeString(hashHexString)
8282
if err != nil {
8383
return nil, err

server/v2/cometbft/client/rpc/utils.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,18 @@ package rpc
33
import (
44
"fmt"
55

6-
v11 "buf.build/gen/go/cometbft/cometbft/protocolbuffers/go/cometbft/types/v1"
6+
cmttypes "github.com/cometbft/cometbft/api/cometbft/types/v1"
77
coretypes "github.com/cometbft/cometbft/rpc/core/types"
88
gogoproto "github.com/cosmos/gogoproto/proto"
9-
protov2 "google.golang.org/protobuf/proto"
109

11-
abciv1beta1 "cosmossdk.io/api/cosmos/base/abci/v1beta1"
10+
sdk "github.com/cosmos/cosmos-sdk/types"
1211
)
1312

1413
// formatBlockResults parses the indexed blocks into a slice of BlockResponse objects.
15-
func formatBlockResults(resBlocks []*coretypes.ResultBlock) ([]*v11.Block, error) {
14+
func formatBlockResults(resBlocks []*coretypes.ResultBlock) ([]*cmttypes.Block, error) {
1615
var (
1716
err error
18-
out = make([]*v11.Block, len(resBlocks))
17+
out = make([]*cmttypes.Block, len(resBlocks))
1918
)
2019
for i := range resBlocks {
2120
out[i], err = NewResponseResultBlock(resBlocks[i])
@@ -30,9 +29,9 @@ func formatBlockResults(resBlocks []*coretypes.ResultBlock) ([]*v11.Block, error
3029
return out, nil
3130
}
3231

33-
func NewSearchBlocksResult(totalCount, count, page, limit int64, blocks []*v11.Block) *abciv1beta1.SearchBlocksResult {
32+
func NewSearchBlocksResult(totalCount, count, page, limit int64, blocks []*cmttypes.Block) *sdk.SearchBlocksResult {
3433
totalPages := calcTotalPages(totalCount, limit)
35-
return &abciv1beta1.SearchBlocksResult{
34+
return &sdk.SearchBlocksResult{
3635
TotalCount: totalCount,
3736
Count: count,
3837
PageNumber: page,
@@ -43,7 +42,7 @@ func NewSearchBlocksResult(totalCount, count, page, limit int64, blocks []*v11.B
4342
}
4443

4544
// NewResponseResultBlock returns a BlockResponse given a ResultBlock from CometBFT
46-
func NewResponseResultBlock(res *coretypes.ResultBlock) (*v11.Block, error) {
45+
func NewResponseResultBlock(res *coretypes.ResultBlock) (*cmttypes.Block, error) {
4746
blkProto, err := res.Block.ToProto()
4847
if err != nil {
4948
return nil, err
@@ -53,8 +52,8 @@ func NewResponseResultBlock(res *coretypes.ResultBlock) (*v11.Block, error) {
5352
return nil, err
5453
}
5554

56-
blk := &v11.Block{}
57-
err = protov2.Unmarshal(blkBz, blk)
55+
blk := &cmttypes.Block{}
56+
err = gogoproto.Unmarshal(blkBz, blk)
5857
if err != nil {
5958
return nil, err
6059
}

server/v2/cometbft/commands.go

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ import (
77
"strconv"
88
"strings"
99

10+
"github.com/spf13/cobra"
11+
"sigs.k8s.io/yaml"
12+
1013
cmtcfg "github.com/cometbft/cometbft/config"
1114
cmtjson "github.com/cometbft/cometbft/libs/json"
1215
"github.com/cometbft/cometbft/node"
1316
"github.com/cometbft/cometbft/p2p"
1417
pvm "github.com/cometbft/cometbft/privval"
1518
rpchttp "github.com/cometbft/cometbft/rpc/client/http"
1619
cmtversion "github.com/cometbft/cometbft/version"
17-
"github.com/spf13/cobra"
18-
"google.golang.org/protobuf/encoding/protojson"
19-
"sigs.k8s.io/yaml"
20+
gogoproto "github.com/cosmos/gogoproto/proto"
2021

2122
"cosmossdk.io/server/v2/cometbft/client/rpc"
2223

@@ -200,7 +201,7 @@ for. Each module documents its respective events under 'xx_events.md'.
200201
return err
201202
}
202203

203-
bz, err := protojson.Marshal(blocks)
204+
bz, err := gogoproto.Marshal(blocks)
204205
if err != nil {
205206
return err
206207
}
@@ -222,7 +223,7 @@ for. Each module documents its respective events under 'xx_events.md'.
222223
// QueryBlockCmd implements the default command for a Block query.
223224
func QueryBlockCmd() *cobra.Command {
224225
cmd := &cobra.Command{
225-
Use: "block --type={height|hash} <height|hash>",
226+
Use: "block --type={height|hash} [height|hash]",
226227
Short: "Query for a committed block by height, hash, or event(s)",
227228
Long: "Query for a specific committed block using the CometBFT RPC `block` and `block_by_hash` method",
228229
Example: strings.TrimSpace(fmt.Sprintf(`
@@ -231,32 +232,45 @@ $ %s query block --%s=%s <hash>
231232
`,
232233
version.AppName, FlagType, TypeHeight,
233234
version.AppName, FlagType, TypeHash)),
234-
Args: cobra.ExactArgs(1),
235+
Args: cobra.MaximumNArgs(1),
235236
RunE: func(cmd *cobra.Command, args []string) error {
236-
typ, _ := cmd.Flags().GetString(FlagType)
237-
238237
rpcclient, err := rpcClient(cmd)
239-
fmt.Println("rpcclient", rpcclient, err)
240238
if err != nil {
241239
return err
242240
}
243241

242+
typ, _ := cmd.Flags().GetString(FlagType)
243+
if len(args) == 0 {
244+
// do not break default v0.50 behavior of block hash
245+
// if no args are provided, set the type to height
246+
typ = TypeHeight
247+
}
248+
244249
switch typ {
245250
case TypeHeight:
246-
if args[0] == "" {
247-
return errors.New("argument should be a block height")
251+
var (
252+
err error
253+
height int64
254+
)
255+
heightStr := ""
256+
if len(args) > 0 {
257+
heightStr = args[0]
248258
}
249259

250-
// optional height
251-
var height *int64
252-
if len(args) > 0 {
253-
height, err = parseOptionalHeight(args[0])
260+
if heightStr == "" {
261+
cmd.Println("Falling back to latest block height:")
262+
height, err = rpc.GetChainHeight(cmd.Context(), rpcclient)
263+
if err != nil {
264+
return fmt.Errorf("failed to get chain height: %w", err)
265+
}
266+
} else {
267+
height, err = strconv.ParseInt(heightStr, 10, 64)
254268
if err != nil {
255-
return err
269+
return fmt.Errorf("failed to parse block height: %w", err)
256270
}
257271
}
258272

259-
output, err := rpc.GetBlockByHeight(cmd.Context(), rpcclient, height)
273+
output, err := rpc.GetBlockByHeight(cmd.Context(), rpcclient, &height)
260274
if err != nil {
261275
return err
262276
}
@@ -271,7 +285,6 @@ $ %s query block --%s=%s <hash>
271285
}
272286

273287
return printOutput(cmd, bz)
274-
275288
case TypeHash:
276289

277290
if args[0] == "" {

server/v2/cometbft/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ replace (
1818
)
1919

2020
require (
21-
buf.build/gen/go/cometbft/cometbft/protocolbuffers/go v1.34.2-20240701160653-fedbb9acfd2f.2
2221
cosmossdk.io/api v0.7.5
2322
cosmossdk.io/core v1.0.0-alpha.2
2423
cosmossdk.io/errors v1.0.1
@@ -45,6 +44,7 @@ require (
4544
)
4645

4746
require (
47+
buf.build/gen/go/cometbft/cometbft/protocolbuffers/go v1.34.2-20240701160653-fedbb9acfd2f.2 // indirect
4848
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.34.2-20240130113600-88ef6483f90f.2 // indirect
4949
cosmossdk.io/collections v0.4.0 // indirect
5050
cosmossdk.io/core/testing v0.0.0-00010101000000-000000000000 // indirect

server/v2/cometbft/utils.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,20 @@ import (
88
"strings"
99
"time"
1010

11-
abciv1 "buf.build/gen/go/cometbft/cometbft/protocolbuffers/go/cometbft/abci/v1"
1211
abci "github.com/cometbft/cometbft/api/cometbft/abci/v1"
1312
cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1"
1413
gogoproto "github.com/cosmos/gogoproto/proto"
1514
gogoany "github.com/cosmos/gogoproto/types/any"
16-
"google.golang.org/protobuf/encoding/protojson"
17-
"google.golang.org/protobuf/types/known/anypb"
1815

19-
v1beta1 "cosmossdk.io/api/cosmos/base/abci/v1beta1"
2016
appmodulev2 "cosmossdk.io/core/appmodule/v2"
2117
"cosmossdk.io/core/comet"
2218
"cosmossdk.io/core/event"
2319
"cosmossdk.io/core/server"
2420
"cosmossdk.io/core/transaction"
2521
errorsmod "cosmossdk.io/errors"
2622
consensus "cosmossdk.io/x/consensus/types"
23+
24+
sdk "github.com/cosmos/cosmos-sdk/types"
2725
)
2826

2927
func queryResponse(res transaction.Msg, height int64) (*abci.QueryResponse, error) {
@@ -148,47 +146,47 @@ func intoABCIEvents(events []event.Event, indexSet map[string]struct{}) []abci.E
148146

149147
func intoABCISimulationResponse(txRes server.TxResult, indexSet map[string]struct{}) ([]byte, error) {
150148
indexAll := len(indexSet) == 0
151-
abciEvents := make([]*abciv1.Event, len(txRes.Events))
149+
abciEvents := make([]abci.Event, len(txRes.Events))
152150
for i, e := range txRes.Events {
153-
abciEvents[i] = &abciv1.Event{
151+
abciEvents[i] = abci.Event{
154152
Type: e.Type,
155-
Attributes: make([]*abciv1.EventAttribute, len(e.Attributes)),
153+
Attributes: make([]abci.EventAttribute, len(e.Attributes)),
156154
}
157155

158156
for j, attr := range e.Attributes {
159157
_, index := indexSet[fmt.Sprintf("%s.%s", e.Type, attr.Key)]
160-
abciEvents[i].Attributes[j] = &abciv1.EventAttribute{
158+
abciEvents[i].Attributes[j] = abci.EventAttribute{
161159
Key: attr.Key,
162160
Value: attr.Value,
163161
Index: index || indexAll,
164162
}
165163
}
166164
}
167165

168-
msgResponses := make([]*anypb.Any, len(txRes.Resp))
166+
msgResponses := make([]*gogoany.Any, len(txRes.Resp))
169167
for i, resp := range txRes.Resp {
170168
// use this hack to maintain the protov2 API here for now
171169
anyMsg, err := gogoany.NewAnyWithCacheWithValue(resp)
172170
if err != nil {
173171
return nil, err
174172
}
175-
msgResponses[i] = &anypb.Any{TypeUrl: anyMsg.TypeUrl, Value: anyMsg.Value}
173+
msgResponses[i] = anyMsg
176174
}
177175

178-
res := &v1beta1.SimulationResponse{
179-
GasInfo: &v1beta1.GasInfo{
176+
res := &sdk.SimulationResponse{
177+
GasInfo: sdk.GasInfo{
180178
GasWanted: txRes.GasWanted,
181179
GasUsed: txRes.GasUsed,
182180
},
183-
Result: &v1beta1.Result{
181+
Result: &sdk.Result{
184182
Data: []byte{},
185183
Log: txRes.Error.Error(),
186184
Events: abciEvents,
187185
MsgResponses: msgResponses,
188186
},
189187
}
190188

191-
return protojson.Marshal(res)
189+
return gogoproto.Marshal(res)
192190
}
193191

194192
// ToSDKEvidence takes comet evidence and returns sdk evidence

0 commit comments

Comments
 (0)