예제 #1
0
    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()
예제 #2
0
    def _wait_until_active(self):
        future = self._stream.receive()
        while True:
            try:
                message = future.result(1)
            except concurrent.futures.TimeoutError:
                continue

            if (message.message_type ==
                    Message.CONSENSUS_NOTIFY_ENGINE_ACTIVATED):
                notification = \
                    consensus_pb2.ConsensusNotifyEngineActivated()
                notification.ParseFromString(message.content)

                startup_state = StartupState(notification.chain_head,
                                             notification.peers,
                                             notification.local_peer_info)

                LOGGER.info(
                    'Received activation message with startup state: %s',
                    startup_state)

                self._stream.send_back(
                    message_type=Message.CONSENSUS_NOTIFY_ACK,
                    correlation_id=message.correlation_id,
                    content=consensus_pb2.ConsensusNotifyAck(
                    ).SerializeToString())

                return startup_state

            LOGGER.warning(
                'Received message type %s while waiting for \
                activation message', message.message_type)
            future = self._stream.receive()
예제 #3
0
    def _wait_until_active(self):
        while True:
            try:
                future = self._stream.receive()
                message = future.result(1)
            except concurrent.futures.TimeoutError:
                continue

            if (message.message_type ==
                    Message.CONSENSUS_NOTIFY_ENGINE_ACTIVATED):
                notification = \
                    consensus_pb2.ConsensusNotifyEngineActivated()
                notification.ParseFromString(message.content)

                self._stream.send_back(
                    message_type=Message.CONSENSUS_NOTIFY_ACK,
                    correlation_id=message.correlation_id,
                    content=consensus_pb2.ConsensusNotifyAck(
                    ).SerializeToString())

                return StartupState(notification.chain_head,
                                    notification.peers,
                                    notification.local_peer_info)