o
    ¢ØÆhª1  ã                   @   s4  d dl Z d dlmZmZmZ d dlmZmZ d dlm	Z	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 d dlmZ zd dlZW n% eyi   d dlZd d	lmZ e d
¡Ze d¡ dd„ Z dd„ Z!Y n	w dd„ Z!dd„ Z d&dd„Z"dZ#dd„ Z$dZ%dd„ Z&dd„ Z'dd„ Z(d'd d!„Z)d"d#„ Z*d$d%„ Z+dS )(é    N)Úbig_endian_to_intÚint_to_big_endianÚis_bytes)ÚDecodingErrorÚEncodingError)Úbig_endian_intÚbinaryÚbooleanÚtext)ÚBinary)ÚListÚis_sedesÚis_sequence)ÚSerializable)Ú	ALL_BYTES)ÚAtomicz	rlp.codecz}Consider installing rusty-rlp to improve pyrlp performance with a rust basedbackend. Not currently functional for Python 3.11c                 C   s®   t | tƒrt| ƒdkr| d dk r| S | }d}n&t | tƒs1t | tjjƒr1d dd„ | D ƒ¡}d}ndt| ƒj	› �}t
|| ƒ‚ztt|ƒ|ƒ}W || S  tyV   t
d	| ƒ‚w )
z4RLP encode (a nested sequence of) :class:`Atomic`\s.é   r   é€   ó    c                 s   s   � | ]}t |ƒV  qd S ©N)Ú
encode_raw)Ú.0Úx© r   úV/var/www/html/HyperOracle/crypto_trader/venv/lib/python3.10/site-packages/rlp/codec.pyÚ	<genexpr>9   s   € zencode_raw.<locals>.<genexpr>éÀ   zCannot encode object of type zItem too big to encode)Ú
isinstancer   ÚlenÚstrÚcollectionsÚabcÚSequenceÚjoinÚtypeÚ__name__r   Úlength_prefixÚ
ValueError)ÚitemÚpayloadÚprefix_offsetÚmsgÚprefixr   r   r   r   1   s    

ý
ÿr   c                 C   sd   z
t | dƒ\}}}W n ty   td| ƒ‚w |t| ƒkr.|r.dt| ƒ| › d�}t|| ƒ‚||fS )Nr   zRLP string too shortzRLP string ends with z superfluous bytes)Úconsume_itemÚ
IndexErrorr   r   )r(   ÚstrictÚ_ÚresultÚper_item_rlpÚendr+   r   r   r   Ú
decode_rawF   s   
ÿ
r4   c              
   C   s:   zt  | ||¡W S  tt jfy } zt|| ƒ‚d }~ww r   )Ú	rusty_rlpr4   Ú	TypeErrorr   )r(   r/   Úpreserve_per_item_rlpÚer   r   r   r4   S   s   
€ÿc              
   C   sD   zt | tƒr
t| ƒ} t | ¡W S  tjy! } zt|| ƒ‚d }~ww r   )r   Ú	bytearrayÚbytesr5   r   r   )Úobjr8   r   r   r   r   Y   s   

€ÿTc                 C   sp   t | tƒr| j}|du r|r|S |o|du }nd}|r!| | ¡}n|r+t| ƒ | ¡}n| }t|ƒ}|r6|| _|S )a  
    Encode a Python object in RLP format.

    By default, the object is serialized in a suitable way first (using
    :func:`rlp.infer_sedes`) and then encoded. Serialization can be explicitly
    suppressed by setting `infer_serializer` to ``False`` and not passing an
    alternative as `sedes`.

    If `obj` has an attribute :attr:`_cached_rlp` (as, notably,
    :class:`rlp.Serializable`) and its value is not `None`, this value is
    returned bypassing serialization and encoding, unless `sedes` is given (as
    the cache is assumed to refer to the standard serialization which can be
    replaced by specifying `sedes`).

    If `obj` is a :class:`rlp.Serializable` and `cache` is true, the result of
    the encoding will be stored in :attr:`_cached_rlp` if it is empty.

    :param sedes: an object implementing a function ``serialize(obj)`` which will be
                  used to serialize ``obj`` before encoding, or ``None`` to use the
                  infered one (if any)
    :param infer_serializer: if ``True`` an appropriate serializer will be selected
                             using :func:`rlp.infer_sedes` to serialize `obj` before
                             encoding
    :param cache: cache the return value in `obj._cached_rlp` if possible
                  (default `True`)
    :returns: the RLP encoded item
    :raises: :exc:`rlp.EncodingError` in the rather unlikely case that the item is too
             big to encode (will not happen)
    :raises: :exc:`rlp.SerializationError` if the serialization fails
    NF)r   r   Ú_cached_rlpÚ	serializeÚinfer_sedesr   )r;   ÚsedesÚinfer_serializerÚcacheÚ
cached_rlpÚreally_cacher(   r1   r   r   r   Úencodeb   s   
rD   l            c                 C   sH   | dk r
t ||   S | tk r t| ƒ}t |d d t|ƒ  | S tdƒ‚)zã
    Construct the prefix to lists or strings denoting their length.

    :param length: the length of the item in bytes
    :param offset: ``0x80`` when encoding raw bytes, ``0xc0`` when encoding a
                   list
    é8   r   zLength greater than 256**8)r   ÚLONG_LENGTHr   r   r'   )ÚlengthÚoffsetÚlength_stringr   r   r   r&   š   s   r&   é¸   c                 C   s¢  | | }|dk rdt d|fS |tk r5|d dkr%| |d  dk r%td| ƒ‚| ||d … t |d |d fS |dk ry|d }| |d |d … dkrNtd	| ƒ‚| |d |d | … }t|ƒ}|d
k rgtd| ƒ‚| ||d … | t ||d | fS |dk r�| ||d … t|d |d fS |d d
 d }| |d |d … dkr¦td	| ƒ‚| |d |d | … }t|ƒ}|d
k r¿td| ƒ‚| ||d … | t||d | fS )aÕ  
    Read a length prefix from an RLP string.

    :param rlp: the rlp byte string to read from
    :param start: the position at which to start reading
    :returns: a tuple ``(prefix, type, length, end)``, where ``type`` is either ``str``
              or ``list`` depending on the type of the following payload,
              ``length`` is the length of the payload in bytes, and ``end`` is
              the position of the first payload byte in the rlp string
    r   r   r   z9Encoded as short string although single byte was possibler   é·   é   ó    zLength starts with zero bytesrE   z(Long string prefix used for short stringéø   z$Long list prefix used for short list)r:   ÚSHORT_STRINGr   r   Úlist)ÚrlpÚstartÚb0ÚllÚ
len_prefixÚlr   r   r   Úconsume_length_prefix®   s8   ÿ 

$ 

$rW   c                 C   sÔ   |t u r| ||| … }||| g|| fS |tu rfg }g }|}|}	|	| }
|	|
k rRt| |	ƒ\}}}}t| ||||ƒ\}}}	| |¡ ||d 7 }| |¡ |	|
k s*| d|¡ |	|
kratd| ƒ‚|||	fS tdƒ‚)a  
    Read the payload of an item from an RLP string.

    :param rlp: the rlp string to read from
    :param type_: the type of the payload (``bytes`` or ``list``)
    :param start: the position at which to start reading
    :param length: the length of the payload in bytes
    :returns: a tuple ``(item, per_item_rlp, end)``, where ``item`` is
              the read item, per_item_rlp is a list containing the RLP
              encoding of each item and ``end`` is the position of the
              first unprocessed byte
    r   z/List length prefix announced a too small lengthz!Type must be either list or bytes)r:   rP   rW   Úconsume_payloadÚappendÚinsertr   r6   )rQ   r,   rR   Útype_rG   r(   Úitemsr2   Úlist_rlpÚnext_item_startr3   ÚpÚtrV   ÚsÚitem_rlpr   r   r   rX   Ø   s.   

ø	ÿ
rX   c                 C   s"   t | |ƒ\}}}}t| ||||ƒS )aŠ  
    Read an item from an RLP string.

    :param rlp: the rlp string to read from
    :param start: the position at which to start reading
    :returns: a tuple ``(item, per_item_rlp, end)``, where ``item`` is
              the read item, per_item_rlp is a list containing the RLP
              encoding of each item and ``end`` is the position of the
              first unprocessed byte
    )rW   rX   )rQ   rR   r_   r`   rV   ra   r   r   r   r-     s   r-   Fc                 K   sz   t | ƒstdt| ƒj | ƒ‚t| ||ƒ\}}t|ƒdkr| g}|r;|j|fi |¤Ž}t|ƒs3t|dƒr9t	|||ƒ |S |S )aš  
    Decode an RLP encoded object.

    If the deserialized result `obj` has an attribute :attr:`_cached_rlp` (e.g. if
    `sedes` is a subclass of :class:`rlp.Serializable`) it will be set to `rlp`, which
    will improve performance on subsequent :func:`rlp.encode` calls. Bear in mind
    however that `obj` needs to make sure that this value is updated whenever one of its
    fields changes or prevent such changes entirely (:class:`rlp.sedes.Serializable`
    does the latter).

    :param sedes: an object implementing a function ``deserialize(code)`` which will be
                  applied after decoding, or ``None`` if no deserialization should be
                  performed
    :param `**kwargs`: additional keyword arguments that will be passed to the
                       deserializer
    :param strict: if false inputs that are longer than necessary don't cause an
                   exception
    :returns: the decoded and maybe deserialized Python object
    :raises: :exc:`rlp.DecodingError` if the input string does not end after the root
             item and `strict` is true
    :raises: :exc:`rlp.DeserializationError` if the deserialization fails
    z&Can only decode RLP bytes, got type %sr   r<   )
r   r   r$   r%   r4   r   Údeserializer   ÚhasattrÚ_apply_rlp_cache)rQ   r?   r/   Úrecursive_cacheÚkwargsr(   r2   r;   r   r   r   Údecode  s   ÿrh   c                 C   s~   |  d¡}t| tttttfƒrd S t| dƒr|| _|sd S | D ]}t|tttttfƒr1|  d¡ q|  d¡}t	|||ƒ qd S )Nr   r<   )
Úpopr   ÚintÚboolr   r:   r9   rd   r<   re   )r;   Ú	split_rlpÚ	recursiverb   ÚsubÚsub_rlpr   r   r   re   :  s   


ûre   c                 C   s˜   t | jƒr| jS t| tƒst| tƒr| dkrtS t | ¡rtS t| t	ƒs2t| t
jjƒr2ttt| ƒƒS t| tƒr9tS t| t	ƒr@tS dt| ƒj› �}t|ƒ‚)a�  
    Try to find a sedes objects suitable for a given Python object.

    The sedes objects considered are `obj`'s class, `big_endian_int` and
    `binary`. If `obj` is a sequence, a :class:`rlp.sedes.List` will be
    constructed recursively.

    :param obj: the python object for which to find a sedes object
    :raises: :exc:`TypeError` if no appropriate sedes could be found
    r   z!Did not find sedes handling type )r   Ú	__class__r   rk   rj   r   ÚBinaryClassÚis_valid_typer   r   r    r!   r"   r   Úmapr>   r	   r
   r$   r%   r6   )r;   r+   r   r   r   r>   J  s   



r>   )NTT)NTF),r    Ú	eth_utilsr   r   r   Úrlp.exceptionsr   r   Ú	rlp.sedesr   r   r	   r
   Úrlp.sedes.binaryr   rq   Úrlp.sedes.listsr   r   r   Úrlp.sedes.serializabler   Ú	rlp.utilsr   r5   ÚImportErrorÚloggingÚ
rlp.atomicr   Ú	getLoggerÚloggerÚdebugr   r4   rD   rF   r&   rO   rW   rX   r-   rh   re   r>   r   r   r   r   Ú<module>   s@    
ÿÞ/
	5*)
*