Parse stratum api reject reason (#472)

* Parse stratum api reject reason

* Remove superfluous nullcheck
This commit is contained in:
mutatrum 2024-12-09 16:35:13 +01:00 committed by GitHub
parent 6364805523
commit 6283480fa1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 28 additions and 2 deletions

View File

@ -59,6 +59,7 @@ typedef struct
uint32_t version_mask;
// result
bool response_success;
char * error_str;
} StratumApiV1Message;
void STRATUM_V1_reset_uid();

View File

@ -146,6 +146,7 @@ void STRATUM_V1_parse(StratumApiV1Message * message, const char * stratum_json)
// parse results
cJSON * result_json = cJSON_GetObjectItem(json, "result");
cJSON * error_json = cJSON_GetObjectItem(json, "error");
cJSON * reject_reason_json = cJSON_GetObjectItem(json, "reject-reason");
//if the result is null, then it's a fail
if (result_json == NULL) {
@ -158,6 +159,15 @@ void STRATUM_V1_parse(StratumApiV1Message * message, const char * stratum_json)
} else {
result = STRATUM_RESULT;
}
if (cJSON_IsArray(error_json)) {
int len = cJSON_GetArraySize(error_json);
if (len >= 2) {
cJSON * error_msg = cJSON_GetArrayItem(error_json, 1);
if (cJSON_IsString(error_msg)) {
message->error_str = strdup(cJSON_GetStringValue(error_msg));
}
}
}
message->response_success = false;
//if the result is a boolean, then parse it
@ -171,6 +181,9 @@ void STRATUM_V1_parse(StratumApiV1Message * message, const char * stratum_json)
message->response_success = true;
} else {
message->response_success = false;
if (cJSON_IsString(reject_reason_json)) {
message->error_str = strdup(cJSON_GetStringValue(reject_reason_json));
}
}
//if the id is STRATUM_ID_SUBSCRIBE parse it

View File

@ -153,4 +153,16 @@ TEST_CASE("Parse stratum result error", "[stratum]")
TEST_ASSERT_EQUAL(1, stratum_api_v1_message.message_id);
TEST_ASSERT_EQUAL(STRATUM_RESULT, stratum_api_v1_message.method);
TEST_ASSERT_FALSE(stratum_api_v1_message.response_success);
TEST_ASSERT_EQUAL("Job not found", stratum_api_v1_message.error_str);
}
TEST_CASE("Parse stratum result alternative error", "[stratum]")
{
StratumApiV1Message stratum_api_v1_message = {};
const char *json_string = "{\"reject-reason\":\"Above target\",\"result\":false,\"error\":null,\"id\":8}";
STRATUM_V1_parse(&stratum_api_v1_message, json_string);
TEST_ASSERT_EQUAL(1, stratum_api_v1_message.message_id);
TEST_ASSERT_EQUAL(STRATUM_RESULT, stratum_api_v1_message.method);
TEST_ASSERT_FALSE(stratum_api_v1_message.response_success);
TEST_ASSERT_EQUAL("Above target 2", stratum_api_v1_message.error_str);
}

View File

@ -302,14 +302,14 @@ void stratum_task(void * pvParameters)
ESP_LOGI(TAG, "message result accepted");
SYSTEM_notify_accepted_share(GLOBAL_STATE);
} else {
ESP_LOGW(TAG, "message result rejected");
ESP_LOGW(TAG, "message result rejected: %s", stratum_api_v1_message.error_str ? stratum_api_v1_message.error_str : "unknown");
SYSTEM_notify_rejected_share(GLOBAL_STATE);
}
} else if (stratum_api_v1_message.method == STRATUM_RESULT_SETUP) {
if (stratum_api_v1_message.response_success) {
ESP_LOGI(TAG, "setup message accepted");
} else {
ESP_LOGE(TAG, "setup message rejected");
ESP_LOGE(TAG, "setup message rejected: %s", stratum_api_v1_message.error_str ? stratum_api_v1_message.error_str : "unknown");
}
}
}