fix stratum parsing not always counting rejected shares (#163)

* fix stratum parsing not counting rejected messages

* add unit tests
This commit is contained in:
MoellerDi 2024-05-24 04:53:05 +02:00 committed by GitHub
parent b53b641c68
commit 1da7132b6a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 48 additions and 10 deletions

View File

@ -118,25 +118,32 @@ void STRATUM_V1_parse(StratumApiV1Message * message, const char * stratum_json)
result = MINING_SET_DIFFICULTY;
} else if (strcmp("mining.set_version_mask", method_json->valuestring) == 0) {
result = MINING_SET_VERSION_MASK;
} else {
ESP_LOGI(TAG, "unhandled method in stratum message: %s", stratum_json);
}
} else {
// parse results
cJSON * result_json = cJSON_GetObjectItem(json, "result");
if (result_json == NULL){
cJSON * error_json = cJSON_GetObjectItem(json, "error");
if (result_json == NULL) {
message->response_success = false;
}
else if (cJSON_IsBool(result_json)) {
result = STRATUM_RESULT;
if (cJSON_IsTrue(result_json)) {
message->response_success = true;
}else{
message->response_success = false;
}
} else {
cJSON * mask = cJSON_GetObjectItem(result_json, "version-rolling.mask");
if (mask != NULL) {
result = STRATUM_RESULT_VERSION_MASK;
message->version_mask = strtoul(mask->valuestring, NULL, 16);
} else if (cJSON_IsBool(result_json)) {
result = STRATUM_RESULT;
if (cJSON_IsTrue(result_json)) {
message->response_success = true;
} else {
message->response_success = false;
}
} else if (!cJSON_IsNull(error_json)) {
result = STRATUM_RESULT;
message->response_success = false;
} else {
ESP_LOGI(TAG, "unhandled result in stratum message: %s", stratum_json);
}
}
}

View File

@ -103,4 +103,34 @@ TEST_CASE("Parse stratum notify params", "[mining.notify]")
// TEST_ASSERT_EQUAL(result, 0);
// TEST_ASSERT_EQUAL_STRING(extranonce, "e9695791");
// TEST_ASSERT_EQUAL_INT(extranonce2_len, 4);
// }
// }
TEST_CASE("Parse stratum mining.set_version_mask params", "[stratum]")
{
StratumApiV1Message stratum_api_v1_message = {};
const char *json_string = "{\"id\":1,\"method\":\"mining.set_version_mask\",\"params\":[\"1fffe000\"]}";
STRATUM_V1_parse(&stratum_api_v1_message, json_string);
TEST_ASSERT_EQUAL(1, stratum_api_v1_message.message_id);
TEST_ASSERT_EQUAL(MINING_SET_VERSION_MASK, stratum_api_v1_message.method);
TEST_ASSERT_EQUAL_HEX32(0x1fffe000, stratum_api_v1_message.version_mask);
}
TEST_CASE("Parse stratum result success", "[stratum]")
{
StratumApiV1Message stratum_api_v1_message = {};
const char *json_string = "{\"id\":1,\"error\":null,\"result\":true}";
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_TRUE(stratum_api_v1_message.response_success);
}
TEST_CASE("Parse stratum result error", "[stratum]")
{
StratumApiV1Message stratum_api_v1_message = {};
const char *json_string = "{\"id\":1,\"result\":null,\"error\":[21,\"Job not found\",\"\"]}";
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);
}

View File

@ -31,6 +31,7 @@ void ASIC_result_task(void *pvParameters)
if (GLOBAL_STATE->valid_jobs[job_id] == 0)
{
ESP_LOGI(TAG, "Invalid job nonce found, id=%d", job_id);
continue;
}
// check the nonce difficulty