@@ -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.
223224func 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 ] == "" {
0 commit comments