Work value in RPC work_validate response (#1943)

* Add value output to RPC work_validate

* Cleanup test

* Include a human-readable multiplier from network.publish_threshold in the response.
This commit is contained in:
Guilherme Lawless 2019-05-06 11:47:36 +01:00 committed by cryptocode
commit f47e858786
2 changed files with 78 additions and 35 deletions

View file

@ -4397,6 +4397,9 @@ void nano::json_handler::work_validate ()
bool invalid (nano::work_validate (hash, work, &result_difficulty));
bool valid (!invalid && result_difficulty >= difficulty);
response_l.put ("valid", valid ? "1" : "0");
response_l.put ("value", nano::to_string_hex (result_difficulty));
float multiplier = static_cast<float> (-node.network_params.network.publish_threshold) / (-result_difficulty);
response_l.put ("multiplier", std::to_string (multiplier));
}
response_errors ();
}

View file

@ -3202,63 +3202,103 @@ TEST (rpc, work_validate)
request.put ("action", "work_validate");
request.put ("hash", hash.to_string ());
request.put ("work", nano::to_string_hex (work1));
test_response response1 (request, rpc.config.port, system.io_ctx);
system.deadline_set (5s);
while (response1.status == 0)
{
ASSERT_NO_ERROR (system.poll ());
test_response response (request, rpc.config.port, system.io_ctx);
system.deadline_set (5s);
while (response.status == 0)
{
ASSERT_NO_ERROR (system.poll ());
}
ASSERT_EQ (200, response.status);
std::string validate_text (response.json.get<std::string> ("valid"));
ASSERT_EQ ("1", validate_text);
std::string value_text (response.json.get<std::string> ("value"));
uint64_t value;
ASSERT_FALSE (nano::from_string_hex (value_text, value));
ASSERT_GE (value, params.network.publish_threshold);
}
ASSERT_EQ (200, response1.status);
std::string validate_text1 (response1.json.get<std::string> ("valid"));
ASSERT_EQ ("1", validate_text1);
uint64_t work2 (0);
request.put ("work", nano::to_string_hex (work2));
test_response response2 (request, rpc.config.port, system.io_ctx);
system.deadline_set (5s);
while (response2.status == 0)
{
ASSERT_NO_ERROR (system.poll ());
test_response response (request, rpc.config.port, system.io_ctx);
system.deadline_set (5s);
while (response.status == 0)
{
ASSERT_NO_ERROR (system.poll ());
}
ASSERT_EQ (200, response.status);
std::string validate_text (response.json.get<std::string> ("valid"));
ASSERT_EQ ("0", validate_text);
std::string value_text (response.json.get<std::string> ("value"));
uint64_t value;
ASSERT_FALSE (nano::from_string_hex (value_text, value));
ASSERT_GE (params.network.publish_threshold, value);
}
ASSERT_EQ (200, response2.status);
std::string validate_text2 (response2.json.get<std::string> ("valid"));
ASSERT_EQ ("0", validate_text2);
uint64_t result_difficulty;
ASSERT_FALSE (nano::work_validate (hash, work1, &result_difficulty));
ASSERT_GE (result_difficulty, params.network.publish_threshold);
request.put ("work", nano::to_string_hex (work1));
request.put ("difficulty", nano::to_string_hex (result_difficulty));
test_response response3 (request, rpc.config.port, system.io_ctx);
system.deadline_set (5s);
while (response3.status == 0)
{
ASSERT_NO_ERROR (system.poll ());
test_response response (request, rpc.config.port, system.io_ctx);
system.deadline_set (5s);
while (response.status == 0)
{
ASSERT_NO_ERROR (system.poll ());
}
ASSERT_EQ (200, response.status);
bool validate (response.json.get<bool> ("valid"));
ASSERT_TRUE (validate);
}
ASSERT_EQ (200, response3.status);
bool validate3 (response3.json.get<bool> ("valid"));
ASSERT_TRUE (validate3);
uint64_t difficulty4 (0xfff0000000000000);
request.put ("work", nano::to_string_hex (work1));
request.put ("difficulty", nano::to_string_hex (difficulty4));
test_response response4 (request, rpc.config.port, system.io_ctx);
system.deadline_set (5s);
while (response4.status == 0)
{
ASSERT_NO_ERROR (system.poll ());
test_response response (request, rpc.config.port, system.io_ctx);
system.deadline_set (5s);
while (response.status == 0)
{
ASSERT_NO_ERROR (system.poll ());
}
ASSERT_EQ (200, response.status);
bool validate (response.json.get<bool> ("valid"));
ASSERT_EQ (result_difficulty >= difficulty4, validate);
}
ASSERT_EQ (200, response4.status);
bool validate4 (response4.json.get<bool> ("valid"));
ASSERT_EQ (result_difficulty >= difficulty4, validate4);
uint64_t work3 (node1.work_generate_blocking (hash, difficulty4));
request.put ("work", nano::to_string_hex (work3));
test_response response5 (request, rpc.config.port, system.io_ctx);
system.deadline_set (5s);
while (response5.status == 0)
{
ASSERT_NO_ERROR (system.poll ());
test_response response (request, rpc.config.port, system.io_ctx);
system.deadline_set (5s);
while (response.status == 0)
{
ASSERT_NO_ERROR (system.poll ());
}
ASSERT_EQ (200, response.status);
bool validate (response.json.get<bool> ("valid"));
ASSERT_TRUE (validate);
}
// Test the multiplier field in the response
// It's a multiplier from the base network threshold, so we make sure the test network threshold has not been changed
// The work and its value/multiplier were calculated beforehand
ASSERT_EQ (params.network.publish_threshold, 0xff00000000000000);
request.put ("work", nano::to_string_hex (0x4b52c90f538bbb60));
{
test_response response (request, rpc.config.port, system.io_ctx);
system.deadline_set (5s);
while (response.status == 0)
{
ASSERT_NO_ERROR (system.poll ());
}
ASSERT_EQ (200, response.status);
bool validate (response.json.get<bool> ("valid"));
ASSERT_TRUE (validate);
std::string value_text (response.json.get<std::string> ("value"));
uint64_t value;
ASSERT_FALSE (nano::from_string_hex (value_text, value));
ASSERT_EQ (value, 0xfff27e7a57c285cd);
double multiplier (response.json.get<double> ("multiplier"));
ASSERT_NEAR (multiplier, 18.9546, 1e-4);
}
ASSERT_EQ (200, response5.status);
bool validate5 (response5.json.get<bool> ("valid"));
ASSERT_TRUE (validate5);
}
TEST (rpc, successors)