def test_remote_sync_request(self):
        self.node.send_msg_to_node = MagicMock()
        remote_caller_connection1 = MockConnection(1, (LOCALHOST, 8001),
                                                   self.node)
        remote_caller_connection1.enqueue_msg = MagicMock()
        remote_caller_connection2 = MockConnection(2, (LOCALHOST, 8002),
                                                   self.node)
        remote_caller_connection2.enqueue_msg = MagicMock()

        remote_blockchain_message = GetHeadersBtcMessage(
            self.MAGIC, self.VERSION, self.HASHES, self.HASH2)
        self.blockchain_sync_service.process_remote_sync_request(
            remote_blockchain_message.command(), remote_blockchain_message,
            remote_caller_connection1)
        self.blockchain_sync_service.process_remote_sync_request(
            remote_blockchain_message.command(), remote_blockchain_message,
            remote_caller_connection2)
        self.node.send_msg_to_node.assert_called_once_with(
            remote_blockchain_message)

        local_blockchain_message = HeadersBtcMessage(self.MAGIC, headers=[])
        self.blockchain_sync_service.process_local_sync_response(
            local_blockchain_message.command(), local_blockchain_message)

        local_response = BlockchainSyncResponseMessage(
            local_blockchain_message.command(),
            local_blockchain_message.rawbytes())

        remote_caller_connection1.enqueue_msg.assert_called_once_with(
            local_response)
    def test_make_sync_request(self):
        other_connections = []
        for i in range(2, 5):
            fileno = i
            port = 8000 + i
            connection = MockConnection(fileno, (LOCALHOST, port), self.node)
            connection.state |= ConnectionState.ESTABLISHED
            connection.enqueue_msg = MagicMock()
            self.node.connection_pool.add(fileno, LOCALHOST, port, connection)
            other_connections.append(connection)

        preferred_connection = other_connections[0]
        self.node.peer_gateways.add(OutboundPeerModel(LOCALHOST, 8002))

        request = GetHeadersBtcMessage(self.MAGIC, self.VERSION, self.HASHES,
                                       self.HASH2)
        self.blockchain_sync_service.make_sync_request(request.command(),
                                                       request)

        request_message = BlockchainSyncRequestMessage(request.command(),
                                                       request.rawbytes())
        preferred_connection.enqueue_msg.assert_called_once_with(
            request_message, prepend=True)

        time.time = MagicMock(
            return_value=time.time() +
            gateway_constants.BLOCKCHAIN_SYNC_BROADCAST_DELAY_S)
        self.node.alarm_queue.fire_alarms()
        self.assertIn(request_message, self.node.broadcast_messages)