-
Notifications
You must be signed in to change notification settings - Fork 103
/
root.go
467 lines (451 loc) · 11.9 KB
/
root.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/**
* @file
* @copyright defined in go-seele/LICENSE
*/
package cmd
import (
"sort"
"github.com/urfave/cli"
)
// AddCommands adds all child commands to app
func AddCommands(app *cli.App, isFullNode bool) {
baseCommands := []cli.Command{
{
Name: "getbalance",
Usage: "get balance info",
Flags: rpcFlags(accountFlag, hashFlag, heightFlag),
Action: rpcAction("seele", "getBalance"),
},
{
Name: "sendtx",
Usage: "send transaction to node",
Flags: rpcFlags(fromFlag, toFlag, amountFlag, priceFlag, gasLimitFlag, payloadFlag, nonceFlag),
Action: rpcActionEx("seele", "addTx", makeTransaction, onTxAdded),
},
{
Name: "getnonce",
Usage: "get account nonce",
Flags: rpcFlags(accountFlag, hashFlag, heightFlag),
Action: rpcAction("seele", "getAccountNonce"),
},
{
Name: "getblockheight",
Usage: "get block height",
Flags: rpcFlags(),
Action: rpcAction("seele", "getBlockHeight"),
},
{
Name: "getblock",
Usage: "get block by height or hash",
Flags: rpcFlags(hashFlag, heightFlag, fulltxFlag),
Action: rpcAction("seele", "getBlock"),
},
{
Name: "gettxpoolcontent",
Usage: "get transaction pool contents",
Flags: rpcFlags(),
Action: rpcAction("txpool", "getTxPoolContent"),
},
{
Name: "gettxpoolcount",
Usage: "get transaction pool transaction count",
Flags: rpcFlags(),
Action: rpcAction("txpool", "getTxPoolTxCount"),
},
{
Name: "getblocktxcount",
Usage: "get block transaction count by block height or block hash",
Flags: rpcFlags(hashFlag, heightFlag),
Action: rpcAction("seele", "getBlockTransactionCount"),
},
{
Name: "gettxinblock",
Usage: "get transaction by block height or block hash with index of the transaction in the block",
Flags: rpcFlags(hashFlag, heightFlag, indexFlag),
Action: rpcAction("seele", "getTransactionByBlockIndex"),
},
{
Name: "gettxfromaccount",
Usage: "get transaction from one account at specific height or blockhash",
Flags: rpcFlags(accountFlag, hashFlag, heightFlag),
Action: rpcAction("seele", "getTransactionsFrom"),
},
{
Name: "gettxtoaccount",
Usage: "get transaction to one account at specific height or blockhash",
Flags: rpcFlags(accountFlag, hashFlag, heightFlag),
Action: rpcAction("seele", "getTransactionsTo"),
},
{
Name: "getaccounttx",
Usage: "get transaction of one account at specific height or blockhash",
Flags: rpcFlags(accountFlag, hashFlag, heightFlag),
Action: rpcAction("seele", "getAccountTransactions"),
},
{
Name: "getblocktx",
Usage: "get transaction by block height or block hash",
Flags: rpcFlags(hashFlag, heightFlag),
Action: rpcAction("seele", "getBlockTransactions"),
},
{
Name: "getblocktxbyheight",
Usage: "get the transitions at the specific height",
Flags: rpcFlags(heightFlag),
Action: rpcAction("seele", "getBlockTransactionsByHeight"),
},
{
Name: "getblocktxbyhash",
Usage: "get the transitions at the specific blockhash",
Flags: rpcFlags(hashFlag),
Action: rpcAction("seele", "getBlockTransactionsByHash"),
},
{
Name: "gettxbyhash",
Usage: "get transaction by transaction hash",
Flags: rpcFlags(hashFlag),
Action: rpcAction("txpool", "getTransactionByHash"),
},
{
Name: "getgasprice",
Usage: "get transaction gas price by transaction hash",
Flags: rpcFlags(hashFlag),
Action: rpcAction("txpool", "getGasPrice"),
},
{
Name: "getreceipt",
Usage: "get receipt by transaction hash",
Flags: rpcFlags(hashFlag, abiFileFlag),
Action: rpcAction("seele", "getReceiptByTxHash"),
},
{
Name: "getpendingtxs",
Usage: "get pending transactions",
Flags: rpcFlags(),
Action: rpcAction("txpool", "getPendingTransactions"),
},
{
Name: "getshardnum",
Usage: "get account shard number",
Flags: []cli.Flag{
accountFlag,
privateKeyFlag,
},
Action: GetAccountShardNumAction,
},
{
Name: "savekey",
Usage: "save private key to a keystore file",
Flags: []cli.Flag{
privateKeyFlag,
fileNameFlag,
},
Action: SaveKeyAction,
},
{
Name: "sign",
Usage: "generate a signed transaction and print it out",
Flags: []cli.Flag{
addressFlag,
privateKeyFlag,
toFlag,
amountFlag,
priceFlag,
gasLimitFlag,
payloadFlag,
nonceFlag,
},
Action: SignTxAction,
},
{
Name: "key",
Usage: "generate key with or without shard number",
Flags: []cli.Flag{
shardFlag,
},
Action: GenerateKeyAction,
},
{
Name: "payload",
Usage: "generate the payload according to the abi file and method name and args",
Flags: []cli.Flag{
abiFileFlag, methodNameFlag, argsFlag,
},
Action: GeneratePayloadAction,
},
{
Name: "deckeyfile",
Usage: "Decrypt key file",
Flags: []cli.Flag{
fileNameFlag,
},
Action: DecryptKeyFileAction,
},
}
htlcCommands := cli.Command{
Name: "htlc",
Usage: "Hash time lock contract commands",
Subcommands: []cli.Command{
{
Name: "create",
Usage: "create HTLC",
Flags: rpcFlags(fromFlag, toFlag, amountFlag, priceFlag, gasLimitFlag, nonceFlag, hashFlag, timeLockFlag),
Action: rpcActionSystemContract("htlc", "create", handleCallResult),
},
{
Name: "withdraw",
Usage: "withdraw from HTLC",
Flags: rpcFlags(fromFlag, priceFlag, gasLimitFlag, nonceFlag, hashFlag, preimageFlag),
Action: rpcActionSystemContract("htlc", "withdraw", handleCallResult),
},
{
Name: "refund",
Usage: "refund from HTLC",
Flags: rpcFlags(fromFlag, priceFlag, gasLimitFlag, nonceFlag, hashFlag),
Action: rpcActionSystemContract("htlc", "refund", handleCallResult),
},
{
Name: "get",
Usage: "get HTLC information",
Flags: rpcFlags(fromFlag, hashFlag),
Action: rpcActionSystemContract("htlc", "get", handleCallResult),
},
{
Name: "decode",
Usage: "decode HTLC contract information",
Flags: []cli.Flag{
payloadFlag,
},
Action: decodeHTLC,
},
{
Name: "key",
Usage: "generate preimage key and key hash",
Action: generateHTLCKey,
},
{
Name: "time",
Usage: "generate unix timestamp",
Flags: []cli.Flag{
timeLockFlag,
},
Action: generateHTLCTime,
},
},
}
domainCommands := cli.Command{
Name: "domain",
Usage: "system domain name commands",
Subcommands: []cli.Command{
{
Name: "register",
Usage: "register a domain name",
Flags: rpcFlags(fromFlag, priceFlag, gasLimitFlag, nameFlag, nonceFlag),
Action: rpcActionSystemContract("domain", "create", handleCallResult),
},
{
Name: "owner",
Usage: "get the domain name owner",
Flags: rpcFlags(fromFlag, priceFlag, gasLimitFlag, nameFlag, nonceFlag),
Action: rpcActionSystemContract("domain", "getOwner", handleCallResult),
},
},
}
subChainCommands := cli.Command{
Name: "subchain",
Usage: "system sub chain commands",
Subcommands: []cli.Command{
{
Name: "register",
Usage: "register a sub chain",
Flags: rpcFlags(fromFlag, priceFlag, gasLimitFlag, nonceFlag, subChainJSONFileFlag),
Action: rpcActionSystemContract("subchain", "register", handleCallResult),
},
{
Name: "query",
Usage: "query sub chain",
Flags: rpcFlags(fromFlag, priceFlag, gasLimitFlag, nonceFlag, nameFlag),
Action: rpcActionSystemContract("subchain", "query", handleCallResult),
},
cli.Command{
Name: "config",
Usage: "generate sub chain config file",
Flags: rpcFlags(coinbaseFlag, algorithmFlag, privateKeyFlag, nameFlag, outPutFlag, shardFlag, staticNodesFlag),
Action: createSubChainConfigFile,
},
{
Name: "template",
Usage: "generate template json file for sub chain register",
Flags: []cli.Flag{nameFlag, subChainJSONFileFlag},
Action: generateTemplate,
},
},
}
p2pCommands := cli.Command{
Name: "p2p",
Usage: "p2p commands",
Subcommands: []cli.Command{
{
Name: "peers",
Usage: "get p2p peer connections",
Flags: rpcFlags(),
Action: rpcAction("network", "getPeerCount"),
},
{
Name: "peersinfo",
Usage: "get p2p peers information",
Flags: rpcFlags(),
Action: rpcAction("network", "getPeersInfo"),
},
{
Name: "netversion",
Usage: "get current net version",
Flags: rpcFlags(),
Action: rpcAction("network", "getNetVersion"),
},
{
Name: "networkid",
Usage: "get current network id",
Flags: rpcFlags(),
Action: rpcAction("network", "getNetworkID"),
},
{
Name: "protocolversion",
Usage: "get seele protocol version",
Flags: rpcFlags(),
Action: rpcAction("network", "getProtocolVersion"),
},
{
Name: "islistening",
Usage: "return whether the node is listen or not",
Flags: rpcFlags(),
Action: rpcAction("network", "isListening"),
},
},
}
minerCommands := cli.Command{
Name: "miner",
Usage: "miner commands",
Subcommands: []cli.Command{
{
Name: "start",
Usage: "start miner",
Flags: rpcFlags(),
Action: rpcAction("miner", "start"),
},
{
Name: "stop",
Usage: "stop miner",
Flags: rpcFlags(),
Action: rpcAction("miner", "stop"),
},
{
Name: "setthreads",
Usage: "set miner thread number",
Flags: rpcFlags(threadsFlag),
Action: rpcAction("miner", "setThreads"),
},
{
Name: "setcoinbase",
Usage: "set miner coinbase",
Flags: rpcFlags(coinbaseFlag),
Action: rpcAction("miner", "setCoinbase"),
},
{
Name: "getcoinbase",
Usage: "get miner coinbase",
Flags: rpcFlags(),
Action: rpcAction("miner", "getCoinbase"),
},
{
Name: "status",
Usage: "get miner status",
Flags: rpcFlags(),
Action: rpcAction("miner", "status"),
},
{
Name: "hashrate",
Usage: "get hashrate",
Flags: rpcFlags(),
Action: rpcAction("miner", "getHashrate"),
},
{
Name: "threads",
Usage: "get thread number",
Flags: rpcFlags(),
Action: rpcAction("miner", "getThreads"),
},
{
Name: "getwork",
Usage: "get miner current mining task",
Flags: rpcFlags(),
Action: rpcAction("miner", "getWork"),
},
{
Name: "gettarget",
Usage: "get current SPOW mining difficulty ",
Flags: rpcFlags(),
// Action: rpcAction("miner", "getTaskDifficulty"),
Action: rpcAction("miner", "getTarget"),
},
},
}
// add full node support api
if isFullNode {
baseCommands = append(baseCommands, []cli.Command{
{
Name: "getinfo",
Usage: "get node info",
Flags: rpcFlags(),
Action: rpcAction("seele", "getInfo"),
},
{
Name: "getdebts",
Usage: "get pending debts",
Flags: rpcFlags(),
Action: rpcAction("txpool", "getPendingDebts"),
},
{
Name: "dumpheap",
Usage: "dump heap for profiling, return the file path",
Flags: rpcFlags(dumpFileFlag, gcBeforeDumpFlag),
Action: rpcAction("debug", "dumpHeap"),
},
{
Name: "call",
Usage: "call contract",
Flags: rpcFlags(toFlag, payloadFlag, heightFlag),
Action: rpcAction("seele", "call"),
},
{
Name: "getlogs",
Usage: "get logs",
Flags: rpcFlags(heightFlag, contractFlag, abiFileFlag, eventNameFlag),
Action: rpcAction("seele", "getLogs"),
},
{
Name: "getdebtbyhash",
Usage: "get debt by debt hash",
Flags: rpcFlags(hashFlag),
Action: rpcAction("txpool", "getDebtByHash"),
},
}...)
baseCommands = append(baseCommands,
htlcCommands,
domainCommands,
subChainCommands,
minerCommands)
}
baseCommands = append(baseCommands, p2pCommands)
app.Commands = baseCommands
// sort commands and flags by name
sortCommands(app.Commands)
}
func sortCommands(commands []cli.Command) {
sort.Sort(cli.CommandsByName(commands))
for _, command := range commands {
if len(command.Subcommands) > 0 {
sortCommands(command.Subcommands)
}
}
}