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
nonce += miner_id;
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");
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
// Primer SHA-256
@ -283,12 +292,15 @@ void runMiner(void * task_id) {
Serial.println(""); */
hashes++;
nonce += 2;
if (nonce > TARGET_NONCE) break; //exit
if(!mMiner.inRun) { Serial.println ("MINER WORK ABORTED >> waiting new job"); break;}
// 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++;
//Check target to submit
@ -305,12 +317,24 @@ void runMiner(void * task_id) {
Serial.print(" - TX SHARE: ");
for (size_t i = 0; i < 32; i++)
Serial.printf("%02x", hash[i]);
Serial.println("");
#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("");
mLastTXtoPool = millis();
}
// 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++;
// check if valid header
@ -324,7 +348,9 @@ void runMiner(void * task_id) {
// exit
nonce = MAX_NONCE;
break;
}
}
// increment nonce
nonce += 2;
} // exit if found a valid result or nonce > MAX_NONCE
wc_Sha256Free(&sha256);

View File

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

View File

@ -83,16 +83,16 @@ double le256todouble(const void *target)
double dcut64;
data64 = (uint64_t *)(target + 24);
dcut64 = bswap64(*data64) * 6277101735386680763835789423207666416102355444464034512896.0;
dcut64 = *data64 * 6277101735386680763835789423207666416102355444464034512896.0;
data64 = (uint64_t *)(target + 16);
dcut64 += bswap64(*data64) * 340282366920938463463374607431768211456.0;
dcut64 += *data64 * 340282366920938463463374607431768211456.0;
data64 = (uint64_t *)(target + 8);
dcut64 += bswap64(*data64) * 18446744073709551616.0;
dcut64 += *data64 * 18446744073709551616.0;
data64 = (uint64_t *)(target);
dcut64 += bswap64(*data64);
dcut64 += *data64;
return dcut64;
}
@ -100,7 +100,6 @@ double le256todouble(const void *target)
double diff_from_target(void *target)
{
double d64, dcut64;
//reverse_bytes((uint8_t *) target, 32);
d64 = truediffone;
dcut64 = le256todouble(target);
@ -285,22 +284,23 @@ miner_data calculateMiningData(mining_subscribe& mWorker, mining_job mJob){
}
merkle_root[65] = 0;
Serial.println("");
// calculate blockheader
// j.block_header = ''.join([j.version, j.prevhash, merkle_root, j.ntime, j.nbits])
String blockheader = mJob.version + mJob.prev_block_hash + String(merkle_root) + mJob.ntime + mJob.nbits + "00000000";
String blockheader = mJob.version + mJob.prev_block_hash + String(merkle_root) + mJob.ntime + mJob.nbits + "00000000";
str_len = blockheader.length()/2;
//uint8_t bytearray_blockheader[str_len];
res = to_byte_array(blockheader.c_str(), str_len*2, mMiner.bytearray_blockheader);
#ifdef DEBUG_MINING
Serial.println(" blockheader: "); Serial.print(blockheader);
Serial.println(" blockheader bytes "); Serial.print(str_len); Serial.print(" -> ");
#endif
// reverse version
uint8_t buff;
size_t bsize, boffset;
size_t bword, bsize, boffset;
boffset = 0;
bsize = 4;
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;
}
// reverse merkle
boffset = 36;
// reverse prev hash (4-byte word swap)
boffset = 4;
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 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++) {
buff = mMiner.bytearray_blockheader[j];
mMiner.bytearray_blockheader[j] = mMiner.bytearray_blockheader[2 * boffset + bsize - 1 - j];
mMiner.bytearray_blockheader[2 * boffset + bsize - 1 - j] = buff;
}
// reverse difficulty
boffset = 72;
bsize = 4;
@ -344,11 +372,11 @@ miner_data calculateMiningData(mining_subscribe& mWorker, mining_job mJob){
for (size_t i = 36; i < 36+32; i++)
Serial.printf("%02x", mMiner.bytearray_blockheader[i]);
Serial.println("");
Serial.print("nbits ");
Serial.print("ntime ");
for (size_t i = 68; i < 68+4; i++)
Serial.printf("%02x", mMiner.bytearray_blockheader[i]);
Serial.println("");
Serial.print("difficulty ");
Serial.print("nbits ");
for (size_t i = 72; i < 72+4; i++)
Serial.printf("%02x", mMiner.bytearray_blockheader[i]);
Serial.println("");