Skip to content

Define and test bitcoind header json serializer #1888

Description

@dsbaars

Summary

chain/json/header exposes the wire (native) and electrumx_tag serializers but not a
bitcoind_tag variant. The block serializer (src/chain/json/block.cpp) already carries the
full bitcoind family (bitcoind_tag / bitcoind_hashed_tag / bitcoind_verbose_tag /
bitcoind_embedded_tag) and inlines the header fields into the block's bitcoind encoding, but
there is no standalone bitcoind header serializer. Consumers that need a Core-format header JSON
must hand-roll it.

Proposed

Mirror the existing electrum header serializer (#1822):

  • include/bitcoin/system/chain/json/header.hpp: add DECLARE_JSON_TAGGED_INVOKE(bitcoind_tag, header).
  • src/chain/json/header.cpp: add DEFINE_JSON_FROM_TAGGED(bitcoind_tag, header).
  • test/chain/json/header.cpp: add header__json__bitcoind__expected.

Fields mirror the header portion of DEFINE_JSON_FROM_TAGGED(bitcoind_tag, block):
hash, version, versionHex, merkleroot, time, nonce, bits, difficulty.

Chain-context properties (confirmations, height, mediantime, previousblockhash,
nextblockhash) stay a consumer responsibility, consistent with the block bitcoind serializer
(which documents them as chain, not block, properties).

Precedent

  • Define and test electrum header json serializer. #1822 "Define and test electrum header json serializer" added DEFINE_JSON_FROM_TAGGED(electrumx_tag, header). This is the exact analog for bitcoind_tag.
  • src/chain/json/block.cpp DEFINE_JSON_FROM_TAGGED(bitcoind_tag, block) is the reference for field formatting (versionHex / bits as big-endian base16, difficulty as the computed double).

Consumer

libbitcoin/libbitcoin-server#795 hand-rolls this as header_to_bitcoind in
protocol_bitcoind_rpc_json.cpp. Once this lands it collapses to value_from(bitcoind(*header))
in both getblockheader and the REST /rest/headers handler, and the protocol-layer helper is
removed. Raised from review feedback on that PR.

Testing

Exact-string assertion via json::value_from(bitcoind(instance)), as in #1822. Reusing the
block bitcoind test instance (header{ 42, null_hash, one_hash, 43, 44, 45 }) yields
"versionHex":"0000002a", "bits":"0000002c", "difficulty":1.0279680609929991E73.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions