|
| 1 | +// --------------------------------------------------------------------------- |
| 2 | +// bch::new_tx_hashes accessor KAT -- the guard for the #905 send-side fix. |
| 3 | +// |
| 4 | +// #905: BCH's tx-forwarding SEND path (node.cpp send_shares) probed |
| 5 | +// `requires { obj->m_new_transaction_hashes; }` directly on the share object. |
| 6 | +// That FLAT probe is false for EVERY BCH share variant -- v17/v33 nest the list |
| 7 | +// inside m_tx_info, v34/v35/v36 carry no list at all -- so the whole needed_txs |
| 8 | +// forwarding block was dead code and BCH forwarded no tx bytes. share_tx_refs.hpp |
| 9 | +// replaces the flat probe with bch::new_tx_hashes(), which returns the nested |
| 10 | +// list for v17/v33 and nullptr for v34+. |
| 11 | +// |
| 12 | +// This test pins two things so the regression cannot silently return: |
| 13 | +// A. Compile-time topology (static_assert): the FLAT spelling is absent on |
| 14 | +// every variant (== the old probe was dead), the NESTED spelling is present |
| 15 | +// exactly on v17/v33, and new_tx_hashes() selects the right branch. |
| 16 | +// B. Runtime behaviour: the accessor delivers the v17/v33 list by reference |
| 17 | +// (send loop sees every referenced hash) and returns nullptr for v34+. |
| 18 | +// |
| 19 | +// Build-INERT wrt consensus surface: pure header-only over share.hpp + |
| 20 | +// share_tx_refs.hpp, no node/RPC. p2pool-merged-v36 surface: NONE (this is a |
| 21 | +// LOCAL relay-plumbing accessor; the share wire-format is unchanged). |
| 22 | +// --------------------------------------------------------------------------- |
| 23 | + |
| 24 | +#include <iostream> |
| 25 | +#include <type_traits> |
| 26 | +#include <vector> |
| 27 | + |
| 28 | +#include <core/uint256.hpp> |
| 29 | + |
| 30 | +#include "../share.hpp" |
| 31 | +#include "../share_tx_refs.hpp" |
| 32 | + |
| 33 | +namespace { |
| 34 | + |
| 35 | +int failures = 0; |
| 36 | +#define CHECK(cond) do { if (!(cond)) { \ |
| 37 | + std::cerr << "FAIL: " #cond " @ line " << __LINE__ << "\n"; ++failures; } } while (0) |
| 38 | + |
| 39 | +// ---- A. Compile-time topology -------------------------------------------- |
| 40 | +// The FLAT spelling the old probe assumed is absent on EVERY variant: that is |
| 41 | +// precisely why the old send path was dead. Held here so it stays absent. |
| 42 | +static_assert(!bch::has_flat_new_tx_hashes<bch::Share>, "v17 must NOT expose the flat list"); |
| 43 | +static_assert(!bch::has_flat_new_tx_hashes<bch::NewShare>, "v33 must NOT expose the flat list"); |
| 44 | +static_assert(!bch::has_flat_new_tx_hashes<bch::SegwitMiningShare>, "v34 must NOT expose the flat list"); |
| 45 | +static_assert(!bch::has_flat_new_tx_hashes<bch::PaddingBugfixShare>, "v35 must NOT expose the flat list"); |
| 46 | +static_assert(!bch::has_flat_new_tx_hashes<bch::MergedMiningShare>, "v36 must NOT expose the flat list"); |
| 47 | + |
| 48 | +// The NESTED spelling is present exactly on v17/v33 (which carry m_tx_info) and |
| 49 | +// absent on v34+ (SegWit-less variants carry no new-tx list). |
| 50 | +static_assert(bch::has_nested_new_tx_hashes<bch::Share>, "v17 must nest the list in m_tx_info"); |
| 51 | +static_assert(bch::has_nested_new_tx_hashes<bch::NewShare>, "v33 must nest the list in m_tx_info"); |
| 52 | +static_assert(!bch::has_nested_new_tx_hashes<bch::SegwitMiningShare>, "v34 carries no new-tx list"); |
| 53 | +static_assert(!bch::has_nested_new_tx_hashes<bch::PaddingBugfixShare>, "v35 carries no new-tx list"); |
| 54 | +static_assert(!bch::has_nested_new_tx_hashes<bch::MergedMiningShare>, "v36 carries no new-tx list"); |
| 55 | + |
| 56 | +// new_tx_hashes() selects a non-null pointer branch for v17/v33 and the null |
| 57 | +// pointer branch for v34+ -- checked at compile time on the return type / at run |
| 58 | +// time on the value below. |
| 59 | +template <typename S> |
| 60 | +constexpr bool returns_null_branch = |
| 61 | + std::is_same_v<decltype(bch::new_tx_hashes(std::declval<S*>())), |
| 62 | + const std::vector<uint256>*>; |
| 63 | +static_assert(returns_null_branch<bch::SegwitMiningShare>, "v34 accessor must be the null branch"); |
| 64 | +static_assert(returns_null_branch<bch::PaddingBugfixShare>, "v35 accessor must be the null branch"); |
| 65 | +static_assert(returns_null_branch<bch::MergedMiningShare>, "v36 accessor must be the null branch"); |
| 66 | + |
| 67 | +// ---- B. Runtime behaviour ------------------------------------------------- |
| 68 | + |
| 69 | +// Reproduce the send-side loop over the accessor result and count the hashes it |
| 70 | +// would forward -- this is exactly what node.cpp send_shares now does. |
| 71 | +template <typename ShareVariant> |
| 72 | +std::size_t forwardable_count(ShareVariant& s) |
| 73 | +{ |
| 74 | + std::size_t n = 0; |
| 75 | + const auto* new_txs = bch::new_tx_hashes(&s); |
| 76 | + if (!new_txs) |
| 77 | + return 0; |
| 78 | + for (const auto& th : *new_txs) |
| 79 | + { |
| 80 | + (void)th; |
| 81 | + ++n; |
| 82 | + } |
| 83 | + return n; |
| 84 | +} |
| 85 | + |
| 86 | +void test_v17_v33_deliver_nested_list() |
| 87 | +{ |
| 88 | + bch::Share s17; |
| 89 | + s17.m_tx_info.m_new_transaction_hashes = {uint256(1), uint256(2), uint256(3)}; |
| 90 | + |
| 91 | + const auto* p = bch::new_tx_hashes(&s17); |
| 92 | + CHECK(p != nullptr); |
| 93 | + // Same object, by reference -- no copy: the send path reads the live list. |
| 94 | + CHECK(p == &s17.m_tx_info.m_new_transaction_hashes); |
| 95 | + CHECK(p->size() == 3); |
| 96 | + CHECK(forwardable_count(s17) == 3); |
| 97 | + |
| 98 | + bch::NewShare s33; |
| 99 | + s33.m_tx_info.m_new_transaction_hashes = {uint256(7)}; |
| 100 | + CHECK(bch::new_tx_hashes(&s33) != nullptr); |
| 101 | + CHECK(forwardable_count(s33) == 1); |
| 102 | + |
| 103 | + // Empty nested list: accessor non-null, nothing to forward (not the same as |
| 104 | + // "no list" -- a v17 share with no new txs still probes cleanly). |
| 105 | + bch::Share s17_empty; |
| 106 | + CHECK(bch::new_tx_hashes(&s17_empty) != nullptr); |
| 107 | + CHECK(forwardable_count(s17_empty) == 0); |
| 108 | +} |
| 109 | + |
| 110 | +void test_v34plus_have_no_list() |
| 111 | +{ |
| 112 | + bch::SegwitMiningShare s34; |
| 113 | + bch::PaddingBugfixShare s35; |
| 114 | + bch::MergedMiningShare s36; |
| 115 | + CHECK(bch::new_tx_hashes(&s34) == nullptr); |
| 116 | + CHECK(bch::new_tx_hashes(&s35) == nullptr); |
| 117 | + CHECK(bch::new_tx_hashes(&s36) == nullptr); |
| 118 | + CHECK(forwardable_count(s34) == 0); |
| 119 | + CHECK(forwardable_count(s35) == 0); |
| 120 | + CHECK(forwardable_count(s36) == 0); |
| 121 | +} |
| 122 | + |
| 123 | +} // namespace |
| 124 | + |
| 125 | +int main() |
| 126 | +{ |
| 127 | + test_v17_v33_deliver_nested_list(); |
| 128 | + test_v34plus_have_no_list(); |
| 129 | + |
| 130 | + if (failures) |
| 131 | + { |
| 132 | + std::cerr << failures << " CHECK(s) failed\n"; |
| 133 | + return 1; |
| 134 | + } |
| 135 | + std::cout << "bch_share_tx_refs_kat_test: OK\n"; |
| 136 | + return 0; |
| 137 | +} |
0 commit comments