Python rlp.Serializable() Examples

The following are 3 code examples of rlp.Serializable(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module rlp , or try the search function .
Example #1
Source File: make_VMTests.py    From manticore with GNU Affero General Public License v3.0 5 votes vote down vote up
def gen_header(testcases):
    header = f'''"""DO NOT MODIFY: Tests generated from `tests/` with {sys.argv[0]}"""
import unittest
from binascii import unhexlify
from manticore import ManticoreEVM, Plugin
from manticore.utils import config
'''

    if any("logs" in testcase for testcase in testcases.values()):
        body += """
import sha3
import rlp
from rlp.sedes import (
    CountableList,
    BigEndianInt,
    Binary,
)
class Log(rlp.Serializable):
    fields = [
        ('address', Binary.fixed_length(20, allow_empty=True)),
        ('topics', CountableList(BigEndianInt(32))),
        ('data', Binary())
    ]
"""

    header += """consts = config.get_group('core')
consts.mprocessing = consts.mprocessing.single
consts = config.get_group('evm')
consts.oog = 'pedantic'

class EVMTest(unittest.TestCase):
    # https://nose.readthedocs.io/en/latest/doc_tests/test_multiprocess/multiprocess.html#controlling-distribution
    _multiprocess_can_split_ = True
    # https://docs.python.org/3.7/library/unittest.html#unittest.TestCase.maxDiff
    maxDiff = None

"""
    return header 
Example #2
Source File: rlp.py    From py-evm with MIT License 5 votes vote down vote up
def diff_rlp_object(left: BaseBlock,
                    right: BaseBlock) -> Optional[Iterable[Tuple[str, str, str]]]:
    if left != right:
        rlp_type = type(left)

        for field_name, field_type in rlp_type._meta.fields:
            left_value = getattr(left, field_name)
            right_value = getattr(right, field_name)
            if isinstance(field_type, type) and issubclass(field_type, rlp.Serializable):
                sub_diff = diff_rlp_object(left_value, right_value)
                for sub_field_name, sub_left_value, sub_right_value in sub_diff:
                    yield (
                        f"{field_name}.{sub_field_name}",
                        sub_left_value,
                        sub_right_value,
                    )
            elif isinstance(field_type, (rlp.sedes.List, rlp.sedes.CountableList)):
                if tuple(left_value) != tuple(right_value):
                    yield (
                        field_name,
                        left_value,
                        right_value,
                    )
            elif left_value != right_value:
                yield (
                    field_name,
                    left_value,
                    right_value,
                )
            else:
                continue 
Example #3
Source File: rlp_decode.py    From trinity with MIT License 5 votes vote down vote up
def decode_all(rlp: bytes,
               sedes: rlp.Serializable = None,
               recursive_cache: bool = False,
               **kwargs: Any) -> Iterable[Any]:
    """Decode multiple 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
    """
    if not is_bytes(rlp):
        raise DecodingError('Can only decode RLP bytes, got type %s' % type(rlp).__name__, rlp)

    end = 0
    rlp_length = len(rlp)

    while rlp_length - end > 0:
        try:
            item, per_item_rlp, end = consume_item(rlp, end)
        except IndexError:
            raise DecodingError('RLP string too short', rlp)
        if sedes:
            obj = sedes.deserialize(item, **kwargs)
            if is_sequence(obj) or hasattr(obj, '_cached_rlp'):
                _apply_rlp_cache(obj, per_item_rlp, recursive_cache)
            yield obj
        else:
            yield item