
    (i=7                     z   d dl Z d dlZd dlmZmZmZmZmZmZm	Z	m
Z
mZmZ d dlmZ d dlmZmZ d dlmZmZmZ d dlmZ d dlmZmZmZ d dlmZ erd d	lmZ  ed
      Z ejB                  dk\  r G d de jD                  e    ee          Z#n G d de jD                  ee          Z# G d de#e          Z$ G d d      Z%y)    N)
TYPE_CHECKINGAnyCallableDictGenericListOptionalTupleTypeVarUnion)BATCH_REQUEST_ID)RequestInformationgenerate_cache_key)SubscriptionProcessingFinishedTaskNotRunningWeb3ValueError)SubscriptionContainer)RPCEndpointRPCIdRPCResponse)SimpleCache)PersistentConnectionProviderT)   	   c                       e Zd Zy_TaskReliantQueueN__name__
__module____qualname__     \/var/www/br/venv/lib/python3.12/site-packages/web3/providers/persistent/request_processor.pyr   r   3       r$   r   c                       e Zd Zyr   r   r#   r$   r%   r   r   8   r&   r$   c                   (     e Zd ZdZdef fdZ xZS )TaskReliantQueuezT
    A queue that relies on a task to be running to process items in the queue.
    returnc                 d   K   t         |           d {   }t        |t              r||S 7 w)N)superget
isinstance	Exception)selfitem	__class__s     r%   r-   zTaskReliantQueue.getA   s0     W[]"dI& J #s   0.0)r    r!   r"   __doc__r   r-   __classcell__)r2   s   @r%   r)   r)   <   s    1  r$   r)   c                      e Zd ZU dZeed<   dZee   ed<   	 	 dddde	de	d	dfd
Z
ed	eeef   fd       Zdee   dededeeeeedef   f   edef   f   edef   edef   f   d	ee   f
dZded	ee   fdZded	efdZdededef   d	dfdZdeee   ef   d	efdZ	 d deded	dfdZ	 d!deded	efdZd"dZd"dZ y)#RequestProcessorF)_subscription_queue_synced_with_ws_streamN_subscription_containerproviderr    subscription_response_queue_sizerequest_information_cache_sizer*   c                     || _         t        |      | _        t        d      | _        t	        |      | _        t	        |      | _        y )N  maxsize)	_providerr   _request_information_cache_request_response_cacher)   _subscription_response_queue_handler_subscription_queue)r0   r9   r:   r;   s       r%   __init__zRequestProcessor.__init__P   sQ     "7B*8
' 5@4D$ %EF 	)
 %EF 	(r$   c                     | j                   j                         D ci c]-  \  }}|j                  dk(  r|j                  d|j                  i/ c}}S c c}}w )Neth_subscribeparams)rA   itemsmethodsubscription_idrH   )r0   keyvalues      r%   active_subscriptionsz%RequestProcessor.active_subscriptionsb   sW     #==CCE
U||. !!Hell#;;
 	
 
s   2A
request_idrJ   rH   response_formatters.c                    t        ||f      }|| j                  j                  j                  v ru| j                  j                  j                  |   }|j	                  d      }t        |      }|| j
                  v r(| j                  j                  j                  d||       y |!| j                  j                  st        d      t        |      }t        |||      }	| j                  j                  j                  d|||	j                         | j
                  j                  ||	       | j
                  j                         r%| j                  j                  j                  d       |S )NidziThis is a cached request, not caching request info because it is not unique:
    method=%s,
    params=%sz7Request id must be provided when not batching requests.zNCaching request info:
    request_id=%s,
    cache_key=%s,
    request_info=%szRequest information cache is full. This may result in unexpected behavior. Consider increasing the ``request_information_cache_size`` on the provider.)r   r@   _request_cache_datar-   rA   loggerdebug_is_batchingr   r   __dict__cacheis_fullwarning)
r0   rO   rJ   rH   rP   cached_requests_keycached_responsecached_response_id	cache_keyrequest_infos
             r%   cache_request_informationz*RequestProcessor.cache_request_informationl   s_    1&&1AB$.."?"?"E"EE"nn;;AABUVO!0!4!4T!:*+=>ID;;;%%++A	 >>..$M  'z2	)

 	##5!!	
 	''--	
 **224NN!!))#
 r$   r_   c                     | j                   j                  |      }|1| j                  j                  j	                  d||j
                         |S )NzERequest info popped from cache:
    cache_key=%s,
    request_info=%s)rA   popr@   rU   rV   rX   )r0   r_   r`   s      r%   pop_cached_request_informationz/RequestProcessor.pop_cached_request_information   sQ     66::9E#NN!!''9%%	 r$   responsec                 0   d|v rW|d   dk(  rOd|vrt        d      d|d   vrt        d      t        |d   d         }| j                  j                  |      }|S t        |d         }|| j                  j
                  j                  j                         v r| j                  j                  |      }n| j                  |      }|M|j                  dk(  r>|j                  d	      d
u r+|j                  d   }t        |      }| j                  |       |S )NrJ   eth_subscriptionrH   z,Subscription response must have params fieldsubscriptionz9Subscription response params must have subscription fieldrR   eth_unsubscriberesultTr   )r   r   rA   get_cache_entryr@   rS   rT   valuesrd   rJ   r-   rH   )r0   re   r_   r`   rK   subscribe_cache_keys         r%   $get_request_information_for_responsez5RequestProcessor.get_request_information_for_response   s?    xHX$6:L$Lx'$%STTXh%77$O 
 +8H+=n+MNI
 //??	J	 D 5 +8D>:I4>>88>>EEGG
 33CCIN	  77	B  ( ''+<<LL*d2 #/"5"5a"8&8&I#334GHr$   middleware_response_processorc                 P   |j                  dd       }|lt        |      }| j                  j                  |      }||j                  j                  |       y | j                  j                  j                  d||       y | j                  j                  j                  d|       y )NrR   ziNo cached request info for response id `%s`. Cannot append middleware response processor for response: %szZNo response `id` in response. Cannot append middleware response processor for response: %s)	r-   r   rA   rk   middleware_response_processorsappendr@   rU   rV   )r0   re   ro   response_idr_   cached_request_info_for_ids         r%   $append_middleware_response_processorz5RequestProcessor.append_middleware_response_processor   s    
 ll4."*;7I//??	J ' *5*IIPP1 %%++L	 NN!!''-r$   raw_responsec                     t        |t              xs= t        |t              xr+ |j                  d      d u xr | j                  j
                  S )NrR   )r.   listdictr-   r@   rW   )r0   rv   s     r%   _is_batch_responsez#RequestProcessor._is_batch_response  sL     ,- 
|T* ,  &$.,++	
r$   rh   c                 "  K   |r=| j                   j                         ru| j                  j                  j	                  d       | j                  j
                  j                          | j                  j
                  j                          d {    | j                  j                  j	                  d|       |j                  di       j                  d      }| j                  }|r5|j                  |      r$| j                  j                  |       d {    y | j                   j                  |       d {    y | j                  |      rSt        t              }| j                  j                  j	                  d||       | j                   j#                  ||       y |j                  d      }t        |      }| j                  j                  j	                  d|||       | j                   j#                  ||       y 7 x7 7 ϭw)NzTSubscription queue is full. Waiting for provider to consume messages before caching.z.Caching subscription response:
    response=%srH   rh   z9Caching batch response:
    cache_key=%s,
    response=%srR   zGCaching response:
    response_id=%s,
    cache_key=%s,
    response=%s)rC   fullr@   rU   rV   _listen_eventclearwaitr-   r8   get_handler_subscription_by_idrD   putrz   r   r   rB   rY   )r0   rv   rh   rK   sub_containerr_   rs   s          r%   cache_raw_responsez#RequestProcessor.cache_raw_response  s     00557%%++/ ,,224nn2277999NN!!''A< +..x<@@PO 88M!M!M" 66::<HHH 77;;LIII$$\2 ++;<INN!!''M
 ((..y,G&**40K*;7INN!!''5 ((..y,GI : I Js8   BHHBHH#H=H>CHHHc                   K   |r#| j                   j                         }| j                   j                          d {   }| j                  j                  j                         s$| j                  j                  j                          |dk(  r9| j                  sfd| _        | j                  j                  j                  d       n9| j                  rd| _        | j                  j                  j                  d|       | j                  j                  j                  d|       |S |st        d      | j                  j                  |      }|'| j                  j                  j                  d||       |S 7 ?w)	Nr   TzASubscription response queue synced with websocket message stream.FzESubscription response queue has %s subscriptions. Processing as FIFO.zLSubscription response popped from queue to be processed:
    raw_response=%sz@Must provide cache key when popping a non-subscription response.zXCached response popped from cache to be processed:
    cache_key=%s,
    raw_response=%s)rC   qsizer-   r@   r}   is_setsetr7   rU   inforV   r   rB   rc   )r0   r_   rh   r   rv   s        r%   pop_raw_responsez!RequestProcessor.pop_raw_response<  sY     55;;=E!%!B!B!F!F!HHL>>//668,,002zEEEIDBNN)).."
 AAEJDB%%*** NN!!''&(  $V   77;;IFL'%%++= 	 S Is   ;E?E<D?E?c                 N    t        | j                  j                        | _        y )Nr>   )r)   rD   r?   r0   s    r%   !_reset_handler_subscription_queuez2RequestProcessor._reset_handler_subscription_queuen  s    +;44<<,
(r$   c                     | j                   j                          | j                  j                          t        | j                  j
                        | _        | j                          y)z#Clear the request processor caches.r>   N)rA   r~   rB   r)   rC   r?   r   r   s    r%   clear_cacheszRequestProcessor.clear_cachess  sP    ''--/$$**,,<55==-
) 	..0r$   )r=   r=   )F)NF)r*   N)!r    r!   r"   r7   bool__annotations__r8   r	   r   intrE   propertyr   strr   rN   r   r   r
   r   r   ra   r   rd   r   rn   ru   r   rz   r   r   r   r   r#   r$   r%   r6   r6   J   s   6;-t; @DX&;<C
 14.1	G0G +.G ),	G
 
G$ 
d38n 
 
6UO6 6 	6
 #$sHS#X../#s(1CCDS#XS#X 
6 
#6p	$	%00 
0d (0S'9 
	@
!${"3["@A
	
 7<.H.H/3.H	.Hb ;@..37.	.d

1r$   r6   )&asynciosystypingr   r   r   r   r   r   r	   r
   r   r   web3._utils.batchingr   web3._utils.cachingr   r   web3.exceptionsr   r   r   .web3.providers.persistent.subscription_managerr   
web3.typesr   r   r   
web3.utilsr   web3.providers.persistentr   r   version_infoQueuer   r)   r6   r#   r$   r%   <module>r      s     
   
 
  CL vGMM!,gaj 
GMM71: (+ p1 p1r$   