mirror of
https://github.com/skot/ESP-Miner.git
synced 2025-08-02 22:02:27 +02:00
Add stratum response time (#1063)
* feat: add stratum response time * fix: use esp_timer and move the response tracking to it's dedicated function call * feat: add response time to UI * feat: tie to the stratum id * fix: refactor parsing * fix: start a bit later * fix: refactor style * fix: modulo the request id * Update main/tasks/stratum_task.c Co-authored-by: Johnny <johnny9dev@pm.me> * Update main/tasks/stratum_task.c Co-authored-by: Johnny <johnny9dev@pm.me> --------- Co-authored-by: Johnny <johnny9dev@pm.me>
This commit is contained in:
@@ -11,4 +11,5 @@ REQUIRES
|
||||
"json"
|
||||
"mbedtls"
|
||||
"app_update"
|
||||
"esp_timer"
|
||||
)
|
@@ -4,11 +4,14 @@
|
||||
#include "cJSON.h"
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
|
||||
#define MAX_MERKLE_BRANCHES 32
|
||||
#define HASH_SIZE 32
|
||||
#define COINBASE_SIZE 100
|
||||
#define COINBASE2_SIZE 128
|
||||
#define MAX_REQUEST_IDS 1024
|
||||
|
||||
typedef enum
|
||||
{
|
||||
@@ -60,6 +63,12 @@ typedef struct
|
||||
char * error_str;
|
||||
} StratumApiV1Message;
|
||||
|
||||
typedef struct {
|
||||
int64_t timestamp_us;
|
||||
bool tracking;
|
||||
} RequestTiming;
|
||||
|
||||
|
||||
void STRATUM_V1_initialize_buffer();
|
||||
|
||||
char *STRATUM_V1_receive_jsonrpc_line(int sockfd);
|
||||
@@ -68,6 +77,8 @@ int STRATUM_V1_subscribe(int socket, int send_uid, const char * model);
|
||||
|
||||
void STRATUM_V1_parse(StratumApiV1Message *message, const char *stratum_json);
|
||||
|
||||
void STRATUM_V1_stamp_tx(int request_id);
|
||||
|
||||
void STRATUM_V1_free_mining_notify(mining_notify *params);
|
||||
|
||||
int STRATUM_V1_authorize(int socket, int send_uid, const char *username, const char *pass);
|
||||
@@ -80,4 +91,6 @@ int STRATUM_V1_submit_share(int socket, int send_uid, const char *username, cons
|
||||
const char *extranonce_2, const uint32_t ntime, const uint32_t nonce,
|
||||
const uint32_t version);
|
||||
|
||||
double STRATUM_V1_get_response_time_ms(int request_id);
|
||||
|
||||
#endif // STRATUM_API_H
|
@@ -10,14 +10,64 @@
|
||||
#include "esp_ota_ops.h"
|
||||
#include "lwip/sockets.h"
|
||||
#include "utils.h"
|
||||
#include "esp_timer.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define BUFFER_SIZE 1024
|
||||
static const char * TAG = "stratum_api";
|
||||
|
||||
static char * json_rpc_buffer = NULL;
|
||||
static size_t json_rpc_buffer_size = 0;
|
||||
static int last_parsed_request_id = -1;
|
||||
|
||||
static RequestTiming request_timings[MAX_REQUEST_IDS];
|
||||
static bool initialized = false;
|
||||
|
||||
static void init_request_timings() {
|
||||
if (!initialized) {
|
||||
for (int i = 0; i < MAX_REQUEST_IDS; i++) {
|
||||
request_timings[i].timestamp_us = 0;
|
||||
request_timings[i].tracking = false;
|
||||
}
|
||||
initialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
static RequestTiming* get_request_timing(int request_id) {
|
||||
if (request_id < 0) return NULL;
|
||||
int index = request_id % MAX_REQUEST_IDS;
|
||||
return &request_timings[index];
|
||||
}
|
||||
|
||||
void STRATUM_V1_stamp_tx(int request_id)
|
||||
{
|
||||
init_request_timings();
|
||||
if (request_id >= 1) {
|
||||
RequestTiming *timing = get_request_timing(request_id);
|
||||
if (timing) {
|
||||
timing->timestamp_us = esp_timer_get_time();
|
||||
timing->tracking = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
double STRATUM_V1_get_response_time_ms(int request_id)
|
||||
{
|
||||
init_request_timings();
|
||||
if (request_id < 0) return -1.0;
|
||||
|
||||
RequestTiming *timing = get_request_timing(request_id);
|
||||
if (!timing || !timing->tracking) {
|
||||
return -1.0;
|
||||
}
|
||||
|
||||
double response_time = (esp_timer_get_time() - timing->timestamp_us) / 1000.0;
|
||||
timing->tracking = false;
|
||||
return response_time;
|
||||
}
|
||||
|
||||
static void debug_stratum_tx(const char *);
|
||||
int _parse_stratum_subscribe_result_message(const char * result_json_str, char ** extranonce, int * extranonce2_len);
|
||||
@@ -110,6 +160,7 @@ void STRATUM_V1_parse(StratumApiV1Message * message, const char * stratum_json)
|
||||
int64_t parsed_id = -1;
|
||||
if (id_json != NULL && cJSON_IsNumber(id_json)) {
|
||||
parsed_id = id_json->valueint;
|
||||
last_parsed_request_id = parsed_id;
|
||||
}
|
||||
message->message_id = parsed_id;
|
||||
|
||||
@@ -375,6 +426,7 @@ int STRATUM_V1_configure_version_rolling(int socket, int send_uid, uint32_t * ve
|
||||
|
||||
static void debug_stratum_tx(const char * msg)
|
||||
{
|
||||
STRATUM_V1_stamp_tx(last_parsed_request_id);
|
||||
//remove the trailing newline
|
||||
char * newline = strchr(msg, '\n');
|
||||
if (newline != NULL) {
|
||||
|
Reference in New Issue
Block a user