|
| 1 | +#pragma once |
| 2 | +// --------------------------------------------------------------------------- |
| 3 | +// dgb::coin::resolve_other_tx_hashes -- the won-block reconstructor's (#82) |
| 4 | +// connecting tissue between a share's transaction_hash_refs and the ordered |
| 5 | +// other_tx hash list that assemble_won_block (block_assembly.hpp) needs. |
| 6 | +// |
| 7 | +// Faithful C++ port of the ancestry resolution inside p2pool data.py |
| 8 | +// Tracker.get_other_tx_hashes(share): |
| 9 | +// |
| 10 | +// parents_needed = max(ref_share_count for ref_share_count, _ in |
| 11 | +// share.share_info['transaction_hash_refs']) ... |
| 12 | +// other_tx_hashes = [ |
| 13 | +// self.items[ self.get_nth_parent_hash(share.hash, ref_share_count) ] |
| 14 | +// .share_info['new_transaction_hashes'][ref_tx_count] |
| 15 | +// for ref_share_count, ref_tx_count in |
| 16 | +// share.share_info['transaction_hash_refs'] |
| 17 | +// ] |
| 18 | +// |
| 19 | +// A transaction_hash_ref is a (share_count, tx_count) pair: walk back |
| 20 | +// `share_count` generations from the won share (share_count == 0 is the won |
| 21 | +// share ITSELF, matching get_nth_parent_hash(h, 0) == h), then index `tx_count` |
| 22 | +// into THAT ancestor's new_transaction_hashes. The result preserves ref order, |
| 23 | +// which is the block's other_txs order ([gentx] ++ other_txs in as_block). |
| 24 | +// |
| 25 | +// Why a ref-walk and NOT a flat known_txs map (integrator 2026-06-19): the |
| 26 | +// share format stores ancestry as back-references into ancestor shares' |
| 27 | +// new_transaction_hashes (V34+ never embeds txs), so the ONLY faithful |
| 28 | +// resolution is to re-walk that ancestry through the tracker. A flat |
| 29 | +// known_txs[hash] lookup would resolve the same hashes by accident on the |
| 30 | +// happy path but diverges the moment two ancestors announce the same txid or |
| 31 | +// a ref points past the locally-known set -- it cannot reproduce how the |
| 32 | +// sharechain actually addresses a tx. |
| 33 | +// |
| 34 | +// The two tracker operations are INJECTED as callables (same seam-first |
| 35 | +// decomposition as won_block_dispatch.hpp / WonBlockReconstructor) so the walk |
| 36 | +// is unit-testable against a synthetic ancestry without standing up a live |
| 37 | +// ShareTracker. In the run-loop these bind to: |
| 38 | +// nth_parent_fn = chain.get_nth_parent_via_skip(h, n) (shared skip list) |
| 39 | +// new_tx_hashes_fn= chain.get_share(h).invoke([](auto* s){ |
| 40 | +// return s->m_tx_info.m_new_transaction_hashes; }) |
| 41 | +// |
| 42 | +// Per-coin isolation: src/impl/dgb/ only. p2pool-merged-v36 surface: NONE -- |
| 43 | +// this only re-reads share_info already validated by share_check; no share |
| 44 | +// format, PoW, coinbase commitment, or PPLNS math is touched. |
| 45 | +// --------------------------------------------------------------------------- |
| 46 | + |
| 47 | +#include <functional> |
| 48 | +#include <stdexcept> |
| 49 | +#include <vector> |
| 50 | + |
| 51 | +#include "../share_types.hpp" // dgb::TxHashRefs, uint256 |
| 52 | + |
| 53 | +namespace dgb |
| 54 | +{ |
| 55 | +namespace coin |
| 56 | +{ |
| 57 | + |
| 58 | +// Resolve a share's transaction_hash_refs to the ordered other_tx hash list. |
| 59 | +// |
| 60 | +// won_share_hash : hash of the share that found the block (walk origin) |
| 61 | +// refs : share.m_tx_info.m_transaction_hash_refs, in order |
| 62 | +// nth_parent_fn : (start, n) -> hash of the n-th parent of `start` |
| 63 | +// (n == 0 returns `start`); IsNull() if the walk runs off |
| 64 | +// the end of the known sharechain |
| 65 | +// new_tx_hashes_fn: share_hash -> that share's new_transaction_hashes |
| 66 | +// |
| 67 | +// Returns the other_tx hashes in ref order. Throws std::out_of_range if an |
| 68 | +// ancestor walk-back exceeds the sharechain depth or a tx_count indexes past |
| 69 | +// an ancestor's new_transaction_hashes -- both are malformed-share conditions |
| 70 | +// that must fail the reconstruction loudly rather than emit a wrong block. |
| 71 | +inline std::vector<uint256> |
| 72 | +resolve_other_tx_hashes( |
| 73 | + const uint256& won_share_hash, |
| 74 | + const std::vector<TxHashRefs>& refs, |
| 75 | + const std::function<uint256(const uint256&, uint64_t)>& nth_parent_fn, |
| 76 | + const std::function<const std::vector<uint256>&(const uint256&)>& new_tx_hashes_fn) |
| 77 | +{ |
| 78 | + std::vector<uint256> out; |
| 79 | + out.reserve(refs.size()); |
| 80 | + |
| 81 | + for (const auto& ref : refs) |
| 82 | + { |
| 83 | + const uint256 ancestor = nth_parent_fn(won_share_hash, ref.m_share_count); |
| 84 | + if (ancestor.IsNull()) |
| 85 | + throw std::out_of_range( |
| 86 | + "resolve_other_tx_hashes: transaction_hash_ref share_count " |
| 87 | + "walks past the known sharechain"); |
| 88 | + |
| 89 | + const std::vector<uint256>& nths = new_tx_hashes_fn(ancestor); |
| 90 | + if (ref.m_tx_count >= nths.size()) |
| 91 | + throw std::out_of_range( |
| 92 | + "resolve_other_tx_hashes: transaction_hash_ref tx_count " |
| 93 | + "out of range for ancestor new_transaction_hashes"); |
| 94 | + |
| 95 | + out.push_back(nths[ref.m_tx_count]); |
| 96 | + } |
| 97 | + |
| 98 | + return out; |
| 99 | +} |
| 100 | + |
| 101 | +} // namespace coin |
| 102 | +} // namespace dgb |
0 commit comments