|
| 1 | +/// Phase S8 — Dash sharechain pool-node leaf 1: per-peer state (src/impl/dash/peer.hpp). |
| 2 | +/// |
| 3 | +/// dash::Peer is the analog of btc::Peer / dgb::Peer: the state a pool Node holds |
| 4 | +/// for each connected p2pool share-network peer. The pool-node bridge (node.hpp, |
| 5 | +/// a later leaf) keys a Peer off every connection and drives it from the share |
| 6 | +/// message handlers. This KAT pins the observable contract those handlers read: |
| 7 | +/// |
| 8 | +/// (a) fresh-peer defaults — no negotiated version yet, empty subversion, |
| 9 | +/// zeroed services/nonce, empty remember-sets, connection time stamped. |
| 10 | +/// (b) version-handshake fields round-trip (what the `version` handler writes). |
| 11 | +/// (c) the remembered-tx protocol state: m_remote_txs is a dedup hash set and |
| 12 | +/// m_remembered_txs maps hash -> full coin::Transaction with erase. |
| 13 | +/// (d) connection timestamps are monotonic across successive peers. |
| 14 | + |
| 15 | +#include <impl/dash/peer.hpp> |
| 16 | +#include <impl/dash/coin/transaction.hpp> |
| 17 | + |
| 18 | +#include <core/uint256.hpp> |
| 19 | + |
| 20 | +#include <gtest/gtest.h> |
| 21 | + |
| 22 | +#include <chrono> |
| 23 | + |
| 24 | +using dash::Peer; |
| 25 | + |
| 26 | +namespace |
| 27 | +{ |
| 28 | +uint256 H(uint8_t b) |
| 29 | +{ |
| 30 | + uint256 h; |
| 31 | + h.begin()[0] = b; |
| 32 | + return h; |
| 33 | +} |
| 34 | +} // namespace |
| 35 | + |
| 36 | +TEST(DashPeer, FreshDefaultsAreSane) |
| 37 | +{ |
| 38 | + Peer p; |
| 39 | + EXPECT_FALSE(p.m_other_version.has_value()); |
| 40 | + EXPECT_TRUE(p.m_other_subversion.empty()); |
| 41 | + EXPECT_EQ(p.m_other_services, 0u); |
| 42 | + EXPECT_EQ(p.m_nonce, 0u); |
| 43 | + EXPECT_TRUE(p.m_remote_txs.empty()); |
| 44 | + EXPECT_TRUE(p.m_remembered_txs.empty()); |
| 45 | + EXPECT_LE(p.m_connected_at, std::chrono::steady_clock::now()); |
| 46 | +} |
| 47 | + |
| 48 | +TEST(DashPeer, VersionHandshakeRoundTrip) |
| 49 | +{ |
| 50 | + Peer p; |
| 51 | + p.m_other_version = 70238u; // dashd-aligned proto seen on the wire |
| 52 | + p.m_other_subversion = "/c2pool-dash:0.1/"; |
| 53 | + p.m_other_services = 0x9u; |
| 54 | + p.m_nonce = 0xdeadbeefull; |
| 55 | + |
| 56 | + ASSERT_TRUE(p.m_other_version.has_value()); |
| 57 | + EXPECT_EQ(*p.m_other_version, 70238u); |
| 58 | + EXPECT_EQ(p.m_other_subversion, "/c2pool-dash:0.1/"); |
| 59 | + EXPECT_EQ(p.m_other_services, 0x9u); |
| 60 | + EXPECT_EQ(p.m_nonce, 0xdeadbeefull); |
| 61 | +} |
| 62 | + |
| 63 | +TEST(DashPeer, RemoteTxSetDedups) |
| 64 | +{ |
| 65 | + Peer p; |
| 66 | + p.m_remote_txs.insert(H(1)); |
| 67 | + p.m_remote_txs.insert(H(2)); |
| 68 | + p.m_remote_txs.insert(H(1)); // duplicate advertise |
| 69 | + EXPECT_EQ(p.m_remote_txs.size(), 2u); |
| 70 | + EXPECT_EQ(p.m_remote_txs.count(H(1)), 1u); |
| 71 | + EXPECT_EQ(p.m_remote_txs.count(H(9)), 0u); |
| 72 | +} |
| 73 | + |
| 74 | +TEST(DashPeer, RememberedTxRoundTripAndErase) |
| 75 | +{ |
| 76 | + Peer p; |
| 77 | + |
| 78 | + dash::coin::MutableTransaction mtx; |
| 79 | + mtx.version = 2; |
| 80 | + mtx.type = 5; // DIP4 CBTX |
| 81 | + mtx.locktime = 99; |
| 82 | + dash::coin::Transaction tx(mtx); |
| 83 | + |
| 84 | + const uint256 key = H(7); |
| 85 | + p.m_remembered_txs.emplace(key, tx); |
| 86 | + |
| 87 | + auto it = p.m_remembered_txs.find(key); |
| 88 | + ASSERT_NE(it, p.m_remembered_txs.end()); |
| 89 | + EXPECT_EQ(it->second.version, 2); |
| 90 | + EXPECT_EQ(it->second.type, 5); |
| 91 | + EXPECT_EQ(it->second.locktime, 99u); |
| 92 | + |
| 93 | + EXPECT_EQ(p.m_remembered_txs.erase(key), 1u); |
| 94 | + EXPECT_TRUE(p.m_remembered_txs.empty()); |
| 95 | +} |
| 96 | + |
| 97 | +TEST(DashPeer, ConnectedAtMonotonicAcrossPeers) |
| 98 | +{ |
| 99 | + Peer a; |
| 100 | + Peer b; |
| 101 | + EXPECT_LE(a.m_connected_at, b.m_connected_at); |
| 102 | +} |
0 commit comments