Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/bitcoin/system/chain/json/header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions include/bitcoin/system/chain/json/macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<class Tag, class Type>
Expand Down Expand Up @@ -67,6 +68,11 @@ inline auto bitcoind_embedded(const Type& value) NOEXCEPT
{
return wrapped<bitcoind_embedded_tag, Type>{ value };
}
template<class Type>
inline auto electrumx(const Type& value) NOEXCEPT
{
return wrapped<electrumx_tag, Type>{ value };
}

/// json aliases
template <typename Value, typename ...Args>
Expand Down
21 changes: 20 additions & 1 deletion src/chain/json/header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) },
Expand All @@ -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
28 changes: 28 additions & 0 deletions test/chain/json/header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,32 @@ BOOST_AUTO_TEST_CASE(header__json__conversions__expected)
BOOST_REQUIRE(json::value_to<header>(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()