|
| 1 | +// --------------------------------------------------------------------------- |
| 2 | +// dgb_other_tx_resolver_test -- pins coin/other_tx_resolver.hpp, the won-block |
| 3 | +// reconstructor's (#82) transaction_hash_refs -> ordered other_tx hash walk. |
| 4 | +// |
| 5 | +// Proves the ref-walk semantics against a SYNTHETIC ancestry (no live |
| 6 | +// ShareTracker): the two tracker operations (nth_parent / new_transaction_hashes |
| 7 | +// lookup) are injected, exactly as resolve_other_tx_hashes takes them, so the |
| 8 | +// faithful p2pool get_other_tx_hashes resolution is verified in isolation: |
| 9 | +// * share_count == 0 indexes the won share's OWN new_transaction_hashes |
| 10 | +// * share_count == N walks back N generations |
| 11 | +// * ref ORDER is the output order (= block other_txs order) |
| 12 | +// * empty refs => empty list |
| 13 | +// * malformed refs (tx_count OOB / walk past chain end) throw, never emit a |
| 14 | +// wrong hash |
| 15 | +// |
| 16 | +// Header-only: links no dgb OBJECT lib, only core (uint256) + GTest. MUST also |
| 17 | +// appear in the build.yml --target allowlist (#143 NOT_BUILT trap). |
| 18 | +// --------------------------------------------------------------------------- |
| 19 | +#include <gtest/gtest.h> |
| 20 | + |
| 21 | +#include <functional> |
| 22 | +#include <map> |
| 23 | +#include <stdexcept> |
| 24 | +#include <vector> |
| 25 | + |
| 26 | +#include <core/uint256.hpp> |
| 27 | + |
| 28 | +#include "../coin/other_tx_resolver.hpp" |
| 29 | + |
| 30 | +using dgb::coin::resolve_other_tx_hashes; |
| 31 | +using dgb::TxHashRefs; |
| 32 | + |
| 33 | +namespace { |
| 34 | + |
| 35 | +uint256 H(const char* two) // 0x"two" repeated to 64 hex chars |
| 36 | +{ |
| 37 | + std::string s; |
| 38 | + for (int i = 0; i < 32; ++i) s += two; |
| 39 | + uint256 h; h.SetHex(s); |
| 40 | + return h; |
| 41 | +} |
| 42 | + |
| 43 | +// A synthetic 3-share chain: won <- p1 <- p2. Each share carries its own |
| 44 | +// new_transaction_hashes. parent[h] gives the immediate parent; null beyond p2. |
| 45 | +struct Ancestry |
| 46 | +{ |
| 47 | + uint256 won = H("a0"), p1 = H("a1"), p2 = H("a2"); |
| 48 | + std::map<uint256, uint256> parent; // child -> parent |
| 49 | + std::map<uint256, std::vector<uint256>> nths; // share -> new_transaction_hashes |
| 50 | + |
| 51 | + Ancestry() |
| 52 | + { |
| 53 | + parent[won] = p1; |
| 54 | + parent[p1] = p2; |
| 55 | + // p2 has no parent -> walking past it returns null. |
| 56 | + nths[won] = { H("c0"), H("c1") }; |
| 57 | + nths[p1] = { H("d0") }; |
| 58 | + nths[p2] = { H("e0"), H("e1"), H("e2") }; |
| 59 | + } |
| 60 | + |
| 61 | + // nth_parent_fn: 0 => start itself; null if walk runs off the chain. |
| 62 | + uint256 nth_parent(const uint256& start, uint64_t n) const |
| 63 | + { |
| 64 | + uint256 cur = start; |
| 65 | + for (uint64_t i = 0; i < n; ++i) |
| 66 | + { |
| 67 | + auto it = parent.find(cur); |
| 68 | + if (it == parent.end()) return uint256(); // null |
| 69 | + cur = it->second; |
| 70 | + } |
| 71 | + return cur; |
| 72 | + } |
| 73 | + |
| 74 | + const std::vector<uint256>& new_tx_hashes(const uint256& h) const |
| 75 | + { |
| 76 | + return nths.at(h); |
| 77 | + } |
| 78 | +}; |
| 79 | + |
| 80 | +std::function<uint256(const uint256&, uint64_t)> walk_of(const Ancestry& a) |
| 81 | +{ |
| 82 | + return [&a](const uint256& s, uint64_t n) { return a.nth_parent(s, n); }; |
| 83 | +} |
| 84 | +std::function<const std::vector<uint256>&(const uint256&)> nths_of(const Ancestry& a) |
| 85 | +{ |
| 86 | + return [&a](const uint256& h) -> const std::vector<uint256>& { return a.new_tx_hashes(h); }; |
| 87 | +} |
| 88 | + |
| 89 | +} // namespace |
| 90 | + |
| 91 | +// share_count == 0 resolves against the won share's own new_transaction_hashes. |
| 92 | +TEST(OtherTxResolver, SelfShareRefs) |
| 93 | +{ |
| 94 | + Ancestry a; |
| 95 | + std::vector<TxHashRefs> refs = { {0, 1}, {0, 0} }; |
| 96 | + auto out = resolve_other_tx_hashes(a.won, refs, walk_of(a), nths_of(a)); |
| 97 | + ASSERT_EQ(out.size(), 2u); |
| 98 | + EXPECT_EQ(out[0], H("c1")); // won.new_tx[1] |
| 99 | + EXPECT_EQ(out[1], H("c0")); // won.new_tx[0] -- ref ORDER preserved |
| 100 | +} |
| 101 | + |
| 102 | +// share_count == N walks N generations back before indexing. |
| 103 | +TEST(OtherTxResolver, AncestorWalkBack) |
| 104 | +{ |
| 105 | + Ancestry a; |
| 106 | + std::vector<TxHashRefs> refs = { {1, 0}, {2, 2} }; |
| 107 | + auto out = resolve_other_tx_hashes(a.won, refs, walk_of(a), nths_of(a)); |
| 108 | + ASSERT_EQ(out.size(), 2u); |
| 109 | + EXPECT_EQ(out[0], H("d0")); // p1.new_tx[0] |
| 110 | + EXPECT_EQ(out[1], H("e2")); // p2.new_tx[2] |
| 111 | +} |
| 112 | + |
| 113 | +// Mixed refs across generations preserve emission order = block other_txs order. |
| 114 | +TEST(OtherTxResolver, MixedOrderPreserved) |
| 115 | +{ |
| 116 | + Ancestry a; |
| 117 | + std::vector<TxHashRefs> refs = { {2, 0}, {0, 1}, {1, 0} }; |
| 118 | + auto out = resolve_other_tx_hashes(a.won, refs, walk_of(a), nths_of(a)); |
| 119 | + ASSERT_EQ(out.size(), 3u); |
| 120 | + EXPECT_EQ(out[0], H("e0")); |
| 121 | + EXPECT_EQ(out[1], H("c1")); |
| 122 | + EXPECT_EQ(out[2], H("d0")); |
| 123 | +} |
| 124 | + |
| 125 | +// No refs => empty other_txs (coinbase-only block). |
| 126 | +TEST(OtherTxResolver, EmptyRefs) |
| 127 | +{ |
| 128 | + Ancestry a; |
| 129 | + auto out = resolve_other_tx_hashes(a.won, {}, walk_of(a), nths_of(a)); |
| 130 | + EXPECT_TRUE(out.empty()); |
| 131 | +} |
| 132 | + |
| 133 | +// tx_count past the ancestor's new_transaction_hashes is a malformed share. |
| 134 | +TEST(OtherTxResolver, TxCountOutOfRangeThrows) |
| 135 | +{ |
| 136 | + Ancestry a; |
| 137 | + std::vector<TxHashRefs> refs = { {1, 5} }; // p1 has only 1 new tx |
| 138 | + EXPECT_THROW(resolve_other_tx_hashes(a.won, refs, walk_of(a), nths_of(a)), |
| 139 | + std::out_of_range); |
| 140 | +} |
| 141 | + |
| 142 | +// share_count past the chain end is a malformed share (null ancestor). |
| 143 | +TEST(OtherTxResolver, WalkPastChainEndThrows) |
| 144 | +{ |
| 145 | + Ancestry a; |
| 146 | + std::vector<TxHashRefs> refs = { {3, 0} }; // only won<-p1<-p2 exist |
| 147 | + EXPECT_THROW(resolve_other_tx_hashes(a.won, refs, walk_of(a), nths_of(a)), |
| 148 | + std::out_of_range); |
| 149 | +} |
0 commit comments