Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize ESP8266 miner #1580

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 69 additions & 10 deletions ESP8266_Code/ESP8266_Code.ino
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ String START_DIFF = "";
// Loop WDT... please don't feed me...
// See lwdtcb() and lwdtFeed() below
Ticker lwdTimer;
#define LWD_TIMEOUT 20000
#define LWD_TIMEOUT 90000

unsigned long lwdCurrentMillis = 0;
unsigned long lwdTimeOutMillis = LWD_TIMEOUT;
Expand All @@ -427,6 +427,50 @@ unsigned long lwdTimeOutMillis = LWD_TIMEOUT;
#define BLINK_CLIENT_CONNECT 3
#define BLINK_RESET_DEVICE 5

template <unsigned int max_digits>
class Counter {
public:
Counter() { reset(); }

void reset() {
memset(buffer, '0', max_digits);
buffer[max_digits] = '\0';
val = 0;
len = 1;
}

inline Counter & operator++() {
inc_string(buffer + max_digits - 1);
++val;
return *this;
}

inline operator unsigned int () const { return val; }
inline const char * c_str() const { return buffer + max_digits - len; }
inline size_t strlen() const { return len; }

protected:
inline void inc_string(char * c) {
// In theory, the line below should be uncommented to avoid writing outside the buffer. In practice however,
// with max_digits set to 10 or more, we can fit all possible unsigned 32-bit integers in the buffer. The
// check is skipped to gain a small extra speed improvement.
// if (c >= buffer) return;

if (*c < '9') {
*c += 1;
} else {
*c = '0';
inc_string(c - 1);
len = max(max_digits - (c - buffer) + 1, len);
}
}

protected:
char buffer[max_digits + 1];
unsigned int val;
size_t len;
};

void SetupWifi() {
Serial.println("Connecting to: " + String(SSID));
WiFi.mode(WIFI_STA); // Setup ESP in client mode
Expand Down Expand Up @@ -518,6 +562,23 @@ void handleSystemEvents(void) {
yield();
}

// https://stackoverflow.com/questions/9072320/split-string-into-string-array
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {0, -1};
int max_index = data.length() - 1;

for (int i = 0; i <= max_index && found <= index; i++) {
if (data.charAt(i) == separator || i == max_index) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == max_index) ? i + 1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}

void waitForClientData(void) {
client_buffer = "";

Expand Down Expand Up @@ -674,8 +735,7 @@ void setup() {
void loop() {
br_sha1_context sha1_ctx, sha1_ctx_base;
uint8_t hashArray[20];
String duco_numeric_result_str;


// 1 minute watchdog
lwdtFeed();

Expand Down Expand Up @@ -737,27 +797,26 @@ void loop() {
br_sha1_init(&sha1_ctx_base);
br_sha1_update(&sha1_ctx_base, job.last_block_hash.c_str(), job.last_block_hash.length());

float start_time = micros();
unsigned long start_time = micros();
max_micros_elapsed(start_time, 0);

String result = "";
if (LED_BLINKING) digitalWrite(LED_BUILTIN, LOW);
for (unsigned int duco_numeric_result = 0; duco_numeric_result < job.difficulty; duco_numeric_result++) {
for (Counter<10> counter; counter < difficulty; ++counter) {
// Difficulty loop
sha1_ctx = sha1_ctx_base;
duco_numeric_result_str = String(duco_numeric_result);

br_sha1_update(&sha1_ctx, duco_numeric_result_str.c_str(), duco_numeric_result_str.length());
br_sha1_update(&sha1_ctx, counter.c_str(), counter.strlen());
br_sha1_out(&sha1_ctx, hashArray);

if (memcmp(job.expected_hash, hashArray, 20) == 0) {
// If result is found
if (LED_BLINKING) digitalWrite(LED_BUILTIN, HIGH);
unsigned long elapsed_time = micros() - start_time;
float elapsed_time_s = elapsed_time * .000001f;
hashrate = duco_numeric_result / elapsed_time_s;
hashrate = counter / elapsed_time_s;
share_count++;
client.print(String(duco_numeric_result)
client.print(String(counter)
+ ","
+ String(hashrate)
+ ","
Expand All @@ -774,7 +833,7 @@ void loop() {
Serial.println(client_buffer
+ " share #"
+ String(share_count)
+ " (" + String(duco_numeric_result) + ")"
+ " (" + String(counter) + ")"
+ " hashrate: "
+ String(hashrate / 1000, 2)
+ " kH/s ("
Expand Down