示例#1
0
    async def start_ipv8(self, statistics, no_rest_api):
        """
        Main method to startup IPv8.
        """
        self.ipv8 = IPv8(get_default_configuration(), enable_statistics=statistics)
        await self.ipv8.start()

        async def signal_handler(sig):
            print("Received shut down signal %s" % sig)
            if not self._stopping:
                self._stopping = True
                if self.restapi:
                    await self.restapi.stop()
                await self.ipv8.stop()
                await gather(*all_tasks())
                get_event_loop().stop()

        signal.signal(signal.SIGINT, lambda sig, _: ensure_future(signal_handler(sig)))
        signal.signal(signal.SIGTERM, lambda sig, _: ensure_future(signal_handler(sig)))

        print("Starting IPv8")

        if not no_rest_api:
            self.restapi = RESTManager(self.ipv8)
            await self.restapi.start()
示例#2
0
    def start_crawler(self, options):
        """
        Main method to startup the TrustChain crawler.
        """
        root = logging.getLogger()
        root.setLevel(logging.INFO)

        stderr_handler = logging.StreamHandler(sys.stderr)
        stderr_handler.setLevel(logging.INFO)
        stderr_handler.setFormatter(
            logging.Formatter("%(asctime)s:%(levelname)s:%(message)s"))
        root.addHandler(stderr_handler)

        if options["statedir"]:
            # If we use a custom state directory, update various variables
            for key in crawler_config["keys"]:
                key["file"] = os.path.join(options["statedir"], key["file"])

            for community in crawler_config["overlays"]:
                if community["class"] == "TrustChainCommunity":
                    community["initialize"]["working_directory"] = options[
                        "statedir"]

        if 'testnet' in options and options['testnet']:
            for community in crawler_config["overlays"]:
                if community["class"] == "TrustChainCommunity":
                    community["class"] = "TrustChainTestnetCommunity"

        self.ipv8 = IPv8(crawler_config)

        def signal_handler(sig, _):
            msg("Received shut down signal %s" % sig)
            if not self._stopping:
                self._stopping = True
                if self.restapi:
                    self.restapi.stop()
                self.ipv8.stop()

                if options['yappi']:
                    yappi.stop()
                    msg("Yappi has shutdown")
                    yappi_stats = yappi.get_func_stats()
                    yappi_stats.sort("tsub")
                    out_file = 'yappi'
                    if options["statedir"]:
                        out_file = os.path.join(options["statedir"], out_file)
                    yappi_stats.save(out_file, type='callgrind')

        signal.signal(signal.SIGINT, signal_handler)
        signal.signal(signal.SIGTERM, signal_handler)

        msg("Starting TrustChain crawler")

        if not options['no-rest-api']:
            self.restapi = RESTManager(self.ipv8)
            reactor.callLater(0.0, self.restapi.start, options["apiport"])

        if options['yappi']:
            yappi.start(builtins=True)
示例#3
0
    async def start_ipv8(self, statedir, listen_port, statistics, no_rest_api):
        """
        Main method to startup IPv8.
        """
        configuration = get_default_configuration()
        configuration['port'] = listen_port

        # Open the database
        self.tc_persistence = TrustChainDB(statedir, 'trustchain')

        if statedir:
            # If we use a custom state directory, update various variables
            for key in configuration["keys"]:
                key["file"] = os.path.join(statedir, key["file"])

            for community in configuration["overlays"]:
                if community["class"] == "TrustChainCommunity":
                    community["initialize"]["persistence"] = self.tc_persistence

        allowed_overlays = ['DHTDiscoveryCommunity', 'DiscoveryCommunity', 'HiddenTunnelCommunity',
                            'TrustChainCommunity']
        configuration['overlays'] = [overlay for overlay in configuration['overlays']
                                     if overlay['class'] in allowed_overlays]

        for overlay in configuration['overlays']:
            if overlay['class'] == 'HiddenTunnelCommunity':
                overlay['initialize']['settings']['min_circuits'] = 0
                overlay['initialize']['settings']['max_circuits'] = 0
                overlay['initialize']['settings']['max_relays_or_exits'] = 1000
                overlay['initialize']['settings']['peer_flags'] = {PEER_FLAG_EXIT_IPV8}

        self.ipv8 = IPv8(configuration, enable_statistics=statistics)
        await self.ipv8.start()

        async def signal_handler(sig):
            print("Received shut down signal %s" % sig)
            if not self._stopping:
                self._stopping = True
                if self.restapi:
                    await self.restapi.stop()
                await self.ipv8.stop()
                await gather(*all_tasks())
                get_event_loop().stop()

        signal.signal(signal.SIGINT, lambda sig, _: ensure_future(signal_handler(sig)))
        signal.signal(signal.SIGTERM, lambda sig, _: ensure_future(signal_handler(sig)))

        print("Starting IPv8")

        if not no_rest_api:
            self.restapi = RESTManager(self.ipv8)
            await self.restapi.start()
示例#4
0
    async def start_ipv8(self, listen_port, statistics, no_rest_api):
        """
        Main method to startup IPv8.
        """
        configuration = get_default_configuration()
        configuration['port'] = listen_port

        allowed_overlays = [
            'DHTDiscoveryCommunity', 'DiscoveryCommunity',
            'HiddenTunnelCommunity', 'TrustChainCommunity'
        ]
        configuration['overlays'] = [
            overlay for overlay in configuration['overlays']
            if overlay['class'] in allowed_overlays
        ]

        for overlay in configuration['overlays']:
            if overlay['class'] == 'HiddenTunnelCommunity':
                overlay['initialize']['settings']['min_circuits'] = 0
                overlay['initialize']['settings']['max_circuits'] = 0
                overlay['initialize']['settings']['max_relays_or_exits'] = 1000
                overlay['initialize']['settings'][
                    'peer_flags'] = PEER_FLAG_EXIT_IPV8

        self.ipv8 = IPv8(configuration, enable_statistics=statistics)
        await self.ipv8.start()

        async def signal_handler(sig):
            print("Received shut down signal %s" % sig)
            if not self._stopping:
                self._stopping = True
                if self.restapi:
                    await self.restapi.stop()
                await self.ipv8.stop()
                await gather(*all_tasks())
                get_event_loop().stop()

        signal.signal(signal.SIGINT,
                      lambda sig, _: ensure_future(signal_handler(sig)))
        signal.signal(signal.SIGTERM,
                      lambda sig, _: ensure_future(signal_handler(sig)))

        print("Starting IPv8")

        if not no_rest_api:
            self.restapi = RESTManager(self.ipv8)
            await self.restapi.start()
示例#5
0
文件: main.py 项目: teddyk251/py-ipv8
async def start_community():
    for peer_id in [1, 2]:
        configuration = get_default_configuration()
        configuration['logger']['level'] = "ERROR"
        configuration['keys'] = [{'alias': "anonymous id",
                                  'generation': u"curve25519",
                                  'file': f"keyfile_{peer_id}.pem"}]
        configuration['working_directory'] = f"state_{peer_id}"
        configuration['overlays'] = []

        # Start the IPv8 service
        ipv8 = IPv8(configuration)
        await ipv8.start()
        rest_manager = RESTManager(ipv8)
        await rest_manager.start(14410 + peer_id)

        # Print the peer for reference
        print("Starting peer", b64encode(ipv8.keys["anonymous id"].mid))
示例#6
0
    def start_ipv8(self, options):
        """
        Main method to startup IPv8.
        """
        configuration = get_default_configuration()

        configuration['port'] = options["listen_port"]

        allowed_overlays = [
            'DHTDiscoveryCommunity', 'DiscoveryCommunity',
            'HiddenTunnelCommunity', 'TrustChainCommunity'
        ]
        configuration['overlays'] = [
            overlay for overlay in configuration['overlays']
            if overlay['class'] in allowed_overlays
        ]

        for overlay in configuration['overlays']:
            if overlay['class'] == 'HiddenTunnelCommunity':
                overlay['initialize']['settings']['min_circuits'] = 0
                overlay['initialize']['settings']['max_circuits'] = 0
                overlay['initialize']['settings']['max_relays_or_exits'] = 1000
                overlay['initialize']['settings'][
                    'peer_flags'] = PEER_FLAG_EXIT_IPV8

        self.ipv8 = IPv8(configuration,
                         enable_statistics=options['statistics'])

        def signal_handler(sig, _):
            msg("Received shut down signal %s" % sig)
            if not self._stopping:
                self._stopping = True
                if self.restapi:
                    self.restapi.stop()
                self.ipv8.stop()

        signal.signal(signal.SIGINT, signal_handler)
        signal.signal(signal.SIGTERM, signal_handler)

        msg("Starting IPv8")

        if not options['no-rest-api']:
            self.restapi = RESTManager(self.ipv8)
            reactor.callLater(0.0, self.restapi.start)
示例#7
0
    async def start_ipv8(self, statistics, no_rest_api, api_key, cert_file):
        """
        Main method to startup IPv8.
        """
        self.ipv8 = IPv8(get_default_configuration(),
                         enable_statistics=statistics)
        await self.ipv8.start()

        async def signal_handler(sig):
            print("Received shut down signal %s" % sig)
            if not self._stopping:
                self._stopping = True
                if self.restapi:
                    await self.restapi.stop()
                await self.ipv8.stop()
                await gather(*all_tasks())
                get_event_loop().stop()

        signal.signal(signal.SIGINT,
                      lambda sig, _: ensure_future(signal_handler(sig)))
        signal.signal(signal.SIGTERM,
                      lambda sig, _: ensure_future(signal_handler(sig)))

        print("Starting IPv8")

        if not no_rest_api:
            # Load the certificate/key file. A new one can be generated as follows:
            # openssl req \
            #     -newkey rsa:2048 -nodes -keyout private.key \
            #     -x509 -days 365 -out certfile.pem
            # cat private.key >> certfile.pem
            # rm private.key
            ssl_context = None
            if cert_file:
                ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
                ssl_context.load_cert_chain(cert_file)

            self.restapi = RESTManager(self.ipv8)
            await self.restapi.start(api_key=api_key, ssl_context=ssl_context)
示例#8
0
    def start_ipv8(self, options):
        """
        Main method to startup IPv8.
        """
        self.ipv8 = IPv8(get_default_configuration())

        def signal_handler(sig, _):
            msg("Received shut down signal %s" % sig)
            if not self._stopping:
                self._stopping = True
                if self.restapi:
                    self.restapi.stop()
                self.ipv8.stop()

        signal.signal(signal.SIGINT, signal_handler)
        signal.signal(signal.SIGTERM, signal_handler)

        msg("Starting IPv8")

        if not options['no-rest-api']:
            self.restapi = RESTManager(self.ipv8)
            reactor.callLater(0.0, self.restapi.start)
示例#9
0
    async def create_node(self, *args, **kwargs):
        ipv8 = MockRestIPv8(u"curve25519",
                            overlay_classes=self.overlay_classes,
                            *args,
                            **kwargs)
        self.rest_manager = RESTManager(ipv8, root_endpoint_class=RootEndpoint)
        ipv8.rest_manager = self.rest_manager
        await self.rest_manager.start(0)
        ipv8.rest_port = self.rest_manager.site._server.sockets[0].getsockname(
        )[1]

        dum1_wallet = DummyWallet1()
        dum2_wallet = DummyWallet2()
        dum1_wallet.MONITOR_DELAY = 0
        dum2_wallet.MONITOR_DELAY = 0

        wallets = {'DUM1': dum1_wallet, 'DUM2': dum2_wallet}

        market_community = ipv8.get_overlay(MarketCommunity)
        market_community.wallets = wallets
        market_community.settings.single_trade = False
        market_community.clearing_policies = []

        return ipv8
示例#10
0
    def start_trust(self, options):
        """
        Main method to startup the trust.
        """
        root = logging.getLogger()
        root.setLevel(logging.INFO)

        stderr_handler = logging.StreamHandler(sys.stderr)
        stderr_handler.setLevel(logging.INFO)
        stderr_handler.setFormatter(
            logging.Formatter("%(asctime)s:%(levelname)s:%(message)s"))
        root.addHandler(stderr_handler)

        self.ipv8 = IPv8(configuration)

        # Peer
        my_peer = self.ipv8.keys.get('my peer')

        # trust community
        trust_community = TrustCommunity(my_peer, self.ipv8.endpoint,
                                         self.ipv8.network)
        self.ipv8.overlays.append(trust_community)
        self.ipv8.strategies.append((RandomWalk(trust_community), 10))

        def signal_handler(sig, _):
            msg("Received shut down signal %s" % sig)
            if not self._stopping:
                self._stopping = True
                self.restapi.stop()
                self.ipv8.stop()

        signal.signal(signal.SIGINT, signal_handler)
        signal.signal(signal.SIGTERM, signal_handler)

        self.restapi = RESTManager(self.ipv8)
        reactor.callLater(0.0, self.restapi.start, 8001)
示例#11
0
    async def start_crawler(self, statedir, apiport, no_rest_api, testnet,
                            yappi):
        """
        Main method to startup the TrustChain crawler.
        """

        # Open the database
        self.tc_persistence = TrustChainDB(statedir, 'trustchain')

        if statedir:
            # If we use a custom state directory, update various variables
            for key in crawler_config["keys"]:
                key["file"] = os.path.join(statedir, key["file"])

            for community in crawler_config["overlays"]:
                if community[
                        "class"] == "TrustChainCrawlerCommunity" or community[
                            "class"] == "TrustChainBackwardsCrawlerCommunity":
                    community["initialize"][
                        "persistence"] = self.tc_persistence

        if testnet:
            for community in crawler_config["overlays"]:
                if community["class"] == "TrustChainCommunity":
                    community["class"] = "TrustChainTestnetCommunity"

        extra_communities = {
            "TrustChainCrawlerCommunity":
            TrustChainCrawlerCommunity,
            "TrustChainBackwardsCrawlerCommunity":
            TrustChainBackwardsCrawlerCommunity
        }
        self.ipv8 = IPv8(crawler_config, extra_communities=extra_communities)

        await self.ipv8.start()

        async def signal_handler(sig):
            print("Received shut down signal %s" % sig)
            if not self._stopping:
                self._stopping = True
                if self.restapi:
                    await self.restapi.stop()
                await self.ipv8.stop()

                if yappi:
                    yappi.stop()
                    print("Yappi has shutdown")
                    yappi_stats = yappi.get_func_stats()
                    yappi_stats.sort("tsub")
                    out_file = 'yappi'
                    if statedir:
                        out_file = os.path.join(statedir, out_file)
                    yappi_stats.save(out_file, type='callgrind')

                await gather(*all_tasks())
                get_event_loop().stop()

        signal.signal(signal.SIGINT,
                      lambda sig, _: ensure_future(signal_handler(sig)))
        signal.signal(signal.SIGTERM,
                      lambda sig, _: ensure_future(signal_handler(sig)))

        print("Starting TrustChain crawler")

        if not no_rest_api:
            self.restapi = RESTManager(self.ipv8)
            await self.restapi.start(apiport)

        if yappi:
            yappi.start(builtins=True)
示例#12
0
    def start(self, options, service):
        """
        Main method to startup the cli and add a signal handler.
        """

        msg("Service: Starting")

        self.service = service

        # State directory
        state_directory = options['statedir']
        util.create_directory_if_not_exists(state_directory)

        # port
        network_port = options['port']

        # Initial configuration
        configuration = get_default_configuration()
        configuration['address'] = "0.0.0.0"
        configuration['port'] = network_port
        configuration['keys'] = [{
            'alias':
            'my peer',
            'generation':
            u"curve25519",
            'file':
            os.path.join(state_directory, u"ec.pem")
        }]
        configuration['logger'] = {'level': "ERROR"}
        configuration['overlays'] = [{
            'class':
            'DiscoveryCommunity',
            'key':
            "my peer",
            'walkers': [{
                'strategy': 'RandomWalk',
                'peers': -1,
                'init': {
                    'timeout': 3.0
                }
            }, {
                'strategy': 'RandomChurn',
                'peers': -1,
                'init': {
                    'sample_size': 64,
                    'ping_interval': 1.0,
                    'inactive_time': 1.0,
                    'drop_time': 3.0
                }
            }],
            'initialize': {},
            'on_start': [('resolve_dns_bootstrap_addresses', )]
        }]
        configuration['overlays'] = []

        # IPv8 instance
        self.ipv8 = IPv8(configuration)

        # Network port
        actual_network_port = self.ipv8.endpoint.get_address()[1]

        # Peer
        self.my_peer = self.ipv8.keys.get('my peer')

        # Trustchain community
        self.trustchain_community = TrustChainTestnetCommunity(
            self.my_peer,
            self.ipv8.endpoint,
            self.ipv8.network,
            working_directory=state_directory)
        self.ipv8.overlays.append(self.trustchain_community)
        self.ipv8.strategies.append((EdgeWalk(self.trustchain_community), 10))

        # Event bus
        self.bus = EventBus()

        # module community
        self.module_community = ModuleCommunity(
            self.my_peer,
            self.ipv8.endpoint,
            self.ipv8.network,
            trustchain=self.trustchain_community,
            bus=self.bus,
            working_directory=state_directory,
            ipv8=self.ipv8,
            service=self.service)
        self.ipv8.overlays.append(self.module_community)
        self.ipv8.strategies.append((RandomWalk(self.module_community), 10))

        # CLI
        self.cli = CLI(self, self.ipv8, self.module_community)

        def signal_handler(sig, _):
            msg("Service: Received shut down signal %s" % sig)
            if not self._stopping:
                self.stop()

        signal.signal(signal.SIGINT, signal_handler)
        signal.signal(signal.SIGTERM, signal_handler)

        self.rest_api = RESTManager(self.ipv8)
        self.rest_api.start(actual_network_port + 1000)
        self.rest_api.root_endpoint.putChild('module',
                                             ModuleRootEndpoint(self.ipv8))

        StandardIO(self.cli)
示例#13
0
class ModuleServiceMaker(object):
    implements(IServiceMaker, IPlugin)
    tapname = "module-loader"
    description = "module loader service"
    options = Options

    def __init__(self):
        """
        Initialize the variables of this service and the logger.
        """

        # Init service state
        self._stopping = False

        # Init variables
        self.service = None
        self.ipv8 = None
        self.my_peer = None
        self.discovery_community = None
        self.trustchain_community = None
        self.bus = None
        self.module_community = None
        self.cli = None
        self.rest_api = None

        # Setup logging
        root = logging.getLogger()
        root.setLevel(logging.INFO)

        stderr_handler = logging.StreamHandler(sys.stderr)
        stderr_handler.setLevel(logging.INFO)
        stderr_handler.setFormatter(
            logging.Formatter("%(asctime)s:%(levelname)s:%(message)s"))
        root.addHandler(stderr_handler)

    def start(self, options, service):
        """
        Main method to startup the cli and add a signal handler.
        """

        msg("Service: Starting")

        self.service = service

        # State directory
        state_directory = options['statedir']
        util.create_directory_if_not_exists(state_directory)

        # port
        network_port = options['port']

        # Initial configuration
        configuration = get_default_configuration()
        configuration['address'] = "0.0.0.0"
        configuration['port'] = network_port
        configuration['keys'] = [{
            'alias':
            'my peer',
            'generation':
            u"curve25519",
            'file':
            os.path.join(state_directory, u"ec.pem")
        }]
        configuration['logger'] = {'level': "ERROR"}
        configuration['overlays'] = [{
            'class':
            'DiscoveryCommunity',
            'key':
            "my peer",
            'walkers': [{
                'strategy': 'RandomWalk',
                'peers': -1,
                'init': {
                    'timeout': 3.0
                }
            }, {
                'strategy': 'RandomChurn',
                'peers': -1,
                'init': {
                    'sample_size': 64,
                    'ping_interval': 1.0,
                    'inactive_time': 1.0,
                    'drop_time': 3.0
                }
            }],
            'initialize': {},
            'on_start': [('resolve_dns_bootstrap_addresses', )]
        }]
        configuration['overlays'] = []

        # IPv8 instance
        self.ipv8 = IPv8(configuration)

        # Network port
        actual_network_port = self.ipv8.endpoint.get_address()[1]

        # Peer
        self.my_peer = self.ipv8.keys.get('my peer')

        # Trustchain community
        self.trustchain_community = TrustChainTestnetCommunity(
            self.my_peer,
            self.ipv8.endpoint,
            self.ipv8.network,
            working_directory=state_directory)
        self.ipv8.overlays.append(self.trustchain_community)
        self.ipv8.strategies.append((EdgeWalk(self.trustchain_community), 10))

        # Event bus
        self.bus = EventBus()

        # module community
        self.module_community = ModuleCommunity(
            self.my_peer,
            self.ipv8.endpoint,
            self.ipv8.network,
            trustchain=self.trustchain_community,
            bus=self.bus,
            working_directory=state_directory,
            ipv8=self.ipv8,
            service=self.service)
        self.ipv8.overlays.append(self.module_community)
        self.ipv8.strategies.append((RandomWalk(self.module_community), 10))

        # CLI
        self.cli = CLI(self, self.ipv8, self.module_community)

        def signal_handler(sig, _):
            msg("Service: Received shut down signal %s" % sig)
            if not self._stopping:
                self.stop()

        signal.signal(signal.SIGINT, signal_handler)
        signal.signal(signal.SIGTERM, signal_handler)

        self.rest_api = RESTManager(self.ipv8)
        self.rest_api.start(actual_network_port + 1000)
        self.rest_api.root_endpoint.putChild('module',
                                             ModuleRootEndpoint(self.ipv8))

        StandardIO(self.cli)

    def stop(self):
        self._stopping = True
        self.ipv8.stop()
        reactor.stop()

    def makeService(self, options):
        """
        Construct a IPv8 service.
        """

        module_service = MultiService()
        module_service.setName("module-loader")

        reactor.callWhenRunning(self.start, options, module_service)

        return module_service
示例#14
0
async def start_communities():
    # Launch two IPv8 services.
    # We run REST endpoints for these services on:
    #  - http://localhost:14411/
    #  - http://localhost:14412/
    # This script also prints the peer ids for reference with:
    #  - http://localhost:1441*/attestation?type=peers
    for i in [1, 2]:
        configuration = get_default_configuration()
        configuration['logger']['level'] = "ERROR"
        configuration['keys'] = [{
            'alias': "anonymous id",
            'generation': u"curve25519",
            'file': u"ec%d_multichain.pem" % i
        }]

        # Only load the basic communities
        configuration['overlays'] = [{
            'class':
            'IsolatedDiscoveryCommunity',
            'key':
            "anonymous id",
            'walkers': [{
                'strategy': "RandomWalk",
                'peers': 20,
                'init': {
                    'timeout': 3.0
                }
            }, {
                'strategy': "RandomChurn",
                'peers': -1,
                'init': {
                    'sample_size': 8,
                    'ping_interval': 10.0,
                    'inactive_time': 27.5,
                    'drop_time': 57.5
                }
            }, {
                'strategy': "PeriodicSimilarity",
                'peers': -1,
                'init': {}
            }],
            'bootstrappers': [DISPERSY_BOOTSTRAPPER],
            'initialize': {},
            'on_start': []
        }, {
            'class':
            'IsolatedAttestationCommunity',
            'key':
            "anonymous id",
            'walkers': [{
                'strategy': "RandomWalk",
                'peers': 20,
                'init': {
                    'timeout': 3.0
                }
            }],
            'bootstrappers': [DISPERSY_BOOTSTRAPPER],
            'initialize': {
                'working_directory': 'state_%d' % i
            },
            'on_start': []
        }, {
            'class':
            'IsolatedIdentityCommunity',
            'key':
            "anonymous id",
            'walkers': [{
                'strategy': "RandomWalk",
                'peers': 20,
                'init': {
                    'timeout': 3.0
                }
            }],
            'bootstrappers': [DISPERSY_BOOTSTRAPPER],
            'initialize': {
                'working_directory': 'state_%d' % i
            },
            'on_start': []
        }]

        # Start the IPv8 service
        ipv8 = IPv8(configuration,
                    extra_communities={
                        'IsolatedDiscoveryCommunity':
                        IsolatedDiscoveryCommunity,
                        'IsolatedAttestationCommunity':
                        IsolatedAttestationCommunity,
                        'IsolatedIdentityCommunity': IsolatedIdentityCommunity
                    })
        await ipv8.start()
        rest_manager = RESTManager(ipv8)
        await rest_manager.start(14410 + i)

        # Print the peer for reference
        print("Starting peer", b64encode(ipv8.keys["anonymous id"].mid))
示例#15
0
        'DiscoveryCommunity', 'AttestationCommunity', 'IdentityCommunity'
    ]
    configuration['overlays'] = [
        o for o in configuration['overlays']
        if o['class'] in requested_overlays
    ]

    # Give each peer a separate working directory
    working_directory_overlays = ['AttestationCommunity', 'IdentityCommunity']
    for overlay in configuration['overlays']:
        if overlay['class'] in working_directory_overlays:
            overlay['initialize'] = {'working_directory': 'temp/state_%d' % i}

    # Start the IPv8 service
    ipv8 = IPv8(configuration)
    rest_manager = RESTManager(ipv8)
    rest_manager.start(14410 + i)

    # Print the peer for reference
    print("Starting peer", i)
    print("port", (14410 + i))
    print("mid_b64", b64encode(ipv8.keys["anonymous id"].mid))
    print("mid_hex", hexlify(ipv8.keys["anonymous id"].mid))

    data[names[i - 1]] = {
        'port': 14410 + i,
        'mid_b64': b64encode(ipv8.keys["anonymous id"].mid),
        'mid_hex': hexlify(ipv8.keys["anonymous id"].mid)
    }

with open('temp/peers.json', 'w') as outfile:
]

# Give a working directory.
working_directory_overlays = ['AttestationCommunity', 'IdentityCommunity']
for overlay in configuration['overlays']:
    if overlay['class'] in working_directory_overlays:
        overlay['initialize'] = {'working_directory': files_dir}

# Override one endpoint in the RootEndpoint.
root_endpoint = modules["ipv8.REST.root_endpoint"]
root_endpoint.AttestationEndpoint = CertificateEndpoint
modules["ipv8.REST.rest_manager"].RootEndpoint = root_endpoint.RootEndpoint

# SetUp the certification service.
ipv8 = IPv8(configuration)
rest_manager = RESTManager(ipv8)
main_loop = new_event_loop()


def run_in_thread(loop):
    # Start the secondary thread which caries the heavy work.
    set_event_loop(loop)
    ensure_future(start_communities())
    loop.run_forever()


async def start_communities():
    # Start the certification service.
    await ipv8.start()
    await rest_manager.start()