Skip to content

Commit 4a056d0

Browse files
authored
Handle response of type None (#4)
1 parent e59dc84 commit 4a056d0

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,12 @@
22

33
## v1.0.0
44

5-
Initial release
5+
Initial release
6+
7+
## v1.0.1
8+
9+
Reworked logic for totalDifficulty to support chains that don't use it
10+
11+
## v1.0.2
12+
13+
Handling response of type `None` in JSON RPC response.

src/probe.py

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -103,21 +103,23 @@ async def _fetch_total_difficulty(self, ws, block_height):
103103
timeout=self.timeout)
104104
response = await asyncio.wait_for(ws.recv(), timeout=self.timeout)
105105
response_json = json.loads(response)
106+
if response_json['result'] == None:
107+
error = "RPC returned response of type `None`"
108+
else:
109+
try:
110+
result = response_json['result']
111+
except KeyError:
112+
logging.debug(response_json)
113+
error = "Key `result` was not found in response while requesting eth_getBlockByNumber"
106114

107-
try:
108-
result = response_json['result']
109-
except KeyError:
110-
logging.debug(response_json)
111-
error = "Key `result` was not found in response while requesting eth_getBlockByNumber"
112-
113-
if "totalDifficulty" in response_json['result']:
114-
total_difficulty = int(result['totalDifficulty'], 16)
115+
if "totalDifficulty" in response_json['result']:
116+
total_difficulty = int(result['totalDifficulty'], 16)
115117

116-
elif "error" in response_json:
117-
error = response_json['error']['message']
118-
else:
119-
# Set totalDifficulty to 0 if blockchain does not use it.
120-
total_difficulty = 0
118+
elif "error" in response_json:
119+
error = response_json['error']['message']
120+
else:
121+
# Set totalDifficulty to 0 if blockchain does not use it.
122+
total_difficulty = 0
121123

122124
return total_difficulty, error
123125

@@ -134,16 +136,19 @@ async def _fetch_block_height(self, ws):
134136
timeout=self.timeout)
135137
# TODO: Implement error handling for JSON-RPC 2.0 Specification https://www.jsonrpc.org/specification#error_object
136138
response = await asyncio.wait_for(ws.recv(), timeout=self.timeout)
137-
138-
result = json.loads(response)
139-
if "error" in result:
140-
error = result['error']['message']
139+
response_json = json.loads(response)
140+
if response_json['result'] == None:
141+
error = "RPC returned response of type `None`"
141142
else:
142-
try:
143-
block_height = int(result['result'], 16)
144-
except KeyError:
145-
logging.debug(response)
146-
error = "Key `result` was not found in response while requesting eth_blockNumber"
143+
result = json.loads(response)
144+
if "error" in result:
145+
error = result['error']['message']
146+
else:
147+
try:
148+
block_height = int(result['result'], 16)
149+
except KeyError:
150+
logging.debug(response)
151+
error = "Key `result` was not found in response while requesting eth_blockNumber"
147152
return block_height, error
148153

149154
async def _collect(self, uri, provider):

0 commit comments

Comments
 (0)