Major Nerdminer code rework

* Rework block header generation
* Fix dual-threaded mining code
* Fix share diff calculation
* Set default difficulty to 1e-4
This commit is contained in:
Stefan Berger 2023-07-23 00:26:19 +02:00
parent 65b08c4733
commit 3a0cb9a83c
3 changed files with 75 additions and 20 deletions

View File

@ -259,10 +259,19 @@ void runMiner(void * task_id) {
// split up odd/even nonces between miner tasks // split up odd/even nonces between miner tasks
nonce += miner_id; nonce += miner_id;
uint32_t startT = micros(); uint32_t startT = micros();
unsigned char *header64 = mMiner.bytearray_blockheader + 64; unsigned char *header64;
// each miner thread needs to track its own blockheader template
memcpy(mMiner.bytearray_blockheader2, &mMiner.bytearray_blockheader, 80);
if (miner_id == 0)
header64 = mMiner.bytearray_blockheader + 64;
else
header64 = mMiner.bytearray_blockheader2 + 64;
Serial.println(">>> STARTING TO HASH NONCES"); Serial.println(">>> STARTING TO HASH NONCES");
while(true) { while(true) {
memcpy(mMiner.bytearray_blockheader + 76, &nonce, 4); if (miner_id == 0)
memcpy(mMiner.bytearray_blockheader + 76, &nonce, 4);
else
memcpy(mMiner.bytearray_blockheader2 + 76, &nonce, 4);
//Con midstate //Con midstate
// Primer SHA-256 // Primer SHA-256
@ -283,12 +292,15 @@ void runMiner(void * task_id) {
Serial.println(""); */ Serial.println(""); */
hashes++; hashes++;
nonce += 2;
if (nonce > TARGET_NONCE) break; //exit if (nonce > TARGET_NONCE) break; //exit
if(!mMiner.inRun) { Serial.println ("MINER WORK ABORTED >> waiting new job"); break;} if(!mMiner.inRun) { Serial.println ("MINER WORK ABORTED >> waiting new job"); break;}
// check if 16bit share // check if 16bit share
if(hash[31] !=0 || hash[30] !=0) continue; if(hash[31] !=0 || hash[30] !=0) {
// increment nonce
nonce += 2;
continue;
}
halfshares++; halfshares++;
//Check target to submit //Check target to submit
@ -305,12 +317,24 @@ void runMiner(void * task_id) {
Serial.print(" - TX SHARE: "); Serial.print(" - TX SHARE: ");
for (size_t i = 0; i < 32; i++) for (size_t i = 0; i < 32; i++)
Serial.printf("%02x", hash[i]); Serial.printf("%02x", hash[i]);
#ifdef DEBUG_MINING
Serial.println("");
Serial.print(" - Current nonce: "); Serial.println(nonce);
Serial.print(" - Current block header: ");
for (size_t i = 0; i < 80; i++) {
Serial.printf("%02x", mMiner.bytearray_blockheader[i]);
}
#endif
Serial.println(""); Serial.println("");
mLastTXtoPool = millis(); mLastTXtoPool = millis();
} }
// check if 32bit share // check if 32bit share
if(hash[29] !=0 || hash[28] !=0) continue; if(hash[29] !=0 || hash[28] !=0) {
// increment nonce
nonce += 2;
continue;
}
shares++; shares++;
// check if valid header // check if valid header
@ -325,6 +349,8 @@ void runMiner(void * task_id) {
nonce = MAX_NONCE; nonce = MAX_NONCE;
break; break;
} }
// increment nonce
nonce += 2;
} // exit if found a valid result or nonce > MAX_NONCE } // exit if found a valid result or nonce > MAX_NONCE
wc_Sha256Free(&sha256); wc_Sha256Free(&sha256);

View File

@ -5,7 +5,7 @@
// Mining // Mining
#define MAX_NONCE 5000000U #define MAX_NONCE 5000000U
#define TARGET_NONCE 471136297U #define TARGET_NONCE 471136297U
#define DEFAULT_DIFFICULTY 1e-9 #define DEFAULT_DIFFICULTY 1e-4
#define KEEPALIVE_TIME_ms 30000 #define KEEPALIVE_TIME_ms 30000
#define POOLINACTIVITY_TIME_ms 60000 #define POOLINACTIVITY_TIME_ms 60000
@ -21,6 +21,7 @@ typedef struct{
uint8_t bytearray_pooltarget[32]; uint8_t bytearray_pooltarget[32];
uint8_t merkle_result[32]; uint8_t merkle_result[32];
uint8_t bytearray_blockheader[80]; uint8_t bytearray_blockheader[80];
uint8_t bytearray_blockheader2[80];
double poolDifficulty; double poolDifficulty;
bool inRun; bool inRun;
bool newJob; bool newJob;

View File

@ -83,16 +83,16 @@ double le256todouble(const void *target)
double dcut64; double dcut64;
data64 = (uint64_t *)(target + 24); data64 = (uint64_t *)(target + 24);
dcut64 = bswap64(*data64) * 6277101735386680763835789423207666416102355444464034512896.0; dcut64 = *data64 * 6277101735386680763835789423207666416102355444464034512896.0;
data64 = (uint64_t *)(target + 16); data64 = (uint64_t *)(target + 16);
dcut64 += bswap64(*data64) * 340282366920938463463374607431768211456.0; dcut64 += *data64 * 340282366920938463463374607431768211456.0;
data64 = (uint64_t *)(target + 8); data64 = (uint64_t *)(target + 8);
dcut64 += bswap64(*data64) * 18446744073709551616.0; dcut64 += *data64 * 18446744073709551616.0;
data64 = (uint64_t *)(target); data64 = (uint64_t *)(target);
dcut64 += bswap64(*data64); dcut64 += *data64;
return dcut64; return dcut64;
} }
@ -100,7 +100,6 @@ double le256todouble(const void *target)
double diff_from_target(void *target) double diff_from_target(void *target)
{ {
double d64, dcut64; double d64, dcut64;
//reverse_bytes((uint8_t *) target, 32);
d64 = truediffone; d64 = truediffone;
dcut64 = le256todouble(target); dcut64 = le256todouble(target);
@ -295,12 +294,13 @@ miner_data calculateMiningData(mining_subscribe& mWorker, mining_job mJob){
res = to_byte_array(blockheader.c_str(), str_len*2, mMiner.bytearray_blockheader); res = to_byte_array(blockheader.c_str(), str_len*2, mMiner.bytearray_blockheader);
#ifdef DEBUG_MINING #ifdef DEBUG_MINING
Serial.println(" blockheader: "); Serial.print(blockheader);
Serial.println(" blockheader bytes "); Serial.print(str_len); Serial.print(" -> "); Serial.println(" blockheader bytes "); Serial.print(str_len); Serial.print(" -> ");
#endif #endif
// reverse version // reverse version
uint8_t buff; uint8_t buff;
size_t bsize, boffset; size_t bword, bsize, boffset;
boffset = 0; boffset = 0;
bsize = 4; bsize = 4;
for (size_t j = boffset; j < boffset + (bsize/2); j++) { for (size_t j = boffset; j < boffset + (bsize/2); j++) {
@ -309,14 +309,42 @@ miner_data calculateMiningData(mining_subscribe& mWorker, mining_job mJob){
mMiner.bytearray_blockheader[2 * boffset + bsize - 1 - j] = buff; mMiner.bytearray_blockheader[2 * boffset + bsize - 1 - j] = buff;
} }
// reverse merkle // reverse prev hash (4-byte word swap)
boffset = 36; boffset = 4;
bword = 4;
bsize = 32; bsize = 32;
for (size_t i = 1; i <= bsize / bword; i++) {
for (size_t j = boffset; j < boffset + bword / 2; j++) {
buff = mMiner.bytearray_blockheader[j];
mMiner.bytearray_blockheader[j] = mMiner.bytearray_blockheader[2 * boffset + bword - 1 - j];
mMiner.bytearray_blockheader[2 * boffset + bword - 1 - j] = buff;
}
boffset += bword;
}
/*
// reverse merkle (4-byte word swap)
boffset = 36;
bword = 4;
bsize = 32;
for (size_t i = 1; i <= bsize / bword; i++) {
for (size_t j = boffset; j < boffset + bword / 2; j++) {
buff = mMiner.bytearray_blockheader[j];
mMiner.bytearray_blockheader[j] = mMiner.bytearray_blockheader[2 * boffset + bword - 1 - j];
mMiner.bytearray_blockheader[2 * boffset + bword - 1 - j] = buff;
}
boffset += bword;
}
*/
// reverse ntime
boffset = 68;
bsize = 4;
for (size_t j = boffset; j < boffset + (bsize/2); j++) { for (size_t j = boffset; j < boffset + (bsize/2); j++) {
buff = mMiner.bytearray_blockheader[j]; buff = mMiner.bytearray_blockheader[j];
mMiner.bytearray_blockheader[j] = mMiner.bytearray_blockheader[2 * boffset + bsize - 1 - j]; mMiner.bytearray_blockheader[j] = mMiner.bytearray_blockheader[2 * boffset + bsize - 1 - j];
mMiner.bytearray_blockheader[2 * boffset + bsize - 1 - j] = buff; mMiner.bytearray_blockheader[2 * boffset + bsize - 1 - j] = buff;
} }
// reverse difficulty // reverse difficulty
boffset = 72; boffset = 72;
bsize = 4; bsize = 4;
@ -344,11 +372,11 @@ miner_data calculateMiningData(mining_subscribe& mWorker, mining_job mJob){
for (size_t i = 36; i < 36+32; i++) for (size_t i = 36; i < 36+32; i++)
Serial.printf("%02x", mMiner.bytearray_blockheader[i]); Serial.printf("%02x", mMiner.bytearray_blockheader[i]);
Serial.println(""); Serial.println("");
Serial.print("nbits "); Serial.print("ntime ");
for (size_t i = 68; i < 68+4; i++) for (size_t i = 68; i < 68+4; i++)
Serial.printf("%02x", mMiner.bytearray_blockheader[i]); Serial.printf("%02x", mMiner.bytearray_blockheader[i]);
Serial.println(""); Serial.println("");
Serial.print("difficulty "); Serial.print("nbits ");
for (size_t i = 72; i < 72+4; i++) for (size_t i = 72; i < 72+4; i++)
Serial.printf("%02x", mMiner.bytearray_blockheader[i]); Serial.printf("%02x", mMiner.bytearray_blockheader[i]);
Serial.println(""); Serial.println("");