Skip to content

Commit

Permalink
rework random and network timeout esp8266
Browse files Browse the repository at this point in the history
  • Loading branch information
Matteo Crippa committed May 2, 2024
1 parent de3fcb4 commit 91c7e56
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
3 changes: 1 addition & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ build_flags =
-DLOG_LEVEL=3
-fexceptions
lib_deps =
https://github.com/matteocrippa/TridentTD_ESP_TrueRandom
https://github.com/DaveGamble/cJSON
https://github.com/me-no-dev/ESPAsyncWebServer.git
vshymanskyy/Preferences@^2.1.0
Expand All @@ -45,7 +44,6 @@ build_src_filter =
-<.*/*>
-<screen/lilygo-t-s3*>
lib_deps =
https://github.com/matteocrippa/TridentTD_ESP_TrueRandom
https://github.com/DaveGamble/cJSON
vshymanskyy/Preferences@^2.1.0
https://github.com/me-no-dev/ESPAsyncWebServer.git
Expand Down Expand Up @@ -173,6 +171,7 @@ monitor_filters =
time
lib_deps =
bodmer/TFT_eSPI @ 2.5.43
https://github.com/DaveGamble/cJSON.git
https://github.com/me-no-dev/ESPAsyncWebServer.git
build_flags =
-O2
Expand Down
2 changes: 0 additions & 2 deletions src/current.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ void current_check_stale()

void handleException()
{
// Handle exceptions gracefully, e.g., log the error, perform cleanup, etc.
l_error(TAG_CURRENT, "Exception occurred. Cleaning up resources...");
cleanupResources();
}
Expand All @@ -267,7 +266,6 @@ void cleanupResources()
{
deleteCurrentJob();
deleteCurrentSubscribe();
// Additional cleanup code can be added here if necessary
}

#if defined(ESP32)
Expand Down
9 changes: 7 additions & 2 deletions src/model/job.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "job.h"
#if defined(ESP8266)
#include "TridentTD_ESP_TrueRandom.h"
#include <ESP8266WiFi.h>
#else
#include "esp_random.h"
#endif
Expand Down Expand Up @@ -79,7 +79,8 @@ std::string Job::generate_extra_nonce2(int extranonce2_size)
{
// Generate a random number between 0 and INT_MAX
#if defined(ESP8266)
uint32_t randomValue = esp.random(0, INT_MAX);
randomSeed(analogRead(A0));
uint32_t randomValue = random();
#else
uint32_t randomValue = esp_random();
#endif
Expand All @@ -89,6 +90,8 @@ std::string Job::generate_extra_nonce2(int extranonce2_size)
char hexString[9]; // Enough to hold a 32-bit integer in hex (including null terminator)
snprintf(hexString, sizeof(hexString), "%08X", randomValue);

l_info(TAG_JOB, "Hex value: %s", hexString);

return std::string(hexString);
}
catch (...)
Expand All @@ -108,6 +111,7 @@ void Job::generateCoinbaseHash(const std::string &coinbase, std::string &coinbas
uint8_t hash[SHA256M_BLOCK_SIZE];
sha256_double(coinbaseBytes, len / 2, hash);
coinbase_hash = byteArrayToHexString(hash, SHA256M_BLOCK_SIZE);
l_debug(TAG_JOB, "Coinbase hash: %s", coinbase_hash.c_str());
}
catch (...)
{
Expand Down Expand Up @@ -138,6 +142,7 @@ void Job::calculateMerkleRoot(const std::string &coinbase_hash, const std::vecto
}

merkle_root = byteArrayToHexString(hash, SHA256M_BLOCK_SIZE);
l_debug(TAG_JOB, "Merkle root: %s", merkle_root.c_str());
}
catch (...)
{
Expand Down
20 changes: 14 additions & 6 deletions src/network/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,24 @@ void network_send(const std::string &job_id, const std::string &extranonce2, con

void network_listen()
{
uint32_t start_time = millis();
uint32_t len = 0;

if (isConnected() == -1)
{
current_resetSession();
return; // Handle connection failure
}

do
{
// Check if 5 seconds have elapsed
if (millis() - start_time > 5000)
{
l_debug(TAG_NETWORK, "Timeout occurred. Exiting network_listen loop.");
return;
}

char data[NETWORK_BUFFER_SIZE];
len = client.readBytesUntil('\n', data, sizeof(data) - 1);
l_debug(TAG_NETWORK, "<<< len: %d", len);
Expand All @@ -417,12 +431,6 @@ void network_listen()
{
response(data);
}
if (isConnected() == -1)
{
current_resetSession();

return; // Handle connection failure
}

} while (len > 0);
}
Expand Down

0 comments on commit 91c7e56

Please sign in to comment.