Browse Source

Automatically add pool configured from bitcoin.conf for failover only

Luke Dashjr 11 years ago
parent
commit
8f3040eeb1
2 changed files with 88 additions and 0 deletions
  1. 1 0
      README
  2. 87 0
      miner.c

+ 1 - 0
README

@@ -260,6 +260,7 @@ Options for both config file and command line:
 --net-delay         Impose small delays in networking to avoid overloading slow routers
 --net-delay         Impose small delays in networking to avoid overloading slow routers
 --no-gbt            Disable getblocktemplate support
 --no-gbt            Disable getblocktemplate support
 --no-getwork        Disable getwork support
 --no-getwork        Disable getwork support
+--no-local-bitcoin  Disable adding pools for local bitcoin RPC servers
 --no-longpoll       Disable X-Long-Polling support
 --no-longpoll       Disable X-Long-Polling support
 --no-pool-redirect  Ignore pool requests to redirect to another server
 --no-pool-redirect  Ignore pool requests to redirect to another server
 --no-restart        Do not attempt to restart devices that hang
 --no-restart        Do not attempt to restart devices that hang

+ 87 - 0
miner.c

@@ -123,6 +123,7 @@ static char packagename[256];
 
 
 bool opt_protocol;
 bool opt_protocol;
 bool opt_dev_protocol;
 bool opt_dev_protocol;
+static bool opt_load_bitcoin_conf = true;
 static bool opt_benchmark;
 static bool opt_benchmark;
 static bool want_longpoll = true;
 static bool want_longpoll = true;
 static bool want_gbt = true;
 static bool want_gbt = true;
@@ -2207,6 +2208,9 @@ static struct opt_table opt_config_table[] = {
 	                opt_hidden
 	                opt_hidden
 #endif
 #endif
 	),
 	),
+	OPT_WITHOUT_ARG("--no-local-bitcoin",
+	                opt_set_invbool, &opt_load_bitcoin_conf,
+	                "Disable adding pools for local bitcoin RPC servers"),
 	OPT_WITHOUT_ARG("--no-longpoll",
 	OPT_WITHOUT_ARG("--no-longpoll",
 			opt_set_invbool, &want_longpoll,
 			opt_set_invbool, &want_longpoll,
 			"Disable X-Long-Polling support"),
 			"Disable X-Long-Polling support"),
@@ -10949,6 +10953,86 @@ out:
 }
 }
 #endif
 #endif
 
 
+static
+bool _add_local_gbt(const char * const filepath, void * __maybe_unused userp)
+{
+	struct pool *pool;
+	char buf[0x100];
+	char *rpcuser = NULL, *rpcpass = NULL;
+	int rpcport = 0, rpcssl = -101;
+	FILE * const F = fopen(filepath, "r");
+	if (!F)
+		applogr(false, LOG_WARNING, "%s: Failed to open %s for reading", "add_local_gbt", filepath);
+	
+	while (fgets(buf, sizeof(buf), F))
+	{
+		if (!strncasecmp(buf, "rpcuser=", 8))
+			rpcuser = trimmed_strdup(&buf[8]);
+		else
+		if (!strncasecmp(buf, "rpcpassword=", 12))
+			rpcpass = trimmed_strdup(&buf[12]);
+		else
+		if (!strncasecmp(buf, "rpcport=", 8))
+			rpcport = atoi(&buf[8]);
+		else
+		if (!strncasecmp(buf, "rpcssl=", 7))
+			rpcssl = atoi(&buf[7]);
+		else
+			continue;
+		if (rpcuser && rpcpass && rpcport && rpcssl != -101)
+			break;
+	}
+	
+	fclose(F);
+	
+	if (!rpcpass)
+	{
+		applog(LOG_DEBUG, "%s: Did not find rpcpassword in %s", "add_local_gbt", filepath);
+err:
+		free(rpcuser);
+		free(rpcpass);
+		goto out;
+	}
+	
+	if (!rpcport)
+		rpcport = 8332;
+	
+	if (rpcssl == -101)
+		rpcssl = 0;
+	
+	const int uri_sz = 0x30;
+	char * const uri = malloc(uri_sz);
+	snprintf(uri, uri_sz, "http%s://localhost:%d/#getcbaddr#allblocks", rpcssl ? "s" : "", rpcport);
+	
+	applog(LOG_DEBUG, "Local bitcoin RPC server on port %d found in %s", rpcport, filepath);
+	
+	pool = add_pool();
+	if (!pool)
+	{
+		applog(LOG_ERR, "%s: Error adding pool for bitcoin configured in %s", "add_local_gbt", filepath);
+		goto err;
+	}
+	
+	if (!rpcuser)
+		rpcuser = "";
+	
+	pool->quota = 0;
+	adjust_quota_gcd();
+	pool->failover_only = true;
+	add_pool_details(pool, false, uri, rpcuser, rpcpass);
+	
+	applog(LOG_NOTICE, "Added local bitcoin RPC server on port %d as pool %d", rpcport, pool->pool_no);
+	
+out:
+	return false;
+}
+
+static
+void add_local_gbt(void)
+{
+	appdata_file_call("Bitcoin", "bitcoin.conf", _add_local_gbt, NULL);
+}
+
 #if defined(unix) || defined(__APPLE__)
 #if defined(unix) || defined(__APPLE__)
 static void fork_monitor()
 static void fork_monitor()
 {
 {
@@ -12299,6 +12383,9 @@ int main(int argc, char *argv[])
 	switch_logsize();
 	switch_logsize();
 #endif
 #endif
 
 
+	if (opt_load_bitcoin_conf && !(opt_scrypt || opt_benchmark))
+		add_local_gbt();
+	
 	if (!total_pools) {
 	if (!total_pools) {
 		applog(LOG_WARNING, "Need to specify at least one pool server.");
 		applog(LOG_WARNING, "Need to specify at least one pool server.");
 #ifdef HAVE_CURSES
 #ifdef HAVE_CURSES