Beispiel #1
0
def main(argv=sys.argv):
    def usage(argv):
        cmd = os.path.basename(argv[0])
        print('usage: %s <config_uri> <network name>\n'
              '(example: "%s conf/production.ini testnet")' % (cmd, cmd))
        sys.exit(1)

    if len(argv) < 2:
        usage(argv)

    config_uri = argv[1]
    network_name = argv[2]

    # console_app sets up colored log output
    from websauna.system.devop.cmdline import init_websauna
    request = init_websauna(config_uri, sanity_check=True)

    services = ServiceCore.parse_network_config(request)
    one_shot = OneShot(request,
                       network_name,
                       services[network_name],
                       require_unlock=False)
    one_shot.setup()

    coinbase = one_shot.web3.eth.coinbase

    pw = getpass("Give password to unlock {} on {}:".format(
        coinbase, network_name))

    one_shot.web3.personal.unlockAccount(coinbase, pw, 30 * 24 * 3600)

    print("Unlock complete")
    sys.exit(0)
Beispiel #2
0
def setup_house(request):
    """Setup different networks supported by the instance.

    Setup house wallets on each network.

    Setup ETH giveaway on testnet and private testnet.
    """

    dbsession = request.dbsession

    services = ServiceCore.parse_network_config(request)
    networks = services.keys()

    for network_name in networks:

        print("Setting up house wallet on ", network_name)

        network = get_eth_network(dbsession, network_name)
        assert is_network_alive(network), "Network was dead when we started to create address {}".format(network)

        dbsession.flush()
        house_address = get_house_address(network)

        if not house_address:
            create_house_address(network)

        if not "initial_assets" in network.other_data:
            network.other_data["initial_assets"] = {}

        if network_name == "testnet":
            network.other_data["human_friendly_name"] = "Ethereum Testnet"

        # Setup testnet ETH give away
        if network_name in ("testnet", "private testnet"):
            network.other_data["initial_assets"]["eth_amount"] = "5.0"
Beispiel #3
0
def setup_house(request):
    """Setup different networks supported by the instance.

    Setup house wallets on each network.

    Setup ETH giveaway on testnet and private testnet.
    """

    dbsession = request.dbsession

    services = ServiceCore.parse_network_config(request)
    networks = services.keys()

    for network_name in networks:

        print("Setting up house wallet on ", network_name)

        network = get_eth_network(dbsession, network_name)
        assert is_network_alive(network), "Network was dead when we started to create address {}".format(network)

        dbsession.flush()
        house_address = get_house_address(network)

        if not house_address:
            create_house_address(network)

        if not "initial_assets" in network.other_data:
            network.other_data["initial_assets"] = {}

        if network_name == "testnet":
            network.other_data["human_friendly_name"] = "Ethereum Testnet"

        # Setup testnet ETH give away
        if network_name in ("testnet", "private testnet"):
            network.other_data["initial_assets"]["eth_amount"] = "5.0"
Beispiel #4
0
def ensure_networks_online(request):
    """Give time to ethereum-service process to catch up.

    Don't go forward until we have confirmed that Ethereum service is alive and talking to us.
    """
    services = ServiceCore.parse_network_config(request)
    networks = services.keys()

    while True:
        remaining_networks = []
        network_stats = []

        for network_name in networks:
            with transaction.manager:
                network = get_eth_network(request.dbsession, network_name)
                if not is_network_alive(network):
                    remaining_networks.append(network_name)
                    network_stats.append(dump_network_heartbeat(network))

        if remaining_networks:
            print("Waiting ethereum-service to wake up for networks ", network_stats)
            time.sleep(15)
            networks = remaining_networks
        else:
            break

    print("All networks green")
Beispiel #5
0
def ensure_networks_online(request):
    """Give time to ethereum-service process to catch up.

    Don't go forward until we have confirmed that Ethereum service is alive and talking to us.
    """
    services = ServiceCore.parse_network_config(request)
    networks = services.keys()

    while True:
        remaining_networks = []
        network_stats = []

        for network_name in networks:
            with transaction.manager:
                network = get_eth_network(request.dbsession, network_name)
                if not is_network_alive(network):
                    remaining_networks.append(network_name)
                    network_stats.append(dump_network_heartbeat(network))

        if remaining_networks:
            print("Waiting ethereum-service to wake up for networks ", network_stats)
            time.sleep(15)
            networks = remaining_networks
        else:
            break

    print("All networks green")
Beispiel #6
0
def main(argv=sys.argv):

    def usage(argv):
        cmd = os.path.basename(argv[0])
        print('usage: %s <config_uri> <network name>\n'
              '(example: "%s conf/production.ini testnet")' % (cmd, cmd))
        sys.exit(1)

    if len(argv) < 2:
        usage(argv)

    config_uri = argv[1]
    network_name = argv[2]

    # console_app sets up colored log output
    from websauna.system.devop.cmdline import init_websauna
    request = init_websauna(config_uri, sanity_check=True)

    services = ServiceCore.parse_network_config(request)
    one_shot = OneShot(request, network_name, services[network_name], require_unlock=False)
    one_shot.setup()

    coinbase = one_shot.web3.eth.coinbase

    pw = getpass("Give password to unlock {} on {}:".format(coinbase, network_name))

    one_shot.web3.personal.unlockAccount(coinbase, pw, 30*24*3600)

    print("Unlock complete")
    sys.exit(0)
Beispiel #7
0
def post_network_stats(self: Task):
    request = self.request.request
    dbsession = request.dbsession
    services = ServiceCore.parse_network_config(request)
    for network in dbsession.query(AssetNetwork).all():
        if network.name in services.keys():
            stats = dump_network_heartbeat(network)
            request.registry.notify(NetworkStats(request, network.name, stats))
Beispiel #8
0
def post_network_stats(self: Task):
    request = self.request.request
    dbsession = request.dbsession
    services = ServiceCore.parse_network_config(request)
    for network in dbsession.query(AssetNetwork).all():
        if network.name in services.keys():
            stats = dump_network_heartbeat(network)
            request.registry.notify(NetworkStats(request, network.name, stats))
Beispiel #9
0
def update_networks(self: Task):
    """Update all incoming and outgoing events from a network through Celery.

    Offer an alternative for runnign standalone ethereum-service.
    """

    request = self.request.request
    redis = get_redis(request)

    # Get list of configured networks
    services = ServiceCore.parse_network_config(request)

    for network_name in services.keys():
        # Update each network separately and have a lock to ensure we don't
        # accidentally do two overlapping update runs
        # https://pypi.python.org/pypi/python-redis-lock
        lock = redis_lock.Lock(redis, "network-update-lock-{}".format(network_name))

        if not lock.acquire(blocking=False):
            # This network is still procesing pending operations from the previous task run

            lock_acquired_at = redis.get("network-update-lock-started-{}".format(network_name))
            lock_acquired_by = redis.get("network-update-lock-started-by-{}".format(network_name))

            if lock_acquired_by:
                lock_acquired_by = lock_acquired_by.decode("utf-8")

            if lock_acquired_at:
                try:
                    friendly_at = datetime.datetime.utcfromtimestamp(lock_acquired_at)
                except:
                    friendly_at = 0

                diff = time.time() - float(lock_acquired_at)
                if diff > BAD_LOCK_TIMEOUT:
                    logger.warn("Failed to get wallet update lock on %s network when doing update_networks for %f seconds, originally acquired by %s at %s", network_name, diff, friendly_at, lock_acquired_by)

            continue

        lock.release()

        with lock:
            redis.set("network-update-lock-started-{}".format(network_name), time.time())
            redis.set("network-update-lock-started-by-{}".format(network_name), "process: {} thread:{}".format(os.getpid(), threading.current_thread()))

            logger.info("Updating network %s", network_name)
            start = time.time()
            one_shot = OneShot(request, network_name, services[network_name])
            one_shot.run_shot()
            logger.info("Updated network %s in %d seconds", network_name, time.time() - start)

            redis.delete("network-update-lock-started-{}".format(network_name))
            redis.delete("network-update-lock-started-by-{}".format(network_name))

            request.registry.notify(ServiceUpdated(request, network_name, time.time() - start))
Beispiel #10
0
def main(argv=sys.argv):

    def usage(argv):
        cmd = os.path.basename(argv[0])
        print('usage: %s <config_uri> <network name>\n'
              '(example: "%s conf/production.ini")' % (cmd, cmd))
        sys.exit(1)

    if len(argv) < 2:
        usage(argv)

    config_uri = argv[1]

    # console_app sets up colored log output
    from websauna.system.devop.cmdline import init_websauna
    request = init_websauna(config_uri, sanity_check=True)

    # Get list of configured networks
    services = ServiceCore.parse_network_config(request)
    redis = get_redis(request)

    for network_name in services.keys():
        # Update each network separately and have a lock to ensure we don't
        # accidentally do two overlapping update runs
        # https://pypi.python.org/pypi/python-redis-lock
        lock = redis_lock.Lock(redis, "network-update-lock-{}".format(network_name))

        if not lock.acquire(blocking=False):
            # This network is still procesing pending operations from the previous task run
            print("Lock {} is blocked, reseting".format(network_name))
            lock.reset()
        else:
            lock.release()

    print("Unlock complete")
    sys.exit(0)
Beispiel #11
0
def main(argv=sys.argv):
    def usage(argv):
        cmd = os.path.basename(argv[0])
        print('usage: %s <config_uri> <network name>\n'
              '(example: "%s conf/production.ini")' % (cmd, cmd))
        sys.exit(1)

    if len(argv) < 2:
        usage(argv)

    config_uri = argv[1]

    # console_app sets up colored log output
    from websauna.system.devop.cmdline import init_websauna
    request = init_websauna(config_uri, sanity_check=True)

    # Get list of configured networks
    services = ServiceCore.parse_network_config(request)
    redis = get_redis(request)

    for network_name in services.keys():
        # Update each network separately and have a lock to ensure we don't
        # accidentally do two overlapping update runs
        # https://pypi.python.org/pypi/python-redis-lock
        lock = redis_lock.Lock(redis,
                               "network-update-lock-{}".format(network_name))

        if not lock.acquire(blocking=False):
            # This network is still procesing pending operations from the previous task run
            print("Lock {} is blocked, reseting".format(network_name))
            lock.reset()
        else:
            lock.release()

    print("Unlock complete")
    sys.exit(0)
Beispiel #12
0
def update_networks(self: Task):
    """Update all incoming and outgoing events from a network through Celery.

    Offer an alternative for runnign standalone ethereum-service.
    """

    request = self.request.request
    redis = get_redis(request)

    # Get list of configured networks
    services = ServiceCore.parse_network_config(request)

    for network_name in services.keys():
        # Update each network separately and have a lock to ensure we don't
        # accidentally do two overlapping update runs
        # https://pypi.python.org/pypi/python-redis-lock
        lock = redis_lock.Lock(redis, "network-update-lock-{}".format(network_name))

        if not lock.acquire(blocking=False):
            # This network is still procesing pending operations from the previous task run

            lock_acquired_at = redis.get("network-update-lock-started-{}".format(network_name))
            lock_acquired_by = redis.get("network-update-lock-started-by-{}".format(network_name))

            if lock_acquired_by:
                lock_acquired_by = lock_acquired_by.decode("utf-8")

            if lock_acquired_at:
                try:
                    friendly_at = datetime.datetime.utcfromtimestamp(lock_acquired_at)
                except:
                    friendly_at = 0

                diff = time.time() - float(lock_acquired_at)
                if diff > BAD_LOCK_TIMEOUT:
                    logger.warn(
                        "Failed to get wallet update lock on %s network when doing update_networks for %f seconds, originally acquired by %s at %s",
                        network_name,
                        diff,
                        friendly_at,
                        lock_acquired_by,
                    )

            continue

        lock.release()

        with lock:
            redis.set("network-update-lock-started-{}".format(network_name), time.time())
            redis.set(
                "network-update-lock-started-by-{}".format(network_name),
                "process: {} thread:{}".format(os.getpid(), threading.current_thread()),
            )

            logger.info("Updating network %s", network_name)
            start = time.time()
            one_shot = OneShot(request, network_name, services[network_name])
            one_shot.run_shot()
            logger.info("Updated network %s in %d seconds", network_name, time.time() - start)

            request.registry.notify(ServiceUpdated(request, network_name, time.time() - start))