def test_sedes(): ls = [ [], [1], [3, 2, 1] ] for l in ls: assert evaluate(rlp.decode_lazy(rlp.encode(l), big_endian_int)) == l sedes = CountableList(big_endian_int) l = [[], [1, 2], 'asdf', [3]] invalid_lazy = rlp.decode_lazy(rlp.encode(l), sedes) assert invalid_lazy[0] == l[0] assert invalid_lazy[1] == l[1] with pytest.raises(DeserializationError): invalid_lazy[2]
def on_receive_getblockhashes(self, proto, child_block_hash, count): log.debug('----------------------------------') log.debug("handle_get_blockhashes", count=count, block_hash=encode_hex(child_block_hash)) max_hashes = min(count, self.wire_protocol.max_getblockhashes_count) found = [] if child_block_hash not in self.chain: log.debug("unknown block") proto.send_blockhashes(*[]) return last = child_block_hash while len(found) < max_hashes: try: last = rlp.decode_lazy(self.chain.db.get(last))[0][0] # [head][prevhash] except KeyError: # this can happen if we started a chain download, which did not complete # should not happen if the hash is part of the canonical chain log.warn('KeyError in getblockhashes', hash=last) break if last: found.append(last) else: break log.debug("sending: found block_hashes", count=len(found)) proto.send_blockhashes(*found)
def result(): try: Message.deserialize(rlp.decode_lazy(s)) except (DecodingError, DeserializationError): return not valid else: return valid
def on_receive_getblockhashes(self, proto, child_block_hash, count): log.debug('----------------------------------') log.debug("handle_get_blockhashes", count=count, block_hash=encode_hex(child_block_hash)) max_hashes = min(count, self.wire_protocol.max_getblockhashes_count) found = [] if child_block_hash not in self.chain: log.debug("unknown block") proto.send_blockhashes(*[]) return last = child_block_hash while len(found) < max_hashes: try: last = rlp.decode_lazy( self.chain.db.get(last))[0][0] # [head][prevhash] except KeyError: # this can happen if we started a chain download, which did not complete # should not happen if the hash is part of the canonical chain log.warn('KeyError in getblockhashes', hash=last) break if last: found.append(last) else: break log.debug("sending: found block_hashes", count=len(found)) proto.send_blockhashes(*found)
def decode_payload(cls, rlp_data): # convert to dict txs = [] for i, tx in enumerate(rlp.decode_lazy(rlp_data)): txs.append(Transaction.deserialize(tx)) if not i % 10: gevent.sleep(0.0001) return txs
def decode_payload(cls, rlp_data): # convert to dict ll = rlp.decode_lazy(rlp_data) assert len(ll) == 2 transient_block = TransientBlock.init_from_rlp(ll[0], time.time()) difficulty = rlp.sedes.big_endian_int.deserialize(ll[1]) data = [transient_block, difficulty] return dict((cls.structure[i][0], v) for i, v in enumerate(data))
def test_evaluation_of_lazy_decode_with_list_sedes_and_invalid_value(): sedes = CountableList(big_endian_int) value = [(), (1, 2), b'asdf', (3)] invalid_lazy = rlp.decode_lazy(rlp.encode(value), sedes) assert invalid_lazy[0] == value[0] assert invalid_lazy[1] == value[1] with pytest.raises(DeserializationError): invalid_lazy[2]
def decode_payload(cls, rlp_data): # fn = 'blocks.fromthewire.hex.rlp' # open(fn, 'a').write(rlp_data.encode('hex') + '\n') # convert to dict blocks = [] for block in rlp.decode_lazy(rlp_data): blocks.append(TransientBlock(block)) return blocks
def decode_payload(cls, rlp_data): # fn = 'blocks.fromthewire.hex.rlp' # open(fn, 'a').write(rlp_data.encode('hex') + '\n') # convert to dict blockheaders = [] for blockheader in rlp.decode_lazy(rlp_data): blockheaders.append(BlockHeader(blockheader)) return blockheaders
def decode_payload(cls, rlp_data): # convert to dict # print rlp_data.encode('hex') ll = rlp.decode_lazy(rlp_data) assert len(ll) == 2 transient_block = TransientBlock(ll[0]) difficulty = rlp.sedes.big_endian_int.deserialize(ll[1]) data = [transient_block, difficulty] return dict((cls.structure[i][0], v) for i, v in enumerate(data))
def test_empty_list(): dec = lambda: rlp.decode_lazy(rlp.encode([])) assert isinstance(dec(), Sequence) with pytest.raises(IndexError): dec()[0] with pytest.raises(IndexError): dec()[1] assert len(dec()) == 0 assert evaluate(dec()) == []
def test_string(): for s in (b'', b'asdf', b'a' * 56, b'b' * 123): dec = lambda: rlp.decode_lazy(rlp.encode(s)) assert isinstance(dec(), bytes) assert len(dec()) == len(s) assert dec() == s assert rlp.peek(rlp.encode(s), []) == s with pytest.raises(IndexError): rlp.peek(rlp.encode(s), 0) with pytest.raises(IndexError): rlp.peek(rlp.encode(s), [0])
def test_decode(name, in_out): msg_format = 'Test {} failed (decoded {} to {} instead of {})' rlp_string = in_out['out'].decode('hex') decoded = decode(rlp_string) assert decoded == evaluate(decode_lazy(rlp_string)) expected = in_out['in'] sedes = infer_sedes(expected) data = sedes.deserialize(decoded) assert data == decode(rlp_string, sedes) if data != expected: pytest.fail(msg_format.format(name, rlp_string, decoded, expected))
def test_nested_list(): l = [[], [b'a'], [b'b', b'c', b'd']] dec = lambda: rlp.decode_lazy(rlp.encode(l)) assert isinstance(dec(), Sequence) assert len(dec()) == len(l) assert evaluate(dec()) == l with pytest.raises(IndexError): dec()[0][0] with pytest.raises(IndexError): dec()[1][1] with pytest.raises(IndexError): dec()[2][3] with pytest.raises(IndexError): dec()[3]
def test_nested_list(): l = [[], ['a'], ['b', 'c', 'd']] dec = lambda: rlp.decode_lazy(rlp.encode(l)) assert isinstance(dec(), Sequence) assert len(dec()) == len(l) assert evaluate(dec()) == l with pytest.raises(IndexError): assert dec()[0][0] with pytest.raises(IndexError): assert dec()[1][1] with pytest.raises(IndexError): assert dec()[2][3] with pytest.raises(IndexError): assert dec()[3]
def test_decode(name, in_out): msg_format = 'Test {} failed (decoded {} to {} instead of {})' rlp_string = utils.decode_hex(in_out['out']) decoded = decode(rlp_string) with pytest.raises(DecodingError): decode(rlp_string + b'\x00') assert decoded == decode(rlp_string + b'\x00', strict=False) assert decoded == evaluate(decode_lazy(rlp_string)) expected = in_out['in'] sedes = infer_sedes(expected) data = sedes.deserialize(decoded) assert data == decode(rlp_string, sedes) if data != expected: pytest.fail(msg_format.format(name, rlp_string, decoded, expected))
def run(self): """Import all the blocks in /blocks/""" print('\nImport all the blocks in /blocks/') blocks = sorted(os.listdir('/blocks'), key=str) for block in blocks: data = open('/blocks/' + block, 'r').read() block_data = rlp.decode_lazy(data) header = BlockHeader.deserialize(block_data[0]) transactions = rlp.sedes.CountableList(Transaction).deserialize( block_data[1]) uncles = rlp.sedes.CountableList(BlockHeader).deserialize( block_data[2]) transient_block = TransientBlock(header, transactions, uncles, 0) print '\nIMPORTING BLOCK %s (%s)' % ( block, transient_block.header.hex_hash) self.eth.chain.add_block(transient_block.to_block())
def test_list_getitem(): l = rlp.decode_lazy(rlp.encode([1, 2, 3]), big_endian_int) assert isinstance(l, rlp.lazy.LazyList) assert l[0] == 1 assert l[1] == 2 assert l[2] == 3 assert l[-1] == 3 assert l[-2] == 2 assert l[-3] == 1 assert l[0:3] == [1, 2, 3] assert l[0:2] == [1, 2] assert l[0:1] == [1] assert l[1:2] == [2] assert l[1:] == [2, 3] assert l[1:-1] == [2] assert l[-2:] == [2, 3] assert l[:2] == [1, 2]
def on_receive_getblockhashes(self, proto, child_block_hash, count): log.debug("handle_get_blockhashes", count=count, block_hash=encode_hex(child_block_hash)) max_hashes = min(count, self.wire_protocol.max_getblockhashes_count) found = [] if child_block_hash not in self.chain: log.debug("unknown block") proto.send_blockhashes(*[]) return last = child_block_hash while len(found) < max_hashes: last = rlp.decode_lazy(self.chain.db.get(last))[0][0] if last: found.append(last) else: break log.debug("sending: found block_hashes", count=len(found)) proto.send_blockhashes(*found)
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
def test_string(): for s in ('', 'asdf', 'a' * 56, 'b' * 123): dec = lambda: rlp.decode_lazy(rlp.encode(s)) assert isinstance(dec(), str) assert len(dec()) == len(s) assert dec() == s
def block_from_rlp(this, rlp_data): from eth_protocol import TransientBlock import rlp l = rlp.decode_lazy(rlp_data) return TransientBlock(l).to_block(this.chain.blockchain)
def import_block(chain, rlp_data): ll = rlp.decode_lazy(rlp_data) transient_block = TransientBlock(ll, 0) transient_block.to_block(chain.db)
def test_bytearray_lazy(): e = encode(b'abc') expected = decode(e) actual = decode_lazy(bytearray(e)) assert expected == actual
def dec(): return rlp.decode_lazy(rlp.encode(value))
def dec(): return rlp.decode_lazy(rlp.encode(l))
def test_evaluation_of_lazy_decode_with_simple_value_sedes(value): assert evaluate(rlp.decode_lazy(rlp.encode(value), big_endian_int)) == value
def from_block_rlp(self, rlp_data): block_data = rlp.decode_lazy(rlp_data) r = super(BlockHeader, self).deserialize(block_data[0]) assert isinstance(r, BlockHeader) return r
def block_from_rlp(this, rlp_data): from .eth_protocol import TransientBlock import rlp l = rlp.decode_lazy(rlp_data) return TransientBlock.init_from_rlp(l).to_block()