Merge mining API: added a check for the result field

This commit is contained in:
SChernykh 2023-12-26 20:28:30 +01:00
parent 896eac6d8c
commit 1729b51868

View file

@ -136,7 +136,7 @@ void MergeMiningClient::merge_mining_get_chain_id()
bool MergeMiningClient::parse_merge_mining_get_chain_id(const char* data, size_t size) bool MergeMiningClient::parse_merge_mining_get_chain_id(const char* data, size_t size)
{ {
auto err = [this](const char* msg) { auto err = [this](const char* msg) {
LOGWARN(1, "merge_mining_get_chain_id RPC call failed: " << msg << ". Trying again in 1 second."); LOGWARN(3, "merge_mining_get_chain_id RPC call failed: " << msg << ". Trying again in 1 second.");
std::this_thread::sleep_for(std::chrono::milliseconds(1000)); std::this_thread::sleep_for(std::chrono::milliseconds(1000));
merge_mining_get_chain_id(); merge_mining_get_chain_id();
return false; return false;
@ -152,6 +152,10 @@ bool MergeMiningClient::parse_merge_mining_get_chain_id(const char* data, size_t
return err(doc["error"].IsString() ? doc["error"].GetString() : "an unknown error occurred"); return err(doc["error"].IsString() ? doc["error"].GetString() : "an unknown error occurred");
} }
if (!doc.HasMember("result")) {
return err("\"result\" field not found");
}
const auto& result = doc["result"]; const auto& result = doc["result"];
if (!result.IsObject() || !result.HasMember("chain_id")) { if (!result.IsObject() || !result.HasMember("chain_id")) {
@ -204,7 +208,7 @@ void MergeMiningClient::merge_mining_get_job(uint64_t height, const hash& prev_i
}, },
[this](const char* data, size_t size, double) { [this](const char* data, size_t size, double) {
if (size > 0) { if (size > 0) {
LOGERR(1, "couldn't get merge mining job from " << m_host << ':' << m_port << ", error " << log::const_buf(data, size)); LOGERR(3, "couldn't get merge mining job from " << m_host << ':' << m_port << ", error " << log::const_buf(data, size));
} }
m_getJobRunning = false; m_getJobRunning = false;
}, &m_loop); }, &m_loop);
@ -213,7 +217,7 @@ void MergeMiningClient::merge_mining_get_job(uint64_t height, const hash& prev_i
bool MergeMiningClient::parse_merge_mining_get_job(const char* data, size_t size, bool& changed) bool MergeMiningClient::parse_merge_mining_get_job(const char* data, size_t size, bool& changed)
{ {
auto err = [](const char* msg) { auto err = [](const char* msg) {
LOGWARN(1, "merge_mining_get_job RPC call failed: " << msg); LOGWARN(3, "merge_mining_get_job RPC call failed: " << msg);
return false; return false;
}; };
@ -227,6 +231,10 @@ bool MergeMiningClient::parse_merge_mining_get_job(const char* data, size_t size
return err(doc["error"].IsString() ? doc["error"].GetString() : "an unknown error occurred"); return err(doc["error"].IsString() ? doc["error"].GetString() : "an unknown error occurred");
} }
if (!doc.HasMember("result")) {
return err("\"result\" field not found");
}
const auto& result = doc["result"]; const auto& result = doc["result"];
if (!result.IsObject()) { if (!result.IsObject()) {
@ -296,7 +304,7 @@ void MergeMiningClient::merge_mining_submit_solution(const std::vector<uint8_t>&
}, },
[this](const char* data, size_t size, double) { [this](const char* data, size_t size, double) {
if (size > 0) { if (size > 0) {
LOGERR(1, "couldn't submit merge mining solution to " << m_host << ':' << m_port << ", error " << log::const_buf(data, size)); LOGERR(3, "couldn't submit merge mining solution to " << m_host << ':' << m_port << ", error " << log::const_buf(data, size));
} }
}, &m_loop); }, &m_loop);
} }
@ -304,7 +312,7 @@ void MergeMiningClient::merge_mining_submit_solution(const std::vector<uint8_t>&
bool MergeMiningClient::parse_merge_mining_submit_solution(const char* data, size_t size) bool MergeMiningClient::parse_merge_mining_submit_solution(const char* data, size_t size)
{ {
auto err = [this](const char* msg) { auto err = [this](const char* msg) {
LOGWARN(1, "merge_mining_submit_solution to " << m_host << ':' << m_port << " failed: " << msg); LOGWARN(3, "merge_mining_submit_solution to " << m_host << ':' << m_port << " failed: " << msg);
return false; return false;
}; };
@ -318,6 +326,10 @@ bool MergeMiningClient::parse_merge_mining_submit_solution(const char* data, siz
return err(doc["error"].IsString() ? doc["error"].GetString() : "an unknown error occurred"); return err(doc["error"].IsString() ? doc["error"].GetString() : "an unknown error occurred");
} }
if (!doc.HasMember("result")) {
return err("\"result\" field not found");
}
const auto& result = doc["result"]; const auto& result = doc["result"];
if (!result.IsObject()) { if (!result.IsObject()) {