@@ -38,32 +38,6 @@ def __str__(self):
3838 """Instance string representation."""
3939 return self .address
4040
41- def get_response (
42- self ,
43- endpoint : str ,
44- params : dict = None ,
45- field : str = None ,
46- ) -> dict :
47- """Invokes remote speculative JSON-RPC API and returns parsed response.
48-
49- :endpoint: Target endpoint to invoke.
50- :params: Endpoint parameters.
51- :field: Inner response field.
52- :returns: Parsed JSON-RPC response.
53-
54- """
55- request = jsonrpcclient .request (endpoint , params )
56- response_raw = requests .post (self .address , json = request )
57-
58- response_parsed = jsonrpcclient .parse (response_raw .json ())
59- if isinstance (response_parsed , jsonrpcclient .responses .Error ):
60- raise ProxyError (response_parsed )
61-
62- if field is None :
63- return response_parsed .result
64- else :
65- return response_parsed .result [field ]
66-
6741 def account_put_deploy (self , deploy : Deploy ) -> DeployID :
6842 """Dispatches a deploy to a node for processing.
6943
@@ -75,7 +49,7 @@ def account_put_deploy(self, deploy: Deploy) -> DeployID:
7549 "deploy" : serialisation .to_json (deploy ),
7650 }
7751
78- return self .get_response ( constants .RPC_ACCOUNT_PUT_DEPLOY , params , "deploy_hash" )
52+ return _get_response ( self .address , constants .RPC_ACCOUNT_PUT_DEPLOY , params , "deploy_hash" )
7953
8054 def chain_get_block (self , block_id : BlockID = None ) -> dict :
8155 """Returns on-chain block information.
@@ -86,7 +60,7 @@ def chain_get_block(self, block_id: BlockID = None) -> dict:
8660 """
8761 params : dict = param_utils .get_block_id (block_id , False )
8862
89- return self .get_response ( constants .RPC_CHAIN_GET_BLOCK , params , "block" )
63+ return _get_response ( self .address , constants .RPC_CHAIN_GET_BLOCK , params , "block" )
9064
9165 def chain_get_block_transfers (self , block_id : BlockID = None ) -> dict :
9266 """Returns on-chain block transfers information.
@@ -98,7 +72,7 @@ def chain_get_block_transfers(self, block_id: BlockID = None) -> dict:
9872 """
9973 params : dict = param_utils .get_block_id (block_id , False )
10074
101- return self .get_response ( constants .RPC_CHAIN_GET_BLOCK_TRANSFERS , params )
75+ return _get_response ( self .address , constants .RPC_CHAIN_GET_BLOCK_TRANSFERS , params )
10276
10377 def chain_get_era_info_by_switch_block (self , block_id : BlockID = None ) -> dict :
10478 """Returns consensus era information scoped by block id.
@@ -109,7 +83,7 @@ def chain_get_era_info_by_switch_block(self, block_id: BlockID = None) -> dict:
10983 """
11084 params : dict = param_utils .get_block_id (block_id , False )
11185
112- return self .get_response ( constants .RPC_CHAIN_GET_ERA_INFO_BY_SWITCH_BLOCK , params )
86+ return _get_response ( self .address , constants .RPC_CHAIN_GET_ERA_INFO_BY_SWITCH_BLOCK , params )
11387
11488 def chain_get_era_summary (self , block_id : BlockID = None ) -> dict :
11589 """Returns consensus era summary information.
@@ -120,7 +94,7 @@ def chain_get_era_summary(self, block_id: BlockID = None) -> dict:
12094 """
12195 params : dict = param_utils .get_block_id (block_id , False )
12296
123- return self .get_response ( constants .RPC_CHAIN_GET_ERA_SUMMARY , params , "era_summary" )
97+ return _get_response ( self .address , constants .RPC_CHAIN_GET_ERA_SUMMARY , params , "era_summary" )
12498
12599 def chain_get_state_root_hash (self , block_id : BlockID = None ) -> StateRootID :
126100 """Returns root hash of global state at a finalised block.
@@ -130,30 +104,26 @@ def chain_get_state_root_hash(self, block_id: BlockID = None) -> StateRootID:
130104
131105 """
132106 params : dict = param_utils .get_block_id (block_id , False )
107+ response : str = \
108+ _get_response (self .address , constants .RPC_CHAIN_GET_STATE_ROOT_HASH , params , "state_root_hash" )
133109
134- return bytes .fromhex (
135- self .get_response (
136- constants .RPC_CHAIN_GET_STATE_ROOT_HASH ,
137- params ,
138- "state_root_hash"
139- )
140- )
110+ return bytes .fromhex (response )
141111
142112 def discover (self ) -> dict :
143113 """Returns RPC schema.
144114
145115 :returns: Node JSON-RPC API schema.
146116
147117 """
148- return self .get_response ( constants .RPC_DISCOVER , field = "schema" )
118+ return _get_response ( self .address , constants .RPC_DISCOVER , field = "schema" )
149119
150120 def info_get_chainspec (self ) -> dict :
151121 """Returns canonical network state information.
152122
153123 :returns: Chain spec, genesis accounts and global state information.
154124
155125 """
156- return self .get_response ( constants .RPC_INFO_GET_CHAINSPEC , field = "chainspec_bytes" )
126+ return _get_response ( self .address , constants .RPC_INFO_GET_CHAINSPEC , field = "chainspec_bytes" )
157127
158128 def info_get_deploy (self , deploy_id : DeployID ) -> dict :
159129 """Returns on-chain deploy information.
@@ -164,31 +134,31 @@ def info_get_deploy(self, deploy_id: DeployID) -> dict:
164134 """
165135 params : dict = param_utils .get_deploy_id (deploy_id )
166136
167- return self .get_response ( constants .RPC_INFO_GET_DEPLOY , params , "deploy" )
137+ return _get_response ( self .address , constants .RPC_INFO_GET_DEPLOY , params , "deploy" )
168138
169139 def info_get_peers (self ) -> typing .List [dict ]:
170140 """Returns node peer information.
171141
172142 :returns: Node peer information.
173143
174144 """
175- return self .get_response ( constants .RPC_INFO_GET_PEERS , field = "peers" )
145+ return _get_response ( self .address , constants .RPC_INFO_GET_PEERS , field = "peers" )
176146
177147 def info_get_status (self ) -> dict :
178148 """Returns node status information.
179149
180150 :returns: Node status information.
181151
182152 """
183- return self .get_response ( constants .RPC_INFO_GET_STATUS )
153+ return _get_response ( self .address , constants .RPC_INFO_GET_STATUS )
184154
185155 def info_get_validator_changes (self ) -> typing .List [dict ]:
186156 """Returns validator change set.
187157
188158 :returns: Validator change set.
189159
190160 """
191- return self .get_response ( constants .RPC_INFO_GET_VALIDATOR_CHANGES , field = "changes" )
161+ return _get_response ( self .address , constants .RPC_INFO_GET_VALIDATOR_CHANGES , field = "changes" )
192162
193163 def query_balance (self , purse_id : PurseID , global_state_id : GlobalStateID = None ) -> int :
194164 """Returns account balance at a certain point in global state history.
@@ -210,7 +180,7 @@ def query_balance(self, purse_id: PurseID, global_state_id: GlobalStateID = None
210180 param_utils .get_purse_id (purse_id )
211181
212182 return int (
213- self .get_response ( constants .RPC_QUERY_BALANCE , params , "balance" )
183+ _get_response ( self .address , constants .RPC_QUERY_BALANCE , params , "balance" )
214184 )
215185
216186 def query_global_state (
@@ -235,7 +205,7 @@ def query_global_state(
235205
236206 params : dict = param_utils .get_params_for_query_global_state (key , path , state_id )
237207
238- return self .get_response ( constants .RPC_QUERY_GLOBAL_STATE , params )
208+ return _get_response ( self .address , constants .RPC_QUERY_GLOBAL_STATE , params )
239209
240210 def state_get_account_info (self , account_id : AccountID , block_id : BlockID = None ) -> dict :
241211 """Returns account information at a certain global state root hash.
@@ -249,7 +219,7 @@ def state_get_account_info(self, account_id: AccountID, block_id: BlockID = None
249219 param_utils .get_account_key (account_id ) | \
250220 param_utils .get_block_id (block_id )
251221
252- return self .get_response ( constants .RPC_STATE_GET_ACCOUNT_INFO , params , "account" )
222+ return _get_response ( self .address , constants .RPC_STATE_GET_ACCOUNT_INFO , params , "account" )
253223
254224 def state_get_auction_info (self , block_id : BlockID = None ) -> dict :
255225 """Returns current auction system contract information.
@@ -260,7 +230,7 @@ def state_get_auction_info(self, block_id: BlockID = None) -> dict:
260230 """
261231 params : dict = param_utils .get_block_id (block_id , False )
262232
263- return self .get_response ( constants .RPC_STATE_GET_AUCTION_INFO , params , "auction_state" )
233+ return _get_response ( self .address , constants .RPC_STATE_GET_AUCTION_INFO , params , "auction_state" )
264234
265235 def state_get_dictionary_item (self , identifier : DictionaryID , state_root_hash : StateRootID = None ) -> dict :
266236 """Returns on-chain data stored under a dictionary item.
@@ -275,7 +245,7 @@ def state_get_dictionary_item(self, identifier: DictionaryID, state_root_hash: S
275245
276246 params : dict = param_utils .get_params_for_state_get_dictionary_item (identifier , state_root_hash )
277247
278- return self .get_response ( constants .RPC_STATE_GET_DICTIONARY_ITEM , params )
248+ return _get_response ( self .address , constants .RPC_STATE_GET_DICTIONARY_ITEM , params )
279249
280250 def state_get_item (
281251 self ,
@@ -297,7 +267,7 @@ def state_get_item(
297267
298268 params : dict = param_utils .get_params_for_state_get_item (key , path , state_root_hash )
299269
300- return self .get_response ( constants .RPC_STATE_GET_ITEM , params , "stored_value" )
270+ return _get_response ( self .address , constants .RPC_STATE_GET_ITEM , params , "stored_value" )
301271
302272
303273class ProxyError (Exception ):
@@ -309,3 +279,31 @@ def __init__(self, msg):
309279
310280 """
311281 super (ProxyError , self ).__init__ (msg )
282+
283+
284+ def _get_response (
285+ address : str ,
286+ endpoint : str ,
287+ params : dict = None ,
288+ field : str = None ,
289+ ) -> dict :
290+ """Invokes JSON-RPC API & returns parsed response.
291+
292+ :address: Host address.
293+ :endpoint: Endpoint to invoke.
294+ :params: Endpoint Parameters.
295+ :field: Inner response field.
296+ :returns: Parsed JSON-RPC response.
297+
298+ """
299+ request = jsonrpcclient .request (endpoint , params )
300+ response_raw = requests .post (address , json = request )
301+
302+ response_parsed = jsonrpcclient .parse (response_raw .json ())
303+ if isinstance (response_parsed , jsonrpcclient .responses .Error ):
304+ raise ProxyError (response_parsed )
305+
306+ if field is None :
307+ return response_parsed .result
308+ else :
309+ return response_parsed .result [field ]
0 commit comments