|
| 1 | +// --------------------------------------------------------------------------- |
| 2 | +// dgb_mempool_ingest_test -- pins c2pool::dgb::wire_mempool_ingest, the |
| 3 | +// connector that routes the embedded P2P `tx` relay (dgb::interfaces::Node:: |
| 4 | +// new_tx) into the in-process Mempool so the embedded work template selects |
| 5 | +// from a live pool instead of an always-empty one. |
| 6 | +// |
| 7 | +// This is the tx analog of dgb_header_ingest_test (new_headers -> HeaderChain). |
| 8 | +// Disposition (txid compute, duplicate rejection, weight + byte cap) is |
| 9 | +// delegated to Mempool::add_tx, the insertion SSOT; the connector adds no |
| 10 | +// policy of its own, so the assertions here pin the WIRING (the feed reaches |
| 11 | +// the pool, an unwired node feeds nothing, the handle drives it) rather than |
| 12 | +// re-testing the mempool's internals. |
| 13 | +// |
| 14 | +// Links the full dgb_coin codec like dgb_embedded_tx_select_test (it compiles |
| 15 | +// the tx serialization). MUST be in BOTH build.yml --target allowlists (#143 |
| 16 | +// NOT_BUILT trap). |
| 17 | +// --------------------------------------------------------------------------- |
| 18 | + |
| 19 | +#include <cstdint> |
| 20 | + |
| 21 | +#include <gtest/gtest.h> |
| 22 | + |
| 23 | +#include <impl/dgb/coin/mempool_ingest.hpp> |
| 24 | +#include <impl/dgb/coin/node_interface.hpp> |
| 25 | +#include <impl/dgb/coin/mempool.hpp> |
| 26 | +#include <impl/dgb/coin/transaction.hpp> |
| 27 | + |
| 28 | +using c2pool::dgb::wire_mempool_ingest; |
| 29 | +using dgb::coin::Mempool; |
| 30 | +using dgb::coin::MutableTransaction; |
| 31 | +using dgb::coin::Transaction; |
| 32 | +using dgb::coin::TxIn; |
| 33 | +using dgb::coin::TxOut; |
| 34 | +using dgb::coin::compute_txid; |
| 35 | + |
| 36 | +namespace { |
| 37 | + |
| 38 | +// A minimal, distinct tx tagged by its prevout index so each has a distinct |
| 39 | +// txid (mirrors the builder in dgb_embedded_tx_select_test). |
| 40 | +MutableTransaction tagged_tx(uint32_t index) |
| 41 | +{ |
| 42 | + MutableTransaction tx; |
| 43 | + tx.version = 1; |
| 44 | + tx.locktime = 0; |
| 45 | + TxIn in; |
| 46 | + in.prevout.hash.SetNull(); |
| 47 | + in.prevout.index = index; |
| 48 | + in.sequence = 0xffffffff; |
| 49 | + tx.vin.push_back(in); |
| 50 | + TxOut out; |
| 51 | + out.value = 50'000; |
| 52 | + tx.vout.push_back(out); |
| 53 | + return tx; |
| 54 | +} |
| 55 | + |
| 56 | +} // namespace |
| 57 | + |
| 58 | +// 1. A fresh, wired-but-quiet node leaves the pool empty. |
| 59 | +TEST(MempoolIngest, EmptyPoolBeforeRelay) |
| 60 | +{ |
| 61 | + Mempool pool; |
| 62 | + dgb::interfaces::Node node; |
| 63 | + auto sub = wire_mempool_ingest(node, pool); |
| 64 | + |
| 65 | + EXPECT_EQ(pool.size(), 0u); |
| 66 | +} |
| 67 | + |
| 68 | +// 2. A tx announced on new_tx is ingested through add_tx: the pool grows and |
| 69 | +// the tx is queryable by its txid. |
| 70 | +TEST(MempoolIngest, AnnouncedTxIsIngested) |
| 71 | +{ |
| 72 | + Mempool pool; |
| 73 | + dgb::interfaces::Node node; |
| 74 | + auto sub = wire_mempool_ingest(node, pool); |
| 75 | + |
| 76 | + auto mt = tagged_tx(0); |
| 77 | + node.new_tx.happened(Transaction(mt)); |
| 78 | + |
| 79 | + EXPECT_EQ(pool.size(), 1u); |
| 80 | + EXPECT_TRUE(pool.contains(compute_txid(mt))); |
| 81 | +} |
| 82 | + |
| 83 | +// 3. Multiple distinct relays accumulate in the pool. |
| 84 | +TEST(MempoolIngest, DistinctRelaysAccumulate) |
| 85 | +{ |
| 86 | + Mempool pool; |
| 87 | + dgb::interfaces::Node node; |
| 88 | + auto sub = wire_mempool_ingest(node, pool); |
| 89 | + |
| 90 | + auto a = tagged_tx(0); |
| 91 | + auto b = tagged_tx(1); |
| 92 | + node.new_tx.happened(Transaction(a)); |
| 93 | + node.new_tx.happened(Transaction(b)); |
| 94 | + |
| 95 | + EXPECT_EQ(pool.size(), 2u); |
| 96 | + EXPECT_TRUE(pool.contains(compute_txid(a))); |
| 97 | + EXPECT_TRUE(pool.contains(compute_txid(b))); |
| 98 | +} |
| 99 | + |
| 100 | +// 4. Disposition is delegated to add_tx, not the connector: a duplicate relay |
| 101 | +// of the same tx is rejected by the pool's SSOT, leaving size unchanged. |
| 102 | +TEST(MempoolIngest, DuplicateRelayIsRejected) |
| 103 | +{ |
| 104 | + Mempool pool; |
| 105 | + dgb::interfaces::Node node; |
| 106 | + auto sub = wire_mempool_ingest(node, pool); |
| 107 | + |
| 108 | + auto mt = tagged_tx(0); |
| 109 | + node.new_tx.happened(Transaction(mt)); |
| 110 | + node.new_tx.happened(Transaction(mt)); // same txid |
| 111 | + |
| 112 | + EXPECT_EQ(pool.size(), 1u); |
| 113 | +} |
| 114 | + |
| 115 | +// 5. The connector is the driver: a node with NO ingest subscription drops the |
| 116 | +// relay -- the pool stays empty. |
| 117 | +TEST(MempoolIngest, UnwiredNodeIngestsNothing) |
| 118 | +{ |
| 119 | + Mempool pool; |
| 120 | + dgb::interfaces::Node node; // deliberately NOT wired |
| 121 | + |
| 122 | + node.new_tx.happened(Transaction(tagged_tx(0))); |
| 123 | + |
| 124 | + EXPECT_EQ(pool.size(), 0u); |
| 125 | +} |
0 commit comments