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 27, 2024
1 parent e30bbca commit f108426
Show file tree
Hide file tree
Showing 15 changed files with 370 additions and 188 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
25 changes: 25 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 All @@ -41,6 +54,18 @@ menu "Stratum Configuration"
help
Stratum password to use with pool

config FALLBACK_STRATUM_USER
string "Fallback Stratum username"
default "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa.bitaxe"
help
Fallback Stratum user to use with pool

config FALLBACK_STRATUM_PW
string "Fallback Stratum password"
default "x"
help
Fallback Stratum password to use with pool

config STRATUM_DIFFICULTY
int "Stratum default difficulty"
range 0 4294967296
Expand Down
5 changes: 4 additions & 1 deletion main/global_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "work_queue.h"

#define STRATUM_USER CONFIG_STRATUM_USER
#define FALLBACK_STRATUM_USER CONFIG_FALLBACK_STRATUM_USER

#define HISTORY_LENGTH 100
#define DIFF_STRING_SIZE 10
Expand Down Expand Up @@ -69,9 +70,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
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
</div>
</div>
</div>


<div class="field grid p-fluid">
<label htmlFor="stratumPort" class="col-12 mb-2 md:col-2 md:mb-0">Stratum Port:</label>
<div class="col-12 md:col-10">
Expand All @@ -49,8 +47,34 @@
<input pInputText id="stratumPassword" formControlName="stratumPassword" type="password" />
</div>
</div>


<div class="field grid p-fluid">
<label htmlFor="stratumURL" class="col-12 mb-2 md:col-2 md:mb-0">Fallback Stratum URL:</label>
<div class="col-12 md:col-10">
<input pInputText id="fallbackStratumURL" type="text" formControlName="fallbackStratumURL"
formControlName="fallbackStratumURL" />
<div>
<small>Do not include 'stratum+tcp://' or port.</small>
</div>
</div>
</div>
<div class="field grid p-fluid">
<label htmlFor="fallbackStratumPort" class="col-12 mb-2 md:col-2 md:mb-0">Fallback Stratum Port:</label>
<div class="col-12 md:col-10">
<input pInputText id="fallbackStratumPort" formControlName="fallbackStratumPort" type="number" />
</div>
</div>
<div class="field grid p-fluid">
<label htmlFor="fallbackStratumUser" class="col-12 mb-2 md:col-2 md:mb-0">Fallback Stratum User:</label>
<div class="col-12 md:col-10">
<input pInputText id="fallbackStratumUser" formControlName="fallbackStratumUser" type="text" />
</div>
</div>
<div class="field grid p-fluid">
<label htmlFor="fallbackStratumPassword" class="col-12 mb-2 md:col-2 md:mb-0">Fallback Stratum Password:</label>
<div class="col-12 md:col-10">
<input pInputText id="fallbackStratumPassword" formControlName="fallbackStratumPassword" type="password" />
</div>
</div>

<ng-container *ngIf="!devToolsOpen && ASICModel == eASICModel.BM1366">

Expand Down Expand Up @@ -161,7 +185,7 @@
<label for="autofanspeed">Automatic Fan Control</label>
</div>
</div>

<div class="col-12 md:col-4" *ngIf="form.get('overheat_mode')?.value === 1">
<div class="field-checkbox">
<p-checkbox name="overheat_mode" formControlName="overheat_mode" inputId="overheat_mode"
Expand All @@ -188,4 +212,4 @@


</form>
</ng-container>
</ng-container>
11 changes: 11 additions & 0 deletions main/http_server/axe-os/src/app/components/edit/edit.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,19 @@ export class EditComponent implements OnInit {
Validators.min(0),
Validators.max(65353)
]],
fallbackStratumURL: [info.fallbackStratumURL, [
Validators.pattern(/^(?!.*stratum\+tcp:\/\/).*$/),
]],
fallbackStratumPort: [info.fallbackStratumPort, [
Validators.required,
Validators.pattern(/^[^:]*$/),
Validators.min(0),
Validators.max(65353)
]],
stratumUser: [info.stratumUser, [Validators.required]],
stratumPassword: ['*****', [Validators.required]],
fallbackStratumUser: [info.fallbackStratumUser, [Validators.required]],
fallbackStratumPassword: ['password', [Validators.required]],
hostname: [info.hostname, [Validators.required]],
ssid: [info.ssid, [Validators.required]],
wifiPass: ['*****'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,18 @@ <h5>Pool Information</h5>
<td>User:</td>
<td style="word-break: break-all;">{{info.stratumUser}}</td>
</tr>

<tr>
<td>Fallback URL:</td>
<td style="word-break: break-all;">{{info.fallbackStratumURL}}</td>
</tr>
<tr>
<td>Fallback Port:</td>
<td style="word-break: break-all;">{{info.fallbackStratumPort}}</td>
</tr>
<tr>
<td>Fallback User:</td>
<td style="word-break: break-all;">{{info.fallbackStratumUser}}</td>
</tr>
</table>
</div>
</div>
Expand Down
3 changes: 3 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,7 +44,10 @@ export class SystemService {
ASICModel: eASICModel.BM1366,
stratumURL: "public-pool.io",
stratumPort: 21496,
fallbackStratumURL: "test.public-pool.io",
fallbackStratumPort: 21497,
stratumUser: "bc1q99n3pu025yyu0jlywpmwzalyhm36tg5u37w20d.bitaxe-U1",
fallbackStratumUser: "bc1q99n3pu025yyu0jlywpmwzalyhm36tg5u37w20d.bitaxe-U1",
frequency: 485,
version: "2.0",
boardVersion: "204",
Expand Down
3 changes: 3 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,7 +25,10 @@ export interface ISystemInfo {
ASICModel: eASICModel,
stratumURL: string,
stratumPort: number,
fallbackStratumURL: string,
fallbackStratumPort: number,
stratumUser: string,
fallbackStratumUser: string,
frequency: number,
version: string,
boardVersion: string,
Expand Down
18 changes: 18 additions & 0 deletions main/http_server/http_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,15 +272,27 @@ 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);
}
if ((item = cJSON_GetObjectItem(root, "stratumPassword")) != NULL) {
nvs_config_set_string(NVS_CONFIG_STRATUM_PASS, item->valuestring);
}
if ((item = cJSON_GetObjectItem(root, "fallbackStratumUser")) != NULL) {
nvs_config_set_string(NVS_CONFIG_FALLBACK_STRATUM_USER, item->valuestring);
}
if ((item = cJSON_GetObjectItem(root, "fallbackStratumPassword")) != NULL) {
nvs_config_set_string(NVS_CONFIG_FALLBACK_STRATUM_PASS, item->valuestring);
}
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,7 +380,9 @@ 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 * fallbackStratumUser = nvs_config_get_string(NVS_CONFIG_FALLBACK_STRATUM_USER, CONFIG_FALLBACK_STRATUM_USER);
char * board_version = nvs_config_get_string(NVS_CONFIG_BOARD_VERSION, "unknown");

cJSON * root = cJSON_CreateObject();
Expand Down Expand Up @@ -414,8 +428,11 @@ 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, "fallbackStratumUser", fallbackStratumUser);

cJSON_AddStringToObject(root, "version", esp_app_get_description()->version);
cJSON_AddStringToObject(root, "boardVersion", board_version);
Expand All @@ -434,6 +451,7 @@ static esp_err_t GET_system_info(httpd_req_t * req)
free(ssid);
free(hostname);
free(stratumURL);
free(fallbackStratumURL);
free(stratumUser);
free(board_version);

Expand Down
4 changes: 4 additions & 0 deletions main/nvs_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
#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_FALLBACK_STRATUM_USER "fbstratumuser"
#define NVS_CONFIG_FALLBACK_STRATUM_PASS "fbstratumpass"
#define NVS_CONFIG_ASIC_FREQ "asicfrequency"
#define NVS_CONFIG_ASIC_VOLTAGE "asicvoltage"
#define NVS_CONFIG_ASIC_MODEL "asicmodel"
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
Loading

0 comments on commit f108426

Please sign in to comment.