diff --git a/include/bitcoin/system/chain/json/header.hpp b/include/bitcoin/system/chain/json/header.hpp index 56d73102cc..e969ebd49f 100644 --- a/include/bitcoin/system/chain/json/header.hpp +++ b/include/bitcoin/system/chain/json/header.hpp @@ -30,6 +30,8 @@ namespace chain { DECLARE_JSON_TAG_INVOKE(header); DECLARE_JSON_TAG_INVOKE(header::cptr); +DECLARE_JSON_TAGGED_INVOKE(electrumx_tag, header); + } // namespace chain } // namespace system } // namespace libbitcoin diff --git a/include/bitcoin/system/chain/json/macros.hpp b/include/bitcoin/system/chain/json/macros.hpp index b8f7b67ee3..eb5b6586ca 100644 --- a/include/bitcoin/system/chain/json/macros.hpp +++ b/include/bitcoin/system/chain/json/macros.hpp @@ -29,6 +29,7 @@ struct bitcoind_tag {}; struct bitcoind_hashed_tag {}; struct bitcoind_verbose_tag {}; struct bitcoind_embedded_tag {}; +struct electrumx_tag {}; /// Reference wrapper. template @@ -67,6 +68,11 @@ inline auto bitcoind_embedded(const Type& value) NOEXCEPT { return wrapped{ value }; } +template +inline auto electrumx(const Type& value) NOEXCEPT +{ + return wrapped{ value }; +} /// json aliases template diff --git a/src/chain/json/header.cpp b/src/chain/json/header.cpp index 6aaa43d8b4..ca02e545b9 100644 --- a/src/chain/json/header.cpp +++ b/src/chain/json/header.cpp @@ -43,7 +43,7 @@ DEFINE_JSON_FROM_TAG(header) { value = { - // hash is computed property + // hash is computed property. { "hash", encode_hash(instance.hash()) }, { "version", instance.version() }, { "previous", encode_hash(instance.previous_block_hash()) }, @@ -64,6 +64,25 @@ DEFINE_JSON_FROM_TAG(header::cptr) tag_invoke(from_tag{}, value, *instance); } +// electrumx +// ---------------------------------------------------------------------------- + +DEFINE_JSON_FROM_TAGGED(electrumx_tag, header) +{ + const auto& head = instance.value; + value = + { + { "bits", head.bits() }, + { "merkle_root", encode_hash(head.merkle_root()) }, + { "nonce", head.nonce() }, + { "prev_block_hash", encode_hash(head.previous_block_hash()) }, + { "timestamp", head.timestamp() }, + { "version", head.version() } + + ////{ "block_height", 0 } + }; +} + } // namespace chain } // namespace system } // namespace libbitcoin diff --git a/test/chain/json/header.cpp b/test/chain/json/header.cpp index fda4d60c4b..fd0ef92d78 100644 --- a/test/chain/json/header.cpp +++ b/test/chain/json/header.cpp @@ -57,4 +57,32 @@ BOOST_AUTO_TEST_CASE(header__json__conversions__expected) BOOST_REQUIRE(json::value_to
(value) == instance); } +BOOST_AUTO_TEST_CASE(block__json__electrumx__expected) +{ + const header instance + { + 42, + null_hash, + one_hash, + 43, + 44, + 45 + }; + + const std::string_view text + { + "{" + R"("bits":44,)" + R"("merkle_root":"0000000000000000000000000000000000000000000000000000000000000001",)" + R"("nonce":45,)" + R"("prev_block_hash":"0000000000000000000000000000000000000000000000000000000000000000",)" + R"("timestamp":43,)" + R"("version":42)" + "}" + }; + + const auto value = json::value_from(electrumx(instance)); + BOOST_REQUIRE_EQUAL(json::serialize(value), text); +} + BOOST_AUTO_TEST_SUITE_END()