@@ -48,7 +48,7 @@ async def _listen_forever(self, uri, chain_id):
4848 await websocket .send (
4949 json .dumps ({
5050 "method" : "eth_subscribe" ,
51- "jsonrpc" :"2.0" ,
51+ "jsonrpc" : "2.0" ,
5252 "id" : chain_id ,
5353 "params" : ["newHeads" ]
5454 }))
@@ -96,20 +96,29 @@ async def _fetch_total_difficulty(self, ws, block_height):
9696 await asyncio .wait_for (ws .send (
9797 json .dumps ({
9898 "method" : "eth_getBlockByNumber" ,
99- "jsonrpc" :"2.0" ,
99+ "jsonrpc" : "2.0" ,
100100 "id" : self .chain_id ,
101101 "params" : [hex_block_number , False ]
102102 })),
103103 timeout = self .timeout )
104104 response = await asyncio .wait_for (ws .recv (), timeout = self .timeout )
105105 response_json = json .loads (response )
106- if "error" in response_json :
106+
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+
116+ elif "error" in response_json :
107117 error = response_json ['error' ]['message' ]
108- elif response_json ['result' ] == None :
109- error = "Received response of type `None` for block totalDifficulty."
110118 else :
111- total_difficulty = int (
112- json .loads (response )['result' ]['totalDifficulty' ], 16 )
119+ # Set totalDifficulty to 0 if blockchain does not use it.
120+ total_difficulty = 0
121+
113122 return total_difficulty , error
114123
115124 async def _fetch_block_height (self , ws ):
@@ -119,7 +128,7 @@ async def _fetch_block_height(self, ws):
119128 await asyncio .wait_for (ws .send (
120129 json .dumps ({
121130 "method" : "eth_blockNumber" ,
122- "jsonrpc" :"2.0" ,
131+ "jsonrpc" : "2.0" ,
123132 "id" : self .chain_id
124133 })),
125134 timeout = self .timeout )
@@ -130,8 +139,11 @@ async def _fetch_block_height(self, ws):
130139 if "error" in result :
131140 error = result ['error' ]['message' ]
132141 else :
133- block_height = int (json .loads (response )['result' ], 16 )
134-
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"
135147 return block_height , error
136148
137149 async def _collect (self , uri , provider ):
0 commit comments