Beispiel #1
0
 def setUp(self):
     super(TestP2PService, self).setUp()
     random.seed()
     # Mock saving keys as it takes long to compute and isn't needed here
     KeysAuth._save_private_key = mock.Mock()
     self.keys_auth = KeysAuth(self.path, 'priv_key', 'password')
     self.service = P2PService(None,
                               ClientConfigDescriptor(),
                               self.keys_auth,
                               connect_to_known_hosts=False)
Beispiel #2
0
 def setUp(self):
     super().setUp()
     self.keys_auth = KeysAuth(self.path, 'priv_key', 'password')
     self.service = P2PService(
         node=None,
         config_desc=ClientConfigDescriptor(),
         keys_auth=self.keys_auth,
         connect_to_known_hosts=False,
     )
     self.service.seeds = set()
Beispiel #3
0
    def test_react_to_hello(self):

        conn = MagicMock()
        conf = MagicMock()
        conf.opt_peer_num = 10

        node = Node(node_name='node', key='ffffffff')
        keys_auth = KeysAuth(self.path)
        keys_auth.key = node.key
        keys_auth.key_id = node.key

        peer_session = PeerSession(conn)
        peer_session.p2p_service = P2PService(node, conf, keys_auth, False)
        peer_session.p2p_service.metadata_manager = MagicMock()
        peer_session.send = MagicMock()
        peer_session.disconnect = MagicMock()
        peer_session._solve_challenge = MagicMock()

        def create_verify(value):
            def verify(*args):
                return value

            return verify

        key_id = 'deadbeef'
        peer_info = MagicMock()
        peer_info.key = key_id
        msg = MessageHello(port=1,
                           node_name='node2',
                           client_key_id=key_id,
                           node_info=peer_info,
                           proto_id=-1)

        peer_session.verify = create_verify(False)
        peer_session._react_to_hello(msg)
        peer_session.disconnect.assert_called_with(PeerSession.DCRUnverified)

        peer_session.verify = create_verify(True)
        peer_session._react_to_hello(msg)
        peer_session.disconnect.assert_called_with(
            PeerSession.DCRProtocolVersion)

        msg.proto_id = P2P_PROTOCOL_ID

        peer_session._react_to_hello(msg)
        assert key_id in peer_session.p2p_service.peers
        assert peer_session.p2p_service.peers[key_id]

        peer_session.p2p_service.peers[key_id] = MagicMock()
        conn.opened = True
        peer_session.key_id = None

        peer_session._react_to_hello(msg)
        peer_session.disconnect.assert_called_with(
            PeerSession.DCRDuplicatePeers)
Beispiel #4
0
    def test_react_to_stop_gossip(self):
        conn = MagicMock()
        conf = MagicMock()
        conf.opt_peer_num = 10

        node = Node(node_name='node', key='ffffffff')
        keys_auth = KeysAuth(self.path, 'priv_key', 'password')

        peer_session = PeerSession(conn)
        peer_session.p2p_service = P2PService(node, conf, keys_auth, False)
        peer_session.key_id = "NEW KEY_ID"
        peer_session._react_to_stop_gossip(message.p2p.StopGossip())
Beispiel #5
0
 def setUp(self):
     super().setUp()
     random.seed()
     self.peer_session = PeerSession(MagicMock())
     node = p2p_factories.Node()
     keys_auth = KeysAuth(self.path, 'priv_key', 'password')
     self.peer_session.conn.server = \
         self.peer_session.p2p_service = P2PService(
             node=node,
             config_desc=clientconfigdescriptor.ClientConfigDescriptor(),
             keys_auth=keys_auth,
             connect_to_known_hosts=False,
         )
     client = MagicMock()
     client.datadir = self.path
     with patch(
             'golem.network.concent.handlers_library.HandlersLibrary'
             '.register_handler',):
         self.peer_session.p2p_service.task_server = \
             task_server_factory.TaskServer(client=client)
Beispiel #6
0
    def test_interpret_metadata(self, *_):
        from golem.network.ipfs.daemon_manager import IPFSDaemonManager
        from golem.network.p2p.p2pservice import P2PService

        self.client = Client(datadir=self.path,
                             transaction_system=False,
                             connect_to_known_hosts=False,
                             use_docker_machine_manager=False)

        self.client.p2pservice = P2PService(MagicMock(),
                                            self.client.config_desc,
                                            self.client.keys_auth)
        self.client.ipfs_manager = IPFSDaemonManager()
        meta = self.client.get_metadata()
        assert meta and meta['ipfs']

        ip = '127.0.0.1'
        port = 40102

        node = MagicMock()
        node.prv_addr = ip
        node.prv_port = port

        self.client.interpret_metadata(meta, ip, port, node)
Beispiel #7
0
 def setUp(self):
     super(TestP2PService, self).setUp()
     random.seed()
     self.keys_auth = EllipticalKeysAuth(self.path)
     self.service = P2PService(None, ClientConfigDescriptor(), self.keys_auth, connect_to_known_hosts=False)
Beispiel #8
0
    def start_network(self):
        log.info("Starting network ...")
        self.node.collect_network_info(self.config_desc.seed_host,
                                       use_ipv6=self.config_desc.use_ipv6)
        log.debug("Is super node? %s", self.node.is_super_node())

        # self.ipfs_manager = IPFSDaemonManager(
        #    connect_to_bootstrap_nodes=self.connect_to_known_hosts)
        # self.ipfs_manager.store_client_info()

        self.p2pservice = P2PService(
            self.node,
            self.config_desc,
            self.keys_auth,
            connect_to_known_hosts=self.connect_to_known_hosts)
        self.task_server = TaskServer(
            self.node,
            self.config_desc,
            self.keys_auth,
            self,
            use_ipv6=self.config_desc.use_ipv6,
            use_docker_machine_manager=self.use_docker_machine_manager)

        dir_manager = self.task_server.task_computer.dir_manager

        log.info("Starting resource server ...")
        self.daemon_manager = HyperdriveDaemonManager(self.datadir)
        hyperdrive_ports = self.daemon_manager.start()

        resource_manager = HyperdriveResourceManager(dir_manager)
        self.resource_server = BaseResourceServer(resource_manager,
                                                  dir_manager, self.keys_auth,
                                                  self)

        def connect((p2p_port, task_port)):
            log.info('P2P server is listening on port %s', p2p_port)
            log.info('Task server is listening on port %s', task_port)

            dispatcher.send(signal='golem.p2p',
                            event='listening',
                            port=[p2p_port, task_port] +
                            list(hyperdrive_ports))

            listener = ClientTaskComputerEventListener(self)
            self.task_server.task_computer.register_listener(listener)
            self.p2pservice.connect_to_network()

            if self.monitor:
                self.diag_service.register(self.p2pservice,
                                           self.monitor.on_peer_snapshot)
                self.monitor.on_login()

        def terminate(*exceptions):
            log.error("Golem cannot listen on ports: %s", exceptions)
            self.quit()

        task = Deferred()
        p2p = Deferred()

        gatherResults([p2p, task],
                      consumeErrors=True).addCallbacks(connect, terminate)
        log.info("Starting p2p server ...")
        self.p2pservice.task_server = self.task_server
        self.p2pservice.set_resource_server(self.resource_server)
        self.p2pservice.set_metadata_manager(self)
        self.p2pservice.start_accepting(listening_established=p2p.callback,
                                        listening_failure=p2p.errback)

        log.info("Starting task server ...")
        self.task_server.start_accepting(listening_established=task.callback,
                                         listening_failure=task.errback)