Skip to content

Commit

Permalink
sapling
Browse files Browse the repository at this point in the history
  • Loading branch information
kpooljp committed Jan 5, 2019
1 parent 937c576 commit a65ab2a
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 54 deletions.
104 changes: 89 additions & 15 deletions ccminer.cpp
Expand Up @@ -831,24 +831,25 @@ static bool submit_upstream_work(CURL *curl, struct work *work)

char data_str[2 * sizeof(work->data) + 1];
char *req;
int datasize = work->sapling ? 112 : 80;

for (int i = 0; i < ARRAY_SIZE(work->data); i++)
be32enc(work->data + i, work->data[i]);
cbin2hex(data_str, (char *)work->data, 80);
cbin2hex(data_str, (char *)work->data, datasize);
if (work->workid) {
char *params;
val = json_object();
json_object_set_new(val, "workid", json_string(work->workid));
params = json_dumps(val, 0);
json_decref(val);
req = (char*)malloc(128 + 2 * 80 + strlen(work->txs2) + strlen(params));
req = (char*)malloc(128 + 2 * datasize + strlen(work->txs2) + strlen(params));
sprintf(req,
"{\"method\": \"submitblock\", \"params\": [\"%s%s\", %s], \"id\":4}\r\n",
data_str, work->txs2, params);
free(params);
}
else {
req = (char*)malloc(128 + 2 * 80 + strlen(work->txs2));
req = (char*)malloc(128 + 2 * 2 * datasize + strlen(work->txs2));
sprintf(req,
"{\"method\": \"submitblock\", \"params\": [\"%s%s\"], \"id\":4}\r\n",
data_str, work->txs2);
Expand All @@ -862,8 +863,27 @@ static bool submit_upstream_work(CURL *curl, struct work *work)
}

res = json_object_get(val, "result");
reason = json_object_get(val, "reject-reason");
if (!share_result(json_is_true(res), reason ? json_string_value(reason) : NULL))
int ret;
if (json_is_object(res))
{
char *res_str;
bool sumres = false;
void *iter = json_object_iter(res);
while (iter) {
if (json_is_null(json_object_iter_value(iter)))\
{
sumres = true;
break;
}
iter = json_object_iter_next(res, iter);
}
res_str = json_dumps(res, 0);
ret = share_result(sumres, res_str);
free(res_str);
} else {
ret = share_result(json_is_null(res), json_string_value(res));
}
if (!ret)
{
if (check_dups)
hashlog_purge_job(work->job_id);
Expand All @@ -875,7 +895,6 @@ static bool submit_upstream_work(CURL *curl, struct work *work)
#endif
else
{

/* build hex string */
char *str = NULL;
for(int i = 0; i < (work->datasize >> 2); i++)
Expand All @@ -901,8 +920,27 @@ static bool submit_upstream_work(CURL *curl, struct work *work)
}

res = json_object_get(val, "result");
reason = json_object_get(val, "reject-reason");
if(!share_result(json_is_true(res), reason ? json_string_value(reason) : NULL))
int ret;
if (json_is_object(res))
{
char *res_str;
bool sumres = false;
void *iter = json_object_iter(res);
while (iter) {
if (json_is_null(json_object_iter_value(iter)))\
{
sumres = true;
break;
}
iter = json_object_iter_next(res, iter);
}
res_str = json_dumps(res, 0);
ret = share_result(sumres, res_str);
free(res_str);
} else {
ret = share_result(json_is_null(res), json_string_value(res));
}
if (!ret)
{
if(check_dups)
hashlog_purge_job(work->job_id);
Expand Down Expand Up @@ -1015,12 +1053,14 @@ static bool gbt_work_decode_full(const json_t *val, struct work *work)
int tx_count, tx_size;
uchar txc_vi[9];
uchar(*merkle_tree)[32] = NULL;
uint32_t final_sapling_hash[8];
bool coinbase_append = false;
bool submit_coinbase = false;
bool version_force = false;
bool version_reduce = false;
json_t *tmp, *txa;
bool rc = false;
bool sapling = false;

tmp = json_object_get(val, "mutable");
if (tmp && json_is_array(tmp)) {
Expand Down Expand Up @@ -1054,7 +1094,9 @@ static bool gbt_work_decode_full(const json_t *val, struct work *work)
goto out;
}
version = (uint32_t)json_integer_value(tmp);
if ((version & 0xffU) > BLOCK_VERSION_CURRENT) {
if ((version & 0xffU) == 5) {
sapling = true;
} else if ((version & 0xffU) > BLOCK_VERSION_CURRENT) {
if (version_reduce) {
version = (version & ~0xffU) | BLOCK_VERSION_CURRENT;
}
Expand Down Expand Up @@ -1086,6 +1128,13 @@ static bool gbt_work_decode_full(const json_t *val, struct work *work)
goto out;
}

if (sapling) {
if (unlikely(!jobj_binary(val, "finalsaplingroothash", final_sapling_hash, sizeof(final_sapling_hash)))) {
applog(LOG_ERR, "JSON invalid finalsaplingroothash");
goto out;
}
}

/* find count and size of transactions */
txa = json_object_get(val, "transactions");
if (!txa || !json_is_array(txa)) {
Expand Down Expand Up @@ -1236,9 +1285,21 @@ static bool gbt_work_decode_full(const json_t *val, struct work *work)
work->data[9 + i] = be32dec((uint32_t *)merkle_tree[0] + i);
work->data[17] = swab32(curtime);
work->data[18] = le32dec(&bits);
memset(work->data + 19, 0x00, 52);
work->data[20] = 0x80000000;
work->data[31] = 0x00000280;
if (sapling) {
work->sapling = true;
work->data[19] = 0x00000000;
for (i = 0; i < 8; i++)
work->data[27 - i] = le32dec(final_sapling_hash + i);
work->data[28] = 0x80000000;
work->data[29] = 0x00000000;
work->data[30] = 0x00000000;
work->data[31] = 0x00000380;
} else {
work->sapling = false;
memset(work->data + 19, 0x00, 52);
work->data[20] = 0x80000000;
work->data[31] = 0x00000280;
}

if (unlikely(!jobj_binary(val, "target", target, sizeof(target)))) {
applog(LOG_ERR, "JSON invalid target");
Expand Down Expand Up @@ -1790,8 +1851,19 @@ static bool stratum_gen_work(struct stratum_ctx *sctx, struct work *work)
work->data[9 + i] = be32dec((uint32_t *)merkle_root + i);
work->data[17] = le32dec(sctx->job.ntime);
work->data[18] = le32dec(sctx->job.nbits);
work->data[20] = 0x80000000;
work->data[31] = 0x00000280;
if (be32dec(sctx->job.version) >= 5) {
work->sapling = true;
for (i = 0; i < 8; i++)
work->data[20 + i] = le32dec((uint32_t *)sctx->job.finalsaplinghash + i);
work->data[28] = 0x80000000;
work->data[29] = 0x00000000;
work->data[30] = 0x00000000;
work->data[31] = 0x00000380;
} else {
work->sapling = false;
work->data[20] = 0x80000000;
work->data[31] = 0x00000280;
}
}
else
{
Expand Down Expand Up @@ -1871,6 +1943,7 @@ static void *miner_thread(void *userdata)
bool extrajob = false;
char s[16];
int rc = 0;
int perslen;

memset(&work, 0, sizeof(work)); // prevent work from being used uninitialized

Expand Down Expand Up @@ -2302,8 +2375,9 @@ static void *miner_thread(void *userdata)

#ifndef ORG
case ALGO_YESCRYPT:
perslen = work.sapling ? 112 : 80;
rc = scanhash_yescrypt(thr_id, work.data, work.target,
max_nonce, &hashes_done);
max_nonce, &hashes_done, perslen);
break;

case ALGO_YESCRYPTR8:
Expand Down
4 changes: 3 additions & 1 deletion miner.h
Expand Up @@ -379,7 +379,7 @@ extern int scanhash_x17(int thr_id, uint32_t *pdata,
#ifndef ORG
extern int scanhash_yescrypt(int thr_id, uint32_t *pdata,
uint32_t *ptarget, uint32_t max_nonce,
uint32_t *hashes_done);
uint32_t *hashes_done, int perslen);

extern int scanhash_yescryptr8(int thr_id, uint32_t *pdata,
uint32_t *ptarget, uint32_t max_nonce,
Expand Down Expand Up @@ -612,6 +612,7 @@ uint32_t device_intensity(int thr_id, const char *func, uint32_t defcount);
struct stratum_job {
char *job_id;
unsigned char prevhash[32];
unsigned char finalsaplinghash[32];
size_t coinbase_size;
unsigned char *coinbase;
unsigned char *xnonce2;
Expand Down Expand Up @@ -677,6 +678,7 @@ struct work {
char *txs2;
char *workid;
#endif
bool sapling;
};

enum sha_algos
Expand Down
19 changes: 16 additions & 3 deletions util.cpp
Expand Up @@ -146,7 +146,7 @@ void applog(int prio, const char *fmt, ...)
{
case LOG_ERR: color = CL_RED; break;
case LOG_WARNING: color = CL_YLW; break;
case LOG_NOTICE: color = CL_WHT; break;
case LOG_NOTICE: color = CL_LGR; break;
case LOG_INFO: color = ""; break;
case LOG_DEBUG: color = CL_GRY; break;

Expand Down Expand Up @@ -1526,14 +1526,15 @@ static uint32_t getblocheight(struct stratum_ctx *sctx)

static bool stratum_notify(struct stratum_ctx *sctx, json_t *params)
{
const char *job_id, *prevhash, *coinb1, *coinb2, *version, *nbits, *nreward;
const char *job_id, *prevhash, *coinb1, *coinb2, *version, *nbits, *nreward, *finalsaplinghash;
char *stime;
size_t coinb1_size, coinb2_size;
bool clean, ret = false;
int merkle_count, i;
json_t *merkle_arr;
uchar **merkle = NULL;
int32_t ntime;
int ver;

job_id = json_string_value(json_array_get(params, 0));
prevhash = json_string_value(json_array_get(params, 1));
Expand Down Expand Up @@ -1575,6 +1576,16 @@ static bool stratum_notify(struct stratum_ctx *sctx, json_t *params)
}
}

hex2bin(sctx->job.version, version, 4);
ver = be32dec(sctx->job.version);
if (ver == 5) {
finalsaplinghash = json_string_value(json_array_get(params, 9));
if (!finalsaplinghash || strlen(finalsaplinghash) != 64) {
applog(LOG_ERR, "Stratum notify: invalid parameters");
goto out;
}
}

/* store stratum server time diff */
hex2bin((uchar *)&ntime, stime, 4);
if(opt_algo!=ALGO_SIA)
Expand Down Expand Up @@ -1643,6 +1654,9 @@ static bool stratum_notify(struct stratum_ctx *sctx, json_t *params)
free(sctx->job.job_id);
sctx->job.job_id = strdup(job_id);
hex2bin(sctx->job.prevhash, prevhash, 32);
if (ver == 5) {
hex2bin(sctx->job.finalsaplinghash, finalsaplinghash, 32);
}

if(opt_algo != ALGO_SIA)
sctx->job.height = getblocheight(sctx);
Expand All @@ -1659,7 +1673,6 @@ static bool stratum_notify(struct stratum_ctx *sctx, json_t *params)
sctx->job.merkle = merkle;
sctx->job.merkle_count = merkle_count;

hex2bin(sctx->job.version, version, 4);
hex2bin(sctx->job.nbits, nbits, 4);
hex2bin(sctx->job.ntime, stime, 4);
if(nreward != NULL)
Expand Down

0 comments on commit a65ab2a

Please sign in to comment.