Skip to content

Commit

Permalink
Improve failure handling and add fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
eandersson committed Sep 12, 2024
1 parent 1e508f2 commit 32fa6f4
Show file tree
Hide file tree
Showing 12 changed files with 283 additions and 180 deletions.
8 changes: 4 additions & 4 deletions components/stratum/include/stratum_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ void STRATUM_V1_free_mining_notify(mining_notify *params);

int STRATUM_V1_authenticate(int socket, const char *username, const char *pass);

void STRATUM_V1_configure_version_rolling(int socket, uint32_t * version_mask);
int STRATUM_V1_configure_version_rolling(int socket, uint32_t * version_mask);

int STRATUM_V1_suggest_difficulty(int socket, uint32_t difficulty);

void STRATUM_V1_submit_share(int socket, const char *username, const char *jobid,
const char *extranonce_2, const uint32_t ntime, const uint32_t nonce,
const uint32_t version);
int STRATUM_V1_submit_share(int socket, const char *username, const char *jobid,
const char *extranonce_2, const uint32_t ntime, const uint32_t nonce,
const uint32_t version);

#endif // STRATUM_API_H
22 changes: 9 additions & 13 deletions components/stratum/stratum_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ char * STRATUM_V1_receive_jsonrpc_line(int sockfd)
memset(recv_buffer, 0, BUFFER_SIZE);
nbytes = recv(sockfd, recv_buffer, BUFFER_SIZE - 1, 0);
if (nbytes == -1) {
ESP_LOGI(TAG, "Error: recv");
ESP_LOGI(TAG, "Error: recv (errno %d: %s)", errno, strerror(errno));
if (json_rpc_buffer) {
free(json_rpc_buffer);
json_rpc_buffer=0;
Expand Down Expand Up @@ -314,19 +314,17 @@ int STRATUM_V1_subscribe(int socket, char * model)
const char *version = app_desc->version;
sprintf(subscribe_msg, "{\"id\": %d, \"method\": \"mining.subscribe\", \"params\": [\"bitaxe/%s/%s\"]}\n", send_uid++, model, version);
debug_stratum_tx(subscribe_msg);
write(socket, subscribe_msg, strlen(subscribe_msg));

return 1;
return write(socket, subscribe_msg, strlen(subscribe_msg));
}

int STRATUM_V1_suggest_difficulty(int socket, uint32_t difficulty)
{
char difficulty_msg[BUFFER_SIZE];
sprintf(difficulty_msg, "{\"id\": %d, \"method\": \"mining.suggest_difficulty\", \"params\": [%ld]}\n", send_uid++, difficulty);
debug_stratum_tx(difficulty_msg);
write(socket, difficulty_msg, strlen(difficulty_msg));

return 1;
return write(socket, difficulty_msg, strlen(difficulty_msg));
}

int STRATUM_V1_authenticate(int socket, const char * username, const char * pass)
Expand All @@ -336,9 +334,7 @@ int STRATUM_V1_authenticate(int socket, const char * username, const char * pass
pass);
debug_stratum_tx(authorize_msg);

write(socket, authorize_msg, strlen(authorize_msg));

return 1;
return write(socket, authorize_msg, strlen(authorize_msg));
}

/// @param socket Socket to write to
Expand All @@ -347,28 +343,28 @@ int STRATUM_V1_authenticate(int socket, const char * username, const char * pass
/// @param ntime The hex-encoded time value use in the block header.
/// @param extranonce_2 The hex-encoded value of extra nonce 2.
/// @param nonce The hex-encoded nonce value to use in the block header.
void STRATUM_V1_submit_share(int socket, const char * username, const char * jobid, const char * extranonce_2, const uint32_t ntime,
int STRATUM_V1_submit_share(int socket, const char * username, const char * jobid, const char * extranonce_2, const uint32_t ntime,
const uint32_t nonce, const uint32_t version)
{
char submit_msg[BUFFER_SIZE];
sprintf(submit_msg,
"{\"id\": %d, \"method\": \"mining.submit\", \"params\": [\"%s\", \"%s\", \"%s\", \"%08lx\", \"%08lx\", \"%08lx\"]}\n",
send_uid++, username, jobid, extranonce_2, ntime, nonce, version);
debug_stratum_tx(submit_msg);
write(socket, submit_msg, strlen(submit_msg));

return write(socket, submit_msg, strlen(submit_msg));
}

void STRATUM_V1_configure_version_rolling(int socket, uint32_t * version_mask)
int STRATUM_V1_configure_version_rolling(int socket, uint32_t * version_mask)
{
char configure_msg[BUFFER_SIZE * 2];
sprintf(configure_msg,
"{\"id\": %d, \"method\": \"mining.configure\", \"params\": [[\"version-rolling\"], {\"version-rolling.mask\": "
"\"ffffffff\"}]}\n",
send_uid++);
debug_stratum_tx(configure_msg);
write(socket, configure_msg, strlen(configure_msg));

return;
return write(socket, configure_msg, strlen(configure_msg));
}

static void debug_stratum_tx(const char * msg)
Expand Down
13 changes: 13 additions & 0 deletions main/Kconfig.projbuild
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ menu "Stratum Configuration"
help
The stratum server port to connect to.

config FALLBACK_STRATUM_URL
string "Fallback Stratum Address"
default "solo.ckpool.org"
help
The example will connect to this Stratum pool address if the primary fails.

config FALLBACK_STRATUM_PORT
int "Fallback Stratum Port"
range 0 65535
default 3333
help
The stratum server port to connect to if the primary fails.

config STRATUM_USER
string "Stratum username"
default "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa.bitaxe"
Expand Down
4 changes: 3 additions & 1 deletion main/global_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ typedef struct
char ssid[32];
char wifi_status[20];
char * pool_url;
char * fallback_pool_url;
uint16_t pool_port;
uint16_t fallback_pool_port;
bool is_using_fallback;
uint16_t overheat_mode;

uint32_t lastClockSync;
} SystemModule;

Expand Down
2 changes: 2 additions & 0 deletions main/http_server/axe-os/src/app/services/system.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export class SystemService {
ASICModel: eASICModel.BM1366,
stratumURL: "public-pool.io",
stratumPort: 21496,
fallbackStratumURL: "test.public-pool.io",
fallbackStratumPort: 21497,
stratumUser: "bc1q99n3pu025yyu0jlywpmwzalyhm36tg5u37w20d.bitaxe-U1",
frequency: 485,
version: "2.0",
Expand Down
2 changes: 2 additions & 0 deletions main/http_server/axe-os/src/models/ISystemInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export interface ISystemInfo {
ASICModel: eASICModel,
stratumURL: string,
stratumPort: number,
fallbackStratumURL: string,
fallbackStratumPort: number,
stratumUser: string,
frequency: number,
version: string,
Expand Down
9 changes: 9 additions & 0 deletions main/http_server/http_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ static esp_err_t PATCH_update_settings(httpd_req_t * req)
if ((item = cJSON_GetObjectItem(root, "stratumURL")) != NULL) {
nvs_config_set_string(NVS_CONFIG_STRATUM_URL, item->valuestring);
}
if ((item = cJSON_GetObjectItem(root, "fallbackStratumURL")) != NULL) {
nvs_config_set_string(NVS_CONFIG_FALLBACK_STRATUM_URL, item->valuestring);
}
if ((item = cJSON_GetObjectItem(root, "stratumUser")) != NULL) {
nvs_config_set_string(NVS_CONFIG_STRATUM_USER, item->valuestring);
}
Expand All @@ -281,6 +284,9 @@ static esp_err_t PATCH_update_settings(httpd_req_t * req)
if ((item = cJSON_GetObjectItem(root, "stratumPort")) != NULL) {
nvs_config_set_u16(NVS_CONFIG_STRATUM_PORT, item->valueint);
}
if ((item = cJSON_GetObjectItem(root, "fallbackStratumPort")) != NULL) {
nvs_config_set_u16(NVS_CONFIG_FALLBACK_STRATUM_PORT, item->valueint);
}
if ((item = cJSON_GetObjectItem(root, "ssid")) != NULL) {
nvs_config_set_string(NVS_CONFIG_WIFI_SSID, item->valuestring);
}
Expand Down Expand Up @@ -368,6 +374,7 @@ static esp_err_t GET_system_info(httpd_req_t * req)
char * ssid = nvs_config_get_string(NVS_CONFIG_WIFI_SSID, CONFIG_ESP_WIFI_SSID);
char * hostname = nvs_config_get_string(NVS_CONFIG_HOSTNAME, CONFIG_LWIP_LOCAL_HOSTNAME);
char * stratumURL = nvs_config_get_string(NVS_CONFIG_STRATUM_URL, CONFIG_STRATUM_URL);
char * fallbackStratumURL = nvs_config_get_string(NVS_CONFIG_FALLBACK_STRATUM_URL, CONFIG_FALLBACK_STRATUM_URL);
char * stratumUser = nvs_config_get_string(NVS_CONFIG_STRATUM_USER, CONFIG_STRATUM_USER);
char * board_version = nvs_config_get_string(NVS_CONFIG_BOARD_VERSION, "unknown");

Expand Down Expand Up @@ -414,7 +421,9 @@ static esp_err_t GET_system_info(httpd_req_t * req)
cJSON_AddNumberToObject(root, "smallCoreCount", small_core_count);
cJSON_AddStringToObject(root, "ASICModel", GLOBAL_STATE->asic_model_str);
cJSON_AddStringToObject(root, "stratumURL", stratumURL);
cJSON_AddStringToObject(root, "fallbackStratumURL", fallbackStratumURL);
cJSON_AddNumberToObject(root, "stratumPort", nvs_config_get_u16(NVS_CONFIG_STRATUM_PORT, CONFIG_STRATUM_PORT));
cJSON_AddNumberToObject(root, "fallbackStratumPort", nvs_config_get_u16(NVS_CONFIG_FALLBACK_STRATUM_PORT, CONFIG_FALLBACK_STRATUM_PORT));
cJSON_AddStringToObject(root, "stratumUser", stratumUser);

cJSON_AddStringToObject(root, "version", esp_app_get_description()->version);
Expand Down
2 changes: 2 additions & 0 deletions main/nvs_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#define NVS_CONFIG_HOSTNAME "hostname"
#define NVS_CONFIG_STRATUM_URL "stratumurl"
#define NVS_CONFIG_STRATUM_PORT "stratumport"
#define NVS_CONFIG_FALLBACK_STRATUM_URL "fbstratumurl"
#define NVS_CONFIG_FALLBACK_STRATUM_PORT "fbstratumport"
#define NVS_CONFIG_STRATUM_USER "stratumuser"
#define NVS_CONFIG_STRATUM_PASS "stratumpass"
#define NVS_CONFIG_ASIC_FREQ "asicfrequency"
Expand Down
5 changes: 5 additions & 0 deletions main/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,14 @@ static void _init_system(GlobalState * GLOBAL_STATE)

// set the pool url
module->pool_url = nvs_config_get_string(NVS_CONFIG_STRATUM_URL, CONFIG_STRATUM_URL);
module->fallback_pool_url = nvs_config_get_string(NVS_CONFIG_FALLBACK_STRATUM_URL, CONFIG_FALLBACK_STRATUM_URL);

//set the pool port
module->pool_port = nvs_config_get_u16(NVS_CONFIG_STRATUM_PORT, CONFIG_STRATUM_PORT);
module->fallback_pool_port = nvs_config_get_u16(NVS_CONFIG_FALLBACK_STRATUM_PORT, CONFIG_FALLBACK_STRATUM_PORT);

// set fallback to false.
module->is_using_fallback = false;

// Initialize overheat_mode
module->overheat_mode = nvs_config_get_u16(NVS_CONFIG_OVERHEAT_MODE, 0);
Expand Down
10 changes: 7 additions & 3 deletions main/tasks/asic_result_task.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "esp_log.h"
#include "nvs_config.h"
#include "utils.h"
#include "stratum_task.h"
#include <lwip/tcpip.h>

static const char *TAG = "asic_result";

Expand All @@ -17,7 +19,6 @@ void ASIC_result_task(void *pvParameters)

while (1)
{

task_result *asic_result = (*GLOBAL_STATE->ASIC_functions.receive_result_fn)(GLOBAL_STATE);

if (asic_result == NULL)
Expand All @@ -44,8 +45,7 @@ void ASIC_result_task(void *pvParameters)

if (nonce_diff > GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs[job_id]->pool_diff)
{

STRATUM_V1_submit_share(
int ret = STRATUM_V1_submit_share(
GLOBAL_STATE->sock,
user,
GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs[job_id]->jobid,
Expand All @@ -54,6 +54,10 @@ void ASIC_result_task(void *pvParameters)
asic_result->nonce,
asic_result->rolled_version ^ GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs[job_id]->version);

if (ret < 0) {
ESP_LOGI(TAG, "Unable to write share to socket. Closing connection. Ret: %d (errno %d: %s)", ret, errno, strerror(errno));
stratum_close_connection(GLOBAL_STATE);
}
}

SYSTEM_notify_found_nonce(GLOBAL_STATE, nonce_diff, job_id);
Expand Down
Loading

0 comments on commit 32fa6f4

Please sign in to comment.