@@ -274,14 +274,15 @@ async def process_events(self):
274274 has_transaction_fee_paid_event = True
275275
276276 # Process other events
277+ possible_success = False
277278 for event in await self .triggered_events :
279+ # TODO make this more readable
278280 # Check events
279281 if (
280282 event ["event" ]["module_id" ] == "System"
281283 and event ["event" ]["event_id" ] == "ExtrinsicSuccess"
282284 ):
283- self .__is_success = True
284- self .__error_message = None
285+ possible_success = True
285286
286287 if "dispatch_info" in event ["event" ]["attributes" ]:
287288 self .__weight = event ["event" ]["attributes" ]["dispatch_info" ][
@@ -294,13 +295,26 @@ async def process_events(self):
294295 elif (
295296 event ["event" ]["module_id" ] == "System"
296297 and event ["event" ]["event_id" ] == "ExtrinsicFailed"
298+ ) or (
299+ event ["event" ]["module_id" ] == "MevShield"
300+ and event ["event" ]["event_id" ] == "DecryptedRejected"
297301 ):
302+ possible_success = False
298303 self .__is_success = False
299304
300- dispatch_info = event ["event" ]["attributes" ]["dispatch_info" ]
301- dispatch_error = event ["event" ]["attributes" ]["dispatch_error" ]
302-
303- self .__weight = dispatch_info ["weight" ]
305+ if event ["event" ]["module_id" ] == "System" :
306+ dispatch_info = event ["event" ]["attributes" ]["dispatch_info" ]
307+ dispatch_error = event ["event" ]["attributes" ]["dispatch_error" ]
308+ self .__weight = dispatch_info ["weight" ]
309+ else :
310+ # MEV shield extrinsics
311+ dispatch_info = event ["event" ]["attributes" ]["reason" ][
312+ "post_info"
313+ ]
314+ dispatch_error = event ["event" ]["attributes" ]["reason" ]["error" ]
315+ self .__weight = event ["event" ]["attributes" ]["reason" ][
316+ "post_info"
317+ ]["actual_weight" ]
304318
305319 if "Module" in dispatch_error :
306320 if isinstance (dispatch_error ["Module" ], tuple ):
@@ -365,7 +379,13 @@ async def process_events(self):
365379 event ["event" ]["module_id" ] == "Balances"
366380 and event ["event" ]["event_id" ] == "Deposit"
367381 ):
368- self .__total_fee_amount += event .value ["attributes" ]["amount" ]
382+ self .__total_fee_amount += event ["event" ]["attributes" ][
383+ "amount"
384+ ]
385+ if possible_success is True and self .__error_message is None :
386+ # we delay the positive setting of the __is_success flag until we have finished iteration of the
387+ # events and have ensured nothing has set an error message
388+ self .__is_success = True
369389
370390 @property
371391 async def is_success (self ) -> bool :
@@ -833,7 +853,7 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
833853 pass
834854 if self .ws is not None :
835855 self ._exit_task = asyncio .create_task (self ._exit_with_timer ())
836- self ._attempts = 0
856+ self ._attempts = 0
837857
838858 async def _exit_with_timer (self ):
839859 """
@@ -891,12 +911,22 @@ async def _start_receiving(self, ws: ClientConnection) -> Exception:
891911 logger .debug ("Starting receiving task" )
892912 try :
893913 while True :
894- recd = await self ._wait_with_activity_timeout (
895- ws .recv (decode = False ), self .retry_timeout
896- )
897- await self ._reset_activity_timer ()
898- self ._attempts = 0
899- await self ._recv (recd )
914+ try :
915+ recd = await self ._wait_with_activity_timeout (
916+ ws .recv (decode = False ), self .retry_timeout
917+ )
918+ await self ._reset_activity_timer ()
919+ self ._attempts = 0
920+ await self ._recv (recd )
921+ except TimeoutError :
922+ if (
923+ self ._waiting_for_response <= 0
924+ or self ._sending .qsize () == 0
925+ or len (self ._inflight ) == 0
926+ or len (self ._received_subscriptions ) == 0
927+ ):
928+ # if there's nothing in a queue, we really have no reason to have this, so we continue to wait
929+ continue
900930 except websockets .exceptions .ConnectionClosedOK as e :
901931 logger .debug ("ConnectionClosedOK" )
902932 return e
@@ -939,7 +969,14 @@ async def _start_sending(self, ws) -> Exception:
939969 if not isinstance (
940970 e , (asyncio .TimeoutError , TimeoutError , ConnectionClosed )
941971 ):
942- logger .exception ("Websocket sending exception" , exc_info = e )
972+ logger .exception (
973+ f"Websocket sending exception; "
974+ f"sending: { self ._sending .qsize ()} ; "
975+ f"waiting_for_response: { self ._waiting_for_response } ; "
976+ f"inflight: { len (self ._inflight )} ; "
977+ f"subscriptions: { len (self ._received_subscriptions )} ;" ,
978+ exc_info = e ,
979+ )
943980 if to_send is not None :
944981 to_send_ = json .loads (to_send )
945982 self ._received [to_send_ ["id" ]].set_exception (e )
@@ -3987,6 +4024,32 @@ async def result_handler(message: dict, subscription_id) -> tuple[dict, bool]:
39874024 message_result = {
39884025 k .lower (): v for k , v in message ["params" ]["result" ].items ()
39894026 }
4027+ # check for any subscription indicators of failure
4028+ failure_message = None
4029+ if "usurped" in message_result :
4030+ failure_message = (
4031+ f"Subscription { subscription_id } usurped: { message_result } "
4032+ )
4033+ if "retracted" in message_result :
4034+ failure_message = (
4035+ f"Subscription { subscription_id } retracted: { message_result } "
4036+ )
4037+ if "finalitytimeout" in message_result :
4038+ failure_message = f"Subscription { subscription_id } finalityTimeout: { message_result } "
4039+ if "dropped" in message_result :
4040+ failure_message = (
4041+ f"Subscription { subscription_id } dropped: { message_result } "
4042+ )
4043+ if "invalid" in message_result :
4044+ failure_message = (
4045+ f"Subscription { subscription_id } invalid: { message_result } "
4046+ )
4047+
4048+ if failure_message is not None :
4049+ async with self .ws as ws :
4050+ await ws .unsubscribe (subscription_id )
4051+ logger .error (failure_message )
4052+ raise SubstrateRequestException (failure_message )
39904053
39914054 if "finalized" in message_result and wait_for_finalization :
39924055 logger .debug ("Extrinsic finalized. Unsubscribing." )
@@ -3998,7 +4061,7 @@ async def result_handler(message: dict, subscription_id) -> tuple[dict, bool]:
39984061 "finalized" : True ,
39994062 }, True
40004063 elif (
4001- any ( x in message_result for x in [ "inblock" , "inBlock" ])
4064+ "inblock" in message_result
40024065 and wait_for_inclusion
40034066 and not wait_for_finalization
40044067 ):
0 commit comments