From 4a94b9d38db5bb369c356bc7d695237e6b1d1b49 Mon Sep 17 00:00:00 2001 From: "tony.pizzicato" Date: Tue, 20 Mar 2018 22:37:49 +0300 Subject: [PATCH] send cycle to pool --- .gitignore | 1 + cpu-miner.c | 53 +++++++++++++++++++++++++++++++++++++---------- cuckoo.h | 6 ++++-- cuckoo/cuckoo.cpp | 18 +++++++++------- cuckoo/cuckoo.hpp | 2 +- miner.h | 2 +- sha2.c | 29 +++++++++++++++++++------- util.c | 2 +- 8 files changed, 82 insertions(+), 31 deletions(-) diff --git a/.gitignore b/.gitignore index c643744..3022349 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ minerd minerd.exe *.o *.orig +cuckoo/*.a autom4te.cache .deps diff --git a/cpu-miner.c b/cpu-miner.c index 54364c4..07af2cc 100644 --- a/cpu-miner.c +++ b/cpu-miner.c @@ -36,6 +36,7 @@ #include #include #include "compat.h" +#include "cuckoo.h" #include "miner.h" #define PROGRAM_NAME "minerd" @@ -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; @@ -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"); @@ -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); @@ -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; } @@ -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); @@ -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"); diff --git a/cuckoo.h b/cuckoo.h index 8973e83..c5826ea 100644 --- a/cuckoo.h +++ b/cuckoo.h @@ -5,6 +5,8 @@ #include #include -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 \ No newline at end of file +bool findcycle(const char* hash, uint8_t edgeBits, uint8_t proofSize, uint32_t* cycle); + +#endif // MERIT_MINER_CUCKOO_H diff --git a/cuckoo/cuckoo.cpp b/cuckoo/cuckoo.cpp index 0dd5f67..41d4f11 100644 --- a/cuckoo/cuckoo.cpp +++ b/cuckoo/cuckoo.cpp @@ -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); @@ -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} @@ -1344,13 +1346,13 @@ class solver_ctx }; template -bool run(const uint32_t *hash, uint8_t proofSize, std::set &cycle, ctpl::thread_pool &pool) +bool run(const char *hash, uint8_t proofSize, std::set &cycle, ctpl::thread_pool &pool) { assert(EDGEBITS >= MIN_EDGE_BITS && EDGEBITS <= MAX_EDGE_BITS); uint32_t nTrims = EDGEBITS >= 30 ? 96 : 68; - solver_ctx ctx(pool, hash, 32, nTrims, proofSize); + solver_ctx ctx(pool, hash, 64, nTrims, proofSize); bool found = ctx.solve(); @@ -1361,7 +1363,7 @@ bool run(const uint32_t *hash, uint8_t proofSize, std::set &cycle, ctp return found; } -bool FindCycle(const uint32_t *hash, uint8_t edgeBits, uint8_t proofSize, std::set &cycle, ctpl::thread_pool &pool) +bool FindCycle(const char *hash, uint8_t edgeBits, uint8_t proofSize, std::set &cycle, ctpl::thread_pool &pool) { switch (edgeBits) { @@ -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 sCycle; - ctpl::thread_pool pool{4}; + ctpl::thread_pool pool{2}; FindCycle(hash, edgeBits, proofSize, sCycle, pool); @@ -1424,4 +1426,4 @@ bool findcycle(const uint32_t *hash, uint8_t edgeBits, uint8_t proofSize, uint32 return true; } -} \ No newline at end of file +} diff --git a/cuckoo/cuckoo.hpp b/cuckoo/cuckoo.hpp index ee57b48..9088abe 100644 --- a/cuckoo/cuckoo.hpp +++ b/cuckoo/cuckoo.hpp @@ -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& cycle, ctpl::thread_pool&); +bool FindCycleAdvanced(const char *hash, uint8_t edgeBits, uint8_t proofSize, std::set& cycle, ctpl::thread_pool&); #endif // MERIT_MINER_CUCKOO_H diff --git a/miner.h b/miner.h index a0b03d4..4e8cac1 100644 --- a/miner.h +++ b/miner.h @@ -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, diff --git a/sha2.c b/sha2.c index 5eec6b7..3690db1 100644 --- a/sha2.c +++ b/sha2.c @@ -593,7 +593,7 @@ 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))); @@ -601,12 +601,9 @@ int scanhash_sha256d(int thr_id, uint32_t *pdata, const uint32_t *ptarget, 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()) @@ -631,11 +628,26 @@ 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; @@ -643,6 +655,9 @@ int scanhash_sha256d(int thr_id, uint32_t *pdata, const uint32_t *ptarget, if (fulltest(hash, ptarget)) { *hashes_done = n - first_nonce + 1; + + printf("\t\tfound hash string: %s\n", hash_str); + return 1; } } diff --git a/util.c b/util.c index c5b0b06..43209fc 100644 --- a/util.c +++ b/util.c @@ -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);