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

Add support for client.reconnect method in Stratum V1 protocol #333

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

bears-fan
Copy link

@bears-fan bears-fan commented Sep 10, 2024

#41

This adds support for the client.reconnect method. This allows the miner to handle client.reconnect messages sent by pools, setting the miner to reconnect to a different host and port, optionally after a specified wait time (defaults to 1 second).

Changes:

  • added fields to stratum_api.h
  • added parsing the json msgs from the CLIENT_RECONNECT method
  • on reconnect, close the current connection and connect to the new one
  • improved socket handling by marking the socket as invalid (-1) whenever it's closed

Test instructions:

  1. Setup the mock server and start the server with default settings python3 server.py
  2. Point the bitaxe at the mock server
  3. While the server is running, use the command reconnect, enter host public-pool.io and port 21496
  4. Follow the bitaxe logs to verify expected usage. Should see a log entry similar to Received client.reconnect: host=public-pool.io, port=21496, wait=10
  5. The miner should gracefully close the connection and connect after the wait time specified

@bears-fan bears-fan marked this pull request as ready for review September 10, 2024 02:15
Copy link
Collaborator

@johnny9 johnny9 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have a better commit message. Include the context/module and issue # after the body.

Also, please include instructions on how to verify.

main/tasks/stratum_task.c Show resolved Hide resolved
@bears-fan bears-fan changed the title Add stratum client.reconnect method Add support for client.reconnect method in Stratum V1 protocol Sep 14, 2024
Copy link
Collaborator

@johnny9 johnny9 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay, I finally got my environment all setup again and my Bitaxe Ultra going. Unfortunately, my device failed to connect to public-pool after sending the reconnect.

I (94975) stratum_api: tx: {"id": 49, "method": "mining.submit", "params": ["bc1q69arrz589lcnm9pzmvxf8e9h5qycf8k42jkh99.bitaxeUltra201", "0002", "21000000", "66f1c2f2", "02790094", "00ddc000"]}
I (95295) stratum_task: Socket created, connecting to 192.168.1.148:21496
E (95295) stratum_task: Socket unable to connect to public-pool.io:21496 (errno 104)

@johnny9
Copy link
Collaborator

johnny9 commented Sep 23, 2024

You need to break one more loop so that DNS resolution can happen again.

Here are my suggested changes

From e5aef2e6cb3298fd4a852bde8e0344ce682be7e3 Mon Sep 17 00:00:00 2001
From: johnny9 <985648+johnny9@users.noreply.github.com>
Date: Mon, 23 Sep 2024 16:40:02 -0400
Subject: [PATCH] suggested changes

---
 main/tasks/stratum_task.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/main/tasks/stratum_task.c b/main/tasks/stratum_task.c
index a86fe5f..6bc2d6f 100644
--- a/main/tasks/stratum_task.c
+++ b/main/tasks/stratum_task.c
@@ -76,7 +76,7 @@ void stratum_task(void * pvParameters)
     int ip_protocol = 0;
 	int retry_attempts = 0;
     int delay_ms = BASE_DELAY_MS;
-
+    bool need_reconnect = false;
 
     char *stratum_url = GLOBAL_STATE->SYSTEM_MODULE.pool_url;
     uint16_t port = GLOBAL_STATE->SYSTEM_MODULE.pool_port;
@@ -85,6 +85,7 @@ void stratum_task(void * pvParameters)
         //clear flags used by the dns callback, dns_found_cb()
         bDNSFound = false;
         bDNSInvalid = false;
+        need_reconnect = false;
 
         // check to see if the STRATUM_URL is an ip address already
         if (inet_pton(AF_INET, stratum_url, &ip_Addr) == 1) {
@@ -109,7 +110,7 @@ void stratum_task(void * pvParameters)
                     ip4_addr3(&ip_Addr.u_addr.ip4), ip4_addr4(&ip_Addr.u_addr.ip4));
         ESP_LOGI(TAG, "Connecting to: stratum+tcp://%s:%d (%s)", stratum_url, port, host_ip);
 
-        while (1) {
+        while (!need_reconnect) {
             if (!is_wifi_connected()) {
                 ESP_LOGI(TAG, "WiFi disconnected, attempting to reconnect...");
                 esp_wifi_connect();
@@ -170,7 +171,8 @@ void stratum_task(void * pvParameters)
             free(password);
             free(username);
 
-            while (1) {
+            // Message handling loop
+            while (!need_reconnect) {
                 char * line = STRATUM_V1_receive_jsonrpc_line(GLOBAL_STATE->sock);
                 if (!line) {
                     ESP_LOGE(TAG, "Failed to receive JSON-RPC line, reconnecting...");
@@ -242,8 +244,8 @@ void stratum_task(void * pvParameters)
                     // reset DNS lookup flags 
                     bDNSFound = false;
                     bDNSInvalid = false;
-
-                    break;
+                    need_reconnect = true;
+                    break; // Exit the message handling loop
                 } else if (stratum_api_v1_message.method == STRATUM_RESULT) {
                     if (stratum_api_v1_message.response_success) {
                         ESP_LOGI(TAG, "message result accepted");
@@ -259,6 +261,12 @@ void stratum_task(void * pvParameters)
                         ESP_LOGE(TAG, "setup message rejected");
                     }
                 }
+            } // End of message handling loop
+
+            if (need_reconnect)
+            {
+                // Break out of the connection loop to reconnect
+                break;
             }
 
             if (GLOBAL_STATE->sock != -1) {
-- 
2.43.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants