forked from FISCO-BCOS/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpc_console.py
More file actions
255 lines (237 loc) · 10.3 KB
/
rpc_console.py
File metadata and controls
255 lines (237 loc) · 10.3 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
bcosliteclientpy is a python client for FISCO BCOS2.0 (https://github.com/FISCO-BCOS/)
bcosliteclientpy is free software: you can redistribute it and/or modify it under the
terms of the MIT License as published by the Free Software Foundation. This project is
distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Thanks for
authors and contributors of eth-abi, eth-account, eth-hash,eth-keys, eth-typing, eth-utils,
rlp, eth-rlp , hexbytes ... and relative projects
@file: consensus_precompile.py
@function:
@author: yujiechen
@date: 2019-07
'''
from client.bcosclient import BcosClient
from client.bcoserror import ArgumentsError
from eth_utils import to_checksum_address
from client.common import common
class RPCConsole:
"""
console for RPC
"""
def __init__(self, cmd, params, contract_path):
self.func_prefix = "self.client."
self.cmd = cmd
self.params = params
self.contract_path = contract_path
RPCConsole.define_commands()
@staticmethod
def print_rpc_usage():
"""
print rpc usage information
"""
common.print_info("", "RPC commands")
for command in RPCConsole.functions["zero"]:
print(" ", "[{}]".format(command))
for command in RPCConsole.functions["one_int"]:
print(" ", "[{}] [number]".format(command))
for command in RPCConsole.functions["one_hash"]:
if command == "getCode":
continue
print(" ", "[{}] [hash] [contract_name]".format(command))
print(" ", ("[getCode] [contract_address]"))
print(" ", ("[getTransactionByBlockHashAndIndex] "
"[blockHash] [transactionIndex] [contract_name]"))
print(" ", ("[getTransactionByBlockNumberAndIndex] "
"[blockNumber] [transactionIndex] [contract_name]"))
print(" ", "[{}] [tx_count_limit/tx_gas_limit]".
format("getSystemConfigByKey"))
print(" ", "[{}] [blockNumber] True/False".
format("getBlockByNumber"))
print(" ", "[{}] [blockHash] True/False".
format("getBlockByHash"))
@staticmethod
def define_commands():
"""
define functions
"""
RPCConsole.functions = {}
# function with 0 param
RPCConsole.functions["zero"] = ["getNodeVersion", "getBlockNumber",
"getPbftView", "getSealerList",
"getObserverList",
"getConsensusStatus", "getSyncStatus",
"getPeers", "getGroupPeers", "getNodeIDList",
"getGroupList", "getPendingTxSize",
"getTotalTransactionCount",
"getPendingTransactions"]
# function with one param, and the param is int
RPCConsole.functions["one_int"] = ["getBlockHashByNumber"]
# function with one param, and the param is string
RPCConsole.functions["one_hash"] = ["getCode",
"getTransactionByHash",
"getTransactionReceipt"]
RPCConsole.functions["one_str"] = ["getSystemConfigByKey"]
RPCConsole.functions["two"] = ["getTransactionByBlockHashAndIndex",
"getTransactionByBlockNumberAndIndex"]
RPCConsole.functions["two_bool"] = ["getBlockByNumber", "getBlockByHash"]
RPCConsole.functions["parse_in"] = ["getTransactionByHash",
"getTransactionByBlockHashAndIndex",
"getTransactionByBlockNumberAndIndex"]
RPCConsole.functions["parse_out"] = ["getTransactionReceipt"]
RPCConsole.functions["human_friendly_output"] = ["getTotalTransactionCount",
"getPendingTxSize",
"getPbftView"]
@staticmethod
def get_all_cmd():
"""
get all cmd
"""
command_list = []
for key in RPCConsole.functions.keys():
if key == "parse_in" and key == "parse_output":
continue
for command in RPCConsole.functions[key]:
command_list.append(command)
return command_list
def parse_output(self, cmd, contract_name, result):
"""
parse the result
"""
if cmd in self.functions["parse_in"]:
decode_result = common.parse_input(result["input"],
contract_name,
self.contract_path)
common.print_info("input of transaction: {}", decode_result)
if cmd in self.functions["parse_out"]:
common.print_output_and_input(result["logs"], result["output"], result["input"],
contract_name, self.contract_path)
def get_func_name(self, cmd):
"""
get function name
"""
return self.func_prefix + cmd
def exec_cmd_with_two_bool_param(self, cmd, params):
"""
execute cmd with two params
"""
if cmd not in RPCConsole.functions["two_bool"]:
return
# check param
common.check_param_num(params, 1, False)
if len(params) > 2:
raise ArgumentsError("params must be no more than 2")
self.exec_command(cmd, params)
def exec_cmd_with_zero_param(self, cmd, params):
"""
execute cmd with zero param
"""
if cmd not in RPCConsole.functions["zero"]:
return
# check param
common.check_param_num(params, 0, True)
self.exec_command(cmd, params)
def exec_cmd_with_str_param(self, cmd, params):
"""
execute cmd with one star params
"""
if cmd not in RPCConsole.functions["one_str"]:
return
common.check_param_num(params, 1, True)
self.exec_command(cmd, params)
def exec_cmd_with_int_param(self, cmd, params):
"""
execute cmd with one param, and the param is a int
"""
if cmd not in RPCConsole.functions["one_int"]:
return
# check param
common.check_param_num(params, 1, True)
# check int range
number = common.check_int_range(params[0])
self.exec_command(cmd, [number])
def exec_cmd_with_hash_param(self, cmd, params):
"""
execute cmd with one hash param
"""
if cmd not in RPCConsole.functions["one_hash"]:
return
# check_param
common.check_param_num(params, 1, False)
# check contract address
if cmd == "getCode":
try:
if len(params) > 1:
raise ArgumentsError("{} must provide one param".format(cmd))
address = to_checksum_address(params[0])
self.exec_command(cmd, [address])
except Exception as e:
raise ArgumentsError("invalid address: {}, info: {}"
.format(params[0], e))
else:
if len(params) > 2:
raise ArgumentsError("{} must provide no more than one param".format(cmd))
# check hash
common.check_hash(params[0])
result = self.exec_command(cmd, [params[0]])
if len(params) < 2 or result is None:
return
self.parse_output(cmd, params[1], result)
def exec_cmd_with_two_param(self, cmd, params):
"""
execute command with two params:
"""
if cmd not in RPCConsole.functions["two"]:
return
# check param
common.check_param_num(params, 2, False)
if len(params) > 3:
raise ArgumentsError("{} : params must be no more than 3")
index = common.check_int_range(params[1])
result = None
# check param type
if cmd == "getTransactionByBlockHashAndIndex":
common.check_hash(params[0])
result = self.exec_command(cmd, [params[0], index])
if cmd == "getTransactionByBlockNumberAndIndex":
number = common.check_int_range(params[0])
result = self.exec_command(cmd, [number, index])
# decode input
if len(params) < 3 or result is None:
return
self.parse_output(cmd, params[2], result)
def convertHexToOct(self, cmd, json_str):
if cmd == "getTotalTransactionCount":
json_str["blockNumber"] = int(json_str["blockNumber"], 16)
json_str["failedTxSum"] = int(json_str["failedTxSum"], 16)
json_str["txSum"] = int(json_str["txSum"], 16)
elif cmd == "getPendingTxSize" or cmd == "getPbftView":
if isinstance(json_str, int):
return json_str
json_str = int(json_str, 16)
return json_str
def exec_command(self, cmd, params):
"""
exec_command
"""
self.client = BcosClient()
function_name = self.get_func_name(cmd)
# execute function
ret_json = eval(function_name)(*params)
common.print_info("INFO", self.cmd)
if cmd in RPCConsole.functions["human_friendly_output"]:
ret_json = self.convertHexToOct(cmd, ret_json)
common.print_result(ret_json)
return ret_json
def executeRpcCommand(self):
"""
execute RPC commands
"""
self.exec_cmd_with_zero_param(self.cmd, self.params)
self.exec_cmd_with_int_param(self.cmd, self.params)
self.exec_cmd_with_hash_param(self.cmd, self.params)
self.exec_cmd_with_two_param(self.cmd, self.params)
self.exec_cmd_with_str_param(self.cmd, self.params)
self.exec_cmd_with_two_bool_param(self.cmd, self.params)