Browse Source

Allow pools with #change_goal_malgo attribute to change the mining algorithm used by their assigned goal

Luke Dashjr 11 years ago
parent
commit
89243d984c
3 changed files with 47 additions and 3 deletions
  1. 0 2
      miner.c
  2. 3 0
      miner.h
  3. 44 1
      util.c

+ 0 - 2
miner.c

@@ -1003,7 +1003,6 @@ static void switch_logsize(void);
 
 static void hotplug_trigger();
 
-static
 void goal_set_malgo(struct mining_goal_info * const goal, struct mining_algorithm * const malgo)
 {
 	if (goal->malgo == malgo)
@@ -1019,7 +1018,6 @@ void goal_set_malgo(struct mining_goal_info * const goal, struct mining_algorith
 	goal->malgo = malgo;
 }
 
-static
 struct mining_algorithm *mining_algorithm_by_alias(const char * const alias)
 {
 	struct mining_algorithm *malgo;

+ 3 - 0
miner.h

@@ -1097,7 +1097,9 @@ extern int enabled_pools;
 extern bool get_intrange(const char *arg, int *val1, int *val2);
 extern bool detect_stratum(struct pool *pool, char *url);
 extern void print_summary(void);
+extern struct mining_algorithm *mining_algorithm_by_alias(const char *alias);
 extern struct mining_goal_info *get_mining_goal(const char *name);
+extern void goal_set_malgo(struct mining_goal_info *, struct mining_algorithm *);
 extern void mining_goal_reset(struct mining_goal_info * const goal);
 extern void adjust_quota_gcd(void);
 extern struct pool *add_pool2(struct mining_goal_info *);
@@ -1416,6 +1418,7 @@ struct pool {
 	struct stratum_work swork;
 	char *goalname;
 	char *next_goalname;
+	struct mining_algorithm *next_goal_malgo;
 	uint8_t next_target[0x20];
 	char *next_nonce1;
 	int next_n2size;

+ 44 - 1
util.c

@@ -2570,6 +2570,11 @@ static bool parse_notify(struct pool *pool, json_t *val)
 		free(pool->goalname);
 		pool->goalname = pool->next_goalname;
 		mining_goal_reset(pool->goal);
+		if (pool->next_goal_malgo)
+		{
+			goal_set_malgo(pool->goal, pool->next_goal_malgo);
+			pool->next_goal_malgo = NULL;
+		}
 	}
 	
 	if (pool->next_nonce1)
@@ -2767,15 +2772,53 @@ bool stratum_set_goal(struct pool * const pool, json_t * const val, json_t * con
 		return false;
 	
 	const char * const new_goalname = __json_array_string(params, 0);
+	const char *emsg = NULL;
 	
 	if (pool->next_goalname && pool->next_goalname != pool->goalname)
 		free(pool->next_goalname);
+	if (pool->next_goal_malgo)
+		pool->next_goal_malgo = NULL;
 	
 	// This compares goalname to new_goalname, but matches NULL correctly :)
 	if (pool->goalname ? !strcmp(pool->goalname, new_goalname) : !new_goalname)
 		pool->next_goalname = pool->goalname;
 	else
+	{
 		pool->next_goalname = maybe_strdup(new_goalname);
+		if (json_is_array(params) && json_array_size(params) > 1)
+		{
+			json_t * const j_goaldesc = json_array_get(params, 1);
+			if (json_is_object(j_goaldesc))
+			{
+				json_t * const j_malgo = json_object_get(j_goaldesc, "malgo");
+				if (j_malgo && json_is_string(j_malgo))
+				{
+					const char * const newvalue = json_string_value(j_malgo);
+					struct mining_algorithm * const new_malgo = mining_algorithm_by_alias(newvalue);
+					if (new_malgo == pool->goal->malgo)
+					{} // Nothing to do
+					else
+					if (new_malgo && uri_get_param_bool(pool->rpc_url, "change_goal_malgo", false))
+						pool->next_goal_malgo = new_malgo;
+					else
+						emsg = "Mining algorithm not supported";
+				}
+			}
+		}
+	}
+	
+	json_t * const j_id = json_object_get(val, "id");
+	if (j_id && !json_is_null(j_id))
+	{
+		char * const idstr = json_dumps_ANY(j_id, 0);
+		char buf[0x80];
+		if (unlikely(emsg))
+			snprintf(buf, sizeof(buf), "{\"id\":%s,\"result\":true,\"error\":null}", idstr);
+		else
+			snprintf(buf, sizeof(buf), "{\"id\":%s,\"result\":null,\"error\":[-1,\"%s\",null]}", idstr, emsg);
+		free(idstr);
+		stratum_send(pool, buf, strlen(buf));
+	}
 	
 	return true;
 }
@@ -2946,7 +2989,7 @@ bool parse_method(struct pool *pool, char *s)
 		goto out;
 	}
 	
-	// Usage: mining.set_goal("goal name", [reserved])
+	// Usage: mining.set_goal("goal name", {"malgo":"SHA256d", ...})
 	if (!strncasecmp(buf, "mining.set_goal", 15) && stratum_set_goal(pool, val, params))
 		return_via(out, ret = true);