2023-03-20 01:10:44 +01:00
|
|
|
#include <Arduino.h>
|
|
|
|
#include <ArduinoJson.h>
|
|
|
|
#include <WiFi.h>
|
2023-07-11 17:12:02 +02:00
|
|
|
#include <esp_task_wdt.h>
|
2023-04-17 02:07:18 +02:00
|
|
|
#include <TFT_eSPI.h> // Graphics and font library for ILI9341 driver chip
|
2023-07-30 13:10:39 +02:00
|
|
|
#include "ShaTests/nerdSHA256.h"
|
2023-08-20 00:45:09 +02:00
|
|
|
//#include "ShaTests/nerdSHA256plus.h"
|
2023-04-07 00:43:20 +02:00
|
|
|
#include "media/Free_Fonts.h"
|
|
|
|
#include "media/images.h"
|
2023-03-20 01:10:44 +01:00
|
|
|
#include "OpenFontRender.h"
|
2023-05-26 13:02:14 +02:00
|
|
|
#include "stratum.h"
|
2023-03-20 01:10:44 +01:00
|
|
|
#include "mining.h"
|
2023-05-26 13:02:14 +02:00
|
|
|
#include "utils.h"
|
2023-06-07 10:51:46 +02:00
|
|
|
#include "monitor.h"
|
2023-04-17 02:07:18 +02:00
|
|
|
|
2023-06-07 10:51:46 +02:00
|
|
|
unsigned long templates = 0;
|
|
|
|
unsigned long hashes= 0;
|
|
|
|
unsigned long Mhashes = 0;
|
|
|
|
unsigned long totalKHashes = 0;
|
|
|
|
unsigned long elapsedKHs = 0;
|
2023-05-08 01:31:05 +02:00
|
|
|
|
2023-06-07 10:51:46 +02:00
|
|
|
unsigned int shares; // increase if blockhash has 32 bits of zeroes
|
|
|
|
unsigned int valids; // increased if blockhash <= target
|
2023-03-20 01:10:44 +01:00
|
|
|
|
2023-07-24 21:33:15 +02:00
|
|
|
// Track best diff
|
|
|
|
double best_diff = 0.0;
|
|
|
|
|
2023-03-20 01:10:44 +01:00
|
|
|
// Variables to hold data from custom textboxes
|
|
|
|
extern char poolString[80];
|
|
|
|
extern int portNumber;
|
|
|
|
extern char btcString[80];
|
2023-06-07 14:12:45 +02:00
|
|
|
IPAddress serverIP(1, 1, 1, 1); //Temporally save poolIPaddres
|
2023-03-20 01:10:44 +01:00
|
|
|
|
|
|
|
extern OpenFontRender render;
|
|
|
|
extern TFT_eSprite background;
|
|
|
|
|
2023-05-26 13:02:14 +02:00
|
|
|
//Global work data
|
|
|
|
static WiFiClient client;
|
|
|
|
static miner_data mMiner; //Global miner data (Create a miner class TODO)
|
|
|
|
mining_subscribe mWorker;
|
|
|
|
mining_job mJob;
|
2023-06-07 10:51:46 +02:00
|
|
|
monitor_data mMonitor;
|
|
|
|
bool isMinerSuscribed = false;
|
2023-06-07 14:12:45 +02:00
|
|
|
unsigned long mLastTXtoPool = millis();
|
2023-05-03 21:11:59 +02:00
|
|
|
|
2023-08-03 01:13:52 +02:00
|
|
|
bool checkPoolConnection(void) {
|
2023-06-04 20:02:23 +02:00
|
|
|
|
|
|
|
if (client.connected()) {
|
2023-08-03 01:13:52 +02:00
|
|
|
return true;
|
2023-06-04 20:02:23 +02:00
|
|
|
}
|
|
|
|
|
2023-06-07 14:12:45 +02:00
|
|
|
isMinerSuscribed = false;
|
|
|
|
|
2023-06-04 20:02:23 +02:00
|
|
|
Serial.println("Client not connected, trying to connect...");
|
2023-06-07 14:12:45 +02:00
|
|
|
|
|
|
|
//Resolve first time pool DNS and save IP
|
|
|
|
if(serverIP == IPAddress(1,1,1,1)) {
|
|
|
|
WiFi.hostByName(poolString, serverIP);
|
|
|
|
Serial.printf("Resolved DNS and save ip (first time) got: %s\n", serverIP.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
//Try connecting pool IP
|
|
|
|
if (!client.connect(serverIP, portNumber)) {
|
2023-06-04 20:02:23 +02:00
|
|
|
Serial.println("Imposible to connect to : " + String(poolString));
|
|
|
|
WiFi.hostByName(poolString, serverIP);
|
2023-06-07 14:12:45 +02:00
|
|
|
Serial.printf("Resolved DNS got: %s\n", serverIP.toString());
|
2023-06-04 20:02:23 +02:00
|
|
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
2023-08-05 20:35:39 +02:00
|
|
|
return false;
|
2023-06-04 20:02:23 +02:00
|
|
|
}
|
2023-08-03 01:13:52 +02:00
|
|
|
|
2023-08-05 20:35:39 +02:00
|
|
|
return true;
|
2023-06-04 20:02:23 +02:00
|
|
|
}
|
|
|
|
|
2023-06-07 14:12:45 +02:00
|
|
|
//Implements a socketKeepAlive function and
|
|
|
|
//checks if pool is not sending any data to reconnect again.
|
|
|
|
//Even connection could be alive, pool could stop sending new job NOTIFY
|
2023-06-07 10:51:46 +02:00
|
|
|
unsigned long mStart0Hashrate = 0;
|
2023-06-07 14:12:45 +02:00
|
|
|
bool checkPoolInactivity(unsigned int keepAliveTime, unsigned long inactivityTime){
|
2023-06-07 10:51:46 +02:00
|
|
|
|
|
|
|
unsigned long currentKHashes = (Mhashes*1000) + hashes/1000;
|
|
|
|
unsigned long elapsedKHs = currentKHashes - totalKHashes;
|
|
|
|
|
2023-06-07 14:12:45 +02:00
|
|
|
// If no shares sent to pool
|
|
|
|
// send something to pool to hold socket oppened
|
|
|
|
if(millis() - mLastTXtoPool > keepAliveTime){
|
|
|
|
mLastTXtoPool = millis();
|
|
|
|
Serial.println(" Sending : KeepAlive suggest_difficulty");
|
|
|
|
//if (client.print("{}\n") == 0) {
|
|
|
|
tx_suggest_difficulty(client, DEFAULT_DIFFICULTY);
|
|
|
|
/*if(tx_suggest_difficulty(client, DEFAULT_DIFFICULTY)){
|
|
|
|
Serial.println(" Sending keepAlive to pool -> Detected client disconnected");
|
|
|
|
return true;
|
|
|
|
}*/
|
|
|
|
}
|
|
|
|
|
2023-06-07 10:51:46 +02:00
|
|
|
if(elapsedKHs == 0){
|
|
|
|
//Check if hashrate is 0 during inactivityTIme
|
|
|
|
if(mStart0Hashrate == 0) mStart0Hashrate = millis();
|
|
|
|
if((millis()-mStart0Hashrate) > inactivityTime) { mStart0Hashrate=0; return true;}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mStart0Hashrate = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-05-26 13:02:14 +02:00
|
|
|
void runStratumWorker(void *name) {
|
2023-03-20 01:10:44 +01:00
|
|
|
|
2023-05-07 13:21:00 +02:00
|
|
|
// TEST: https://bitcoin.stackexchange.com/questions/22929/full-example-data-for-scrypt-stratum-client
|
2023-04-17 02:07:18 +02:00
|
|
|
|
|
|
|
Serial.println("");
|
|
|
|
Serial.printf("\n[WORKER] Started. Running %s on core %d\n", (char *)name, xPortGetCoreID());
|
2023-05-07 13:21:00 +02:00
|
|
|
|
|
|
|
#ifdef DEBUG_MEMORY
|
2023-05-01 22:26:01 +02:00
|
|
|
Serial.printf("### [Total Heap / Free heap]: %d / %d \n", ESP.getHeapSize(), ESP.getFreeHeap());
|
2023-05-07 13:21:00 +02:00
|
|
|
#endif
|
2023-04-17 02:07:18 +02:00
|
|
|
|
|
|
|
// connect to pool
|
2023-05-26 13:02:14 +02:00
|
|
|
|
2023-07-20 12:50:35 +02:00
|
|
|
double currentPoolDifficulty = DEFAULT_DIFFICULTY;
|
2023-05-26 13:02:14 +02:00
|
|
|
|
2023-04-17 02:07:18 +02:00
|
|
|
while(true) {
|
2023-03-20 01:10:44 +01:00
|
|
|
|
2023-05-07 13:21:00 +02:00
|
|
|
if(WiFi.status() != WL_CONNECTED){
|
2023-07-13 15:41:58 +02:00
|
|
|
// WiFi is disconnected, so reconnect now
|
|
|
|
WiFi.reconnect();
|
|
|
|
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
2023-05-07 13:21:00 +02:00
|
|
|
continue;
|
|
|
|
}
|
2023-03-20 01:10:44 +01:00
|
|
|
|
2023-05-26 13:02:14 +02:00
|
|
|
//Test vars:
|
2023-05-29 22:37:14 +02:00
|
|
|
//************
|
|
|
|
//Nerdminerpool
|
2023-06-04 20:02:23 +02:00
|
|
|
// strcpy(poolString, "nerdminerPool");
|
|
|
|
// portNumber = 3002;
|
|
|
|
// strcpy(btcString,"test");
|
2023-05-29 22:37:14 +02:00
|
|
|
//Braiins
|
|
|
|
//strcpy(poolString, "eu.stratum.braiins.com");
|
|
|
|
//portNumber = 3333;
|
|
|
|
//strcpy(btcString,"Bitmaker.01");
|
|
|
|
//CKpool
|
2023-06-08 10:21:16 +02:00
|
|
|
//strcpy(poolString, "solo.ckpool.org");
|
|
|
|
//portNumber = 3333;
|
2023-05-29 22:37:14 +02:00
|
|
|
//strcpy(btcString,"test");
|
2023-05-26 13:02:14 +02:00
|
|
|
|
2023-08-03 01:13:52 +02:00
|
|
|
if(!checkPoolConnection())
|
2023-08-05 20:35:39 +02:00
|
|
|
//If server is not reachable add random delay for connection retries
|
|
|
|
srand(millis());
|
|
|
|
//Generate value between 1 and 15 secs
|
|
|
|
vTaskDelay(((1 + rand() % 15) * 1000) / portTICK_PERIOD_MS);
|
2023-05-07 13:21:00 +02:00
|
|
|
|
|
|
|
if(!isMinerSuscribed){
|
2023-05-26 13:02:14 +02:00
|
|
|
|
2023-07-27 23:24:33 +02:00
|
|
|
//Stop miner current jobs
|
|
|
|
mMiner.inRun = false;
|
2023-05-26 13:02:14 +02:00
|
|
|
mWorker = init_mining_subscribe();
|
|
|
|
|
|
|
|
// STEP 1: Pool server connection (SUBSCRIBE)
|
|
|
|
if(!tx_mining_subscribe(client, mWorker)) {
|
2023-05-07 13:21:00 +02:00
|
|
|
client.stop();
|
|
|
|
continue;
|
|
|
|
}
|
2023-05-27 23:54:16 +02:00
|
|
|
|
|
|
|
strcpy(mWorker.wName, btcString);
|
|
|
|
strcpy(mWorker.wPass, "x");
|
2023-05-26 13:02:14 +02:00
|
|
|
// STEP 2: Pool authorize work (Block Info)
|
2023-05-27 23:54:16 +02:00
|
|
|
tx_mining_auth(client, mWorker.wName, mWorker.wPass); //Don't verifies authoritzation, TODO
|
|
|
|
//tx_mining_auth2(client, mWorker.wName, mWorker.wPass); //Don't verifies authoritzation, TODO
|
|
|
|
|
|
|
|
// STEP 3: Suggest pool difficulty
|
|
|
|
tx_suggest_difficulty(client, DEFAULT_DIFFICULTY);
|
2023-05-26 13:02:14 +02:00
|
|
|
|
2023-05-07 13:21:00 +02:00
|
|
|
isMinerSuscribed=true;
|
2023-06-07 14:12:45 +02:00
|
|
|
mLastTXtoPool = millis();
|
2023-04-17 02:07:18 +02:00
|
|
|
}
|
2023-05-07 13:21:00 +02:00
|
|
|
|
2023-06-07 14:12:45 +02:00
|
|
|
//Check if pool is down for almost 5minutes and then restart connection with pool (1min=600000ms)
|
|
|
|
if(checkPoolInactivity(KEEPALIVE_TIME_ms, POOLINACTIVITY_TIME_ms)){
|
2023-06-07 10:51:46 +02:00
|
|
|
//Restart connection
|
|
|
|
Serial.println(" Detected more than 2 min without data form stratum server. Closing socket and reopening...");
|
|
|
|
client.stop();
|
|
|
|
isMinerSuscribed=false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-05-26 13:02:14 +02:00
|
|
|
//Read pending messages from pool
|
2023-06-04 20:02:23 +02:00
|
|
|
while(client.connected() && client.available()){
|
2023-05-26 13:02:14 +02:00
|
|
|
|
|
|
|
Serial.println(" Received message from pool");
|
|
|
|
String line = client.readStringUntil('\n');
|
|
|
|
stratum_method result = parse_mining_method(line);
|
|
|
|
switch (result)
|
|
|
|
{
|
|
|
|
case STRATUM_PARSE_ERROR: Serial.println(" Parsed JSON: error on JSON"); break;
|
|
|
|
case MINING_NOTIFY: if(parse_mining_notify(line, mJob)){
|
|
|
|
//Increse templates readed
|
|
|
|
templates++;
|
2023-07-11 00:25:23 +02:00
|
|
|
//Stop miner current jobs
|
2023-05-26 13:02:14 +02:00
|
|
|
mMiner.inRun = false;
|
2023-07-11 00:25:23 +02:00
|
|
|
//Prepare data for new jobs
|
2023-05-26 13:02:14 +02:00
|
|
|
mMiner=calculateMiningData(mWorker,mJob);
|
2023-05-28 22:58:23 +02:00
|
|
|
mMiner.poolDifficulty = currentPoolDifficulty;
|
2023-05-26 13:02:14 +02:00
|
|
|
mMiner.newJob = true;
|
2023-07-10 22:58:34 +02:00
|
|
|
mMiner.newJob2 = true;
|
2023-05-26 13:02:14 +02:00
|
|
|
//Give new job to miner
|
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
2023-05-28 22:58:23 +02:00
|
|
|
case MINING_SET_DIFFICULTY: parse_mining_set_difficulty(line, currentPoolDifficulty);
|
|
|
|
mMiner.poolDifficulty = currentPoolDifficulty;
|
2023-05-26 13:02:14 +02:00
|
|
|
break;
|
2023-07-27 16:30:57 +02:00
|
|
|
case STRATUM_SUCCESS: Serial.println(" Parsed JSON: Success"); break;
|
2023-05-26 13:02:14 +02:00
|
|
|
default: Serial.println(" Parsed JSON: unknown"); break;
|
2023-03-20 01:10:44 +01:00
|
|
|
|
2023-05-26 13:02:14 +02:00
|
|
|
}
|
2023-03-20 01:10:44 +01:00
|
|
|
}
|
|
|
|
|
2023-06-08 10:21:16 +02:00
|
|
|
vTaskDelay(500 / portTICK_PERIOD_MS); //Small delay
|
2023-05-07 13:21:00 +02:00
|
|
|
|
2023-05-26 13:02:14 +02:00
|
|
|
}
|
2023-03-20 01:10:44 +01:00
|
|
|
|
2023-05-26 13:02:14 +02:00
|
|
|
}
|
2023-03-20 01:10:44 +01:00
|
|
|
|
|
|
|
|
2023-05-26 13:02:14 +02:00
|
|
|
//////////////////THREAD CALLS///////////////////
|
2023-04-17 02:07:18 +02:00
|
|
|
|
2023-05-26 13:02:14 +02:00
|
|
|
//This works only with one thread, TODO -> Class or miner_data for each thread
|
2023-04-17 02:07:18 +02:00
|
|
|
|
2023-08-20 00:45:09 +02:00
|
|
|
|
2023-07-11 21:58:53 +02:00
|
|
|
void runMiner(void * task_id) {
|
|
|
|
|
|
|
|
unsigned int miner_id = (uint32_t)task_id;
|
2023-07-11 00:25:23 +02:00
|
|
|
|
2023-08-11 13:37:25 +02:00
|
|
|
Serial.printf("[MINER] %d Started runMiner Task!\n", miner_id);
|
|
|
|
|
2023-05-26 13:02:14 +02:00
|
|
|
while(1){
|
2023-04-17 15:04:42 +02:00
|
|
|
|
2023-05-26 13:02:14 +02:00
|
|
|
//Wait new job
|
|
|
|
while(1){
|
2023-07-10 22:58:34 +02:00
|
|
|
if(mMiner.newJob==true || mMiner.newJob2==true) break;
|
2023-05-26 13:02:14 +02:00
|
|
|
vTaskDelay(100 / portTICK_PERIOD_MS); //Small delay
|
2023-05-07 13:21:00 +02:00
|
|
|
}
|
2023-07-11 00:25:23 +02:00
|
|
|
vTaskDelay(10 / portTICK_PERIOD_MS); //Small delay to join both mining threads
|
2023-03-20 01:10:44 +01:00
|
|
|
|
2023-07-11 21:58:53 +02:00
|
|
|
if(mMiner.newJob)
|
2023-07-11 00:25:23 +02:00
|
|
|
mMiner.newJob = false; //Clear newJob flag
|
2023-07-11 21:58:53 +02:00
|
|
|
else if(mMiner.newJob2)
|
2023-07-11 00:25:23 +02:00
|
|
|
mMiner.newJob2 = false; //Clear newJob flag
|
2023-05-26 13:02:14 +02:00
|
|
|
mMiner.inRun = true; //Set inRun flag
|
|
|
|
|
|
|
|
//Prepare Premining data
|
2023-07-30 13:10:39 +02:00
|
|
|
nerd_sha256 nerdMidstate;
|
2023-08-20 00:45:09 +02:00
|
|
|
//nerdSHA256_context nerdMidstate; //NerdShaplus
|
2023-07-30 11:01:06 +02:00
|
|
|
uint8_t hash[32];
|
2023-08-20 00:45:09 +02:00
|
|
|
|
2023-05-01 22:26:01 +02:00
|
|
|
|
2023-08-20 00:45:09 +02:00
|
|
|
//Calcular midstate
|
2023-07-30 13:10:39 +02:00
|
|
|
nerd_midstate(&nerdMidstate, mMiner.bytearray_blockheader, 64);
|
2023-08-20 00:45:09 +02:00
|
|
|
//nerd_mids(&nerdMidstate, mMiner.bytearray_blockheader); //NerdShaplus
|
2023-05-01 22:26:01 +02:00
|
|
|
|
2023-07-10 22:58:34 +02:00
|
|
|
|
2023-03-20 01:10:44 +01:00
|
|
|
// search a valid nonce
|
2023-07-11 21:58:53 +02:00
|
|
|
unsigned long nonce = TARGET_NONCE - MAX_NONCE;
|
|
|
|
// split up odd/even nonces between miner tasks
|
|
|
|
nonce += miner_id;
|
2023-03-20 01:10:44 +01:00
|
|
|
uint32_t startT = micros();
|
2023-07-23 00:26:19 +02:00
|
|
|
unsigned char *header64;
|
|
|
|
// each miner thread needs to track its own blockheader template
|
2023-08-20 00:45:09 +02:00
|
|
|
uint8_t temp;
|
|
|
|
|
2023-07-23 00:26:19 +02:00
|
|
|
memcpy(mMiner.bytearray_blockheader2, &mMiner.bytearray_blockheader, 80);
|
|
|
|
if (miner_id == 0)
|
|
|
|
header64 = mMiner.bytearray_blockheader + 64;
|
|
|
|
else
|
|
|
|
header64 = mMiner.bytearray_blockheader2 + 64;
|
2023-08-20 00:45:09 +02:00
|
|
|
|
|
|
|
bool is16BitShare=true;
|
2023-04-15 23:56:13 +02:00
|
|
|
Serial.println(">>> STARTING TO HASH NONCES");
|
2023-03-20 01:10:44 +01:00
|
|
|
while(true) {
|
2023-07-23 00:26:19 +02:00
|
|
|
if (miner_id == 0)
|
|
|
|
memcpy(mMiner.bytearray_blockheader + 76, &nonce, 4);
|
|
|
|
else
|
|
|
|
memcpy(mMiner.bytearray_blockheader2 + 76, &nonce, 4);
|
2023-03-20 01:10:44 +01:00
|
|
|
|
2023-08-20 00:45:09 +02:00
|
|
|
|
|
|
|
nerd_double_sha2(&nerdMidstate, header64, hash);
|
|
|
|
//is16BitShare=nerd_sha256d(&nerdMidstate, header64, hash); //Boosted 80Khs sha
|
|
|
|
|
|
|
|
/*Serial.print("hash1: ");
|
|
|
|
for (size_t i = 0; i < 32; i++)
|
2023-05-01 22:26:01 +02:00
|
|
|
Serial.printf("%02x", hash[i]);
|
2023-07-10 22:58:34 +02:00
|
|
|
Serial.println("");
|
2023-08-20 00:45:09 +02:00
|
|
|
Serial.print("hash2: ");
|
2023-07-10 22:58:34 +02:00
|
|
|
for (size_t i = 0; i < 32; i++)
|
2023-08-20 00:45:09 +02:00
|
|
|
Serial.printf("%02x", hash2[i]);
|
2023-07-10 22:58:34 +02:00
|
|
|
Serial.println(""); */
|
2023-08-20 00:45:09 +02:00
|
|
|
|
2023-05-01 22:26:01 +02:00
|
|
|
hashes++;
|
2023-07-11 21:58:53 +02:00
|
|
|
if (nonce > TARGET_NONCE) break; //exit
|
2023-05-26 13:02:14 +02:00
|
|
|
if(!mMiner.inRun) { Serial.println ("MINER WORK ABORTED >> waiting new job"); break;}
|
2023-05-01 22:26:01 +02:00
|
|
|
|
|
|
|
// check if 16bit share
|
2023-07-23 00:26:19 +02:00
|
|
|
if(hash[31] !=0 || hash[30] !=0) {
|
2023-08-20 00:45:09 +02:00
|
|
|
//if(!is16BitShare){
|
2023-07-23 00:26:19 +02:00
|
|
|
// increment nonce
|
|
|
|
nonce += 2;
|
|
|
|
continue;
|
|
|
|
}
|
2023-07-24 21:33:15 +02:00
|
|
|
|
2023-05-26 13:02:14 +02:00
|
|
|
//Check target to submit
|
|
|
|
//Difficulty of 1 > 0x00000000FFFF0000000000000000000000000000000000000000000000000000
|
|
|
|
//NM2 pool diff 1e-9 > Target = diff_1 / diff_pool > 0x00003B9ACA00....00
|
|
|
|
//Swapping diff bytes little endian >>>>>>>>>>>>>>>> 0x0000DC59D300....00
|
|
|
|
//if((hash[29] <= 0xDC) && (hash[28] <= 0x59)) //0x00003B9ACA00 > diff value for 1e-9
|
|
|
|
double diff_hash = diff_from_target(hash);
|
2023-07-24 21:33:15 +02:00
|
|
|
|
|
|
|
// update best diff
|
|
|
|
if (diff_hash > best_diff)
|
|
|
|
best_diff = diff_hash;
|
|
|
|
|
2023-05-28 22:58:23 +02:00
|
|
|
if(diff_hash > mMiner.poolDifficulty)//(hash[29] <= 0x3B)//(diff_hash > 1e-9)
|
2023-05-26 13:02:14 +02:00
|
|
|
{
|
|
|
|
tx_mining_submit(client, mWorker, mJob, nonce);
|
2023-05-28 22:58:23 +02:00
|
|
|
Serial.print(" - Current diff share: "); Serial.println(diff_hash,12);
|
|
|
|
Serial.print(" - Current pool diff : "); Serial.println(mMiner.poolDifficulty,12);
|
2023-05-26 13:02:14 +02:00
|
|
|
Serial.print(" - TX SHARE: ");
|
|
|
|
for (size_t i = 0; i < 32; i++)
|
|
|
|
Serial.printf("%02x", hash[i]);
|
2023-07-23 00:26:19 +02:00
|
|
|
#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("");
|
2023-06-07 14:12:45 +02:00
|
|
|
mLastTXtoPool = millis();
|
2023-05-26 13:02:14 +02:00
|
|
|
}
|
|
|
|
|
2023-05-01 22:26:01 +02:00
|
|
|
// check if 32bit share
|
2023-07-23 00:26:19 +02:00
|
|
|
if(hash[29] !=0 || hash[28] !=0) {
|
|
|
|
// increment nonce
|
|
|
|
nonce += 2;
|
|
|
|
continue;
|
|
|
|
}
|
2023-05-01 22:26:01 +02:00
|
|
|
shares++;
|
|
|
|
|
2023-08-01 22:39:37 +02:00
|
|
|
// check if valid header
|
2023-05-26 13:02:14 +02:00
|
|
|
if(checkValid(hash, mMiner.bytearray_target)){
|
2023-07-11 21:58:53 +02:00
|
|
|
Serial.printf("[WORKER] %d CONGRATULATIONS! Valid block found with nonce: %d | 0x%x\n", miner_id, nonce, nonce);
|
2023-05-07 13:21:00 +02:00
|
|
|
valids++;
|
2023-07-11 21:58:53 +02:00
|
|
|
Serial.printf("[WORKER] %d Submitted work valid!\n", miner_id);
|
2023-08-01 22:39:37 +02:00
|
|
|
// wait for new job
|
2023-05-07 13:21:00 +02:00
|
|
|
break;
|
2023-07-23 00:26:19 +02:00
|
|
|
}
|
|
|
|
// increment nonce
|
|
|
|
nonce += 2;
|
2023-03-20 01:10:44 +01:00
|
|
|
} // exit if found a valid result or nonce > MAX_NONCE
|
2023-04-17 02:07:18 +02:00
|
|
|
|
2023-07-30 13:19:01 +02:00
|
|
|
//wc_Sha256Free(&sha256);
|
|
|
|
//wc_Sha256Free(midstate);
|
2023-05-01 22:26:01 +02:00
|
|
|
|
2023-06-07 10:51:46 +02:00
|
|
|
mMiner.inRun = false;
|
2023-06-07 14:12:45 +02:00
|
|
|
Serial.print(">>> Finished job waiting new data from pool");
|
2023-06-07 10:51:46 +02:00
|
|
|
|
2023-08-05 21:32:37 +02:00
|
|
|
if(hashes>=MAX_NONCE_STEP) {
|
|
|
|
Mhashes=Mhashes+MAX_NONCE_STEP/1000000;
|
|
|
|
hashes=hashes-MAX_NONCE_STEP;
|
2023-06-07 10:51:46 +02:00
|
|
|
}
|
2023-05-01 22:26:01 +02:00
|
|
|
|
2023-03-20 01:10:44 +01:00
|
|
|
uint32_t duration = micros() - startT;
|
2023-07-11 17:12:02 +02:00
|
|
|
if (esp_task_wdt_reset() == ESP_OK)
|
|
|
|
Serial.print(">>> Resetting watchdog timer");
|
2023-03-20 01:10:44 +01:00
|
|
|
}
|
2023-05-01 22:26:01 +02:00
|
|
|
}
|
|
|
|
|
2023-05-03 21:11:59 +02:00
|
|
|
void runMonitor(void *name){
|
2023-03-20 01:10:44 +01:00
|
|
|
|
2023-05-08 01:31:05 +02:00
|
|
|
Serial.println("[MONITOR] started");
|
2023-05-07 13:21:00 +02:00
|
|
|
|
2023-05-08 01:31:05 +02:00
|
|
|
unsigned long mLastCheck = 0;
|
2023-06-08 10:21:16 +02:00
|
|
|
mMonitor.screen = SCREEN_MINING;
|
2023-05-08 01:31:05 +02:00
|
|
|
|
2023-08-11 13:37:25 +02:00
|
|
|
#ifdef DEVKITV1
|
|
|
|
mMonitor.screen = NO_SCREEN;
|
|
|
|
#endif
|
|
|
|
|
2023-05-08 01:31:05 +02:00
|
|
|
while(1){
|
2023-06-07 10:51:46 +02:00
|
|
|
|
2023-03-20 01:10:44 +01:00
|
|
|
|
2023-05-08 01:31:05 +02:00
|
|
|
unsigned long mElapsed = millis()-mLastCheck;
|
|
|
|
mLastCheck = millis();
|
|
|
|
unsigned long currentKHashes = (Mhashes*1000) + hashes/1000;
|
2023-06-07 10:51:46 +02:00
|
|
|
elapsedKHs = currentKHashes - totalKHashes;
|
2023-05-08 01:31:05 +02:00
|
|
|
totalKHashes = currentKHashes;
|
2023-05-07 13:21:00 +02:00
|
|
|
|
2023-06-07 10:51:46 +02:00
|
|
|
switch(mMonitor.screen){
|
|
|
|
case SCREEN_MINING: show_MinerScreen(mElapsed); break;
|
2023-06-07 17:07:30 +02:00
|
|
|
case SCREEN_CLOCK: show_ClockScreen(mElapsed); break;
|
2023-06-08 01:11:47 +02:00
|
|
|
case SCREEN_GLOBAL: show_GlobalHashScreen(mElapsed); break;
|
2023-08-11 13:37:25 +02:00
|
|
|
case NO_SCREEN: show_NoScreen(mElapsed); break;
|
2023-06-07 10:51:46 +02:00
|
|
|
}
|
2023-03-20 01:10:44 +01:00
|
|
|
|
2023-06-07 10:51:46 +02:00
|
|
|
//Monitor state when hashrate is 0.0
|
|
|
|
if(elapsedKHs == 0) {
|
|
|
|
Serial.printf(">>> [i] Miner: newJob>%s / inRun>%s) - Client: connected>%s / subscribed>%s / wificonnected>%s\n",
|
|
|
|
mMiner.newJob ? "true" : "false", mMiner.inRun ? "true" : "false",
|
|
|
|
client.connected() ? "true" : "false", isMinerSuscribed ? "true" : "false", WiFi.status() == WL_CONNECTED ? "true" : "false");
|
|
|
|
}
|
|
|
|
|
2023-06-07 17:07:30 +02:00
|
|
|
// Pause the task for 1000ms
|
|
|
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
2023-05-08 01:31:05 +02:00
|
|
|
}
|
2023-03-20 01:10:44 +01:00
|
|
|
}
|
2023-06-07 10:51:46 +02:00
|
|
|
|