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

Network resets2 #74

Merged
merged 4 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions main/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ static void _init_system(GlobalState * global_state, SystemModule * module)
module->lastClockSync = 0;
module->FOUND_BLOCK = false;
module->startup_done = false;

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

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

// set the best diff string
_suffix_string(module->best_nonce_diff, module->best_diff_string, DIFF_STRING_SIZE, 0);
Expand Down
2 changes: 2 additions & 0 deletions main/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ typedef struct
bool startup_done;
char ssid[20];
char wifi_status[20];
char * pool_url;
uint16_t pool_port;

uint32_t lastClockSync;
} SystemModule;
Expand Down
46 changes: 31 additions & 15 deletions main/tasks/stratum_task.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,21 @@
static const char * TAG = "stratum_task";
static ip_addr_t ip_Addr;
static bool bDNSFound = false;
static bool bDNSInvalid = false;

static StratumApiV1Message stratum_api_v1_message = {};

static SystemTaskModule SYSTEM_TASK_MODULE = {.stratum_difficulty = 8192};

void dns_found_cb(const char * name, const ip_addr_t * ipaddr, void * callback_arg)
{
ip_Addr = *ipaddr;
bDNSFound = true;
}
if (ipaddr != NULL) {
ip_Addr = *ipaddr;
} else {
bDNSInvalid = true;
}
}

void stratum_task(void * pvParameters)
{
Expand All @@ -39,26 +44,32 @@ void stratum_task(void * pvParameters)
int addr_family = 0;
int ip_protocol = 0;

char * stratum_url = nvs_config_get_string(NVS_CONFIG_STRATUM_URL, STRATUM_URL);
uint16_t port = nvs_config_get_u16(NVS_CONFIG_STRATUM_PORT, PORT);

char *stratum_url = GLOBAL_STATE->SYSTEM_MODULE.pool_url;
uint16_t port = GLOBAL_STATE->SYSTEM_MODULE.pool_port;

// check to see if the STRATUM_URL is an ip address already
if (inet_pton(AF_INET, stratum_url, &ip_Addr) == 1) {
bDNSFound = true;
} else {
// it's a hostname. Lookup the ip address.
IP_ADDR4(&ip_Addr, 0, 0, 0, 0);
}
else
{
ESP_LOGI(TAG, "Get IP for URL: %s\n", stratum_url);
dns_gethostbyname(stratum_url, &ip_Addr, dns_found_cb, NULL);
while (!bDNSFound)
;
while (!bDNSFound);

if (bDNSInvalid) {
ESP_LOGE(TAG, "DNS lookup failed for URL: %s\n", stratum_url);
//set ip_Addr to 0.0.0.0 so that connect() will fail
IP_ADDR4(&ip_Addr, 0, 0, 0, 0);
}

}

// make IP address string from ip_Addr
snprintf(host_ip, sizeof(host_ip), "%d.%d.%d.%d", ip4_addr1(&ip_Addr.u_addr.ip4), ip4_addr2(&ip_Addr.u_addr.ip4),
ip4_addr3(&ip_Addr.u_addr.ip4), ip4_addr4(&ip_Addr.u_addr.ip4));
ESP_LOGI(TAG, "Connecting to: stratum+tcp://%s:%d (%s)\n", stratum_url, port, host_ip);
free(stratum_url);

while (1) {
struct sockaddr_in dest_addr;
Expand All @@ -76,11 +87,16 @@ void stratum_task(void * pvParameters)
}
ESP_LOGI(TAG, "Socket created, connecting to %s:%d", host_ip, port);

int err = connect(GLOBAL_STATE->sock, (struct sockaddr *) &dest_addr, sizeof(struct sockaddr_in6));
if (err != 0) {
ESP_LOGE(TAG, "Socket unable to connect: errno %d", errno);
esp_restart();
break;
int err = connect(GLOBAL_STATE->sock, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr_in6));
if (err != 0)
{
ESP_LOGE(TAG, "Socket unable to connect to %s:%d (errno %d)", stratum_url, port, errno);
// close the socket
shutdown(GLOBAL_STATE->sock, SHUT_RDWR);
close(GLOBAL_STATE->sock);
// instead of restarting, retry this every 5 seconds
vTaskDelay(5000 / portTICK_PERIOD_MS);
continue;
}

STRATUM_V1_configure_version_rolling(GLOBAL_STATE->sock, &GLOBAL_STATE->version_mask);
Expand Down