Skip to content

Commit

Permalink
Getwork Fixed, Midstate Removed
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostlander committed Sep 20, 2014
1 parent db344c9 commit cdf56e9
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 115 deletions.
96 changes: 7 additions & 89 deletions cgminer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1734,19 +1734,6 @@ static bool jobj_binary(const json_t *obj, const char *key,
}
#endif

static void calc_midstate(struct work *work)
{
unsigned char data[64];
uint32_t *data32 = (uint32_t *)data;
sha256_ctx ctx;

flip64(data32, work->data);
sha256_init(&ctx);
sha256_update(&ctx, data, 64);
memcpy(work->midstate, ctx.h, 32);
endian_flip32(work->midstate, work->midstate);
}

static struct work *make_work(void)
{
struct work *work = calloc(1, sizeof(struct work));
Expand Down Expand Up @@ -1948,7 +1935,6 @@ static void gen_gbt_work(struct pool *pool, struct work *work)
free(header);
}

calc_midstate(work);
local_work++;
work->pool = pool;
work->gbt = true;
Expand Down Expand Up @@ -2056,18 +2042,13 @@ static bool gbt_decode(struct pool *pool, json_t *res_val)

static bool getwork_decode(json_t *res_val, struct work *work)
{
if (unlikely(!jobj_binary(res_val, "data", work->data, sizeof(work->data), true))) {

if (unlikely(!jobj_binary(res_val, "data", work->data, 80, true))) {
applog(LOG_ERR, "JSON inval data");
return false;
}

if (!jobj_binary(res_val, "midstate", work->midstate, sizeof(work->midstate), false)) {
// Calculate it ourselves
applog(LOG_DEBUG, "Calculating midstate locally");
calc_midstate(work);
}

if (unlikely(!jobj_binary(res_val, "target", work->target, sizeof(work->target), true))) {
if (unlikely(!jobj_binary(res_val, "target", work->target, 32, true))) {
applog(LOG_ERR, "JSON invalid target");
return false;
}
Expand Down Expand Up @@ -2755,9 +2736,9 @@ static bool submit_upstream_work(struct work *work, CURL *curl, bool resubmit)

endian_flip128(work->data, work->data);

/* build hex string */
#ifdef USE_KECCAK
hexstr = bin2hex(work->data, /*opt_keccak ? 80 : */sizeof(work->data));
#ifdef USE_NEOSCRYPT
/* Convert binary to hexadecimal string */
hexstr = bin2hex(work->data, 80);
#else
hexstr = bin2hex(work->data, sizeof(work->data));
#endif
Expand Down Expand Up @@ -6176,7 +6157,7 @@ static void gen_stratum_work(struct pool *pool, struct work *work)
#ifdef USE_NEOSCRYPT
if(!opt_neoscrypt)
#endif
calc_midstate(work);

#ifdef USE_NEOSCRYPT
if(opt_neoscrypt)
set_target_neoscrypt(work->target, work->sdiff);
Expand Down Expand Up @@ -6622,54 +6603,6 @@ struct work *get_queued(struct cgpu_info *cgpu)
return work;
}

/* This function is for finding an already queued work item in the
* given que hashtable. Code using this function must be able
* to handle NULL as a return which implies there is no matching work.
* The calling function must lock access to the que if it is required.
* The common values for midstatelen, offset, datalen are 32, 64, 12 */
struct work *__find_work_bymidstate(struct work *que, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen)
{
struct work *work, *tmp, *ret = NULL;

HASH_ITER(hh, que, work, tmp) {
if (memcmp(work->midstate, midstate, midstatelen) == 0 &&
memcmp(work->data + offset, data, datalen) == 0) {
ret = work;
break;
}
}

return ret;
}

/* This function is for finding an already queued work item in the
* device's queued_work hashtable. Code using this function must be able
* to handle NULL as a return which implies there is no matching work.
* The common values for midstatelen, offset, datalen are 32, 64, 12 */
struct work *find_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen)
{
struct work *ret;

rd_lock(&cgpu->qlock);
ret = __find_work_bymidstate(cgpu->queued_work, midstate, midstatelen, data, offset, datalen);
rd_unlock(&cgpu->qlock);

return ret;
}

struct work *clone_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen)
{
struct work *work, *ret = NULL;

rd_lock(&cgpu->qlock);
work = __find_work_bymidstate(cgpu->queued_work, midstate, midstatelen, data, offset, datalen);
if (work)
ret = copy_work(work);
rd_unlock(&cgpu->qlock);

return ret;
}

void __work_completed(struct cgpu_info *cgpu, struct work *work)
{
cgpu->queued_count--;
Expand All @@ -6686,21 +6619,6 @@ void work_completed(struct cgpu_info *cgpu, struct work *work)
free_work(work);
}

/* Combines find_queued_work_bymidstate and work_completed in one function
* withOUT destroying the work so the driver must free it. */
struct work *take_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen)
{
struct work *work;

wr_lock(&cgpu->qlock);
work = __find_work_bymidstate(cgpu->queued_work, midstate, midstatelen, data, offset, datalen);
if (work)
__work_completed(cgpu, work);
wr_unlock(&cgpu->qlock);

return work;
}

static void flush_queue(struct cgpu_info *cgpu)
{
struct work *work = NULL;
Expand Down
8 changes: 0 additions & 8 deletions driver-opencl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,6 @@ static cl_int queue_diablo_kernel(_clState *clState, dev_blk_ctx *blk, cl_uint t
#ifdef USE_SCRYPT
static cl_int queue_scrypt_kernel(_clState *clState, dev_blk_ctx *blk, __maybe_unused cl_uint threads)
{
unsigned char *midstate = blk->work->midstate;
cl_kernel *kernel = &clState->kernel;
unsigned int num = 0;
cl_uint le_target;
Expand All @@ -1115,8 +1114,6 @@ static cl_int queue_scrypt_kernel(_clState *clState, dev_blk_ctx *blk, __maybe_u
CL_SET_ARG(clState->CLbuffer0);
CL_SET_ARG(clState->outputBuffer);
CL_SET_ARG(clState->padbuffer8);
CL_SET_VARG(4, &midstate[0]);
CL_SET_VARG(4, &midstate[16]);
CL_SET_ARG(le_target);

return status;
Expand All @@ -1143,7 +1140,6 @@ static cl_int queue_keccak_kernel(_clState *clState, dev_blk_ctx *blk, __maybe_u
#ifdef USE_NEOSCRYPT
static cl_int queue_neoscrypt_kernel(_clState *clState, dev_blk_ctx *blk, __maybe_unused cl_uint threads)
{
//unsigned char *midstate = blk->work->midstate;
cl_kernel *kernel = &clState->kernel;
unsigned int num = 0;
cl_uint le_target;
Expand Down Expand Up @@ -1587,19 +1583,15 @@ static bool opencl_prepare_work(struct thr_info __maybe_unused *thr, struct work
#ifdef USE_SCRYPT
if (opt_scrypt)
work->blk.work = work;
else
#endif
#ifdef USE_NEOSCRYPT
if (opt_neoscrypt)
work->blk.work = work;
else
#endif
#ifdef USE_KECCAK
if (opt_keccak)
keccak_prepare_work(thr, work);
else
#endif
precalc_hash(&work->blk, (uint32_t *)(work->midstate), (uint32_t *)(work->data + 64));
return true;
}

Expand Down
13 changes: 0 additions & 13 deletions miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -1050,14 +1050,6 @@ extern void __bin2hex(char *s, const unsigned char *p, size_t len);
extern char *bin2hex(const unsigned char *p, size_t len);
extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);

typedef bool (*sha256_func)(struct thr_info*, const unsigned char *pmidstate,
unsigned char *pdata,
unsigned char *phash1, unsigned char *phash,
const unsigned char *ptarget,
uint32_t max_nonce,
uint32_t *last_nonce,
uint32_t nonce);

extern bool fulltest(const unsigned char *hash, const unsigned char *target);

extern int opt_queue;
Expand Down Expand Up @@ -1389,7 +1381,6 @@ struct pool {

struct work {
unsigned char data[128];
unsigned char midstate[32];
unsigned char target[32];
unsigned char hash[32];

Expand Down Expand Up @@ -1500,12 +1491,8 @@ extern bool submit_noffset_nonce(struct thr_info *thr, struct work *work, uint32
int noffset);
extern struct work *get_work(struct thr_info *thr, const int thr_id);
extern struct work *get_queued(struct cgpu_info *cgpu);
extern struct work *__find_work_bymidstate(struct work *que, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
extern struct work *find_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
extern struct work *clone_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
extern void __work_completed(struct cgpu_info *cgpu, struct work *work);
extern void work_completed(struct cgpu_info *cgpu, struct work *work);
extern struct work *take_queued_work_bymidstate(struct cgpu_info *cgpu, char *midstate, size_t midstatelen, char *data, int offset, size_t datalen);
extern void hash_driver_work(struct thr_info *mythr);
extern void hash_queued_work(struct thr_info *mythr);
extern void _wlog(const char *str);
Expand Down
8 changes: 3 additions & 5 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ json_t *json_rpc_call(CURL *curl, const char *url,
headers = curl_slist_append(headers,
"Content-type: application/json");
headers = curl_slist_append(headers,
"X-Mining-Extensions: longpoll midstate rollntime submitold");
"X-Mining-Extensions: longpoll rollntime submitold");

if (likely(global_hashrate)) {
char ghashrate[255];
Expand Down Expand Up @@ -600,7 +600,7 @@ void __bin2hex(char *s, const unsigned char *p, size_t len)
char *it= s;

for (i = 0; i < (int)len; ++i, it+= 2)
sprintf(it, "%02x", (unsigned int)p[i]);
sprintf(it, "%02X", (unsigned int)p[i]);

}

Expand Down Expand Up @@ -654,9 +654,7 @@ bool hex2bin(unsigned char *p, const char *hexstr, size_t len)
len--;
}

if (likely(len == 0 && *hexstr == 0))
ret = true;
return ret;
return(!len) ? true : false;
}

bool fulltest(const unsigned char *hash, const unsigned char *target)
Expand Down

0 comments on commit cdf56e9

Please sign in to comment.