Skip to content

Commit 16b2545

Browse files
mergify[bot]julienrbrttac0turtle
authored
feat: check latest block if no arg in q block and q block-results (backport #21084) (#21110)
Co-authored-by: Julien Robert <julien@rbrt.fr> Co-authored-by: marbar3778 <marbar3778@yahoo.com>
1 parent 2ee5230 commit 16b2545

2 files changed

Lines changed: 32 additions & 54 deletions

File tree

schema/module_schema_test.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ func TestModuleSchema_Validate(t *testing.T) {
112112
},
113113
{
114114
name: "same enum",
115-
<<<<<<< HEAD
116115
moduleSchema: ModuleSchema{
117116
ObjectTypes: []ObjectType{
118117
{
@@ -138,31 +137,6 @@ func TestModuleSchema_Validate(t *testing.T) {
138137
Name: "enum1",
139138
Values: []string{"a", "b"},
140139
},
141-
=======
142-
objectTypes: []ObjectType{
143-
{
144-
Name: "object1",
145-
KeyFields: []Field{
146-
{
147-
Name: "k",
148-
Kind: EnumKind,
149-
EnumType: EnumType{
150-
Name: "enum1",
151-
Values: []string{"a", "b"},
152-
},
153-
},
154-
},
155-
},
156-
{
157-
Name: "object2",
158-
KeyFields: []Field{
159-
{
160-
Name: "k",
161-
Kind: EnumKind,
162-
EnumType: EnumType{
163-
Name: "enum1",
164-
Values: []string{"a", "b"},
165-
>>>>>>> 5c90246b3 (feat(log): remove core dependency and update core interface to be dependency free (#21045))
166140
},
167141
},
168142
},

server/cmt_cmds.go

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -226,32 +226,45 @@ $ %s query block --%s=%s <hash>
226226
`,
227227
version.AppName, auth.FlagType, auth.TypeHeight,
228228
version.AppName, auth.FlagType, auth.TypeHash)),
229-
Args: cobra.ExactArgs(1),
229+
Args: cobra.MaximumNArgs(1),
230230
RunE: func(cmd *cobra.Command, args []string) error {
231231
clientCtx, err := client.GetClientQueryContext(cmd)
232232
if err != nil {
233233
return err
234234
}
235235

236236
typ, _ := cmd.Flags().GetString(auth.FlagType)
237+
if len(args) == 0 {
238+
// do not break default v0.50 behavior of block hash
239+
// if no args are provided, set the type to height
240+
typ = auth.TypeHeight
241+
}
237242

238243
switch typ {
239244
case auth.TypeHeight:
240-
241-
if args[0] == "" {
242-
return fmt.Errorf("argument should be a block height")
245+
var (
246+
err error
247+
height int64
248+
)
249+
heightStr := ""
250+
if len(args) > 0 {
251+
heightStr = args[0]
243252
}
244253

245-
// optional height
246-
var height *int64
247-
if len(args) > 0 {
248-
height, err = parseOptionalHeight(args[0])
254+
if heightStr == "" {
255+
cmd.Println("Falling back to latest block height:")
256+
height, err = rpc.GetChainHeight(clientCtx)
257+
if err != nil {
258+
return fmt.Errorf("failed to get chain height: %w", err)
259+
}
260+
} else {
261+
height, err = strconv.ParseInt(heightStr, 10, 64)
249262
if err != nil {
250-
return err
263+
return fmt.Errorf("failed to parse block height: %w", err)
251264
}
252265
}
253266

254-
output, err := rpc.GetBlockByHeight(clientCtx, height)
267+
output, err := rpc.GetBlockByHeight(clientCtx, &height)
255268
if err != nil {
256269
return err
257270
}
@@ -311,15 +324,21 @@ func QueryBlockResultsCmd() *cobra.Command {
311324
}
312325

313326
// optional height
314-
var height *int64
327+
var height int64
315328
if len(args) > 0 {
316-
height, err = parseOptionalHeight(args[0])
329+
height, err = strconv.ParseInt(args[0], 10, 64)
317330
if err != nil {
318331
return err
319332
}
333+
} else {
334+
cmd.Println("Falling back to latest block height:")
335+
height, err = rpc.GetChainHeight(clientCtx)
336+
if err != nil {
337+
return fmt.Errorf("failed to get chain height: %w", err)
338+
}
320339
}
321340

322-
blockRes, err := node.BlockResults(context.Background(), height)
341+
blockRes, err := node.BlockResults(context.Background(), &height)
323342
if err != nil {
324343
return err
325344
}
@@ -341,21 +360,6 @@ func QueryBlockResultsCmd() *cobra.Command {
341360
return cmd
342361
}
343362

344-
func parseOptionalHeight(heightStr string) (*int64, error) {
345-
h, err := strconv.Atoi(heightStr)
346-
if err != nil {
347-
return nil, err
348-
}
349-
350-
if h == 0 {
351-
return nil, nil
352-
}
353-
354-
tmp := int64(h)
355-
356-
return &tmp, nil
357-
}
358-
359363
func BootstrapStateCmd[T types.Application](appCreator types.AppCreator[T]) *cobra.Command {
360364
cmd := &cobra.Command{
361365
Use: "bootstrap-state",

0 commit comments

Comments
 (0)