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()
def _process(self, message): type_tag = message.message_type if type_tag == Message.CONSENSUS_NOTIFY_PEER_CONNECTED: notification = consensus_pb2.ConsensusNotifyPeerConnected() notification.ParseFromString(message.content) data = notification.peer_info elif type_tag == Message.CONSENSUS_NOTIFY_PEER_DISCONNECTED: notification = consensus_pb2.ConsensusNotifyPeerDisconnected() notification.ParseFromString(message.content) data = notification.peer_id elif type_tag == Message.CONSENSUS_NOTIFY_PEER_MESSAGE: notification = consensus_pb2.ConsensusNotifyPeerMessage() notification.ParseFromString(message.content) data = notification.message, notification.sender_id elif type_tag == Message.CONSENSUS_NOTIFY_BLOCK_NEW: notification = consensus_pb2.ConsensusNotifyBlockNew() notification.ParseFromString(message.content) data = notification.block elif type_tag == Message.CONSENSUS_NOTIFY_BLOCK_VALID: notification = consensus_pb2.ConsensusNotifyBlockValid() notification.ParseFromString(message.content) data = notification.block_id elif type_tag == Message.CONSENSUS_NOTIFY_BLOCK_INVALID: notification = consensus_pb2.ConsensusNotifyBlockInvalid() notification.ParseFromString(message.content) data = notification.block_id elif type_tag == Message.CONSENSUS_NOTIFY_BLOCK_COMMIT: notification = consensus_pb2.ConsensusNotifyBlockCommit() notification.ParseFromString(message.content) data = notification.block_id else: raise exceptions.ReceiveError( 'Received unexpected message type: {}'.format(type_tag)) self._stream.send_back( message_type=Message.CONSENSUS_NOTIFY_ACK, correlation_id=message.correlation_id, content=consensus_pb2.ConsensusNotifyAck().SerializeToString()) return type_tag, data
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 _process(self, message): type_tag = message.message_type if type_tag == Message.CONSENSUS_NOTIFY_PEER_CONNECTED: notification = consensus_pb2.ConsensusNotifyPeerConnected() notification.ParseFromString(message.content) data = notification.peer_info elif type_tag == Message.CONSENSUS_NOTIFY_PEER_DISCONNECTED: notification = consensus_pb2.ConsensusNotifyPeerDisconnected() notification.ParseFromString(message.content) data = notification.peer_id elif type_tag == Message.CONSENSUS_NOTIFY_PEER_MESSAGE: notification = consensus_pb2.ConsensusNotifyPeerMessage() notification.ParseFromString(message.content) header = consensus_pb2.ConsensusPeerMessageHeader() header.ParseFromString(notification.message.header) peer_message = PeerMessage( header=header, header_bytes=notification.message.header, header_signature=notification.message.header_signature, content=notification.message.content) data = peer_message, notification.sender_id elif type_tag == Message.CONSENSUS_NOTIFY_BLOCK_NEW: notification = consensus_pb2.ConsensusNotifyBlockNew() notification.ParseFromString(message.content) data = notification.block elif type_tag == Message.CONSENSUS_NOTIFY_BLOCK_VALID: notification = consensus_pb2.ConsensusNotifyBlockValid() notification.ParseFromString(message.content) data = notification.block_id elif type_tag == Message.CONSENSUS_NOTIFY_BLOCK_INVALID: notification = consensus_pb2.ConsensusNotifyBlockInvalid() notification.ParseFromString(message.content) data = notification.block_id elif type_tag == Message.CONSENSUS_NOTIFY_BLOCK_COMMIT: notification = consensus_pb2.ConsensusNotifyBlockCommit() notification.ParseFromString(message.content) data = notification.block_id elif type_tag == Message.CONSENSUS_NOTIFY_ENGINE_DEACTIVATED: self.stop() data = None elif type_tag == Message.PING_REQUEST: data = None else: raise exceptions.ReceiveError( 'Received unexpected message type: {}'.format(type_tag)) self._stream.send_back( message_type=Message.CONSENSUS_NOTIFY_ACK, correlation_id=message.correlation_id, content=consensus_pb2.ConsensusNotifyAck().SerializeToString()) return type_tag, data