Skip to content

Commit

Permalink
Stratum: Support for mining.set_goal("goal name") - currently just re…
Browse files Browse the repository at this point in the history
…setting the user-configured goal
  • Loading branch information
luke-jr committed Oct 29, 2014
1 parent adf7671 commit 5071e2f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
28 changes: 23 additions & 5 deletions miner.c
Original file line number Diff line number Diff line change
Expand Up @@ -1006,23 +1006,29 @@ int mining_goals_name_cmp(const struct mining_goal_info * const a, const struct
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)
{
static unsigned next_goal_id;
struct mining_goal_info *goal;
HASH_FIND_STR(mining_goals, name, 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));
goal = (void*)(&blkchain[1]);

*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){
.id = next_goal_id++,
Expand All @@ -1042,6 +1048,18 @@ struct mining_goal_info *get_mining_goal(const char * const name)
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";

/* Adjust all the pools' quota to the greatest common denominator after a pool
Expand Down
2 changes: 2 additions & 0 deletions miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 void print_summary(void);
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 struct pool *add_pool(void);
extern bool add_pool_details(struct pool *pool, bool live, char *url, char *user, char *pass);
Expand Down Expand Up @@ -1405,6 +1406,7 @@ struct pool {
bool stratum_init;
bool stratum_notify;
struct stratum_work swork;
bool next_goalreset;
uint8_t next_target[0x20];
char *next_nonce1;
int next_n2size;
Expand Down
25 changes: 25 additions & 0 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -2553,6 +2553,12 @@ static bool parse_notify(struct pool *pool, json_t *val)
pool->submit_old = !clean;
pool->swork.clean = true;

if (pool->next_goalreset)
{
pool->next_goalreset = false;
mining_goal_reset(pool->goal);
}

if (pool->next_nonce1)
{
free(pool->swork.nonce1);
Expand Down Expand Up @@ -2739,6 +2745,16 @@ bool stratum_set_extranonce(struct pool * const pool, json_t * const val, json_t
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)
{
if (opt_disable_client_reconnect)
Expand Down Expand Up @@ -2905,6 +2921,10 @@ bool parse_method(struct pool *pool, char *s)
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:
if (val)
json_decref(val);
Expand Down Expand Up @@ -3282,6 +3302,11 @@ bool initiate_stratum(struct pool *pool)
sprintf(s, "{\"id\": \"xnsub\", \"method\": \"mining.extranonce.subscribe\", \"params\": []}");
_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 {
if (recvd)
{
Expand Down

0 comments on commit 5071e2f

Please sign in to comment.