def test_rlp_complex(): 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.ListWrapper(list_of_codecs=[ m_rlp.DictWrapper([("x", m_rlp.BlobKind()), ("y", m_rlp.NumericKind())]), 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
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
def test_blobKind_encode(): kind = m_rlp.BlobKind() assert kind.serialize('0x1234567890').hex() == '1234567890' with pytest.raises(SerializationError, match=".+even.+"): kind.serialize('0x1') with pytest.raises(SerializationError): kind.serialize('0xxy') with pytest.raises(Exception): kind.serialize(1)
def test_blobKind_decode(): kind = m_rlp.BlobKind() assert kind.deserialize(bytes([1, 2, 3, 4, 5])) == '0x0102030405'