hashrate calc, WIP

This commit is contained in:
Ben 2023-06-04 13:16:34 -04:00 committed by johnny9
parent dfcd831bdf
commit 18d0f651e8
5 changed files with 120 additions and 34 deletions

View File

@ -159,7 +159,7 @@ void set_job_difficulty_mask(int difficulty){
// The mask must be a power of 2 so there are no holes
// Correct: {0b00000000, 0b00000000, 0b11111111, 0b11111111}
// Incorrect: {0b00000000, 0b00000000, 0b11100111, 0b11111111}
difficulty = largestPowerOfTwo(difficulty) -1;
difficulty = largestPowerOfTwo(difficulty) -1; // (difficulty - 1) if it is a pow 2 then step down to second largest for more hashrate sampling
// convert difficulty into char array
// Ex: 256 = {0b00000000, 0b00000000, 0b00000000, 0b11111111}, {0x00, 0x00, 0x00, 0xff}
@ -169,6 +169,7 @@ void set_job_difficulty_mask(int difficulty){
//The char is read in backwards to the register so we need to reverse them
//So a mask of 512 looks like 0b00000000 00000000 00000001 1111111
//and not 0b00000000 00000000 10000000 1111111
job_difficulty_mask[5 - i] = reverseBits(value);
}

View File

@ -2,7 +2,6 @@
#define MINING_H
#include "stratum_api.h"
#include <stdbool.h>
typedef struct {
@ -15,8 +14,6 @@ typedef struct {
uint8_t midstate[32];
uint32_t pool_diff;
// is the pool diff a power of 2?
bool pool_diff_pow2;
char * jobid;
char * extranonce2;
} bm_job;

View File

@ -91,6 +91,8 @@ static void ASIC_task(void * pvParameters)
// TODO
// set_max_baud();
ESP_LOGI(TAG, "Mining!");
while (1) {
bm_job * next_bm_job = (bm_job *) queue_dequeue(&ASIC_jobs_queue);
struct job_packet job;
@ -158,20 +160,11 @@ static void ASIC_task(void * pvParameters)
prev_nonce = nonce.nonce;
}
// If the pool diff is a power of 2, the ASIC won't return nonces lower than the diff
// hence we can skip all the checking
if(active_jobs[nonce.job_id]->pool_diff_pow2){
submit_share(sock, STRATUM_USER, active_jobs[nonce.job_id]->jobid, active_jobs[nonce.job_id]->ntime,
active_jobs[nonce.job_id]->extranonce2, nonce.nonce);
notify_system_submitted_share();
continue;
}
// check the nonce difficulty
double nonce_diff = test_nonce_value(active_jobs[nonce.job_id], nonce.nonce);
ESP_LOGI(TAG, "Nonce difficulty %.2f of %d.", nonce_diff, active_jobs[nonce.job_id]->pool_diff);
notify_system_found_nonce(nonce_diff);
if (nonce_diff > active_jobs[nonce.job_id]->pool_diff)
{
//print_hex((uint8_t *)&job, sizeof(struct job_packet), sizeof(struct job_packet), "job: ");
@ -206,7 +199,6 @@ static void create_jobs_task(void * pvParameters)
bm_job next_job = construct_bm_job(&params, merkle_root);
next_job.pool_diff = stratum_difficulty; //each job is tied to the _current_ difficulty
next_job.pool_diff_pow2 = (stratum_difficulty != 0 && (stratum_difficulty & (stratum_difficulty - 1)) == 0);
//ESP_LOGI(TAG, "bm_job: ");
// print_hex((uint8_t *) &next_job.target, 4, 4, "nbits: ");

View File

@ -11,20 +11,110 @@
#include "INA260.h"
#include "adc.h"
#include "oled.h"
#include <time.h>
#include <sys/time.h>
static const char *TAG = "system";
#define BM1397_VOLTAGE CONFIG_BM1397_VOLTAGE
#define HISTORY_LENGTH 100
#define HISTORY_WINDOW_SIZE 5
static char oled_buf[20];
static int screen_page = 0;
static int shares_submitted = 0;
static time_t start_time, current_time;
static time_t start_time;
static double last_found_nonce_time = 0;
static int historical_hashrate_rolling_index = 0;
static double historical_hashrate[HISTORY_LENGTH] = {0.0};
static int historical_hashrate_init = 0;
static double current_hashrate = 0;
void logArrayContents(const double* array, size_t length) {
char logMessage[1024]; // Adjust the buffer size as needed
int offset = 0;
offset += snprintf(logMessage + offset, sizeof(logMessage) - offset, "Array Contents: [");
for (size_t i = 0; i < length; i++) {
offset += snprintf(logMessage + offset, sizeof(logMessage) - offset, "%.1f%s", array[i], (i < length - 1) ? ", " : "]");
}
ESP_LOGI(TAG, "%s", logMessage);
}
void update_hashrate(void){
if(screen_page != 0){
return;
}
OLED_clearLine(1);
memset(oled_buf, 0, 20);
snprintf(oled_buf, 20, "GH/s%s: %.1f", historical_hashrate_init < HISTORY_LENGTH ? "*": "", current_hashrate);
OLED_writeString(0, 1, oled_buf);
}
void update_shares(void){
if(screen_page != 0){
return;
}
OLED_clearLine(2);
memset(oled_buf, 0, 20);
snprintf(oled_buf, 20, "Shares: %i", shares_submitted);
OLED_writeString(0, 2, oled_buf);
}
void notify_system_submitted_share(void){
shares_submitted++;
update_shares();
}
void notify_system_found_nonce(double nonce_diff){
//init
if(last_found_nonce_time == 0){
last_found_nonce_time = esp_timer_get_time();
return;
}
// Calculate the time difference in seconds with sub-second precision
double time_to_find = (double)(esp_timer_get_time() - last_found_nonce_time) / 1000000;
// hashrate = (nonce_difficulty * 2^32) / time_to_find
historical_hashrate[historical_hashrate_rolling_index] = (nonce_diff * 4294967296) / (time_to_find * 1000000000);
if(historical_hashrate_init < HISTORY_LENGTH){
historical_hashrate_init++;
}
double sum = 0;
for (int i = 0; i < historical_hashrate_init; i++) {
sum += historical_hashrate[i];
}
current_hashrate = sum / historical_hashrate_init;
ESP_LOGI(TAG, "CH: %.1f", current_hashrate);
// Increment all the stuff
historical_hashrate_rolling_index = (historical_hashrate_rolling_index + 1) % HISTORY_LENGTH;
last_found_nonce_time = esp_timer_get_time();
update_hashrate();
logArrayContents(historical_hashrate, HISTORY_LENGTH);
}
void init_system(void) {
start_time = time(NULL);
@ -93,42 +183,46 @@ void update_system_info(void) {
}
void update_system_performance(){
char oled_buf[20];
// Get the current system time as the current time
current_time = time(NULL);
// Calculate the uptime in seconds
double uptime_in_seconds = difftime(current_time, start_time);
// Calculate the uptime in days and hours
double uptime_in_seconds = difftime(time(NULL), start_time);
int uptime_in_days = uptime_in_seconds / (3600 * 24);
int remaining_seconds = (int)uptime_in_seconds % (3600 * 24);
double uptime_in_hours = (double)remaining_seconds / 3600;
int uptime_in_hours = remaining_seconds / 3600;
remaining_seconds %= 3600;
int uptime_in_minutes = remaining_seconds / 60;
if (OLED_status()) {
OLED_clearLine(1);
OLED_clearLine(2);
OLED_clearLine(3);
memset(oled_buf, 0, 20);
snprintf(oled_buf, 20, "Shares: %i", shares_submitted);
OLED_writeString(0, 1, oled_buf);
update_hashrate();
update_shares();
memset(oled_buf, 0, 20);
snprintf(oled_buf, 20, "Uptime: %dd %.2fh", uptime_in_days, uptime_in_hours);
OLED_writeString(0, 2, oled_buf);
snprintf(oled_buf, 20, "UT: %dd %ih %im", uptime_in_days, uptime_in_hours, uptime_in_minutes);
OLED_writeString(0, 3, oled_buf);
}
}
void system_task(void *arg) {
init_system();
while(1){
screen_page = 0;
update_system_performance();
vTaskDelay(30000 / portTICK_RATE_MS);
screen_page = 1;
update_system_info();
vTaskDelay(5000 / portTICK_RATE_MS);
update_system_performance();
vTaskDelay(5000 / portTICK_RATE_MS);
}
}

View File

@ -5,5 +5,7 @@ void system_task(void *arg);
void init_system(void);
void get_stats(void);
void notify_system_submitted_share(void);
void notify_system_found_nonce(double nonce_diff);
#endif /* SYSTEM_H_ */