-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
query.go
113 lines (94 loc) · 2.99 KB
/
query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package cli
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/x/upgrade/types"
)
// GetQueryCmd returns the parent command for all x/upgrade CLi query commands.
func GetQueryCmd() *cobra.Command {
cmd := &cobra.Command{
Use: types.ModuleName,
Short: "Querying commands for the upgrade module",
}
cmd.AddCommand(
GetCurrentPlanCmd(),
GetAppliedPlanCmd(),
)
return cmd
}
// GetCurrentPlanCmd returns the query upgrade plan command.
func GetCurrentPlanCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "plan",
Short: "get upgrade plan (if one exists)",
Long: "Gets the currently scheduled upgrade plan, if one exists",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
params := types.QueryCurrentPlanRequest{}
res, err := queryClient.CurrentPlan(context.Background(), ¶ms)
if err != nil {
return err
}
if res.Plan == nil {
return fmt.Errorf("no upgrade scheduled")
}
return clientCtx.PrintProto(res.GetPlan())
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetAppliedPlanCmd returns information about the block at which a completed
// upgrade was applied.
func GetAppliedPlanCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "applied [upgrade-name]",
Short: "block header for height at which a completed upgrade was applied",
Long: "If upgrade-name was previously executed on the chain, this returns the header for the block at which it was applied.\n" +
"This helps a client determine which binary was valid over a given range of blocks, as well as more context to understand past migrations.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
params := types.QueryAppliedPlanRequest{Name: args[0]}
res, err := queryClient.AppliedPlan(context.Background(), ¶ms)
if err != nil {
return err
}
if res.Height == 0 {
return fmt.Errorf("no upgrade found")
}
// we got the height, now let's return the headers
node, err := clientCtx.GetNode()
if err != nil {
return err
}
headers, err := node.BlockchainInfo(context.Background(), res.Height, res.Height)
if err != nil {
return err
}
if len(headers.BlockMetas) == 0 {
return fmt.Errorf("no headers returned for height %d", res.Height)
}
// always output json as Header is unreable in toml ([]byte is a long list of numbers)
bz, err := clientCtx.LegacyAmino.MarshalJSONIndent(headers.BlockMetas[0], "", " ")
if err != nil {
return err
}
return clientCtx.PrintString(fmt.Sprintf("%s\n", string(bz)))
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}