def export_transactions(chain, head_number=None): """ Export transactions rlp and hex encoded, separated by newline """ head_number = head_number or chain.head.header.number block_number = 0 seen = 0 while block_number < head_number: h = chain.index.get_block_by_number(block_number) raw = chain.blockchain.get(h) # block [[], [], []] typ, length, end = consume_length_prefix(raw, 0) assert typ == list typ, length, end = consume_length_prefix(raw, end) # header list assert typ == list txs_start = end + length typ, length, end = consume_length_prefix(raw, txs_start) # tx list txrlp = raw[txs_start:end + length] r = rlp.decode(txrlp) assert isinstance(r, list) for tx in r: s = rlp.encode(tx) rlp.decode(s, Transaction) print s.encode('hex') seen += 1 _progress(seen) block_number += 1
def compare_length(rlpdata, length): _, _typ, _len, _pos = consume_length_prefix(rlpdata, 0) assert _typ is list lenlist = 0 if rlpdata == EMPTYLIST: return -1 if length > 0 else 1 if length < 0 else 0 while 1: if lenlist > length: return 1 _, _, _l, _p = consume_length_prefix(rlpdata, _pos) lenlist += 1 if _l + _p >= len(rlpdata): break _pos = _l + _p return 0 if lenlist == length else -1
def get_packet_decoder(encoded_packet: bytes) -> Callable[[bytes], Packet]: # Both message and WhoAreYou packets start with 32 bytes (either magic or tag) followed by rlp # encoded data. Only in the case of message packets this is followed by the encrypted message. # Therefore, we distinguish the two by reading the RLP length prefix and then checking if there # is additional data. if MAGIC_SIZE != TAG_SIZE: raise Exception( "Invariant: This check works as magic and tag size are equal" ) prefix_size = MAGIC_SIZE if len(encoded_packet) < prefix_size + 1: raise ValidationError(f"Packet is with {len(encoded_packet)} bytes too short") try: _, _, rlp_size, _ = consume_length_prefix(encoded_packet, MAGIC_SIZE) except DecodingError as error: raise ValidationError("RLP section of packet starts with invalid length prefix") from error expected_who_are_you_size = prefix_size + 1 + rlp_size if len(encoded_packet) > expected_who_are_you_size: return decode_message_packet elif len(encoded_packet) == expected_who_are_you_size: return decode_who_are_you_packet else: raise ValidationError("RLP section of packet is incomplete")
def decode(self, data): # cmdid = rlp.peek(data, 0, sedes=t_int) # cmdid is first element t, l, pos = consume_length_prefix(data, 0) cmdid = t_int.deserialize(consume_item(data[pos], 0)[0]) cls = self.message_class_by_id[cmdid] print 'DECODING', cls return cls.decode(data)