def handle_block_height(self, source, message: qrllegacy_pb2.LegacyMessage): """ Sends / Receives Blockheight :param source: :param message: :return: """ if message.bhData.block_number == 0: block = source.factory.get_last_block() cumulative_difficulty = source.factory.get_cumulative_difficulty() if block.block_number == 0: return bhdata = qrl_pb2.BlockHeightData( block_number=block.block_number, block_headerhash=block.headerhash, cumulative_difficulty=bytes(cumulative_difficulty)) msg = qrllegacy_pb2.LegacyMessage( func_name=qrllegacy_pb2.LegacyMessage.BH, bhData=bhdata) source.send(msg) return try: UInt256ToString(message.chainStateData.cumulative_difficulty) except ValueError: logger.warning('Invalid Block Height Data') source.loseConnection() return source.factory.update_peer_blockheight( source.addr_remote, message.bhData.block_number, message.bhData.block_headerhash, message.bhData.cumulative_difficulty)
def test_handle_block_height_incoming_information(self, m_logger): """ If the incoming message.bhData.block_number is not 0, this means that we should update our knowledge of the peer's blockheight. """ some_cumulative_difficulty = bytes(StringToUInt256('0')) incoming_info = make_message( func_name=qrllegacy_pb2.LegacyMessage.BH, bhData=qrl_pb2.BlockHeightData( block_number=1, block_headerhash=sha256(b'some_hash'), cumulative_difficulty=some_cumulative_difficulty)) self.manager.handle_block_height(self.channel, incoming_info) self.channel.send.assert_not_called() self.channel.factory.update_peer_blockheight.assert_called()
def test_handle_block_height_incoming_request_but_node_at_blockheight_zero( self, m_logger): """ If the incoming message.bhData.block_number is 0, this means that the peer wants to know our block height. However, if we are at blockheight 0 ourselves, we shamefully reply with silence. """ self.channel.factory = Mock( last_block=Mock(autospec=Block, block_number=0, headerhash=b''), get_cumulative_difficulty=Mock(return_value=(0, ))) incoming_request = make_message( func_name=qrllegacy_pb2.LegacyMessage.BH, bhData=qrl_pb2.BlockHeightData(block_number=0, )) self.manager.handle_block_height(self.channel, incoming_request) self.channel.send.assert_not_called() self.channel.factory.update_peer_blockheight.assert_not_called()
def test_handle_block_height_incoming_request(self, m_logger): """ handle_block_height(), unlike other handlers in this class, deals with requests and sends out responses to requests, all in the same function! If the incoming message.bhData.block_number is 0, this means that the peer wants to know our block height. If the incoming message.bhData.block_number is not 0, this means that we should update our knowledge of the peer's blockheight. """ self.channel.factory = Mock( last_block=Mock(autospec=Block, block_number=5, headerhash=b''), get_cumulative_difficulty=Mock(return_value=(0, ))) incoming_request = make_message( func_name=qrllegacy_pb2.LegacyMessage.BH, bhData=qrl_pb2.BlockHeightData(block_number=0)) self.manager.handle_block_height(self.channel, incoming_request) self.channel.send.assert_called() self.channel.factory.update_peer_blockheight.assert_not_called()
def handle_block_height(self, source, message: qrllegacy_pb2.LegacyMessage): """ Sends / Receives Blockheight :param source: :param message: :return: """ if message.bhData.block_number == 0: block = source.factory.get_last_block() cumulative_difficulty = source.factory.get_cumulative_difficulty() if block.block_number == 0: return bhdata = qrl_pb2.BlockHeightData(block_number=block.block_number, block_headerhash=block.headerhash, cumulative_difficulty=bytes(cumulative_difficulty)) msg = qrllegacy_pb2.LegacyMessage(func_name=qrllegacy_pb2.LegacyMessage.BH, bhData=bhdata) source.send(msg) else: source.factory.update_peer_blockheight(source.connection_id, message.bhData.block_number, message.bhData.block_headerhash, message.bhData.cumulative_difficulty)
def request_peer_blockheight(self): for peer in self._peer_connections: msg = qrllegacy_pb2.LegacyMessage( func_name=qrllegacy_pb2.LegacyMessage.BH, bhData=qrl_pb2.BlockHeightData(block_number=0)) peer.send(msg)