Exemplo n.º 1
0
def test_rlp_complex_homo():
    my_data = {
        "foo": 123,
        "bar": '0x12345678',
        "baz": [{
            "x": '0x11',
            "y": 1234
        }, {
            "x": '0x12',
            "y": 5678
        }]
    }

    my_wrapper = m_rlp.DictWrapper([
        ("foo", m_rlp.NumericKind()), ("bar", m_rlp.FixedBlobKind(4)),
        ("baz",
         m_rlp.HomoListWrapper(codec=m_rlp.DictWrapper([(
             "x", m_rlp.BlobKind()), ("y", m_rlp.NumericKind())])))
    ])

    cc = m_rlp.ComplexCodec(my_wrapper)

    assert cc.encode(my_data).hex() == 'd17b8412345678cac4118204d2c41282162e'

    assert cc.decode(
        bytes.fromhex('d17b8412345678cac4118204d2c41282162e')) == my_data
Exemplo n.º 2
0
def test_fixedBlob_decode():
    kind = m_rlp.FixedBlobKind(4)

    assert kind.deserialize(bytes([1, 2, 3, 4])) == '0x01020304'

    with pytest.raises(DeserializationError):
        kind.deserialize(bytes([0, 0]))

    with pytest.raises(DeserializationError):
        kind.deserialize(bytes(0))
Exemplo n.º 3
0
def test_fixedBlob_encode():
    kind = m_rlp.FixedBlobKind(4)

    assert kind.serialize('0x12345678').hex() == '12345678'

    with pytest.raises(SerializationError):
        kind.serialize('0x1234567z')

    with pytest.raises(SerializationError):
        kind.serialize('0x1234567890')

    with pytest.raises(SerializationError):
        kind.serialize('0x1234567')

    with pytest.raises(Exception):
        kind.serialize(1)

    with pytest.raises(Exception):
        kind.serialize(None)
Exemplo n.º 4
0
def test_rlp_complex_strange():
    my_data = {
        "foo": 123,
        "bar": '0x12345678',
        "baz": [
            { "x": '0x11', "y": 1234 },
            { "x": '0x12', "y": 5678 },
            789,
            [
                123,
                {
                    "a": 1
                }
            ]
        ]
    }

    my_wrapper = m_rlp.DictWrapper([
        ("foo", m_rlp.NumericKind()),
        ("bar", m_rlp.FixedBlobKind(4)),
        ("baz", m_rlp.ListWrapper([
            m_rlp.DictWrapper([
                ("x", m_rlp.BlobKind()),
                ("y", m_rlp.NumericKind())
            ]),
            m_rlp.DictWrapper([
                ("x", m_rlp.BlobKind()),
                ("y", m_rlp.NumericKind())
            ]),
            m_rlp.NumericKind(),
            m_rlp.ListWrapper([
                m_rlp.NumericKind(),
                m_rlp.DictWrapper([
                    ("a", m_rlp.NumericKind())
                ])
            ])
        ]))
    ])

    cc = m_rlp.ComplexCodec(my_wrapper)

    my_bytes = cc.encode(my_data) # encode
    assert cc.decode(my_bytes) == my_data # decode