|
| 1 | +// --------------------------------------------------------------------------- |
| 2 | +// dgb_header_ingest_test -- guards c2pool::dgb::wire_header_ingest, the |
| 3 | +// connector that feeds the embedded P2P header-download feed |
| 4 | +// (dgb::interfaces::Node::new_headers, fired by coin/p2p_node.hpp's |
| 5 | +// ADD_P2P_HANDLER(headers)) into HeaderChain::validate_and_append through the |
| 6 | +// make_header_sample SSOT. This is what turns the HeaderChain from a unit-test |
| 7 | +// fixture into a chain that advances on LIVE wire headers (lighting up |
| 8 | +// tip_hash() -> previousblockhash for the embedded work template). |
| 9 | +// |
| 10 | +// What it pins: |
| 11 | +// 1. Routing -- a header announced on new_headers lands in the chain via |
| 12 | +// make_header_sample (block_hash = sha256d(header)) + validate_and_append. |
| 13 | +// 2. Batch fidelity -- a multi-header batch is ingested in arrival order; the |
| 14 | +// tip is the LAST header and the chain grew by the full batch length. |
| 15 | +// 3. The connector is the driver -- an un-wired node ingests nothing |
| 16 | +// (no hidden side path appends headers). |
| 17 | +// 4. Disposition is DELEGATED, not overridden -- a header validate_and_append |
| 18 | +// REJECTS (unknown algo bits) never reaches the chain; the connector adds |
| 19 | +// no policy of its own and never force-appends. |
| 20 | +// |
| 21 | +// Pulls dgb::interfaces::Node (block.hpp codec) + header_chain.hpp + the |
| 22 | +// make_header_sample SSOT, so it links the proven dgb OBJECT-lib SCC set like |
| 23 | +// dgb_header_sample_build_test. MUST also appear in BOTH build.yml --target |
| 24 | +// allowlists (#143 NOT_BUILT trap). |
| 25 | +// --------------------------------------------------------------------------- |
| 26 | + |
| 27 | +#include <cstdint> |
| 28 | +#include <vector> |
| 29 | + |
| 30 | +#include <gtest/gtest.h> |
| 31 | + |
| 32 | +#include <impl/dgb/coin/header_ingest.hpp> |
| 33 | +#include <impl/dgb/coin/node_interface.hpp> |
| 34 | +#include <impl/dgb/coin/header_chain.hpp> |
| 35 | +#include <impl/dgb/coin/header_sample_build.hpp> |
| 36 | +#include <impl/dgb/coin/hash_format.hpp> |
| 37 | +#include <impl/dgb/coin/dgb_block_algo.hpp> |
| 38 | + |
| 39 | +using c2pool::dgb::HeaderChain; |
| 40 | +using c2pool::dgb::make_header_sample; |
| 41 | +using c2pool::dgb::wire_header_ingest; |
| 42 | +using dgb::coin::BlockHeaderType; |
| 43 | +using dgb::coin::u256_be_display_hex; |
| 44 | +using dgb::coin::DGB_BLOCK_VERSION_ALGO; |
| 45 | + |
| 46 | +namespace { |
| 47 | + |
| 48 | +// Canonical Bitcoin genesis header -- same 80-byte serialization DGB uses, and |
| 49 | +// a Scrypt header (version 1 -> algo nibble 0 == SCRYPT), so it walks the |
| 50 | +// VALIDATE_SCRYPT path. pow_hash is left 0 by make_header_sample (trivially |
| 51 | +// satisfies any target), and a default-ctor HeaderChain leaves pow_limit / |
| 52 | +// target_timespan 0 so the ceiling + DigiShield gates are no-ops -- exactly the |
| 53 | +// bootstrap posture the embedded port starts from. |
| 54 | +BlockHeaderType genesis_header() |
| 55 | +{ |
| 56 | + BlockHeaderType h; |
| 57 | + h.m_version = 1; |
| 58 | + h.m_previous_block.SetNull(); |
| 59 | + h.m_merkle_root.SetHex( |
| 60 | + "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"); |
| 61 | + h.m_timestamp = 1231006505u; |
| 62 | + h.m_bits = 0x1d00ffffu; |
| 63 | + h.m_nonce = 2083236893u; |
| 64 | + return h; |
| 65 | +} |
| 66 | + |
| 67 | +// A second Scrypt header whose nTime is strictly greater than genesis' (the MTP |
| 68 | +// monotonicity gate requires nTime > median-of-ancestors), with a distinct |
| 69 | +// nonce so its sha256d block id differs from genesis'. |
| 70 | +BlockHeaderType second_scrypt_header() |
| 71 | +{ |
| 72 | + BlockHeaderType h = genesis_header(); |
| 73 | + h.m_timestamp = 1231006506u; // genesis + 1 -> passes MTP over [genesis] |
| 74 | + h.m_nonce = 12345u; // distinct id |
| 75 | + return h; |
| 76 | +} |
| 77 | + |
| 78 | +// An unknown-algo header: algo nibble 1 (0x0100) maps to no DigiByte algo, so |
| 79 | +// dgb_header_disposition() -> REJECT. validate_and_append must drop it. |
| 80 | +BlockHeaderType unknown_algo_header() |
| 81 | +{ |
| 82 | + BlockHeaderType h = genesis_header(); |
| 83 | + h.m_version = 1 | 0x0100; // nibble 1 == ALGO_UNKNOWN |
| 84 | + return h; |
| 85 | +} |
| 86 | + |
| 87 | +const std::string GENESIS_ID = |
| 88 | + "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"; |
| 89 | + |
| 90 | +} // namespace |
| 91 | + |
| 92 | +// A fresh HeaderChain is empty -- no tip height, no tip hash. |
| 93 | +TEST(HeaderIngest, EmptyChainHasNoTip) |
| 94 | +{ |
| 95 | + HeaderChain chain; |
| 96 | + EXPECT_FALSE(chain.tip_height().has_value()); |
| 97 | + EXPECT_FALSE(chain.tip_hash().has_value()); |
| 98 | +} |
| 99 | + |
| 100 | +// 1. A Scrypt header announced on new_headers is ingested through |
| 101 | +// make_header_sample + validate_and_append: the chain grows and tip_hash() |
| 102 | +// is the header's sha256d block id (the well-known genesis hash). |
| 103 | +TEST(HeaderIngest, AnnouncedScryptHeaderIsIngested) |
| 104 | +{ |
| 105 | + HeaderChain chain; |
| 106 | + dgb::interfaces::Node node; |
| 107 | + auto sub = wire_header_ingest(node, chain); |
| 108 | + |
| 109 | + node.new_headers.happened(std::vector<BlockHeaderType>{ genesis_header() }); |
| 110 | + |
| 111 | + ASSERT_TRUE(chain.tip_height().has_value()); |
| 112 | + ASSERT_TRUE(chain.tip_hash().has_value()); |
| 113 | + EXPECT_EQ(u256_be_display_hex(*chain.tip_hash()), GENESIS_ID); |
| 114 | +} |
| 115 | + |
| 116 | +// 2. A multi-header batch is ingested in arrival order: the tip is the LAST |
| 117 | +// header, and the chain grew by the full batch length (height delta == 1 |
| 118 | +// versus a single-header chain). |
| 119 | +TEST(HeaderIngest, BatchIngestedInArrivalOrder) |
| 120 | +{ |
| 121 | + HeaderChain one; |
| 122 | + dgb::interfaces::Node node_one; |
| 123 | + auto sub_one = wire_header_ingest(node_one, one); |
| 124 | + node_one.new_headers.happened(std::vector<BlockHeaderType>{ genesis_header() }); |
| 125 | + |
| 126 | + HeaderChain two; |
| 127 | + dgb::interfaces::Node node_two; |
| 128 | + auto sub_two = wire_header_ingest(node_two, two); |
| 129 | + node_two.new_headers.happened( |
| 130 | + std::vector<BlockHeaderType>{ genesis_header(), second_scrypt_header() }); |
| 131 | + |
| 132 | + ASSERT_TRUE(one.tip_height().has_value()); |
| 133 | + ASSERT_TRUE(two.tip_height().has_value()); |
| 134 | + // The two-header chain is exactly one block taller than the one-header chain. |
| 135 | + EXPECT_EQ(*two.tip_height(), *one.tip_height() + 1u); |
| 136 | + // Tip is the LAST header of the batch, not the first. |
| 137 | + ASSERT_TRUE(two.tip_hash().has_value()); |
| 138 | + EXPECT_EQ(u256_be_display_hex(*two.tip_hash()), |
| 139 | + u256_be_display_hex(make_header_sample(second_scrypt_header()).block_hash)); |
| 140 | + EXPECT_NE(u256_be_display_hex(*two.tip_hash()), GENESIS_ID); |
| 141 | +} |
| 142 | + |
| 143 | +// 3. The connector is the driver: a node with NO ingest subscription appends |
| 144 | +// nothing when headers are announced. |
| 145 | +TEST(HeaderIngest, UnwiredNodeIngestsNothing) |
| 146 | +{ |
| 147 | + HeaderChain chain; |
| 148 | + dgb::interfaces::Node node; // deliberately NOT wired |
| 149 | + |
| 150 | + node.new_headers.happened(std::vector<BlockHeaderType>{ genesis_header() }); |
| 151 | + |
| 152 | + EXPECT_FALSE(chain.tip_height().has_value()); |
| 153 | + EXPECT_FALSE(chain.tip_hash().has_value()); |
| 154 | +} |
| 155 | + |
| 156 | +// 4. Disposition is delegated to validate_and_append, not overridden by the |
| 157 | +// connector: an unknown-algo header (REJECT) never reaches the chain. |
| 158 | +TEST(HeaderIngest, RejectedHeaderIsNotAppended) |
| 159 | +{ |
| 160 | + HeaderChain chain; |
| 161 | + dgb::interfaces::Node node; |
| 162 | + auto sub = wire_header_ingest(node, chain); |
| 163 | + |
| 164 | + // Sanity: this header really is on the unknown-algo (reject) path. |
| 165 | + ASSERT_EQ(unknown_algo_header().m_version & DGB_BLOCK_VERSION_ALGO, 0x0100); |
| 166 | + |
| 167 | + node.new_headers.happened(std::vector<BlockHeaderType>{ unknown_algo_header() }); |
| 168 | + |
| 169 | + EXPECT_FALSE(chain.tip_height().has_value()); |
| 170 | + EXPECT_FALSE(chain.tip_hash().has_value()); |
| 171 | +} |
0 commit comments