Browse Source

uri_get_param_bool helper function for pool parameters

Luke Dashjr 11 years ago
parent
commit
57cdd25cc9
3 changed files with 65 additions and 5 deletions
  1. 4 5
      miner.c
  2. 58 0
      util.c
  3. 3 0
      util.h

+ 4 - 5
miner.c

@@ -79,6 +79,7 @@
 #include "driver-cpu.h"
 #include "driver-opencl.h"
 #include "scrypt.h"
+#include "util.h"
 
 #ifdef USE_AVALON
 #include "driver-avalon.h"
@@ -2813,8 +2814,7 @@ void pool_set_opaque(struct pool *pool, bool opaque)
 
 bool pool_may_redirect_to(struct pool * const pool, const char * const uri)
 {
-	const char *p = strchr(pool->rpc_url, '#');
-	if (unlikely(p && strstr(&p[1], "redirect")))
+	if (uri_get_param_bool(pool->rpc_url, "redirect", false))
 		return true;
 	return match_domains(pool->rpc_url, strlen(pool->rpc_url), uri, strlen(uri));
 }
@@ -4347,12 +4347,10 @@ void maybe_local_submit(const struct work *work)
 		// This is a block with a full template (GBT)
 		// Regardless of the result, submit to local bitcoind(s) as well
 		struct work *work_cp;
-		char *p;
 		
 		for (int i = 0; i < total_pools; ++i)
 		{
-			p = strchr(pools[i]->rpc_url, '#');
-			if (likely(!(p && strstr(&p[1], "allblocks"))))
+			if (!uri_get_param_bool(pools[i]->rpc_url, "allblocks", false))
 				continue;
 			
 			applog(LOG_DEBUG, "Attempting submission of full block to pool %d", pools[i]->pool_no);
@@ -11957,6 +11955,7 @@ int main(int argc, char *argv[])
 		test_decimal_width();
 		test_domain_funcs();
 		test_target();
+		test_uri_get_param();
 		utf8_test();
 	}
 

+ 58 - 0
util.c

@@ -2058,6 +2058,64 @@ void test_domain_funcs()
 	_test_get_regd_domain("2001:db8::1", "2001:db8::1");
 }
 
+bool uri_get_param_bool(const char * const uri, const char * const param, const bool defval)
+{
+	const char *start = strchr(uri, '#');
+	bool invert = false, foundval = true;
+	if (!start)
+		return defval;
+	const char *p = start;
+	++start;
+nextmatch:
+	p = strstr(&p[1], param);
+	if (!p)
+		return defval;
+	const char *q = &p[strlen(param)];
+	if (isCalpha(q[0]))
+		goto nextmatch;
+	if (p - start >= 2 && (!strncasecmp(&p[-2], "no", 2)) && !isCalpha(p[-3]))
+		invert = true;
+	else
+	if (isCalpha(p[-1]))
+		goto nextmatch;
+	if (q[0] == '=')
+	{
+		++q;
+		if (q[0] == '0' && !isCalpha(q[1]))
+			foundval = false;
+	}
+	if (invert)
+		foundval = !foundval;
+	return foundval;
+}
+
+static
+void _test_uri_get_param(const char * const uri, const char * const param, const bool defval, const bool expect)
+{
+	const bool actual = uri_get_param_bool(uri, param, defval);
+	if (actual != expect)
+		applog(LOG_WARNING, "%s(\"%s\", \"%s\", %s) test failed",
+		       "uri_get_param_bool", uri, param, defval ? "true" : "false");
+}
+
+void test_uri_get_param()
+{
+	_test_uri_get_param("stratum+tcp://footest/#redirect", "redirect", false, true);
+	_test_uri_get_param("stratum+tcp://footest/#redirectme", "redirect", false, false);
+	_test_uri_get_param("stratum+tcp://footest/#noredirect", "redirect", false, false);
+	_test_uri_get_param("stratum+tcp://footest/#redirect=0", "redirect", false, false);
+	_test_uri_get_param("stratum+tcp://footest/#redirect=1", "redirect", false, true);
+	_test_uri_get_param("stratum+tcp://footest/#redirect", "redirect", true, true);
+	_test_uri_get_param("stratum+tcp://footest/#redirectme", "redirect", true, true);
+	_test_uri_get_param("stratum+tcp://footest/#noredirect", "redirect", true, false);
+	_test_uri_get_param("stratum+tcp://footest/#redirect=0", "redirect", true, false);
+	_test_uri_get_param("stratum+tcp://footest/#redirect=1", "redirect", true, true);
+	_test_uri_get_param("stratum+tcp://footest/#redirect=0,foo=1", "redirect", true, false);
+	_test_uri_get_param("stratum+tcp://footest/#redirect=1,foo=0", "redirect", false, true);
+	_test_uri_get_param("stratum+tcp://footest/#foo=1,noredirect=0,foo=1", "redirect", false, true);
+	_test_uri_get_param("stratum+tcp://footest/#bar=0,noredirect=1,foo=0", "redirect", true, false);
+}
+
 void stratum_probe_transparency(struct pool *pool)
 {
 	// Request transaction data to discourage pools from doing anything shady

+ 3 - 0
util.h

@@ -119,6 +119,9 @@ extern const char *extract_domain(size_t *out_len, const char *uri, size_t urile
 extern bool match_domains(const char *a, size_t alen, const char *b, size_t blen);
 extern void test_domain_funcs();
 
+extern bool uri_get_param_bool(const char *uri, const char *param, bool defval);
+extern void test_uri_get_param();
+
 
 enum bfg_gpio_value {
 	BGV_LOW   =  0,