def set_nodes(role, id_with_key, hop, build_tunnels=False): on_start = [('started', )] if build_tunnels: on_start.append(('build_tunnels', hop)) configuration = get_default_configuration() configuration['keys'] = [{ 'alias': "my peer", 'generation': u"curve25519", # 'file': u"ec_{1}{0}.pem".format(*id_with_key) 'file': u"ec{}_{}_{!r}.pem".format(*((id_with_key) + (os.urandom(2), ))) }] configuration['logger'] = {'level': 'ERROR'} configuration['overlays'] = [{ 'class': role, 'key': "my peer", 'walkers': [{ 'strategy': "RandomWalk", 'peers': 10, 'init': { 'timeout': 3.0 } }], 'hops': hop, 'initialize': {}, 'on_start': on_start }] IPv8(configuration)
def set_nodes(role, id_with_key): configuration = get_default_configuration() configuration['keys'] = [{ 'alias': "my peer", 'generation': u"curve25519", # 'file': u"ec_{1}{0}.pem".format(*id_with_key) 'file': u"ec{}_{}_{!r}.pem".format(*((id_with_key) + (str(uuid.uuid4()), ))) }] configuration['logger'] = {'level': 'ERROR'} configuration['overlays'] = [{ 'class': role, 'key': "my peer", 'walkers': [{ 'strategy': "RandomWalk", 'peers': 10, 'init': { 'timeout': 3.0 } }], 'initialize': {}, 'on_start': [('started', )] }] IPv8(configuration)
def trust_client(): _COMMUNITIES['TrustClient'] = TrustClient for i in [4]: configuration = get_default_configuration() configuration['keys'] = [{ 'alias': "my peer", 'generation': u"curve25519", 'file': u"ec%d.pem" % i }] configuration['overlays'] = [{ 'class': 'TrustClient', 'key': "my peer", 'walkers': [{ 'strategy': "RandomWalk", 'peers': 10, 'init': { 'timeout': 3.0 } }], 'initialize': {}, 'on_start': [('started', )] }] IPv8(configuration)
async def start_communities(): for i, v in enumerate(["site", "service", "app"]): configuration = get_default_configuration() configuration['logger']['level'] = "ERROR" configuration['keys'] = [ {'alias': "anonymous id", 'generation': u"curve25519", 'file': u"certs/ec%d_multichain.pem" % i}, {'alias': i, 'generation': u"medium", 'file': u"certs/ec%d.pem" % i} ] # Only load the basic communities requested_overlays = ['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': 'db/state_%d' % i} # Start the IPv8 service ipv8 = IPv8(configuration) await ipv8.start() rest_manager = RESTManager(ipv8) await rest_manager.start(14410 + i, '0.0.0.0') # Print the peer for reference print("Starting " + v, b64encode(ipv8.keys["anonymous id"].mid))
def server(): _COMMUNITIES['Server'] = Server for i in [1]: configuration = get_default_configuration() configuration['keys'] = [{ 'alias': "my peer2", 'generation': u"curve25519", 'file': u"ec%d.pem" % i }] configuration['logger'] = {'level': 'DEBUG'} configuration['overlays'] = [{ 'class': 'Server', 'key': "my peer2", 'walkers': [{ 'strategy': "RandomWalk", 'peers': 10, 'init': { 'timeout': 3.0 } }], 'initialize': {}, 'on_start': [('started', )] }] IPv8(configuration)
async def start_communities(): bootstrap_ips = [] for i in range(2): # We start two bootstrap nodes bootstrap_endpoint = SimulationEndpoint() await bootstrap_endpoint.open() bootstrap_overlay = EndpointServer(bootstrap_endpoint) # We assume all peers in our simulation have a public WAN IP (to avoid conflicts with our SimulationEndpoint) bootstrap_overlay.my_estimated_lan = ("0.0.0.0", 0) bootstrap_overlay.my_estimated_wan = bootstrap_endpoint.wan_address bootstrap_ips.append(bootstrap_endpoint.wan_address) instances = [] for i in range(20): builder = ConfigBuilder().clear_keys().clear_overlays() builder.add_key("my peer", "medium", f"ec{i}.pem") bootstrappers = [ BootstrapperDefinition(Bootstrapper.DispersyBootstrapper, { "ip_addresses": bootstrap_ips, "dns_addresses": [] }) ] walkers = [WalkerDefinition(Strategy.RandomWalk, 10, {'timeout': 3.0})] builder.add_overlay("SimpleCommunity", "my peer", walkers, bootstrappers, {'id': i}, [('started', )]) endpoint = SimulationEndpoint() instance = IPv8(builder.finalize(), endpoint_override=endpoint, extra_communities={'SimpleCommunity': SimpleCommunity}) # We assume all peers in our simulation have a public WAN IP (to avoid conflicts with our SimulationEndpoint) instance.overlays[0].my_estimated_lan = ("0.0.0.0", 0) instance.overlays[0].my_estimated_wan = endpoint.wan_address await instance.start() instances.append(instance)
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, v in enumerate(["employee", "employer", "brewer"]): configuration = get_default_configuration() configuration['logger']['level'] = "ERROR" configuration['keys'] = [ {'alias': "anonymous id", 'generation': u"curve25519", 'file': u"certs/ec%d_multichain.pem" % i}, {'alias': i, 'generation': u"medium", 'file': u"certs/ec%d.pem" % i} ] # Only load the basic communities requested_overlays = ['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': 'db/state_%d' % i} # Start the IPv8 service ipv8 = IPv8(configuration) await ipv8.start() rest_manager = RESTManager(ipv8) await rest_manager.start(14410 + i) # Print the peer for reference print("Starting " + v, b64encode(ipv8.keys["anonymous id"].mid))
async def start_communities(): rest_port = 8000 ipv8_port = 8090 hostname = GATEWAY_HOSTNAME ip_address = GATEWAY_IP configuration = get_default_configuration() configuration['port'] = ipv8_port configuration['keys'] = [{ 'alias': "my peer", 'generation': u"curve25519", 'file': (f"/vol/keys/trustchain/ec.pem" if DOCKER else resolve_user("~/.ssh/eurotoken/trustchain/ec.pem")) }] configuration['address'] = ip_address configuration['logger'] = { 'level': "INFO", } configuration['overlays'] = [{ 'class': 'MyTrustChainCommunity', 'key': "my peer", 'walkers': [{ 'strategy': "RandomWalk", 'peers': 10, 'init': { 'timeout': 3.0 } }], 'initialize': { 'working_directory': (f'/vol/database' if DOCKER else f'.local') }, 'on_start': [('started', )] }, { 'class': 'EuroTokenCommunity', 'key': "my peer", 'walkers': [{ 'strategy': "RandomWalk", 'peers': 10, 'init': { 'timeout': 3.0 } }], 'initialize': {}, 'on_start': [('started', )] }] ipv8 = IPv8(configuration, extra_communities={ 'MyTrustChainCommunity': MyTrustChainCommunity, 'EuroTokenCommunity': EuroTokenCommunity }) await ipv8.start() interactor = buildSI(ipv8, hostname, ipv8_port) rest_manager = get_rest_manager(interactor) await rest_manager.start(ip_address, rest_port)
async def start_communities(): ipv8_port = 8090 settings = TrustChainSettings() settings.broadcast_fanout = 0 configuration = get_default_configuration() configuration['port'] = ipv8_port configuration['keys'] = [{ 'alias': "my peer", 'generation': u"curve25519", 'file': KEY_PATH }] configuration['address'] = "0.0.0.0" configuration['logger'] = { 'level': "WARNING", } configuration['overlays'] = [{ 'class': 'EvalTrustChainCommunity', 'key': "my peer", 'walkers': [{ 'strategy': "RandomWalk", 'peers': 10, 'init': { 'timeout': 3.0 } }], 'initialize': { 'working_directory': f'/vol/database', 'is_gateway': IS_GATEWAY, 'n_gateways': GATEWAYS, 'n_clients': CLIENTS, 'key_file': KEY_FILE, 'settings': settings, 'transactions_to_do': TRANSACTIONS_TO_DO, 'min_checkpoint_freq': CHECKPOINT_EVERY, }, 'on_start': [('started', )] }] global ipv8 ipv8 = IPv8( configuration, extra_communities={'EvalTrustChainCommunity': EvalTrustChainCommunity}) def stop(): global STOP STOP = True ipv8.get_overlay(EvalTrustChainCommunity).set_stop(stop) await ipv8.start()
async def start_community(): for peer_id in [1, 2]: configuration = get_default_configuration() 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))
def initialize_peer(path): configuration = get_default_configuration() configuration['logger'] = {'level': "ERROR"} configuration['walker_interval'] = 4 overlays = ['AttestationCommunity', 'IdentityCommunity'] configuration['overlays'] = [o for o in configuration['overlays'] if o['class'] in overlays] for o in configuration['overlays']: o['walkers'] = [{ 'strategy': "RandomWalk", 'peers': 4, 'init': { 'timeout': 60.0 } }] create_working_dir(path) os.chdir(path) ipv8 = IPv8(configuration) os.chdir(os.path.dirname(__file__)) rest_manager = TestRESTAPI(ipv8) rest_manager.start() return ipv8, "http://localhost:%d/attestation" % rest_manager.port
async def start_communities(): for i in [1, 2]: builder = ConfigBuilder().clear_keys().clear_overlays() # If we actually want to communicate between two different peers # we need to assign them different keys. # We will generate an EC key called 'my peer' which has 'medium' # security and will be stored in file 'ecI.pem' where 'I' is replaced # by the peer number (1 or 2). builder.add_key("my peer", "medium", f"ec{i}.pem") # Instruct IPv8 to load our custom overlay, registered in _COMMUNITIES. # We use the 'my peer' key, which we registered before. # We will attempt to find other peers in this overlay using the # RandomWalk strategy, until we find 10 peers. # We do not provide additional startup arguments or a function to run # once the overlay has been initialized. builder.add_overlay( "MyCommunity", "my peer", [WalkerDefinition(Strategy.RandomWalk, 10, {'timeout': 3.0})], default_bootstrap_defs, {}, []) ipv8 = IPv8(builder.finalize(), extra_communities={'MyCommunity': MyCommunity}) await ipv8.start()
async def start_communities(): instances = [] for i in [1, 2]: builder = ConfigBuilder().clear_keys().clear_overlays() builder.add_key("my peer", "medium", f"ec{i}.pem") builder.add_overlay("PingPongCommunity", "my peer", [], [], {}, [('started', )]) endpoint = SimulationEndpoint() instance = IPv8( builder.finalize(), endpoint_override=endpoint, extra_communities={'PingPongCommunity': PingPongCommunity}) await instance.start() instances.append(instance) # Introduce peers to each other for from_instance in instances: for to_instance in instances: if from_instance == to_instance: continue from_instance.overlays[0].walk_to(to_instance.endpoint.wan_address)
async def start_community(): for peer_id in [1, 2]: configuration = get_default_configuration() configuration['keys'] = [{ 'alias': "anonymous id", 'generation': u"curve25519", 'file': f"keyfile_{peer_id}.pem" }] configuration['working_directory'] = f"state_{peer_id}" configuration['overlays'] = [ overlay for overlay in configuration['overlays'] if overlay['class'] == 'HiddenTunnelCommunity' ] # Start the IPv8 service ipv8 = IPv8(configuration) await ipv8.start() ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) ssl_context.load_cert_chain(cert_fileX) rest_manager = RESTManager(ipv8) await rest_manager.start(14410 + peer_id, ssl_context=ssl_context) # Print the peer for reference print("Starting peer", b64encode(ipv8.keys["anonymous id"].mid))
_COMMUNITIES['MyCommunity'] = MyCommunity for i in [3]: configuration = get_default_configuration() configuration['keys'] = [{ 'alias': "my peer", 'generation': u"curve25519", 'file': u"ec%d.pem" % i }] # We provide the 'started' function to the 'on_start'. # We will call the overlay's 'started' function without any # arguments once IPv8 is initialized. configuration['overlays'] = [{ 'class': 'MyCommunity', 'key': "my peer", 'walkers': [{ 'strategy': "RandomWalk", 'peers': 10, 'init': { 'timeout': 3.0 } }], 'initialize': {}, 'on_start': [('started', )] }] IPv8(configuration) reactor.run()
async def start_peer(self, name, i): # Give the peer its own working directory workdir = u"%s/%s" % (tempdir, name) os.mkdir(workdir) # Set up its IPv8 Configuration configuration = get_default_configuration() configuration['logger']['level'] = "ERROR" configuration['keys'] = [{ 'alias': "anonymous id", 'generation': u"curve25519", 'file': u"%s/multichain.pem" % (workdir) }, { 'alias': "my peer", 'generation': u"medium", 'file': u"%s/ec.pem" % (workdir) }] # Only load the basic communities requested_overlays = [ 'AttestationCommunity', 'IdentityCommunity', 'DHTDiscoveryCommunity', 'DiscoveryCommunity' ] configuration['overlays'] = [ o for o in configuration['overlays'] if o['class'] in requested_overlays ] # Provide the working directory to its overlays working_directory_overlays = [ 'AttestationCommunity', 'IdentityCommunity', 'TrustChainCommunity' ] for overlay in configuration['overlays']: if overlay['class'] in working_directory_overlays: overlay['initialize'] = {'working_directory': workdir} # Start its IPv8 service ipv8 = IPv8(configuration) await ipv8.start() # Print the peer for reference port = self.first_port + i url = "http://localhost:%d" % port mid_b64 = b64encode(ipv8.keys["anonymous id"].mid).decode('utf-8') print("Starting peer %d (%s) running at %s" % (i, name, url)) print("- workdir: %s" % workdir) print("- mid_b64: %s" % mid_b64) print("- mid_hex: %s" % hexlify(ipv8.keys["anonymous id"].mid)) data = { 'port': port, 'mid_b64': mid_b64, } with open('%s/config.json' % workdir, 'w') as outfile: json.dump(data, outfile) outfile.close() # Start its API api = RESTManager(ipv8) await api.start(port) return (i, name, ipv8, api)
def start(self, options): """ Main method to startup the DAppCrowd service. """ 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 config["keys"]: key["file"] = os.path.join(options["statedir"], key["file"]) config['port'] = options["port"] self.ipv8 = IPv8(config) # Load TrustChain + DAppCrowd community my_peer = self.ipv8.overlays[0].my_peer tc_settings = TrustChainSettings() tc_settings.crawler = True dappcrowd_tc_community = DAppCrowdTrustchainCommunity( my_peer, self.ipv8.endpoint, self.ipv8.network, settings=tc_settings, working_directory=options["statedir"]) self.ipv8.overlays.append(dappcrowd_tc_community) self.ipv8.strategies.append((RandomWalk(dappcrowd_tc_community), 20)) self.ipfs_api = ipfsapi.connect('127.0.0.1', 5001) dappcrowd_community = DAppCrowdCommunity( my_peer, self.ipv8.endpoint, self.ipv8.network, trustchain=dappcrowd_tc_community, ipfs_api=self.ipfs_api) self.ipv8.overlays.append(dappcrowd_community) self.ipv8.strategies.append((RandomWalk(dappcrowd_community), 20)) 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 DAppCrowd") self.restapi = RESTManager(self.ipv8) self.restapi.start(options["apiport"]) self.restapi.root_endpoint.putChild( "dappcrowd", RootEndpoint(self.ipv8, self.ipfs_api))
async def start_ipv8(): # Create an IPv8 object with the default settings. ipv8 = IPv8(get_default_configuration()) await ipv8.start()