def _register(self):
        self._stream.wait_for_ready()

        request = consensus_pb2.ConsensusRegisterRequest(
            name=self._engine.name(),
            version=self._engine.version(),
        ).SerializeToString()

        while True:
            future = self._stream.send(
                message_type=Message.CONSENSUS_REGISTER_REQUEST,
                content=request)
            response = consensus_pb2.ConsensusRegisterResponse()
            response.ParseFromString(future.result(REGISTER_TIMEOUT).content)

            if (response.status ==
                    consensus_pb2.ConsensusRegisterResponse.NOT_READY):
                continue

            if response.status == consensus_pb2.ConsensusRegisterResponse.OK:
                return StartupState(response.chain_head, response.peers,
                                    response.local_peer_info)

            raise exceptions.ReceiveError(
                'Registration failed with status {}'.format(response.status))
    def test_driver(self):
        # Start the driver in a separate thread to simulate the
        # validator and the driver
        driver_thread = threading.Thread(target=self.driver.start,
                                         args=(self.url, ))

        driver_thread.start()

        response = consensus_pb2.ConsensusRegisterResponse(
            status=consensus_pb2.ConsensusRegisterResponse.OK)

        request = self.recv_rep(consensus_pb2.ConsensusRegisterRequest,
                                response, Message.CONSENSUS_REGISTER_RESPONSE)

        additional_protocols = \
            [(p.name, p.version) for p in request.additional_protocols]

        self.assertEqual(request.name, 'test-name')
        self.assertEqual(request.version, 'test-version')
        self.assertEqual(additional_protocols, [('Test-Name', 'Test-Version')])

        self.send_req_rep(consensus_pb2.ConsensusNotifyEngineActivated(),
                          Message.CONSENSUS_NOTIFY_ENGINE_ACTIVATED)

        self.send_req_rep(consensus_pb2.ConsensusNotifyPeerConnected(),
                          Message.CONSENSUS_NOTIFY_PEER_CONNECTED)

        self.send_req_rep(consensus_pb2.ConsensusNotifyPeerDisconnected(),
                          Message.CONSENSUS_NOTIFY_PEER_DISCONNECTED)

        self.send_req_rep(consensus_pb2.ConsensusNotifyPeerMessage(),
                          Message.CONSENSUS_NOTIFY_PEER_MESSAGE)

        self.send_req_rep(consensus_pb2.ConsensusNotifyBlockNew(),
                          Message.CONSENSUS_NOTIFY_BLOCK_NEW)

        self.send_req_rep(consensus_pb2.ConsensusNotifyBlockValid(),
                          Message.CONSENSUS_NOTIFY_BLOCK_VALID)

        self.send_req_rep(consensus_pb2.ConsensusNotifyBlockInvalid(),
                          Message.CONSENSUS_NOTIFY_BLOCK_INVALID)

        self.send_req_rep(consensus_pb2.ConsensusNotifyBlockCommit(),
                          Message.CONSENSUS_NOTIFY_BLOCK_COMMIT)

        self.send_req_rep(network_pb2.PingRequest(), Message.PING_REQUEST)

        self.assertEqual(
            [msg_type for (msg_type, data) in self.engine.updates], [
                Message.CONSENSUS_NOTIFY_PEER_CONNECTED,
                Message.CONSENSUS_NOTIFY_PEER_DISCONNECTED,
                Message.CONSENSUS_NOTIFY_PEER_MESSAGE,
                Message.CONSENSUS_NOTIFY_BLOCK_NEW,
                Message.CONSENSUS_NOTIFY_BLOCK_VALID,
                Message.CONSENSUS_NOTIFY_BLOCK_INVALID,
                Message.CONSENSUS_NOTIFY_BLOCK_COMMIT,
            ])

        self.driver.stop()
        driver_thread.join()
Exemple #3
0
    def _register(self):
        self._stream.wait_for_ready()

        request = consensus_pb2.ConsensusRegisterRequest(
            name=self._engine.name(),
            version=self._engine.version(),
        )

        for (name, version) in self._engine.additional_protocols():
            protocol = request.additional_protocols.add()
            protocol.name = name
            protocol.version = version

        while True:
            future = self._stream.send(
                message_type=Message.CONSENSUS_REGISTER_REQUEST,
                content=request.SerializeToString())
            response = consensus_pb2.ConsensusRegisterResponse()
            response.ParseFromString(future.result(REGISTER_TIMEOUT).content)

            if (response.status ==
                    consensus_pb2.ConsensusRegisterResponse.NOT_READY):
                continue

            if response.status == consensus_pb2.ConsensusRegisterResponse.OK:
                if (response.HasField('chain_head')
                        and response.HasField('local_peer_info')):
                    return StartupState(response.chain_head, response.peers,
                                        response.local_peer_info)

                return None

            raise exceptions.ReceiveError(
                'Registration failed with status {}'.format(response.status))
    def test_driver(self):
        # Start the driver in a separate thread to simulate the
        # validator and the driver
        driver_thread = threading.Thread(target=self.driver.start,
                                         args=(self.url, ))

        driver_thread.start()

        response = consensus_pb2.ConsensusRegisterResponse(
            status=consensus_pb2.ConsensusRegisterResponse.OK)

        request = self.recv_rep(consensus_pb2.ConsensusRegisterRequest,
                                response, Message.CONSENSUS_REGISTER_RESPONSE)

        self.assertEqual(request.name, 'test-name')
        self.assertEqual(request.version, 'test-version')

        self.send_req_rep(consensus_pb2.ConsensusNotifyPeerConnected(),
                          Message.CONSENSUS_NOTIFY_PEER_CONNECTED)

        self.send_req_rep(consensus_pb2.ConsensusNotifyPeerDisconnected(),
                          Message.CONSENSUS_NOTIFY_PEER_DISCONNECTED)

        self.send_req_rep(consensus_pb2.ConsensusNotifyPeerMessage(),
                          Message.CONSENSUS_NOTIFY_PEER_MESSAGE)

        self.send_req_rep(consensus_pb2.ConsensusNotifyBlockNew(),
                          Message.CONSENSUS_NOTIFY_BLOCK_NEW)

        self.send_req_rep(consensus_pb2.ConsensusNotifyBlockValid(),
                          Message.CONSENSUS_NOTIFY_BLOCK_VALID)

        self.send_req_rep(consensus_pb2.ConsensusNotifyBlockInvalid(),
                          Message.CONSENSUS_NOTIFY_BLOCK_INVALID)

        self.send_req_rep(consensus_pb2.ConsensusNotifyBlockCommit(),
                          Message.CONSENSUS_NOTIFY_BLOCK_COMMIT)

        self.assertEqual(
            [msg_type for (msg_type, data) in list(self.engine.updates.queue)],
            [
                Message.CONSENSUS_NOTIFY_PEER_CONNECTED,
                Message.CONSENSUS_NOTIFY_PEER_DISCONNECTED,
                Message.CONSENSUS_NOTIFY_PEER_MESSAGE,
                Message.CONSENSUS_NOTIFY_BLOCK_NEW,
                Message.CONSENSUS_NOTIFY_BLOCK_VALID,
                Message.CONSENSUS_NOTIFY_BLOCK_INVALID,
                Message.CONSENSUS_NOTIFY_BLOCK_COMMIT,
            ])

        self.driver.stop()
        driver_thread.join()
    def _register(self):
        self._stream.wait_for_ready()

        future = self._stream.send(
            message_type=Message.CONSENSUS_REGISTER_REQUEST,
            content=consensus_pb2.ConsensusRegisterRequest(
                name=self._engine.name(),
                version=self._engine.version(),
            ).SerializeToString(),
        )

        response = consensus_pb2.ConsensusRegisterResponse()
        response.ParseFromString(future.result().content)

        if response.status != consensus_pb2.ConsensusRegisterResponse.OK:
            raise exceptions.ReceiveError(
                'Registration failed with status {}'.format(response.status))