diff --git a/src/json_parsers.h b/src/json_parsers.h index 6a4faab..82cc81a 100644 --- a/src/json_parsers.h +++ b/src/json_parsers.h @@ -104,7 +104,7 @@ struct parse_wrapper if (!from_hex(s[i], d)) { return false; } - out_value.hi = (out_value.hi << 4) || (out_value.lo >> 60); + out_value.hi = (out_value.hi << 4) | (out_value.lo >> 60); out_value.lo = (out_value.lo << 4) | d; } diff --git a/tests/src/difficulty_type_tests.cpp b/tests/src/difficulty_type_tests.cpp index a742176..5554045 100644 --- a/tests/src/difficulty_type_tests.cpp +++ b/tests/src/difficulty_type_tests.cpp @@ -16,6 +16,8 @@ */ #include "common.h" +#include "json_parsers.h" +#include #include "gtest/gtest.h" #include #include @@ -146,6 +148,33 @@ TEST(difficulty_type, input_output) test_value(std::numeric_limits::max(), std::numeric_limits::max(), "340282366920938463463374607431768211455"); } +TEST(difficulty_type, json_parser) +{ + auto test_value = [](uint64_t lo, uint64_t hi, const char* s) { + difficulty_type diff{ lo, hi }; + std::stringstream ss; + ss << "{\"diff\":\"" << s << "\"}"; + + using namespace rapidjson; + Document doc; + doc.Parse(ss.str().c_str()); + + difficulty_type diff2; + parseValue(doc, "diff", diff2); + ASSERT_EQ(diff2, diff); + }; + + test_value(0, 0, "0x0"); + test_value(1, 0, "0x1"); + test_value(0x123456789abcdefull, 0, "0x123456789abcdef"); + test_value(0x123456789abcdefull, 0, "0x123456789ABCDEF"); + test_value(std::numeric_limits::max(), 0, "0xffffffffffffffff"); + test_value(0, 1, "0x10000000000000000"); + test_value(1, 1, "0x10000000000000001"); + test_value(0x1122334455667788ull, 0x99aabbccddeeff00ull, "0x99aabbccddeeff001122334455667788"); + test_value(std::numeric_limits::max(), std::numeric_limits::max(), "0xffffffffffffffffffffffffffffffff"); +} + TEST(difficulty_type, check_pow) { hash h; diff --git a/tests/src/hash_tests.cpp b/tests/src/hash_tests.cpp index 1d8a845..8fa0c4b 100644 --- a/tests/src/hash_tests.cpp +++ b/tests/src/hash_tests.cpp @@ -16,6 +16,8 @@ */ #include "common.h" +#include "json_parsers.h" +#include #include "gtest/gtest.h" #include #include @@ -90,4 +92,36 @@ TEST(hash, input_output) check(h, "fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0efeeedecebeae9e8e7e6e5e4e3e2e1e0"); } +TEST(hash, json_parser) +{ + auto check = [](const hash& h, const char* s) { + std::stringstream ss; + ss << "{\"hash\":\"" << s << "\"}"; + + using namespace rapidjson; + Document doc; + doc.Parse(ss.str().c_str()); + + hash h2; + parseValue(doc, "hash", h2); + ASSERT_EQ(h2, h); + }; + + hash h; + check(h, "0000000000000000000000000000000000000000000000000000000000000000"); + + memset(h.h, -1, HASH_SIZE); + check(h, "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"); + + for (uint8_t i = 0; i < HASH_SIZE; ++i) { + h.h[i] = i; + } + check(h, "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"); + + for (uint8_t i = 0; i < HASH_SIZE; ++i) { + h.h[i] = 0xff - i; + } + check(h, "fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0efeeedecebeae9e8e7e6e5e4e3e2e1e0"); +} + }