Пример #1
0
def get_default_config(services):
    "collect default_config from services"
    config = dict()
    assert isinstance(services, list)
    for s in services:
        assert isinstance(s, (BaseService, BaseApp)) or issubclass(s, (BaseService, BaseApp))
        update_config_with_defaults(config, s.default_config)
    return config
Пример #2
0
def get_default_config(services):
    "collect default_config from services"
    config = dict()
    assert isinstance(services, list)
    for s in services:
        assert isinstance(s, (BaseService, BaseApp)) or issubclass(
            s, (BaseService, BaseApp))
        update_config_with_defaults(config, s.default_config)
    return config
Пример #3
0
def test_config_update():
    c = dict(a=dict(b=1), g=5)
    d = dict(a=dict(b=2, c=3), d=4, e=dict(f=1))
    r = dict(a=dict(b=1, c=3), d=4, e=dict(f=1), g=5)
    assert utils.update_config_with_defaults(c, d) == r
    assert c == r

    c = dict(a=dict(b=1), g=5, h=[], k=[2])
    d = dict(a=dict(b=2, c=3), d=4, e=dict(f=1, i=[1, 2]), j=[])
    r = dict(a=dict(b=1, c=3), d=4, e=dict(f=1, i=[1, 2]), j=[], g=5, h=[], k=[2])
    assert utils.update_config_with_defaults(c, d) == r
    assert c == r
Пример #4
0
def test_config_update():
    c = dict(a=dict(b=1), g=5)
    d = dict(a=dict(b=2, c=3), d=4, e=dict(f=1))
    r = dict(a=dict(b=1, c=3), d=4, e=dict(f=1), g=5)
    assert utils.update_config_with_defaults(c, d) == r
    assert c == r

    c = dict(a=dict(b=1), g=5, h=[], k=[2])
    d = dict(a=dict(b=2, c=3), d=4, e=dict(f=1, i=[1, 2]), j=[])
    r = dict(a=dict(b=1, c=3), d=4, e=dict(f=1, i=[1, 2]), j=[], g=5, h=[], k=[2])
    assert utils.update_config_with_defaults(c, d) == r
    assert c == r
Пример #5
0
 def __init__(self, app):
     Greenlet.__init__(self)
     self.is_stopped = False
     self.app = app
     self.config = utils.update_config_with_defaults(app.config, self.default_config)
     available_service = [s.__class__ for s in self.app.services.values()]
     for r in self.required_services:
         assert r in available_service, (r, available_service)
Пример #6
0
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
Пример #7
0
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)
Пример #8
0
def main():
    import gevent
    import signal

    client_name = 'exampleapp'
    version = '0.1'
    client_version = '%s/%s/%s' % (version, sys.platform,
                                   'py%d.%d.%d' % sys.version_info[:3])
    client_version_string = '%s/v%s' % (client_name, client_version)
    default_config = dict(BaseApp.default_config)
    update_config_with_defaults(default_config,
                                peermanager.PeerManager.default_config)
    update_config_with_defaults(default_config, NodeDiscovery.default_config)
    default_config['client_version_string'] = client_version_string
    default_config['post_app_start_callback'] = None
    default_config['node'][
        'privkey_hex'] = "b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291"
    default_config['discovery']['bootstrap_nodes'] = [
        "enode://762dd8a0636e07a54b31169eba0c7a20a1ac1ef68596f1f283b5c676bae4064abfcce24799d09f67e392632d3ffdc12e3d6430dcb0ea19c318343ffa7aae74d4@127.0.0.1:22332"
    ]
    app = BaseApp(default_config)
    peermanager.PeerManager.register_with_app(app)
    NodeDiscovery.register_with_app(app)

    gevent.get_hub(
    ).SYSTEM_ERROR = BaseException  # (KeyboardInterrupt, SystemExit, SystemError)

    app.start()

    # wait for interupt
    evt = gevent.event.Event()
    gevent.signal(signal.SIGQUIT, evt.set)
    gevent.signal(signal.SIGTERM, evt.set)
    gevent.signal(signal.SIGINT, evt.set)
    evt.wait()

    # finally stop
    app.stop()
Пример #9
0
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)
Пример #10
0
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])
Пример #11
0
 def __init__(self, config=default_config):
     self.config = utils.update_config_with_defaults(
         config, self.default_config)
     self.services = IterableUserDict()