def handle_blockmeta_request(self, request: BlockMetaDataRequest): self.log.important( "Masternode received BlockMetaDataRequest: {}".format(request)) # Get a list of block hashes up until this most recent block # TODO get_child_block_hashes return an error/assertion/something if block cannot be found child_hashes = BlockStorageDriver.get_child_block_hashes( request.current_block_hash) self.log.debugv( "Got descended block hashes {} for block hash {}".format( child_hashes, request.current_block_hash)) # If this hash could not be found or if it was the latest hash, no need to lookup any blocks if not child_hashes: self.log.debug( "Requested block hash {} is already up to date".format( request.current_block_hash)) reply = BlockMetaDataReply.create(block_metas=None) return reply # Build a BlockMetaData object for each descendant block block_metas = [] for block_hash in child_hashes: block_data = BlockStorageDriver.get_block(hash=block_hash, include_number=False) meta = BlockMetaData.create(**block_data) block_metas.append(meta) reply = BlockMetaDataReply.create(block_metas=block_metas) return reply
def test_create_fails_with_invalid_blockmetas(self): """ Tests that an assertion is raised if BlockMetaDataReply.create(...) is called with a list that contains a non BlockMetaData instance """ with self.assertRaises(Exception) as e: reply = BlockMetaDataReply.create(block_metas=[b'ack'])
def handle_blockmeta_request(self, request: BlockMetaDataRequest, envelope: Envelope): vk = envelope.seal.verifying_key assert vk in VKBook.get_delegates( ), "Got BlockMetaDataRequest from VK {} not in delegate VKBook!".format( vk) self.log.notice( "Masternode received BlockMetaDataRequest from delegate {}\n...request={}" .format(vk, request)) # Get a list of block hashes up until this most recent block # TODO get_child_block_hashes return an error/assertion/something if block cannot be found child_hashes = BlockStorageDriver.get_child_block_hashes( request.current_block_hash) self.log.debugv( "Got descendant block hashes {} for block hash {}".format( child_hashes, request.current_block_hash)) # If this hash could not be found or if it was the latest hash, no need to lookup any blocks if not child_hashes: self.log.debug( "Requested block hash {} is already up to date".format( request.current_block_hash)) reply = BlockMetaDataReply.create(block_metas=None) return reply # Build a BlockMetaData object for each descendant block block_metas = [] for block_hash in child_hashes: block_data = BlockStorageDriver.get_block(hash=block_hash, include_number=False) meta = BlockMetaData.create(**block_data) block_metas.append(meta) reply = BlockMetaDataReply.create(block_metas=block_metas) return reply
def test_serialize_deserialize(self): reply = BlockMetaDataReply.create(block_metas=self.block_metas) clone = BlockMetaDataReply.from_bytes(reply.serialize()) self.assertEqual(clone, reply)
def test_create_with_invalid_blocks(self): self.block_metas.append(self._bad_block()) reply = BlockMetaDataReply.create(block_metas=self.block_metas) for expected, actual in zip(reply.block_metas, self.block_metas): self.assertEqual(expected, actual)
def test_create_with_blocks(self): reply = BlockMetaDataReply.create(block_metas=self.block_metas) for expected, actual in zip(reply.block_metas, self.block_metas): self.assertEqual(expected, actual)
def test_create_with_block_metas_none(self): reply = BlockMetaDataReply.create(block_metas=None) self.assertFalse(reply.block_metas)
def test_create_with_no_blocks(self): reply = BlockMetaDataReply.create(block_metas=[]) self.assertFalse(reply.block_metas)