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

Parse stratum api reject reason #472

Merged
merged 2 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions components/stratum/include/stratum_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ typedef struct
uint32_t version_mask;
// result
bool response_success;
char * error_str;
} StratumApiV1Message;

void STRATUM_V1_reset_uid();
Expand Down
15 changes: 15 additions & 0 deletions components/stratum/stratum_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -171,6 +181,11 @@ void STRATUM_V1_parse(StratumApiV1Message * message, const char * stratum_json)
message->response_success = true;
} else {
message->response_success = false;
if (!cJSON_IsNull(reject_reason_json)) {
mutatrum marked this conversation as resolved.
Show resolved Hide resolved
if (cJSON_IsString(reject_reason_json)) {
message->error_str = strdup(cJSON_GetStringValue(reject_reason_json));
}
}
}

//if the id is STRATUM_ID_SUBSCRIBE parse it
Expand Down
12 changes: 12 additions & 0 deletions components/stratum/test/test_stratum_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
4 changes: 2 additions & 2 deletions main/tasks/stratum_task.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,14 +299,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");
}
}
}
Expand Down