Browse Source

Merge branch 'lp_refactor_201405' into bfgminer

Luke Dashjr 11 years ago
parent
commit
7f25d9019f
3 changed files with 56 additions and 20 deletions
  1. 32 8
      miner.c
  2. 1 0
      miner.h
  3. 23 12
      util.c

+ 32 - 8
miner.c

@@ -4671,6 +4671,22 @@ bool pool_actively_in_use(const struct pool * const pool, const struct pool *cp)
 	return (!pool_unworkable(pool)) && pool_actively_desired(pool, cp);
 }
 
+static
+bool pool_supports_block_change_notification(struct pool * const pool)
+{
+	return pool->has_stratum || pool->lp_url;
+}
+
+static
+bool pool_has_active_block_change_notification(struct pool * const pool)
+{
+	return pool->stratum_active || pool->lp_active;
+}
+
+static struct pool *_select_longpoll_pool(struct pool *, bool(*)(struct pool *));
+#define select_longpoll_pool(pool)  _select_longpoll_pool(pool, pool_supports_block_change_notification)
+#define pool_active_lp_pool(pool)  _select_longpoll_pool(pool, pool_has_active_block_change_notification)
+
 /* In balanced mode, the amount of diff1 solutions per pool is monitored as a
  * rolling average per 10 minutes and if pools start getting more, it biases
  * away from them to distribute work evenly. The share count is reset to the
@@ -8390,6 +8406,10 @@ static bool cnx_needed(struct pool *pool)
 	/* We've run out of work, bring anything back to life. */
 	if (no_work)
 		return true;
+	
+	// If the current pool lacks its own block change detection, see if we are needed for that
+	if (pool_active_lp_pool(cp) == pool)
+		return true;
 
 	return false;
 }
@@ -9791,16 +9811,17 @@ static void convert_to_work(json_t *val, int rolltime, struct pool *pool, struct
 /* If we want longpoll, enable it for the chosen default pool, or, if
  * the pool does not support longpoll, find the first one that does
  * and use its longpoll support */
-static struct pool *select_longpoll_pool(struct pool *cp)
+static
+struct pool *_select_longpoll_pool(struct pool *cp, bool(*func)(struct pool *))
 {
 	int i;
 
-	if (cp->lp_url)
+	if (func(cp))
 		return cp;
 	for (i = 0; i < total_pools; i++) {
 		struct pool *pool = pools[i];
 
-		if (pool->has_stratum || pool->lp_url)
+		if (func(pool))
 			return pool;
 	}
 	return NULL;
@@ -9813,6 +9834,7 @@ static void wait_lpcurrent(struct pool *pool)
 {
 	while (!cnx_needed(pool))
 	{
+		pool->lp_active = false;
 		mutex_lock(&lp_lock);
 		pthread_cond_wait(&lp_cond, &lp_lock);
 		mutex_unlock(&lp_lock);
@@ -9929,12 +9951,13 @@ retry_pool:
 			 * time. */
 			cgtime(&end);
 			free_work(work);
-			if (end.tv_sec - start.tv_sec > 30)
-				continue;
-			if (failures == 1)
-				applog(LOG_WARNING, "longpoll failed for %s, retrying every 30s", lp_url);
+			if (end.tv_sec - start.tv_sec <= 30)
+			{
+				if (failures == 1)
+					applog(LOG_WARNING, "longpoll failed for %s, retrying every 30s", lp_url);
 lpfail:
-			cgsleep_ms(30000);
+				cgsleep_ms(30000);
+			}
 		}
 
 		if (pool != cp) {
@@ -9953,6 +9976,7 @@ lpfail:
 	}
 
 out:
+	pool->lp_active = false;
 	curl_easy_cleanup(curl);
 
 	return NULL;

+ 1 - 0
miner.h

@@ -1223,6 +1223,7 @@ struct pool {
 	char *lp_id;
 	enum pool_protocol lp_proto;
 	curl_socket_t lp_socket;
+	bool lp_active;
 
 	unsigned int getwork_requested;
 	unsigned int stale_shares;

+ 23 - 12
util.c

@@ -104,6 +104,17 @@ static void databuf_free(struct data_buffer *db)
 	memset(db, 0, sizeof(*db));
 }
 
+struct json_rpc_call_state {
+	struct data_buffer all_data;
+	struct header_info hi;
+	void *priv;
+	char curl_err_str[CURL_ERROR_SIZE];
+	struct curl_slist *headers;
+	struct upload_buffer upload_data;
+	struct pool *pool;
+	bool longpoll;
+};
+
 // aka data_buffer_write
 static size_t all_data_cb(const void *ptr, size_t size, size_t nmemb,
 			  void *user_data)
@@ -151,8 +162,15 @@ static size_t all_data_cb(const void *ptr, size_t size, size_t nmemb,
 static size_t upload_data_cb(void *ptr, size_t size, size_t nmemb,
 			     void *user_data)
 {
-	struct upload_buffer *ub = user_data;
+	struct json_rpc_call_state * const state = user_data;
+	struct upload_buffer * const ub = &state->upload_data;
 	unsigned int len = size * nmemb;
+	
+	if (state->longpoll)
+	{
+		struct pool * const pool = state->pool;
+		pool->lp_active = true;
+	}
 
 	if (len > ub->len)
 		len = ub->len;
@@ -377,16 +395,6 @@ static int curl_debug_cb(__maybe_unused CURL *handle, curl_infotype type,
 	return 0;
 }
 
-struct json_rpc_call_state {
-	struct data_buffer all_data;
-	struct header_info hi;
-	void *priv;
-	char curl_err_str[CURL_ERROR_SIZE];
-	struct curl_slist *headers;
-	struct upload_buffer upload_data;
-	struct pool *pool;
-};
-
 void json_rpc_call_async(CURL *curl, const char *url,
 		      const char *userpass, const char *rpc_req,
 		      bool longpoll,
@@ -403,7 +411,10 @@ void json_rpc_call_async(CURL *curl, const char *url,
 	struct curl_slist *headers = NULL;
 
 	if (longpoll)
+	{
 		state->all_data.idlemarker = &pool->lp_socket;
+		state->longpoll = true;
+	}
 
 	/* it is assumed that 'curl' is freshly [re]initialized at this pt */
 
@@ -428,7 +439,7 @@ void json_rpc_call_async(CURL *curl, const char *url,
 	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, all_data_cb);
 	curl_easy_setopt(curl, CURLOPT_WRITEDATA, &state->all_data);
 	curl_easy_setopt(curl, CURLOPT_READFUNCTION, upload_data_cb);
-	curl_easy_setopt(curl, CURLOPT_READDATA, &state->upload_data);
+	curl_easy_setopt(curl, CURLOPT_READDATA, state);
 	curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &state->curl_err_str[0]);
 	curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
 	curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, resp_hdr_cb);