Browse Source

Create helper functions for duplicating json strings to avoid keeping json references in use.

nfactor-troky
Con Kolivas 12 years ago
parent
commit
9d4a44e88e
  1. 4
      miner.h
  2. 40
      util.c

4
miner.h

@ -754,8 +754,6 @@ enum pool_enable {
struct stratum_work { struct stratum_work {
/* id we sent to receive this work */ /* id we sent to receive this work */
int id; int id;
/* Reference to json structure all the following were extracted from */
json_t *json_val;
char *job_id; char *job_id;
char *prev_hash; char *prev_hash;
@ -843,8 +841,6 @@ struct pool {
bool has_stratum; bool has_stratum;
bool stratum_active; bool stratum_active;
bool stratum_auth; bool stratum_auth;
/* Store json reference to clear it if we close connection */
json_t *stratum_val;
struct stratum_work swork; struct stratum_work swork;
}; };

40
util.c

@ -926,8 +926,41 @@ out:
return sret; return sret;
} }
/* Extracts a string value from a json array with error checking. To be used
* when the value of the string returned is only examined and not to be stored.
* See json_array_string below */
static char *__json_array_string(json_t *val, unsigned int entry)
{
json_t *arr_entry;
if (json_is_null(val))
return NULL;
if (!json_is_array(val))
return NULL;
if (entry > json_array_size(val))
return NULL;
arr_entry = json_array_get(val, entry);
if (!json_is_string(arr_entry))
return NULL;
return (json_string_value(arr_entry));
}
/* Creates a freshly malloced dup of __json_array_string */
static char *json_array_string(json_t *val, unsigned int entry)
{
char *buf = __json_array_string(val, entry);
if (buf)
return strdup(buf);
return NULL;
}
static bool parse_notify(struct pool *pool, json_t *val) static bool parse_notify(struct pool *pool, json_t *val)
{ {
json_t *arr;
return true; return true;
} }
@ -1093,18 +1126,18 @@ bool initiate_stratum(struct pool *pool)
goto out; goto out;
} }
buf = (char *)json_string_value(json_array_get(notify_val, 0)); buf = __json_array_string(notify_val, 0);
if (!buf || strcasecmp(buf, "mining.notify")) { if (!buf || strcasecmp(buf, "mining.notify")) {
applog(LOG_WARNING, "Failed to get mining notify in initiate_stratum"); applog(LOG_WARNING, "Failed to get mining notify in initiate_stratum");
goto out; goto out;
} }
pool->subscription = strdup(json_string_value(json_array_get(notify_val, 1))); pool->subscription = json_array_string(notify_val, 1);
if (!pool->subscription) { if (!pool->subscription) {
applog(LOG_WARNING, "Failed to get a subscription in initiate_stratum"); applog(LOG_WARNING, "Failed to get a subscription in initiate_stratum");
goto out; goto out;
} }
pool->nonce1 = strdup(json_string_value(json_array_get(res_val, 1))); pool->nonce1 = json_array_string(res_val, 1);
if (!pool->nonce1) { if (!pool->nonce1) {
applog(LOG_WARNING, "Failed to get nonce1 in initiate_stratum"); applog(LOG_WARNING, "Failed to get nonce1 in initiate_stratum");
goto out; goto out;
@ -1122,7 +1155,6 @@ out:
if (ret) { if (ret) {
pool->stratum_active = true; pool->stratum_active = true;
pool->stratum_val = val;
if (opt_protocol) { if (opt_protocol) {
applog(LOG_DEBUG, "Pool %d confirmed mining.notify with subscription %s extranonce1 %s extranonce2 %d", applog(LOG_DEBUG, "Pool %d confirmed mining.notify with subscription %s extranonce1 %s extranonce2 %d",
pool->pool_no, pool->subscription, pool->nonce1, pool->nonce2); pool->pool_no, pool->subscription, pool->nonce1, pool->nonce2);

Loading…
Cancel
Save