Exemplo n.º 1
0
 def get_most_profitable_miner(self):
     LOG.debug("Finding most profitable algo...")
     algo_info = self._get_algo_info()
     algo_info = self._filter_blacklisted_algos_from_algo_info(algo_info)
     algo_to_miners = self._create_miners_for_algo_info(algo_info)
     algo_to_benchmarks = self._get_benchmarks(algo_to_miners)
     best_algo = self._get_most_profitable_algo(algo_info, algo_to_benchmarks)
     return algo_to_miners[best_algo]
Exemplo n.º 2
0
    def stop_mining_and_return_when_stopped(self):
        LOG.debug("Terminating ccminer (%s)...", self.algo)
        term_proc(self.miner_proc)

        LOG.debug("Terminating logging thread for ccminer (%s)...", self.algo)
        self.logger_thread.join()
        self.miner_proc = None
        self.logger_thread = None
Exemplo n.º 3
0
def start():
    try:
        _parse_args_and_start_mining()
    except SystemExit as ex:
        raise ex
    except Exception:
        LOG.exception("Uncaught exception caused a program crash!")
        LOG.debug("Exiting...")
        sys.exit(1)
Exemplo n.º 4
0
    def get_most_profitable_miner(self):
        if self._calc_all_algo_data_sources:
            curr_src = self._algo_data_source
            other_src = "currency" if curr_src == "algo" else "algo"
            LOG.debug("Switching to source \"%s\"..." % other_src)
            self._algo_data_source = other_src
            super().get_most_profitable_miner()
            LOG.debug("Reverting to source \"%s\"..." % curr_src)
            self._algo_data_source = curr_src

        return super().get_most_profitable_miner()
Exemplo n.º 5
0
    def add_rate(self, rate):
        LOG.debug("Rate added: %s...", rate)

        if len(self._rates) > 0:
            if rate > self._rates[-1]:
                self._direction.append(1)
            elif rate == self._rates[-1]:
                self._direction.append(0.5)
            else:
                self._direction.append(0)

        self._rates.append(rate)
Exemplo n.º 6
0
    def _stdout_printer(stdout, name, share_cond, algo):
        for line in stdout:
            line = line.decode("UTF-8").strip()
            if "booooo" in line or "yes!" in line:
                share_cond.acquire()
                share_cond.notify_all()
                share_cond.release()
                speed = line.split(",")[-1].rpartition(" ")[0].strip()

                LOG.share(algo, "yes!" in line, Rate(speed))

            LOG.debug(line)
Exemplo n.º 7
0
    def _get_most_profitable_algo(self, algo_info, algo_to_benchmarks):
        def _get_prof(a):
            return algo_to_benchmarks[a.algo].get_mbtc_per_day(a.prof_str)

        sorted_algo_info = sorted(
            [a for a in algo_info if a.algo in algo_to_benchmarks],
            key=_get_prof,
            reverse=True,
        )
        for a in sorted_algo_info:
            LOG.debug("    Profitability of %s = %s mBTC / day", a.algo, _get_prof(a))

        return sorted_algo_info[0].algo
Exemplo n.º 8
0
    def _create_miners_for_algo_info(self, algo_info):
        LOG.debug("Prepping miners...")
        miners = {}
        algo_to_info = {a.algo: a for a in algo_info}
        supported_algos = get_supported_algos() & set(algo_to_info.keys())

        for algo in supported_algos:
            url = algo_to_info[algo].url
            port = algo_to_info[algo].port
            password = self._generate_password(algo)

            miners[algo] = get_miner_for_algo(algo, url, port, self._wallet, password)

        return miners
Exemplo n.º 9
0
def start_proc(cmd, pipe_stdout=False, preexec_fn=None):
    global pid_to_procs
    kwargs = {}
    if pipe_stdout:
        kwargs["stdout"] = PIPE
    if preexec_fn:
        kwargs["preexec_fn"] = preexec_fn

    p = Popen(cmd.split(" "), **kwargs)

    procs.append(p)

    LOG.debug("Started \"%s\" with pid of %i", cmd, p.pid)
    return p
Exemplo n.º 10
0
    def _wait(current_miner):
        start_time = time()
        LOG.debug("Sleeping for %i seconds...", MiningMonitor.CHECK_INTERVAL)

        # We have to busy wait because of some complex exit scenarios.
        while time(
        ) < start_time + MiningMonitor.CHECK_INTERVAL and current_miner.is_mining(
        ):
            if MiningMonitor._exit_status is not None:
                LOG.debug("Exiting program with status %i...",
                          MiningMonitor._exit_status)
                sys.exit(MiningMonitor._exit_status)

            sleep(0.01)
Exemplo n.º 11
0
    def mine(mining_group):
        LOG.info("Starting miner for \033[92m%s\033[0m!", mining_group)
        current_miner = None

        while True:
            LOG.debug("Finding most profitable miner for \033[92m%s\033[0m...",
                      mining_group)
            best_miner = mining_group.get_most_profitable_miner()
            LOG.debug("Found best miner: \033[92m%s\033[0m!", best_miner)

            if best_miner != current_miner:
                LOG.info("Switching to \033[92m%s\033[0m...", best_miner)
                MiningMonitor.switch_miner_and_return_when_started(
                    best_miner, current_miner)
                current_miner = best_miner
                LOG.info("Switch complete! Shares incoming...")

            MiningMonitor._check_file_descriptors()
            MiningMonitor._wait(current_miner)
Exemplo n.º 12
0
def term_proc(proc, term_wait_time=3):
    global pid_to_procs
    if proc.poll() is None:
        LOG.debug(
            "Terminating process %i and waiting up to %i seconds for it to end...",
            proc.pid,
            term_wait_time,
        )
        proc.terminate()
        try:
            proc.wait(term_wait_time)
        except TimeoutExpired:
            pass

    if proc.poll() is None:
        LOG.warning(
            "Process %i didn't terminate. Killing process and waiting until process exits...",
            proc.pid,
        )
        proc.kill()
        proc.wait()

    if proc.stdout:
        proc.stdout.close()

    if proc.stderr:
        proc.stder.close()

    if proc.stdin:
        proc.stdin.close()

    assert proc.poll(
    ) is not None, "Proc %i didn't terminate properly" % proc.pid

    LOG.debug("Process %i terminated with %i", proc.pid, proc.poll())
Exemplo n.º 13
0
def _parse_args_and_start_mining():
    _init_exit_handling()
    parser = ArgumentParser()
    parser.add_argument(
        "-c",
        type=str,
        dest="config_path",
        help="Specify the path to your mining config file as CONFIG_PATH.",
        required=True,
    )
    args = parser.parse_args()

    config_man = MiningConfigManager()

    main_config_loader = MinerConfigLoader()
    config_man.load_from_file(main_config_loader, args.config_path)
    LOG.set_level(config_man.get_config()["log_level"])

    mining_group_cls = config_man.get_config()["group"]
    group_config_loader = mining_group_cls.get_group_config_loader()
    config_man.load_from_file(group_config_loader, args.config_path)

    config = config_man.get_config()
    MiningMonitor.mine(mining_group_cls.init_from_config(config))
Exemplo n.º 14
0
    def benchmark(self):
        cmd = self._get_run_cmd(self.path_to_exec,
                                self.algo,
                                "",
                                "",
                                "",
                                "",
                                kwargs={
                                    "--benchmark": "",
                                    "--no-color": ""
                                })
        LOG.debug("Benchmarking \033[92m%s\033[0m...", self.algo)
        cache_key = "BENCHHR%s" % (cmd)
        cached_benchmark = MinerStore.get(cache_key)
        if cached_benchmark:
            b = Rate(cached_benchmark)
            LOG.debug("Benchmark found in cache: %s!", b)
            return b

        LOG.info("Benchmark not found for \033[92m%s\033[0m. Benchmarking...",
                 self.algo)

        bench_proc = start_proc(cmd, pipe_stdout=True)
        bm = Benchmarker()

        for line in bench_proc.stdout:
            line = line.strip().decode("UTF-8")

            if "Total:" in line:
                r = Rate(line.split(":")[-1].strip())
                bm.add_rate(r)

                final_hashrate = bm.get_benchmark()
                if final_hashrate:
                    break

        term_proc(bench_proc)

        MinerStore.set(cache_key, str(final_hashrate))
        LOG.info("Benchmark found: %s!", final_hashrate)

        return final_hashrate
Exemplo n.º 15
0
    def fetch_json_api(url, use_cache_on_failure=False):
        LOG.debug("Fetching %s...", url)
        num_tries = 0
        while True:
            resp_json = Fetcher._try_fetching_resp(url)
            LOG.debug("Response from %s: %s", url, resp_json)

            if resp_json:
                Fetcher._cache[url] = resp_json
                return resp_json
            elif use_cache_on_failure and url in Fetcher._cache:
                LOG.warn(
                    "Bad response from '%s\'! Falling back to cache...\033[0m\n",
                    url)
                return Fetcher._cache[url]

            num_tries += 1
            sleep_time = min(2**num_tries - 1, Fetcher._MAX_WAIT)
            LOG.warning(
                "Bad response from '%s\'! Backing off for %i second(s)...",
                url, sleep_time)
            sleep(sleep_time)
Exemplo n.º 16
0
def _signal_handler(signum, frame):
    LOG.debug('Signal handler called with signal %i', signum)
    term_all_procs()
    os._exit(128 + signum)
Exemplo n.º 17
0
 def _get_benchmarks(self, algo_to_miners):
     LOG.debug("Loading benchmarks from cache...")
     return {algo: miner.benchmark() for algo, miner in algo_to_miners.items()}
Exemplo n.º 18
0
 def _get_algo_info_from_algos(self):
     LOG.debug("Fetching algo info...")
     return self._status_to_algo_info(
         list(
             Fetcher.fetch_json_api(
                 "http://www.zpool.ca/api/status").values()))
Exemplo n.º 19
0
 def _get_algo_info_from_currencies(self):
     LOG.debug("Fetching currency info...")
     return self._currency_to_algo_info(
         list(
             Fetcher.fetch_json_api(
                 "http://www.zpool.ca/api/currencies").values()))
Exemplo n.º 20
0
    def _check_file_descriptors():
        warn_at = 20
        num_fds = psutil.Process().num_fds()

        if num_fds > warn_at:
            LOG.warn("%i file descriptors open!", num_fds)
Exemplo n.º 21
0
 def run(self):
     try:
         super().run()
     except Exception:
         LOG.exception("Thread %s crashed. Crashing program...", self.name)
         MiningMonitor.mark_monitor_for_exit(1)
Exemplo n.º 22
0
def term_all_procs(term_wait_time=1):
    global procs
    LOG.debug("Terminating all processes...")
    for p in procs:
        if p.poll() is None:
            term_proc(p, term_wait_time=term_wait_time)