1717#include < list>
1818#include < map>
1919#include " mbedtls/sha256.h"
20+ // I2C mining support (disabled by default for performance)
21+ #ifdef ENABLE_I2C_MINING
2022#include " i2c_master.h"
23+ #define I2C_SLAVE_ENABLED
24+ #endif
2125
22- // 10 Jobs per second
23- #define NONCE_PER_JOB_SW 4096
24- #define NONCE_PER_JOB_HW 16 *1024
25-
26- // #define I2C_SLAVE
26+ // Reduce memory usage for ESP32 classic to prevent crashes
27+ #if defined(CONFIG_IDF_TARGET_ESP32)
28+ #define NONCE_PER_JOB_SW 2048 // Reduced from 4096
29+ #define NONCE_PER_JOB_HW 8 *1024 // Reduced from 16*1024
30+ #define JOB_QUEUE_SIZE 2 // Reduced queue size
31+ #define RESULT_QUEUE_SIZE 8 // Reduced result queue
32+ #else
33+ #define NONCE_PER_JOB_SW 4096 // Normal for S3/S2/C3
34+ #define NONCE_PER_JOB_HW 16 *1024
35+ #define JOB_QUEUE_SIZE 4 // Normal queue size
36+ #define RESULT_QUEUE_SIZE 16 // Normal result queue
37+ #endif
2738
2839// #define SHA256_VALIDATE
2940// #define RANDOM_NONCE
@@ -93,8 +104,11 @@ bool checkPoolConnection(void) {
93104 // Try connecting pool IP
94105 if (!client.connect (serverIP, Settings.PoolPort )) {
95106 Serial.println (" Imposible to connect to : " + Settings.PoolAddress );
96- WiFi.hostByName (Settings.PoolAddress .c_str (), serverIP);
97- Serial.printf (" Resolved DNS got: %s\n " , serverIP.toString ());
107+ // Only retry DNS resolution if we have an invalid IP
108+ if (serverIP == IPAddress (1 ,1 ,1 ,1 )) {
109+ WiFi.hostByName (Settings.PoolAddress .c_str (), serverIP);
110+ Serial.printf (" Resolved DNS got: %s\n " , serverIP.toString ());
111+ }
98112 return false ;
99113 }
100114
@@ -229,7 +243,7 @@ void runStratumWorker(void *name) {
229243
230244 std::map<uint32_t , std::shared_ptr<Submition>> s_submition_map;
231245
232- #ifdef I2C_SLAVE
246+ #ifdef I2C_SLAVE_ENABLED_ENABLED
233247 std::vector<uint8_t > i2c_slave_vector;
234248
235249 // scan for i2c slaves
@@ -330,7 +344,8 @@ void runStratumWorker(void *name) {
330344 // Read pending messages from pool
331345 while (client.connected () && client.available ())
332346 {
333- String line = client.readStringUntil (' \n ' );
347+ static String line; // Reutilizar el mismo objeto String
348+ line = client.readStringUntil (' \n ' );
334349 // Serial.println(" Received message from pool");
335350 stratum_method result = parse_mining_method (line);
336351 switch (result)
@@ -384,7 +399,7 @@ void runStratumWorker(void *name) {
384399 #ifdef RANDOM_NONCE
385400 nonce_pool = RandomGet () & RANDOM_NONCE_MASK;
386401 #else
387- #ifdef I2C_SLAVE
402+ #ifdef I2C_SLAVE_ENABLED
388403 if (!i2c_slave_vector.empty ())
389404 nonce_pool = 0x10000000 ;
390405 else
@@ -419,7 +434,7 @@ void runStratumWorker(void *name) {
419434 #endif
420435 }
421436 }
422- #ifdef I2C_SLAVE
437+ #ifdef I2C_SLAVE_ENABLED
423438 // Nonce for nonce_pool starts from 0x10000000
424439 // For i2c slave we give nonces from 0x20000000, that is 0x10000000 nonces per slave
425440 i2c_feed_slaves (i2c_slave_vector, job_pool & 0xFF , 0x20 , currentPoolDifficulty, mMiner .bytearray_blockheader );
@@ -468,7 +483,7 @@ void runStratumWorker(void *name) {
468483 }
469484
470485 std::list<std::shared_ptr<JobResult>> job_result_list;
471- #ifdef I2C_SLAVE
486+ #ifdef I2C_SLAVE_ENABLED
472487 if (i2c_slave_vector.empty () || job_pool == 0xFFFFFFFF )
473488 {
474489 vTaskDelay (50 / portTICK_PERIOD_MS); // Small delay
@@ -516,7 +531,7 @@ void runStratumWorker(void *name) {
516531 s_job_result_list.clear ();
517532
518533#if 1
519- while (s_job_request_list_sw.size () < 4 )
534+ while (s_job_request_list_sw.size () < JOB_QUEUE_SIZE )
520535 {
521536 JobPush ( s_job_request_list_sw, job_pool, nonce_pool, NONCE_PER_JOB_SW, currentPoolDifficulty, mMiner .bytearray_blockheader , diget_mid, bake);
522537 #ifdef RANDOM_NONCE
@@ -528,7 +543,7 @@ void runStratumWorker(void *name) {
528543#endif
529544
530545 #ifdef HARDWARE_SHA265
531- while (s_job_request_list_hw.size () < 4 )
546+ while (s_job_request_list_hw.size () < JOB_QUEUE_SIZE )
532547 {
533548 #if defined(CONFIG_IDF_TARGET_ESP32)
534549 JobPush ( s_job_request_list_hw, job_pool, nonce_pool, NONCE_PER_JOB_HW, currentPoolDifficulty, sha_buffer_swap, hw_midstate, bake);
@@ -598,7 +613,7 @@ void minerWorkerSw(void * task_id)
598613 std::lock_guard<std::mutex> lock (s_job_mutex);
599614 if (result)
600615 {
601- if (s_job_result_list.size () < 16 )
616+ if (s_job_result_list.size () < RESULT_QUEUE_SIZE )
602617 s_job_result_list.push_back (result);
603618 result.reset ();
604619 }
@@ -804,7 +819,7 @@ void minerWorkerHw(void * task_id)
804819 std::lock_guard<std::mutex> lock (s_job_mutex);
805820 if (result)
806821 {
807- if (s_job_result_list.size () < 16 )
822+ if (s_job_result_list.size () < RESULT_QUEUE_SIZE )
808823 s_job_result_list.push_back (result);
809824 result.reset ();
810825 }
@@ -835,17 +850,13 @@ void minerWorkerHw(void * task_id)
835850 uint32_t nend = job->nonce_start + job->nonce_count ;
836851 for (uint32_t n = job->nonce_start ; n < nend; ++n)
837852 {
838- // nerd_sha_hal_wait_idle();
839853 nerd_sha_ll_write_digest (digest_mid);
840- // nerd_sha_hal_wait_idle();
841854 nerd_sha_ll_fill_text_block_sha256 (sha_buffer, n);
842- // sha_ll_continue_block(SHA2_256);
843855 REG_WRITE (SHA_CONTINUE_REG, 1 );
844856
845857 sha_ll_load (SHA2_256);
846858 nerd_sha_hal_wait_idle ();
847859 nerd_sha_ll_fill_text_block_sha256_inter ();
848- // sha_ll_start_block(SHA2_256);
849860 REG_WRITE (SHA_START_REG, 1 );
850861 sha_ll_load (SHA2_256);
851862 nerd_sha_hal_wait_idle ();
@@ -1045,7 +1056,7 @@ void minerWorkerHw(void * task_id)
10451056 std::lock_guard<std::mutex> lock (s_job_mutex);
10461057 if (result)
10471058 {
1048- if (s_job_result_list.size () < 16 )
1059+ if (s_job_result_list.size () < RESULT_QUEUE_SIZE )
10491060 s_job_result_list.push_back (result);
10501061 result.reset ();
10511062 }
@@ -1067,32 +1078,27 @@ void minerWorkerHw(void * task_id)
10671078 memcpy (sha_buffer, job->sha_buffer , 80 );
10681079
10691080 esp_sha_lock_engine (SHA2_256);
1081+ uint32_t processed_nonces = 0 ; // Track actually processed nonces
10701082 for (uint32_t n = 0 ; n < job->nonce_count ; ++n)
10711083 {
1072- // ((uint32_t*)(sha_buffer+64+12))[0] = __builtin_bswap32(job->nonce_start+n);
1073-
1074- // sha_hal_hash_block(SHA2_256, s_test_buffer, 64/4, true);
1075- // nerd_sha_hal_wait_idle();
10761084 nerd_sha_ll_fill_text_block_sha256 (sha_buffer);
10771085 sha_ll_start_block (SHA2_256);
10781086
1079- // sha_hal_hash_block(SHA2_256, s_test_buffer+64, 64/4, false);
10801087 nerd_sha_hal_wait_idle ();
10811088 nerd_sha_ll_fill_text_block_sha256_upper (sha_buffer+64 , job->nonce_start +n);
10821089 sha_ll_continue_block (SHA2_256);
10831090
10841091 nerd_sha_hal_wait_idle ();
10851092 sha_ll_load (SHA2_256);
10861093
1087- // sha_hal_hash_block(SHA2_256, interResult, 64/4, true);
1088- nerd_sha_hal_wait_idle ();
10891094 nerd_sha_ll_fill_text_block_sha256_double ();
10901095 sha_ll_start_block (SHA2_256);
10911096
10921097 nerd_sha_hal_wait_idle ();
10931098 sha_ll_load (SHA2_256);
10941099 if (nerd_sha_ll_read_digest_swap_if (hash))
10951100 {
1101+ processed_nonces++; // Only count successful hash operations
10961102 // ~5 per second
10971103 double diff_hash = diff_from_target (hash);
10981104 if (diff_hash > result->difficulty )
@@ -1109,10 +1115,12 @@ void minerWorkerHw(void * task_id)
11091115 (uint8_t )(n & 0xFF ) == 0 &&
11101116 s_working_current_job_id != job_in_work)
11111117 {
1112- result->nonce_count = n+ 1 ;
1118+ result->nonce_count = processed_nonces; // Use actual processed count
11131119 break ;
11141120 }
11151121 }
1122+ // Update final count with actual processed nonces
1123+ result->nonce_count = processed_nonces;
11161124 esp_sha_unlock_engine (SHA2_256);
11171125 } else
11181126 vTaskDelay (2 / portTICK_PERIOD_MS);
0 commit comments