-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
219 lines (188 loc) · 6.13 KB
/
main.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
import (
"fmt"
"net/http"
"os"
"path"
"github.com/rakyll/statik/fs"
"github.com/spf13/cobra"
"github.com/spf13/viper"
amino "github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/libs/cli"
"my-cosmos/cosmos-sdk/client"
"my-cosmos/cosmos-sdk/client/keys"
"my-cosmos/cosmos-sdk/client/lcd"
"my-cosmos/cosmos-sdk/client/rpc"
"my-cosmos/cosmos-sdk/client/tx"
"my-cosmos/cosmos-sdk/cmd/gaia/app"
sdk "my-cosmos/cosmos-sdk/types"
"my-cosmos/cosmos-sdk/version"
at "my-cosmos/cosmos-sdk/x/auth"
auth "my-cosmos/cosmos-sdk/x/auth/client/rest"
bank "my-cosmos/cosmos-sdk/x/bank/client/rest"
dist "my-cosmos/cosmos-sdk/x/distribution/client/rest"
gv "my-cosmos/cosmos-sdk/x/gov"
gov "my-cosmos/cosmos-sdk/x/gov/client/rest"
sl "my-cosmos/cosmos-sdk/x/slashing"
slashing "my-cosmos/cosmos-sdk/x/slashing/client/rest"
st "my-cosmos/cosmos-sdk/x/staking"
staking "my-cosmos/cosmos-sdk/x/staking/client/rest"
authcmd "my-cosmos/cosmos-sdk/x/auth/client/cli"
bankcmd "my-cosmos/cosmos-sdk/x/bank/client/cli"
distcmd "my-cosmos/cosmos-sdk/x/distribution"
distClient "my-cosmos/cosmos-sdk/x/distribution/client"
govClient "my-cosmos/cosmos-sdk/x/gov/client"
slashingClient "my-cosmos/cosmos-sdk/x/slashing/client"
stakingClient "my-cosmos/cosmos-sdk/x/staking/client"
_ "my-cosmos/cosmos-sdk/client/lcd/statik"
)
func main() {
// Configure cobra to sort commands
cobra.EnableCommandSorting = false
// Instantiate the codec for the command line application
cdc := app.MakeCodec()
// Read in the configuration file for the sdk
config := sdk.GetConfig()
config.SetBech32PrefixForAccount(sdk.Bech32PrefixAccAddr, sdk.Bech32PrefixAccPub)
config.SetBech32PrefixForValidator(sdk.Bech32PrefixValAddr, sdk.Bech32PrefixValPub)
config.SetBech32PrefixForConsensusNode(sdk.Bech32PrefixConsAddr, sdk.Bech32PrefixConsPub)
config.Seal()
// TODO: setup keybase, viper object, etc. to be passed into
// the below functions and eliminate global vars, like we do
// with the cdc
// Module clients hold cli commnads (tx,query) and lcd routes
// TODO: Make the lcd command take a list of ModuleClient
mc := []sdk.ModuleClients{
govClient.NewModuleClient(gv.StoreKey, cdc),
distClient.NewModuleClient(distcmd.StoreKey, cdc),
stakingClient.NewModuleClient(st.StoreKey, cdc),
slashingClient.NewModuleClient(sl.StoreKey, cdc),
}
rootCmd := &cobra.Command{
Use: "gaiacli",
Short: "Command line interface for interacting with gaiad",
}
// Add --chain-id to persistent flags and mark it required
rootCmd.PersistentFlags().String(client.FlagChainID, "", "Chain ID of tendermint node")
rootCmd.PersistentPreRunE = func(_ *cobra.Command, _ []string) error {
return initConfig(rootCmd)
}
// Construct Root Command
rootCmd.AddCommand(
rpc.StatusCommand(),
client.ConfigCmd(app.DefaultCLIHome),
// 查询的命令行
queryCmd(cdc, mc),
// TODO 发交易的命令行 ()
txCmd(cdc, mc),
client.LineBreak,
lcd.ServeCommand(cdc, registerRoutes),
client.LineBreak,
keys.Commands(),
client.LineBreak,
version.VersionCmd,
client.NewCompletionCmd(rootCmd, true),
)
// Add flags and prefix all env exposed with GA
executor := cli.PrepareMainCmd(rootCmd, "GA", app.DefaultCLIHome)
/**
所有的 cmd 都在这里执行
*/
err := executor.Execute()
if err != nil {
fmt.Printf("Failed executing CLI command: %s, exiting...\n", err)
os.Exit(1)
}
}
func queryCmd(cdc *amino.Codec, mc []sdk.ModuleClients) *cobra.Command {
queryCmd := &cobra.Command{
Use: "query",
Aliases: []string{"q"},
Short: "Querying subcommands",
}
queryCmd.AddCommand(
rpc.ValidatorCommand(cdc),
rpc.BlockCommand(),
tx.SearchTxCmd(cdc),
tx.QueryTxCmd(cdc),
client.LineBreak,
authcmd.GetAccountCmd(at.StoreKey, cdc),
)
for _, m := range mc {
queryCmd.AddCommand(m.GetQueryCmd())
}
return queryCmd
}
/**
TODO 重要
交易命令行
*/
func txCmd(cdc *amino.Codec, mc []sdk.ModuleClients) *cobra.Command {
txCmd := &cobra.Command{
Use: "tx",
Short: "Transactions subcommands",
}
txCmd.AddCommand(
// TODO 普通交易
bankcmd.SendTxCmd(cdc),
client.LineBreak,
// 单签名
authcmd.GetSignCommand(cdc),
// 多签名
authcmd.GetMultiSignCommand(cdc),
// tx广播的命令行
tx.GetBroadcastCommand(cdc),
// 编码 cmd
tx.GetEncodeCommand(cdc),
client.LineBreak,
)
for _, m := range mc {
/**
TODO 设置 获取回来的 其他类型交易 (如: staking slashing governonce 等等)
*/
txCmd.AddCommand(m.GetTxCmd())
}
return txCmd
}
// registerRoutes registers the routes from the different modules for the LCD.
// NOTE: details on the routes added for each module are in the module documentation
// NOTE: If making updates here you also need to update the test helper in client/lcd/test_helper.go
func registerRoutes(rs *lcd.RestServer) {
registerSwaggerUI(rs)
rpc.RegisterRoutes(rs.CliCtx, rs.Mux)
tx.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc)
auth.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, at.StoreKey)
bank.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, rs.KeyBase)
dist.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, distcmd.StoreKey)
staking.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, rs.KeyBase)
slashing.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc, rs.KeyBase)
gov.RegisterRoutes(rs.CliCtx, rs.Mux, rs.Cdc)
}
func registerSwaggerUI(rs *lcd.RestServer) {
statikFS, err := fs.New()
if err != nil {
panic(err)
}
staticServer := http.FileServer(statikFS)
rs.Mux.PathPrefix("/swagger-ui/").Handler(http.StripPrefix("/swagger-ui/", staticServer))
}
func initConfig(cmd *cobra.Command) error {
home, err := cmd.PersistentFlags().GetString(cli.HomeFlag)
if err != nil {
return err
}
cfgFile := path.Join(home, "config", "config.toml")
if _, err := os.Stat(cfgFile); err == nil {
viper.SetConfigFile(cfgFile)
if err := viper.ReadInConfig(); err != nil {
return err
}
}
if err := viper.BindPFlag(client.FlagChainID, cmd.PersistentFlags().Lookup(client.FlagChainID)); err != nil {
return err
}
if err := viper.BindPFlag(cli.EncodingFlag, cmd.PersistentFlags().Lookup(cli.EncodingFlag)); err != nil {
return err
}
return viper.BindPFlag(cli.OutputFlag, cmd.PersistentFlags().Lookup(cli.OutputFlag))
}