Skip to content

Commit

Permalink
send cycle to pool
Browse files Browse the repository at this point in the history
  • Loading branch information
tonypizzicato committed Mar 20, 2018
1 parent ee6d4c6 commit 4a94b9d
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,6 +3,7 @@ minerd
minerd.exe
*.o
*.orig
cuckoo/*.a

autom4te.cache
.deps
Expand Down
53 changes: 42 additions & 11 deletions cpu-miner.c
Expand Up @@ -36,6 +36,7 @@
#include <jansson.h>
#include <curl/curl.h>
#include "compat.h"
#include "cuckoo.h"
#include "miner.h"

#define PROGRAM_NAME "minerd"
Expand Down Expand Up @@ -242,6 +243,7 @@ static struct option const options[] = {
struct work {
uint32_t data[32];
uint32_t target[8];
uint32_t cycle[CUCKOO_CYCLE_LENGTH];

int height;
char *txs;
Expand Down Expand Up @@ -610,8 +612,7 @@ static bool gbt_work_decode(const json_t *val, struct work *work)
work->data[18] = le32dec(&bits);
memset(work->data + 19, 0x00, 52);
work->data[20] = (edgebits << 23) & (1 << 22);
work->data[21] = 0x80000000;
work->data[31] = 0x00000280;
work->data[31] = 0x00000288;

if (unlikely(!jobj_binary(val, "target", target, sizeof(target)))) {
applog(LOG_ERR, "JSON invalid target");
Expand Down Expand Up @@ -700,10 +701,38 @@ static bool submit_upstream_work(CURL *curl, struct work *work)
bin2hex(ntimestr, (const unsigned char *)(&ntime), 4);
bin2hex(noncestr, (const unsigned char *)(&nonce), 4);
xnonce2str = abin2hex(work->xnonce2, work->xnonce2_len);
req = malloc(256 + strlen(rpc_user) + strlen(work->job_id) + 2 * work->xnonce2_len);
// max length of hex encoded edges + commas
int cycle_str_length = (32 / 4 + 1) * CUCKOO_CYCLE_LENGTH - 1;
req = malloc(256 + cycle_str_length + strlen(rpc_user) + strlen(work->job_id) + 2 * work->xnonce2_len);
char str_cycle[cycle_str_length];
for (int i = 0; i < CUCKOO_CYCLE_LENGTH; i++) {
sprintf(str_cycle, "%s%s%x", str_cycle, i > 0 ? "," : "", work->cycle[i]);
}

// uint32_t hash_be[8];
// char hash_str[65];

// for (i = 0; i < 8; i++) {
// be32enc(hash_be + i, hash[7 - i]);
// }

// bin2hex(hash_str, (unsigned char *)hash_be, 32);

// applog(LOG_DEBUG, "DEBUG: %s\nHash: %s\nTarget: %s",
// rc ? "hash <= target"
// : "hash > target (false positive)",
// hash_str,
// target_str);

// applog(LOG_INFO, "hash to send: %s", hash_str);
applog(LOG_INFO, "");
applog(LOG_INFO, "nonce to send: %lu", work->data[19]);
applog(LOG_INFO, "cycle to send: %s", str_cycle);
applog(LOG_INFO, "");

sprintf(req,
"{\"method\": \"mining.submit\", \"params\": [\"%s\", \"%s\", \"%s\", \"%s\", \"%s\"], \"id\":4}",
rpc_user, work->job_id, xnonce2str, ntimestr, noncestr);
"{\"method\": \"mining.submit\", \"params\": [\"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\"], \"id\":4}",
rpc_user, work->job_id, xnonce2str, ntimestr, noncestr, str_cycle);
free(xnonce2str);

rc = stratum_send_line(&stratum, req);
Expand Down Expand Up @@ -983,8 +1012,9 @@ static bool get_work(struct thr_info *thr, struct work *work)
memset(work->data, 0x55, 76);
work->data[17] = swab32(time(NULL));
memset(work->data + 19, 0x00, 52);
work->data[21] = 0x80000000;
work->data[31] = 0x00000280;
// edgebits should be set here
// work->data[20] = 24;
work->data[31] = 0x00000288;
memset(work->target, 0x00, sizeof(work->target));
return true;
}
Expand Down Expand Up @@ -1077,11 +1107,12 @@ static void stratum_gen_work(struct stratum_ctx *sctx, struct work *work)
work->data[18] = le32dec(sctx->job.nbits);
// move one byte of edgebits to the end of the message
// followed by "1" bit
work->data[20] = (*sctx->job.nedgebits << 23) | (1 << 22);
work->data[20] = (*sctx->job.nedgebits << 24) | (1 << 23);
// lenth of the message in bits
work->data[31] = 0x00000281;
work->data[31] = 0x00000288;

pthread_mutex_unlock(&sctx->work_lock);
applog(LOG_INFO, "DEBUG: work->data[20]=%x", work->data[20]);

if (opt_debug) {
char *xnonce2str = abin2hex(work->xnonce2, work->xnonce2_len);
Expand Down Expand Up @@ -1189,11 +1220,11 @@ static void *miner_thread(void *userdata)
}
bin2hex(target_str, (unsigned char *)target_be, 32);

applog(LOG_INFO, "before scanhash; nonce = %d, edgebits = %d; max_nonce = %d", work.data[19], work.data[20] >> 23, max_nonce);
applog(LOG_INFO, "before scanhash; nonce = %lu, edgebits = %d; max_nonce = %lu", work.data[19], work.data[20] >> 24, max_nonce);
applog(LOG_INFO, "target = %s", target_str);

/* scan nonces for a proof-of-work hash */
rc = scanhash_sha256d(thr_id, work.data, work.target, max_nonce, &hashes_done);
rc = scanhash_sha256d(thr_id, work.data, work.target, work.cycle, max_nonce, &hashes_done);

applog(LOG_INFO, "after scanhash");

Expand Down
6 changes: 4 additions & 2 deletions cuckoo.h
Expand Up @@ -5,6 +5,8 @@
#include <stdint.h>
#include <stdbool.h>

bool findcycle(const uint32_t* hash, uint8_t edgeBits, uint8_t proofSize, uint32_t* cycle);
#define CUCKOO_CYCLE_LENGTH 42

#endif // MERIT_MINER_CUCKOO_H
bool findcycle(const char* hash, uint8_t edgeBits, uint8_t proofSize, uint32_t* cycle);

#endif // MERIT_MINER_CUCKOO_H
18 changes: 10 additions & 8 deletions cuckoo/cuckoo.cpp
Expand Up @@ -78,9 +78,11 @@ static const uint16_t MIN_EDGE_BITS = 16;
static const uint16_t MAX_EDGE_BITS = 31;

// convenience function for extracting siphash keys from header
void setHeader(const uint32_t *header, const uint32_t headerlen, siphash_keys *keys)
void setHeader(const char *header, const uint32_t headerlen, siphash_keys *keys)
{
char hdrkey[32];
// printf("header len: %d\n", headerlen);

// SHA256((unsigned char *)header, headerlen, (unsigned char *)hdrkey);
blake2b((void *)hdrkey, sizeof(hdrkey), (const void *)header, headerlen, 0, 0);
setkeys(keys, hdrkey);
Expand Down Expand Up @@ -1077,7 +1079,7 @@ class solver_ctx

solver_ctx(
ctpl::thread_pool &poolIn,
const uint32_t *header,
const char *header,
const uint32_t headerlen,
const uint32_t nTrims,
const uint8_t proofSizeIn) : pool{poolIn}, proofSize{proofSizeIn}
Expand Down Expand Up @@ -1344,13 +1346,13 @@ class solver_ctx
};

template <typename offset_t, uint8_t EDGEBITS, uint8_t XBITS>
bool run(const uint32_t *hash, uint8_t proofSize, std::set<uint32_t> &cycle, ctpl::thread_pool &pool)
bool run(const char *hash, uint8_t proofSize, std::set<uint32_t> &cycle, ctpl::thread_pool &pool)
{
assert(EDGEBITS >= MIN_EDGE_BITS && EDGEBITS <= MAX_EDGE_BITS);

uint32_t nTrims = EDGEBITS >= 30 ? 96 : 68;

solver_ctx<offset_t, EDGEBITS, XBITS> ctx(pool, hash, 32, nTrims, proofSize);
solver_ctx<offset_t, EDGEBITS, XBITS> ctx(pool, hash, 64, nTrims, proofSize);

bool found = ctx.solve();

Expand All @@ -1361,7 +1363,7 @@ bool run(const uint32_t *hash, uint8_t proofSize, std::set<uint32_t> &cycle, ctp
return found;
}

bool FindCycle(const uint32_t *hash, uint8_t edgeBits, uint8_t proofSize, std::set<uint32_t> &cycle, ctpl::thread_pool &pool)
bool FindCycle(const char *hash, uint8_t edgeBits, uint8_t proofSize, std::set<uint32_t> &cycle, ctpl::thread_pool &pool)
{
switch (edgeBits)
{
Expand Down Expand Up @@ -1406,10 +1408,10 @@ bool FindCycle(const uint32_t *hash, uint8_t edgeBits, uint8_t proofSize, std::s
}

extern "C" {
bool findcycle(const uint32_t *hash, uint8_t edgeBits, uint8_t proofSize, uint32_t *cycle)
bool findcycle(const char* hash, uint8_t edgeBits, uint8_t proofSize, uint32_t *cycle)
{
std::set<uint32_t> sCycle;
ctpl::thread_pool pool{4};
ctpl::thread_pool pool{2};

FindCycle(hash, edgeBits, proofSize, sCycle, pool);

Expand All @@ -1424,4 +1426,4 @@ bool findcycle(const uint32_t *hash, uint8_t edgeBits, uint8_t proofSize, uint32

return true;
}
}
}
2 changes: 1 addition & 1 deletion cuckoo/cuckoo.hpp
Expand Up @@ -23,6 +23,6 @@ extern const char* errstr[];


// Find proofsize-length cuckoo cycle in random graph
bool FindCycleAdvanced(const uint32_t& hash, uint8_t edgeBits, uint8_t proofSize, std::set<uint32_t>& cycle, ctpl::thread_pool&);
bool FindCycleAdvanced(const char *hash, uint8_t edgeBits, uint8_t proofSize, std::set<uint32_t>& cycle, ctpl::thread_pool&);

#endif // MERIT_MINER_CUCKOO_H
2 changes: 1 addition & 1 deletion miner.h
Expand Up @@ -152,7 +152,7 @@ void sha256_transform_8way(uint32_t *state, const uint32_t *block, int swap);
#endif

extern int scanhash_sha256d(int thr_id, uint32_t *pdata,
const uint32_t *ptarget, uint32_t max_nonce, unsigned long *hashes_done);
const uint32_t *ptarget, uint32_t *cycle, uint32_t max_nonce, unsigned long *hashes_done);

extern unsigned char *scrypt_buffer_alloc(int N);
extern int scanhash_scrypt(int thr_id, uint32_t *pdata,
Expand Down
29 changes: 22 additions & 7 deletions sha2.c
Expand Up @@ -593,20 +593,17 @@ static inline int scanhash_sha256d_8way(int thr_id, uint32_t *pdata,

#endif /* HAVE_SHA256_8WAY */

int scanhash_sha256d(int thr_id, uint32_t *pdata, const uint32_t *ptarget,
int scanhash_sha256d(int thr_id, uint32_t *pdata, const uint32_t *ptarget, uint32_t *cycle,
uint32_t max_nonce, unsigned long *hashes_done)
{
uint32_t data[64] __attribute__((aligned(128)));
uint32_t hash[8] __attribute__((aligned(32)));
uint32_t midstate[8] __attribute__((aligned(32)));
uint32_t prehash[8] __attribute__((aligned(32)));
uint32_t n = pdata[19] - 1;
uint8_t edgebits = pdata[20] >> 23;
uint8_t edgebits = pdata[20] >> 24;
const uint32_t first_nonce = pdata[19];
const uint32_t Htarg = ptarget[7];
uint32_t cycle[42];

const char* shash = "26324790d26e15c0da01538db8ffa73467b3de3d4f23bf8dac3b777a55836bdc";

#ifdef HAVE_SHA256_8WAY
if (sha256_use_8way())
Expand All @@ -631,18 +628,36 @@ int scanhash_sha256d(int thr_id, uint32_t *pdata, const uint32_t *ptarget,
data[3] = ++n;
sha256d_ms(hash, data, midstate, prehash);


if (swab32(hash[7]) <= Htarg) {
pdata[19] = data[3];
sha256d_80_swap(hash, pdata);
bool cycleFound = findcycle(hash, edgebits, 42, cycle);

// char str_hash[64];
// for (int i = 0; i < 32; i++) {
// sprintf(str_hash + (i * 2), "%02x", ((const char *)hash)[i]);
// }

// printf("\t\tConverted hash: %s\n", str_hash);

uint32_t hash_be[8];
char hash_str[65];

for (int i = 0; i < 8; i++) {
be32enc(hash_be + i, hash[7 - i]);
}
bin2hex(hash_str, (unsigned char *)hash_be, 32);

bool cycleFound = findcycle(hash_str, edgebits, 42, cycle);

if (!cycleFound) {
continue;
}

if (fulltest(hash, ptarget)) {
*hashes_done = n - first_nonce + 1;

printf("\t\tfound hash string: %s\n", hash_str);

return 1;
}
}
Expand Down
2 changes: 1 addition & 1 deletion util.c
Expand Up @@ -741,7 +741,7 @@ bool fulltest(const uint32_t *hash, const uint32_t *target)
char hash_str[65], target_str[65];

for (i = 0; i < 8; i++) {
be32enc(hash_be + i, hash[7 - i]);
le32enc(hash_be + i, hash[7 - i]);
be32enc(target_be + i, target[7 - i]);
}
bin2hex(hash_str, (unsigned char *)hash_be, 32);
Expand Down

0 comments on commit 4a94b9d

Please sign in to comment.