Disable i2c master

Added HW sha256 bug workaround
Added list size limit
This commit is contained in:
evgenykz
2025-02-17 10:52:30 +02:00
parent 5640c9a6ee
commit 4b5eae1c86
3 changed files with 46 additions and 15 deletions

View File

@@ -23,6 +23,7 @@
#define NONCE_PER_JOB_SW 4096 #define NONCE_PER_JOB_SW 4096
#define NONCE_PER_JOB_HW 16*1024 #define NONCE_PER_JOB_HW 16*1024
//#define I2C_SLAVE
//#define SHA256_VALIDATE //#define SHA256_VALIDATE
//#define RANDOM_NONCE //#define RANDOM_NONCE
@@ -226,13 +227,14 @@ void runStratumWorker(void *name) {
Serial.printf("### [Total Heap / Free heap / Min free heap]: %d / %d / %d \n", ESP.getHeapSize(), ESP.getFreeHeap(), ESP.getMinFreeHeap()); Serial.printf("### [Total Heap / Free heap / Min free heap]: %d / %d / %d \n", ESP.getHeapSize(), ESP.getFreeHeap(), ESP.getMinFreeHeap());
#endif #endif
std::vector<uint8_t> i2c_slave_vector;
std::map<uint32_t, std::shared_ptr<Submition>> s_submition_map; std::map<uint32_t, std::shared_ptr<Submition>> s_submition_map;
#ifdef I2C_SLAVE
std::vector<uint8_t> i2c_slave_vector;
//scan for i2c slaves //scan for i2c slaves
if (i2c_master_start() == 0) if (i2c_master_start() == 0)
i2c_slave_vector = i2c_master_scan(0x0, 0x80); i2c_slave_vector = i2c_master_scan(0x0, 0x80);
Serial.printf("Found %d slave workers\n", i2c_slave_vector.size()); Serial.printf("Found %d slave workers\n", i2c_slave_vector.size());
if (!i2c_slave_vector.empty()) if (!i2c_slave_vector.empty())
{ {
@@ -241,6 +243,7 @@ void runStratumWorker(void *name) {
Serial.printf("0x%02X,", (uint32_t)i2c_slave_vector[n]); Serial.printf("0x%02X,", (uint32_t)i2c_slave_vector[n]);
Serial.println(""); Serial.println("");
} }
#endif
// connect to pool // connect to pool
double currentPoolDifficulty = DEFAULT_DIFFICULTY; double currentPoolDifficulty = DEFAULT_DIFFICULTY;
@@ -381,10 +384,12 @@ void runStratumWorker(void *name) {
#ifdef RANDOM_NONCE #ifdef RANDOM_NONCE
nonce_pool = RandomGet() & RANDOM_NONCE_MASK; nonce_pool = RandomGet() & RANDOM_NONCE_MASK;
#else #else
if (i2c_slave_vector.empty()) #ifdef I2C_SLAVE
nonce_pool = 0xDA54E700; //nonce 0x00000000 is not possible, start from some random nonce if (!i2c_slave_vector.empty())
else
nonce_pool = 0x10000000; nonce_pool = 0x10000000;
else
#endif
nonce_pool = 0xDA54E700; //nonce 0x00000000 is not possible, start from some random nonce
#endif #endif
@@ -414,9 +419,11 @@ void runStratumWorker(void *name) {
#endif #endif
} }
} }
#ifdef I2C_SLAVE
//Nonce for nonce_pool starts from 0x10000000 //Nonce for nonce_pool starts from 0x10000000
//For i2c slave we give nonces from 0x20000000, that is 0x10000000 nonces per slave //For i2c slave we give nonces from 0x20000000, that is 0x10000000 nonces per slave
i2c_feed_slaves(i2c_slave_vector, job_pool & 0xFF, 0x20, currentPoolDifficulty, mMiner.bytearray_blockheader); i2c_feed_slaves(i2c_slave_vector, job_pool & 0xFF, 0x20, currentPoolDifficulty, mMiner.bytearray_blockheader);
#endif
} else } else
{ {
Serial.println("Parsing error, need restart"); Serial.println("Parsing error, need restart");
@@ -461,6 +468,7 @@ void runStratumWorker(void *name) {
} }
std::list<std::shared_ptr<JobResult>> job_result_list; std::list<std::shared_ptr<JobResult>> job_result_list;
#ifdef I2C_SLAVE
if (i2c_slave_vector.empty() || job_pool == 0xFFFFFFFF) if (i2c_slave_vector.empty() || job_pool == 0xFFFFFFFF)
{ {
vTaskDelay(50 / portTICK_PERIOD_MS); //Small delay vTaskDelay(50 / portTICK_PERIOD_MS); //Small delay
@@ -496,6 +504,9 @@ void runStratumWorker(void *name) {
} else } else
vTaskDelay(40 / portTICK_PERIOD_MS); vTaskDelay(40 / portTICK_PERIOD_MS);
} }
#else
vTaskDelay(50 / portTICK_PERIOD_MS); //Small delay
#endif
if (job_pool != 0xFFFFFFFF) if (job_pool != 0xFFFFFFFF)
@@ -587,6 +598,7 @@ void minerWorkerSw(void * task_id)
std::lock_guard<std::mutex> lock(s_job_mutex); std::lock_guard<std::mutex> lock(s_job_mutex);
if (result) if (result)
{ {
if (s_job_result_list.size() < 16)
s_job_result_list.push_back(result); s_job_result_list.push_back(result);
result.reset(); result.reset();
} }
@@ -792,6 +804,7 @@ void minerWorkerHw(void * task_id)
std::lock_guard<std::mutex> lock(s_job_mutex); std::lock_guard<std::mutex> lock(s_job_mutex);
if (result) if (result)
{ {
if (s_job_result_list.size() < 16)
s_job_result_list.push_back(result); s_job_result_list.push_back(result);
result.reset(); result.reset();
} }
@@ -855,12 +868,15 @@ void minerWorkerHw(void * task_id)
//~5 per second //~5 per second
double diff_hash = diff_from_target(hash); double diff_hash = diff_from_target(hash);
if (diff_hash > result->difficulty) if (diff_hash > result->difficulty)
{
if (isSha256Valid(hash))
{ {
result->difficulty = diff_hash; result->difficulty = diff_hash;
result->nonce = n; result->nonce = n;
memcpy(result->hash, hash, sizeof(hash)); memcpy(result->hash, hash, sizeof(hash));
} }
} }
}
if ( if (
(uint8_t)(n & 0xFF) == 0 && (uint8_t)(n & 0xFF) == 0 &&
s_working_current_job_id != job_in_work) s_working_current_job_id != job_in_work)
@@ -1029,6 +1045,7 @@ void minerWorkerHw(void * task_id)
std::lock_guard<std::mutex> lock(s_job_mutex); std::lock_guard<std::mutex> lock(s_job_mutex);
if (result) if (result)
{ {
if (s_job_result_list.size() < 16)
s_job_result_list.push_back(result); s_job_result_list.push_back(result);
result.reset(); result.reset();
} }
@@ -1079,12 +1096,15 @@ void minerWorkerHw(void * task_id)
//~5 per second //~5 per second
double diff_hash = diff_from_target(hash); double diff_hash = diff_from_target(hash);
if (diff_hash > result->difficulty) if (diff_hash > result->difficulty)
{
if (isSha256Valid(hash))
{ {
result->difficulty = diff_hash; result->difficulty = diff_hash;
result->nonce = job->nonce_start+n; result->nonce = job->nonce_start+n;
memcpy(result->hash, hash, sizeof(hash)); memcpy(result->hash, hash, sizeof(hash));
} }
} }
}
if ( if (
(uint8_t)(n & 0xFF) == 0 && (uint8_t)(n & 0xFF) == 0 &&
s_working_current_job_id != job_in_work) s_working_current_job_id != job_in_work)

View File

@@ -108,6 +108,16 @@ double diff_from_target(void *target)
return d64 / dcut64; return d64 / dcut64;
} }
bool isSha256Valid(const void* sha256)
{
for(uint8_t i=0; i < 8; ++i)
{
if ( ((const uint32_t*)sha256)[i] != 0 )
return true;
}
return false;
}
/****************** PREMINING CALCULATIONS ********************/ /****************** PREMINING CALCULATIONS ********************/

View File

@@ -20,6 +20,7 @@ uint8_t hex(char ch);
int to_byte_array(const char *in, size_t in_size, uint8_t *out); int to_byte_array(const char *in, size_t in_size, uint8_t *out);
double le256todouble(const void *target); double le256todouble(const void *target);
double diff_from_target(void *target); double diff_from_target(void *target);
bool isSha256Valid(const void* sha256);
miner_data calculateMiningData(mining_subscribe& mWorker, mining_job mJob); miner_data calculateMiningData(mining_subscribe& mWorker, mining_job mJob);
bool checkValid(unsigned char* hash, unsigned char* target); bool checkValid(unsigned char* hash, unsigned char* target);
void suffix_string(double val, char *buf, size_t bufsiz, int sigdigits); void suffix_string(double val, char *buf, size_t bufsiz, int sigdigits);