def __init__(self, app): self.config = app.config self.log('----------------------------------') self.log('config test:', config=self.config) self.address = privtopub_raw( decode_hex(self.config['node']['privkey_hex'])) super(ExampleService, self).__init__(app)
def rundummy(ctx, num_validators, node_num, seed): # reduce key derivation iterations PBKDF2_CONSTANTS['c'] = 100 config = ctx.obj['config'] # create bootstrap node priv_key and enode bootstrap_node_privkey = mk_privkey('%d:udp:%d' % (seed, 0)) bootstrap_node_pubkey = privtopub_raw(bootstrap_node_privkey) assert len(bootstrap_node_pubkey) == 64, len(bootstrap_node_pubkey) base_port = 29870 host = b'0.0.0.0' bootstrap_node = host_port_pubkey_to_uri(host, base_port, bootstrap_node_pubkey) config['discovery']['bootstrap_nodes'] = [bootstrap_node] # create this node priv_key config['node']['privkey_hex'] = mk_privkey('%d:udp:%d' % (seed, node_num)).encode('hex') # create validator addresses validators = [privtoaddr(mk_privkey('%d:account:%d' % (seed, i))) for i in range(num_validators)] config['hdc']['validators'] = validators # create this node account account = Account.new(password='', key=mk_privkey('%d:account:%d' % (seed, node_num))) # set ports based on node config['discovery']['listen_port'] = base_port + node_num config['p2p']['listen_port'] = base_port + node_num config['p2p']['min_peers'] = 2 config['jsonrpc']['listen_port'] += node_num _start_app(account, config)
def mk_enode(seed, node_num, host, port): print( host_port_pubkey_to_uri( host, port, privtopub_raw( mk_privkey( '%d:udp:%d' % (seed, node_num)))))
def parse_additional_bootstraps(bootstraps): retv = [] if not bootstraps: return retv for p in bootstraps.split(","): ip, port = p.split(":") seed = 0 privkey = sha3("{}:udp:{}:{}".format(seed, ip, port).encode("utf-8")) pubkey = privtopub_raw(privkey) enode = host_port_pubkey_to_uri(ip, port, pubkey) retv.append(enode) return retv
def setup_apps(app_class, service_class, num_nodes=3, seed=0, min_peers=2, max_peers=10, random_port=False): gevent.get_hub().SYSTEM_ERROR = BaseException if random_port: base_port = random.randint(10000, 60000) else: base_port = 29870 # Bootnode always use fixed port. So far only one bootnode per machine. if seed == 0: base_port = 29000 # get bootstrap node (node0) enode bootstrap_node_privkey = mk_privkey('{}:udp:{}'.format(0, 0).encode('utf-8')) bootstrap_node_pubkey = privtopub_raw(bootstrap_node_privkey) enode = host_port_pubkey_to_uri('0.0.0.0', 29000, bootstrap_node_pubkey) services = [NodeDiscovery, peermanager.PeerManager, service_class] # prepare config base_config = dict() for s in services: update_config_with_defaults(base_config, s.default_config) base_config['discovery']['bootstrap_nodes'] = [enode] base_config['seed'] = seed base_config['base_port'] = base_port base_config['num_nodes'] = num_nodes base_config['min_peers'] = min_peers base_config['max_peers'] = max_peers log.info('run:', base_config=base_config) # prepare apps apps = [] for node_num in range(num_nodes): app = create_app(node_num, base_config, services, app_class) apps.append(app) return apps
def run(app_class, service_class, num_nodes=3, seed=0, min_peers=2, max_peers=2, random_port=False): gevent.get_hub().SYSTEM_ERROR = BaseException if random_port: base_port = random.randint(10000, 60000) else: base_port = 29870 # get bootstrap node (node0) enode bootstrap_node_privkey = mk_privkey('%d:udp:%d' % (seed, 0)) bootstrap_node_pubkey = privtopub_raw(bootstrap_node_privkey) enode = host_port_pubkey_to_uri(b'0.0.0.0', base_port, bootstrap_node_pubkey) services = [NodeDiscovery, peermanager.PeerManager, service_class] # prepare config base_config = dict() for s in services: update_config_with_defaults(base_config, s.default_config) base_config['discovery']['bootstrap_nodes'] = [enode] base_config['seed'] = seed base_config['base_port'] = base_port base_config['num_nodes'] = num_nodes base_config['min_peers'] = min_peers base_config['max_peers'] = max_peers # prepare apps apps = [] for node_num in range(num_nodes): app = create_app(node_num, base_config, services, app_class) apps.append(app) # start apps serve_until_stopped(apps)
def __init__(self, app): Logger.info("Devp2pService init") self.config = app.config self.address = privtopub_raw( decode_hex(self.config["node"]["privkey_hex"])) super(Devp2pService, self).__init__(app)
def devp2p_app(env, network): seed = 0 gevent.get_hub().SYSTEM_ERROR = BaseException # get bootstrap node (node0) enode bootstrap_node_privkey = sha3("{}:udp:{}:{}".format( seed, env.cluster_config.P2P.BOOTSTRAP_HOST, env.cluster_config.P2P.BOOTSTRAP_PORT, ).encode("utf-8")) bootstrap_node_pubkey = privtopub_raw(bootstrap_node_privkey) enode = host_port_pubkey_to_uri( env.cluster_config.P2P.BOOTSTRAP_HOST, env.cluster_config.P2P.BOOTSTRAP_PORT, bootstrap_node_pubkey, ) services = [NodeDiscovery, peermanager.PeerManager, Devp2pService] # prepare config base_config = dict() for s in services: update_config_with_defaults(base_config, s.default_config) base_config["discovery"]["bootstrap_nodes"] = [ enode ] + parse_additional_bootstraps( env.cluster_config.P2P.ADDITIONAL_BOOTSTRAPS) base_config["seed"] = seed base_config["base_port"] = env.cluster_config.P2P.DISCOVERY_PORT base_config["min_peers"] = env.cluster_config.P2P.MIN_PEERS base_config["max_peers"] = env.cluster_config.P2P.MAX_PEERS min_peers = base_config["min_peers"] max_peers = base_config["max_peers"] assert min_peers <= max_peers config = copy.deepcopy(base_config) node_num = 0 config["node_num"] = env.cluster_config.P2P.DISCOVERY_PORT # create this node priv_key config["node"]["privkey_hex"] = encode_hex( sha3("{}:udp:{}:{}".format( seed, network.ip, env.cluster_config.P2P.DISCOVERY_PORT).encode("utf-8"))) # set ports based on node config["discovery"]["listen_port"] = env.cluster_config.P2P.DISCOVERY_PORT config["p2p"]["listen_port"] = env.cluster_config.P2P.DISCOVERY_PORT config["p2p"]["min_peers"] = min_peers config["p2p"]["max_peers"] = max_peers ip = network.ip config["client_version_string"] = "{}:{}".format(ip, network.port) app = Devp2pApp(config, network) Logger.info("create_app config={}".format(app.config)) # register services for service in services: assert issubclass(service, BaseService) if service.name not in app.config["deactivated_services"]: assert service.name not in app.services service.register_with_app(app) assert hasattr(app.services, service.name) serve_app(app)
def get_bootstrap_node(seed, base_port=29870, host=b"0.0.0.0"): # create bootstrap node priv_key and enode bootstrap_node_privkey = mk_privkey("%d:udp:%d" % (seed, 0)) bootstrap_node_pubkey = privtopub_raw(bootstrap_node_privkey) return host_port_pubkey_to_uri(host, base_port, bootstrap_node_pubkey)
def __init__(self, app): log.info("ExampleService init") self.config = app.config self.address = privtopub_raw( decode_hex(self.config["node"]["privkey_hex"])) super(ExampleService, self).__init__(app)
def main(): parser = argparse.ArgumentParser() parser.add_argument("--bootstrap_host", default="0.0.0.0", type=str) parser.add_argument("--bootstrap_port", default=29000, type=int) # p2p port for this node parser.add_argument("--node_port", default=29000, type=int) parser.add_argument("--node_num", default=0, type=int) parser.add_argument("--min_peers", default=2, type=int) parser.add_argument("--max_peers", default=10, type=int) seed = 0 args = parser.parse_args() gevent.get_hub().SYSTEM_ERROR = BaseException # get bootstrap node (node0) enode bootstrap_node_privkey = sha3("{}:udp:{}".format(0, 0).encode("utf-8")) bootstrap_node_pubkey = privtopub_raw(bootstrap_node_privkey) enode = host_port_pubkey_to_uri(args.bootstrap_host, args.bootstrap_port, bootstrap_node_pubkey) services = [NodeDiscovery, peermanager.PeerManager, ExampleService] # prepare config base_config = dict() for s in services: update_config_with_defaults(base_config, s.default_config) base_config["discovery"]["bootstrap_nodes"] = [enode] base_config["seed"] = seed base_config["base_port"] = args.node_port base_config["min_peers"] = args.min_peers base_config["max_peers"] = args.max_peers log.info("run:", base_config=base_config) min_peers = base_config["min_peers"] max_peers = base_config["max_peers"] assert min_peers <= max_peers config = copy.deepcopy(base_config) config["node_num"] = args.node_num # create this node priv_key config["node"]["privkey_hex"] = encode_hex( sha3("{}:udp:{}".format(seed, args.node_num).encode("utf-8"))) # set ports based on node config["discovery"]["listen_port"] = args.node_port config["p2p"]["listen_port"] = args.node_port config["p2p"]["min_peers"] = min(10, min_peers) config["p2p"]["max_peers"] = max_peers config["client_version_string"] = "NODE{}".format( args.node_num).encode('utf-8') app = ExampleApp(config) log.info("create_app", config=app.config) # register services for service in services: assert issubclass(service, BaseService) if service.name not in app.config["deactivated_services"]: assert service.name not in app.services service.register_with_app(app) assert hasattr(app.services, service.name) app_helper.serve_until_stopped([app])
def mk_enode(seed, node_num, host, port): print( host_port_pubkey_to_uri( host, port, privtopub_raw(mk_privkey('%d:udp:%d' % (seed, node_num)))))
def __init__(self, app): log.info('ExampleService init') self.config = app.config self.address = privtopub_raw(decode_hex(self.config['node']['privkey_hex'])) super(ExampleService, self).__init__(app)
def get_bootstrap_node(seed, base_port=29870, host=b'0.0.0.0'): # create bootstrap node priv_key and enode bootstrap_node_privkey = mk_privkey('%d:udp:%d' % (seed, 0)) bootstrap_node_pubkey = privtopub_raw(bootstrap_node_privkey) return host_port_pubkey_to_uri(host, base_port, bootstrap_node_pubkey)
def __init__(self, app): self.config = app.config self.address = privtopub_raw( self.config['node']['privkey_hex'].decode('hex')) super(ExampleService, self).__init__(app)