def _decode_block_pair(placeholder, offset, data): """ Decode an incoming block pair message. :param placeholder: :param offset: Start of the BlockPair message in the data. :param data: ByteStream containing the message. :return: (offset, BlockPairPayload.impl) """ if len(data) < offset + block_pack_size * 2: raise DropPacket("Unable to decode the payload") try: new_offset, block1 = TrustChainBlock.unpack(data, offset) _, block2 = TrustChainBlock.unpack(data, new_offset) except (IndexError, ValueError): raise DropPacket("Invalid block contents") return len(data), placeholder.meta.payload.implement(block1, block2)
def _decode_half_block(placeholder, offset, data): """ Decode an incoming half block message. :param placeholder: :param offset: Start of the HalfBlock message in the data. :param data: ByteStream containing the message. :return: (offset, HalfBlockPayload.impl) """ if len(data) < offset + block_pack_size: raise DropPacket("Unable to decode the payload") try: block = TrustChainBlock.unpack(data, offset) except IndexError: raise DropPacket("Invalid block contents") return len(data), placeholder.meta.payload.implement(block)
def test_unpack(self): block = TrustChainBlock.unpack( ' fish, so sad that it should come to this. - We tried to warn you ' 'all but oh dear! - You may not share our intellect, which might explain your ' 'disrespect, for all the natural wonders that grow around you. - So long, ' 'so long, and thanks for all the fish\x00\x00\x00\na1d2bid1i3')[1] self.assertEqual(block.transaction, {'id': 3}) self.assertEqual( block.public_key, ' fish, so sad that it should come to this. - We tried to warn you all but ' ) self.assertEqual(block.sequence_number, 1869095012) self.assertEqual( block.link_public_key, 'ear! - You may not share our intellect, which might explain your disrespec' ) self.assertEqual(block.link_sequence_number, 1949048934) self.assertEqual(block.previous_hash, 'or all the natural wonders that ') self.assertEqual( block.signature, 'grow around you. - So long, so long, and thanks for all the fish')