From f47e858786251152185ae967868c5c1fd5c37f85 Mon Sep 17 00:00:00 2001 From: Guilherme Lawless Date: Mon, 6 May 2019 11:47:36 +0100 Subject: [PATCH] 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. --- nano/node/json_handler.cpp | 3 + nano/rpc_test/rpc.cpp | 110 +++++++++++++++++++++++++------------ 2 files changed, 78 insertions(+), 35 deletions(-) diff --git a/nano/node/json_handler.cpp b/nano/node/json_handler.cpp index 613bc511..2c345a8f 100644 --- a/nano/node/json_handler.cpp +++ b/nano/node/json_handler.cpp @@ -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 (-node.network_params.network.publish_threshold) / (-result_difficulty); + response_l.put ("multiplier", std::to_string (multiplier)); } response_errors (); } diff --git a/nano/rpc_test/rpc.cpp b/nano/rpc_test/rpc.cpp index b1043132..a5e4e449 100644 --- a/nano/rpc_test/rpc.cpp +++ b/nano/rpc_test/rpc.cpp @@ -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 ("valid")); + ASSERT_EQ ("1", validate_text); + std::string value_text (response.json.get ("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 ("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 ("valid")); + ASSERT_EQ ("0", validate_text); + std::string value_text (response.json.get ("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 ("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 ("valid")); + ASSERT_TRUE (validate); } - ASSERT_EQ (200, response3.status); - bool validate3 (response3.json.get ("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 ("valid")); + ASSERT_EQ (result_difficulty >= difficulty4, validate); } - ASSERT_EQ (200, response4.status); - bool validate4 (response4.json.get ("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 ("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 ("valid")); + ASSERT_TRUE (validate); + std::string value_text (response.json.get ("value")); + uint64_t value; + ASSERT_FALSE (nano::from_string_hex (value_text, value)); + ASSERT_EQ (value, 0xfff27e7a57c285cd); + double multiplier (response.json.get ("multiplier")); + ASSERT_NEAR (multiplier, 18.9546, 1e-4); } - ASSERT_EQ (200, response5.status); - bool validate5 (response5.json.get ("valid")); - ASSERT_TRUE (validate5); } TEST (rpc, successors)