示例#1
0
def test_countable_list():
    l1 = CountableList(big_endian_int)
    serializable = ([], [1, 2], list(range(500)))
    for s in serializable:
        r = l1.serialize(s)
        assert l1.deserialize(r) == s
    not_serializable = ([1, "asdf"], ["asdf"], [1, [2]], [[]])
    for n in not_serializable:
        with pytest.raises(SerializationError):
            l1.serialize(n)

    l2 = CountableList(CountableList(big_endian_int))
    serializable = ([], [[]], [[1, 2, 3], [4]], [[5], [6, 7, 8]], [[], [], [9, 0]])
    for s in serializable:
        r = l2.serialize(s)
        assert l2.deserialize(r) == s
    not_serializable = ([[[]]], [1, 2], [1, ["asdf"], ["fdsa"]])
    for n in not_serializable:
        with pytest.raises(SerializationError):
            l2.serialize(n)
示例#2
0
 def __init__(self, data: bytes, db):
     self.token_trie = SecureTrie(Trie(db))
     self.balances = {}
     self.enum = b"\x00"
     if len(data) != 0:
         self.enum = data[:1]
         if self.enum == b"\x00":
             for p in rlp.decode(data[1:], CountableList(TokenBalancePair)):
                 self.balances[p.token_id] = p.balance
         elif self.enum == b"\x01":
             raise Exception("Token balance trie is not yet implemented")
         else:
             raise Exception("Unknown enum byte in token_balances")
示例#3
0
    def __init__(self, data: bytes, db):
        self.token_trie = None
        # We don't want outside world to know this
        self._balances = {}
        self._db = db

        if len(data) != 0:
            enum = data[:1]
            if enum == b"\x00":
                for p in rlp.decode(data[1:], CountableList(TokenBalancePair)):
                    self._balances[p.token_id] = p.balance
            elif enum == b"\x01":
                self.token_trie = SecureTrie(Trie(db, data[1:]))
            else:
                raise ValueError("Unknown enum byte in token_balances")
示例#4
0
def test_countable_list():
    l1 = CountableList(big_endian_int)
    serializable = [(), (1, 2), tuple(range(500))]
    for s in serializable:
        r = l1.serialize(s)
        assert l1.deserialize(r) == s
    not_serializable = ([1, 'asdf'], ['asdf'], [1, [2]], [[]])
    for n in not_serializable:
        with pytest.raises(SerializationError):
            l1.serialize(n)

    l2 = CountableList(CountableList(big_endian_int))
    serializable = ((), ((), ), ((1, 2, 3), (4, )), ((5, ), (6, 7, 8)),
                    ((), (), (9, 0)))
    for s in serializable:
        r = l2.serialize(s)
        assert l2.deserialize(r) == s
    not_serializable = ([[[]]], [1, 2], [1, ['asdf'], ['fdsa']])
    for n in not_serializable:
        with pytest.raises(SerializationError):
            l2.serialize(n)

    l3 = CountableList(big_endian_int, max_length=3)
    serializable = [(), (1, ), (1, 2), (1, 2, 3)]
    for s in serializable:
        r = l3.serialize(s)
        assert r == l1.serialize(s)
        assert l3.deserialize(r) == s
    not_serializable = [(1, 2, 3, 4), (1, 2, 3, 4, 5, 6, 7), range(500)]
    for s in not_serializable:
        with pytest.raises(SerializationError):
            l3.serialize(s)
        r = l1.serialize(s)
        with pytest.raises(DeserializationError):
            l3.deserialize(r)
        ll = rlp.decode_lazy(rlp.encode(r))
        with pytest.raises(DeserializationError):
            l3.deserialize(ll)
        assert len(
            ll._elements) == 3 + 1  # failed early, did not consume fully
示例#5
0
def test_countable_list():
    l1 = CountableList(big_endian_int)
    serializable = ([], [1, 2], list(xrange(500)))
    for s in serializable:
        r = l1.serialize(s)
        assert l1.deserialize(r) == s
    not_serializable = ([1, 'asdf'], ['asdf'], [1, [2]], [[]])
    for n in not_serializable:
        with pytest.raises(SerializationError):
            l1.serialize(n)

    l2 = CountableList(CountableList(big_endian_int))
    serializable = ([], [[]], [[1, 2, 3], [4]], [[5], [6, 7, 8]], [[], [],
                    [9, 0]])
    for s in serializable:
        r = l2.serialize(s)
        assert l2.deserialize(r) == s
    not_serializable = ([[[]]], [1, 2], [1, ['asdf'], ['fdsa']])
    for n in not_serializable:
        with pytest.raises(SerializationError):
            l2.serialize(n)