Browse Source

Stratum: Support for mining.set_goal("goal name") - currently just resetting the user-configured goal

Luke Dashjr 11 years ago
parent
commit
5071e2f1d0
3 changed files with 50 additions and 5 deletions
  1. 23 5
      miner.c
  2. 2 0
      miner.h
  3. 25 0
      util.c

+ 23 - 5
miner.c

@@ -1006,6 +1006,15 @@ int mining_goals_name_cmp(const struct mining_goal_info * const a, const struct
 	return strcmp(a->name, b->name);
 	return strcmp(a->name, b->name);
 }
 }
 
 
+static
+void blkchain_init_block(struct blockchain_info * const blkchain)
+{
+	struct block_info * const dummy_block = calloc(sizeof(*dummy_block), 1);
+	memset(dummy_block->prevblkhash, 0, 0x20);
+	HASH_ADD(hh, blkchain->blocks, prevblkhash, sizeof(dummy_block->prevblkhash), dummy_block);
+	blkchain->currentblk = dummy_block;
+}
+
 struct mining_goal_info *get_mining_goal(const char * const name)
 struct mining_goal_info *get_mining_goal(const char * const name)
 {
 {
 	static unsigned next_goal_id;
 	static unsigned next_goal_id;
@@ -1013,16 +1022,13 @@ struct mining_goal_info *get_mining_goal(const char * const name)
 	HASH_FIND_STR(mining_goals, name, goal);
 	HASH_FIND_STR(mining_goals, name, goal);
 	if (!goal)
 	if (!goal)
 	{
 	{
-		struct block_info * const dummy_block = calloc(sizeof(*dummy_block), 1);
-		memset(dummy_block->prevblkhash, 0, 0x20);
-		
 		struct blockchain_info * const blkchain = malloc(sizeof(*blkchain) + sizeof(*goal));
 		struct blockchain_info * const blkchain = malloc(sizeof(*blkchain) + sizeof(*goal));
 		goal = (void*)(&blkchain[1]);
 		goal = (void*)(&blkchain[1]);
 		
 		
 		*blkchain = (struct blockchain_info){
 		*blkchain = (struct blockchain_info){
-			.currentblk = dummy_block,
+			.currentblk = NULL,
 		};
 		};
-		HASH_ADD(hh, blkchain->blocks, prevblkhash, sizeof(dummy_block->prevblkhash), dummy_block);
+		blkchain_init_block(blkchain);
 		
 		
 		*goal = (struct mining_goal_info){
 		*goal = (struct mining_goal_info){
 			.id = next_goal_id++,
 			.id = next_goal_id++,
@@ -1042,6 +1048,18 @@ struct mining_goal_info *get_mining_goal(const char * const name)
 	return goal;
 	return goal;
 }
 }
 
 
+void mining_goal_reset(struct mining_goal_info * const goal)
+{
+	struct blockchain_info * const blkchain = goal->blkchain;
+	struct block_info *blkinfo, *tmpblkinfo;
+	HASH_ITER(hh, blkchain->blocks, blkinfo, tmpblkinfo)
+	{
+		HASH_DEL(blkchain->blocks, blkinfo);
+		free(blkinfo);
+	}
+	blkchain_init_block(blkchain);
+}
+
 static char *getwork_req = "{\"method\": \"getwork\", \"params\": [], \"id\":0}\n";
 static char *getwork_req = "{\"method\": \"getwork\", \"params\": [], \"id\":0}\n";
 
 
 /* Adjust all the pools' quota to the greatest common denominator after a pool
 /* Adjust all the pools' quota to the greatest common denominator after a pool

+ 2 - 0
miner.h

@@ -1094,6 +1094,7 @@ extern bool get_intrange(const char *arg, int *val1, int *val2);
 extern bool detect_stratum(struct pool *pool, char *url);
 extern bool detect_stratum(struct pool *pool, char *url);
 extern void print_summary(void);
 extern void print_summary(void);
 extern struct mining_goal_info *get_mining_goal(const char *name);
 extern struct mining_goal_info *get_mining_goal(const char *name);
+extern void mining_goal_reset(struct mining_goal_info * const goal);
 extern void adjust_quota_gcd(void);
 extern void adjust_quota_gcd(void);
 extern struct pool *add_pool(void);
 extern struct pool *add_pool(void);
 extern bool add_pool_details(struct pool *pool, bool live, char *url, char *user, char *pass);
 extern bool add_pool_details(struct pool *pool, bool live, char *url, char *user, char *pass);
@@ -1405,6 +1406,7 @@ struct pool {
 	bool stratum_init;
 	bool stratum_init;
 	bool stratum_notify;
 	bool stratum_notify;
 	struct stratum_work swork;
 	struct stratum_work swork;
+	bool next_goalreset;
 	uint8_t next_target[0x20];
 	uint8_t next_target[0x20];
 	char *next_nonce1;
 	char *next_nonce1;
 	int next_n2size;
 	int next_n2size;

+ 25 - 0
util.c

@@ -2553,6 +2553,12 @@ static bool parse_notify(struct pool *pool, json_t *val)
 	pool->submit_old = !clean;
 	pool->submit_old = !clean;
 	pool->swork.clean = true;
 	pool->swork.clean = true;
 	
 	
+	if (pool->next_goalreset)
+	{
+		pool->next_goalreset = false;
+		mining_goal_reset(pool->goal);
+	}
+	
 	if (pool->next_nonce1)
 	if (pool->next_nonce1)
 	{
 	{
 		free(pool->swork.nonce1);
 		free(pool->swork.nonce1);
@@ -2739,6 +2745,16 @@ err:
 	return true;
 	return true;
 }
 }
 
 
+static
+bool stratum_set_goal(struct pool * const pool, json_t * const val, json_t * const params)
+{
+	if (!uri_get_param_bool(pool->rpc_url, "goalreset", false))
+		return false;
+	
+	pool->next_goalreset = true;
+	return true;
+}
+
 static bool parse_reconnect(struct pool *pool, json_t *val)
 static bool parse_reconnect(struct pool *pool, json_t *val)
 {
 {
 	if (opt_disable_client_reconnect)
 	if (opt_disable_client_reconnect)
@@ -2905,6 +2921,10 @@ bool parse_method(struct pool *pool, char *s)
 		goto out;
 		goto out;
 	}
 	}
 	
 	
+	// Usage: mining.set_goal("goal name", [reserved])
+	if (!strncasecmp(buf, "mining.set_goal", 15) && stratum_set_goal(pool, val, params))
+		return_via(out, ret = true);
+	
 out:
 out:
 	if (val)
 	if (val)
 		json_decref(val);
 		json_decref(val);
@@ -3282,6 +3302,11 @@ out:
 			sprintf(s, "{\"id\": \"xnsub\", \"method\": \"mining.extranonce.subscribe\", \"params\": []}");
 			sprintf(s, "{\"id\": \"xnsub\", \"method\": \"mining.extranonce.subscribe\", \"params\": []}");
 			_stratum_send(pool, s, strlen(s), true);
 			_stratum_send(pool, s, strlen(s), true);
 		}
 		}
+		if (uri_get_param_bool(pool->rpc_url, "goalreset", false))
+		{
+			sprintf(s, "{\"id\": \"goalsub\", \"method\": \"mining.goal.subscribe\", \"params\": []}");
+			_stratum_send(pool, s, strlen(s), true);
+		}
 	} else {
 	} else {
 		if (recvd)
 		if (recvd)
 		{
 		{