|
| 1 | +// Copyright (c) 2021 The Bitcoin Core developers |
| 2 | +// Distributed under the MIT software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +#include <node/txreconciliation.h> |
| 6 | + |
| 7 | +#include <test/util/setup_common.h> |
| 8 | + |
| 9 | +#include <boost/test/unit_test.hpp> |
| 10 | + |
| 11 | +BOOST_FIXTURE_TEST_SUITE(txreconciliation_tests, BasicTestingSetup) |
| 12 | + |
| 13 | +BOOST_AUTO_TEST_CASE(RegisterPeerTest) |
| 14 | +{ |
| 15 | + TxReconciliationTracker tracker(1); |
| 16 | + const uint64_t salt = 0; |
| 17 | + |
| 18 | + // Prepare a peer for reconciliation. |
| 19 | + tracker.PreRegisterPeer(0); |
| 20 | + |
| 21 | + // Both roles are false, don't register. |
| 22 | + BOOST_CHECK(tracker.RegisterPeer(/*peer_id=*/0, /*is_peer_inbound=*/true, |
| 23 | + /*is_peer_recon_initiator=*/false, |
| 24 | + /*is_peer_recon_responder=*/false, |
| 25 | + /*peer_recon_version=*/1, salt) == |
| 26 | + ReconciliationRegisterResult::PROTOCOL_VIOLATION); |
| 27 | + |
| 28 | + // Invalid roles for the given connection direction. |
| 29 | + BOOST_CHECK(tracker.RegisterPeer(0, true, false, true, 1, salt) == ReconciliationRegisterResult::PROTOCOL_VIOLATION); |
| 30 | + BOOST_CHECK(tracker.RegisterPeer(0, false, true, false, 1, salt) == ReconciliationRegisterResult::PROTOCOL_VIOLATION); |
| 31 | + |
| 32 | + // Invalid version. |
| 33 | + BOOST_CHECK(tracker.RegisterPeer(0, true, true, false, 0, salt) == ReconciliationRegisterResult::PROTOCOL_VIOLATION); |
| 34 | + |
| 35 | + // Valid registration. |
| 36 | + BOOST_REQUIRE(!tracker.IsPeerRegistered(0)); |
| 37 | + BOOST_REQUIRE(tracker.RegisterPeer(0, true, true, false, 1, salt) == ReconciliationRegisterResult::SUCCESS); |
| 38 | + BOOST_CHECK(tracker.IsPeerRegistered(0)); |
| 39 | + |
| 40 | + // Reconciliation version is higher than ours, should be able to register. |
| 41 | + BOOST_REQUIRE(!tracker.IsPeerRegistered(1)); |
| 42 | + tracker.PreRegisterPeer(1); |
| 43 | + BOOST_REQUIRE(tracker.RegisterPeer(1, true, true, false, 2, salt) == ReconciliationRegisterResult::SUCCESS); |
| 44 | + BOOST_CHECK(tracker.IsPeerRegistered(1)); |
| 45 | + |
| 46 | + // Do not register if there were no pre-registration for the peer. |
| 47 | + BOOST_REQUIRE(tracker.RegisterPeer(100, true, true, false, 1, salt) == ReconciliationRegisterResult::NOT_FOUND); |
| 48 | + BOOST_CHECK(!tracker.IsPeerRegistered(100)); |
| 49 | +} |
| 50 | + |
| 51 | +BOOST_AUTO_TEST_CASE(ForgetPeerTest) |
| 52 | +{ |
| 53 | + TxReconciliationTracker tracker(1); |
| 54 | + NodeId peer_id0 = 0; |
| 55 | + |
| 56 | + // Removing peer after pre-registring works and does not let to register the peer. |
| 57 | + tracker.PreRegisterPeer(peer_id0); |
| 58 | + tracker.ForgetPeer(peer_id0); |
| 59 | + BOOST_CHECK(tracker.RegisterPeer(peer_id0, true, true, false, 1, 1) == ReconciliationRegisterResult::NOT_FOUND); |
| 60 | + |
| 61 | + // Removing peer after it is registered works. |
| 62 | + tracker.PreRegisterPeer(peer_id0); |
| 63 | + BOOST_REQUIRE(!tracker.IsPeerRegistered(peer_id0)); |
| 64 | + BOOST_REQUIRE(tracker.RegisterPeer(peer_id0, true, true, false, 1, 1) == ReconciliationRegisterResult::SUCCESS); |
| 65 | + BOOST_CHECK(tracker.IsPeerRegistered(peer_id0)); |
| 66 | + tracker.ForgetPeer(peer_id0); |
| 67 | + BOOST_CHECK(!tracker.IsPeerRegistered(peer_id0)); |
| 68 | +} |
| 69 | + |
| 70 | +BOOST_AUTO_TEST_CASE(IsPeerRegisteredTest) |
| 71 | +{ |
| 72 | + TxReconciliationTracker tracker(1); |
| 73 | + NodeId peer_id0 = 0; |
| 74 | + |
| 75 | + BOOST_REQUIRE(!tracker.IsPeerRegistered(peer_id0)); |
| 76 | + tracker.PreRegisterPeer(peer_id0); |
| 77 | + BOOST_REQUIRE(!tracker.IsPeerRegistered(peer_id0)); |
| 78 | + |
| 79 | + BOOST_REQUIRE(tracker.RegisterPeer(peer_id0, true, true, false, 1, 1) == ReconciliationRegisterResult::SUCCESS); |
| 80 | + BOOST_CHECK(tracker.IsPeerRegistered(peer_id0)); |
| 81 | + |
| 82 | + tracker.ForgetPeer(peer_id0); |
| 83 | + BOOST_CHECK(!tracker.IsPeerRegistered(peer_id0)); |
| 84 | +} |
| 85 | + |
| 86 | +BOOST_AUTO_TEST_SUITE_END() |
0 commit comments