Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to stop mining after finding X blocks #192

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions cpu-miner.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ bool use_syslog = false;
static bool opt_background = false;
static bool opt_quiet = false;
static int opt_retries = -1;
static int opt_max_blocks = -1;
static int opt_fail_pause = 30;
int opt_timeout = 0;
static int opt_scantime = 5;
Expand Down Expand Up @@ -194,7 +195,9 @@ Options:\n\
--no-redirect ignore requests to change the URL of the mining server\n\
-q, --quiet disable per-thread hashmeter output\n\
-D, --debug enable debug output\n\
-P, --protocol-dump verbose dump of protocol-level activities\n"
-P, --protocol-dump verbose dump of protocol-level activities\n\
-X, --max-blocks=N maximum number of blocks to mine before exiting\n"

#ifdef HAVE_SYSLOG_H
"\
-S, --syslog use system log for output messages\n"
Expand All @@ -217,7 +220,7 @@ static char const short_options[] =
#ifdef HAVE_SYSLOG_H
"S"
#endif
"a:c:Dhp:Px:qr:R:s:t:T:o:u:O:V";
"a:c:Dhp:PxX:qr:R:s:t:T:o:u:O:V";

static struct option const options[] = {
{ "algo", 1, NULL, 'a' },
Expand All @@ -241,6 +244,7 @@ static struct option const options[] = {
{ "proxy", 1, NULL, 'x' },
{ "quiet", 0, NULL, 'q' },
{ "retries", 1, NULL, 'r' },
{ "max-blocks", 1, NULL, 'X' },
{ "retry-pause", 1, NULL, 'R' },
{ "scantime", 1, NULL, 's' },
#ifdef HAVE_SYSLOG_H
Expand Down Expand Up @@ -682,6 +686,11 @@ static void share_result(int result, const char *reason)
s,
result ? "(yay!!!)" : "(booooo)");

if (accepted_count >= opt_max_blocks) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This relies on subtleties of comparing signed and unsigned in C. You may want to make this code more explicit by doing e.g.:

    if (opt_max_blocks > 0 && accepted_count >= opt_max_blocks)

applog(LOG_DEBUG, "DEBUG: Maximum blocks found as per set program option, exiting.");
exit(0);
}

if (opt_debug && reason)
applog(LOG_DEBUG, "DEBUG: reject reason: %s", reason);
}
Expand Down Expand Up @@ -1717,6 +1726,12 @@ static void parse_arg(int key, char *arg, char *pname)
free(opt_proxy);
opt_proxy = strdup(arg);
break;
case 'X':
v = atoi(arg);
if (v < -1) /* sanity check */

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

atoi returns 0 on failure. You may want to detect that case here.

show_usage_and_exit(1);
opt_max_blocks = v;
break;
case 1001:
free(opt_cert);
opt_cert = strdup(arg);
Expand Down