Bitcoin Core  25.99.0
P2P Digital Currency
txreconciliation_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2021-2022 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 
6 
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 {
16  const uint64_t salt = 0;
17 
18  // Prepare a peer for reconciliation.
19  tracker.PreRegisterPeer(0);
20 
21  // Invalid version.
22  BOOST_CHECK_EQUAL(tracker.RegisterPeer(/*peer_id=*/0, /*is_peer_inbound=*/true,
23  /*peer_recon_version=*/0, salt),
25 
26  // Valid registration (inbound and outbound peers).
27  BOOST_REQUIRE(!tracker.IsPeerRegistered(0));
28  BOOST_REQUIRE_EQUAL(tracker.RegisterPeer(0, true, 1, salt), ReconciliationRegisterResult::SUCCESS);
29  BOOST_CHECK(tracker.IsPeerRegistered(0));
30  BOOST_REQUIRE(!tracker.IsPeerRegistered(1));
31  tracker.PreRegisterPeer(1);
32  BOOST_REQUIRE(tracker.RegisterPeer(1, false, 1, salt) == ReconciliationRegisterResult::SUCCESS);
33  BOOST_CHECK(tracker.IsPeerRegistered(1));
34 
35  // Reconciliation version is higher than ours, should be able to register.
36  BOOST_REQUIRE(!tracker.IsPeerRegistered(2));
37  tracker.PreRegisterPeer(2);
38  BOOST_REQUIRE(tracker.RegisterPeer(2, true, 2, salt) == ReconciliationRegisterResult::SUCCESS);
39  BOOST_CHECK(tracker.IsPeerRegistered(2));
40 
41  // Try registering for the second time.
42  BOOST_REQUIRE(tracker.RegisterPeer(1, false, 1, salt) == ReconciliationRegisterResult::ALREADY_REGISTERED);
43 
44  // Do not register if there were no pre-registration for the peer.
45  BOOST_REQUIRE_EQUAL(tracker.RegisterPeer(100, true, 1, salt), ReconciliationRegisterResult::NOT_FOUND);
46  BOOST_CHECK(!tracker.IsPeerRegistered(100));
47 }
48 
49 BOOST_AUTO_TEST_CASE(ForgetPeerTest)
50 {
52  NodeId peer_id0 = 0;
53 
54  // Removing peer after pre-registring works and does not let to register the peer.
55  tracker.PreRegisterPeer(peer_id0);
56  tracker.ForgetPeer(peer_id0);
58 
59  // Removing peer after it is registered works.
60  tracker.PreRegisterPeer(peer_id0);
61  BOOST_REQUIRE(!tracker.IsPeerRegistered(peer_id0));
62  BOOST_REQUIRE_EQUAL(tracker.RegisterPeer(peer_id0, true, 1, 1), ReconciliationRegisterResult::SUCCESS);
63  BOOST_CHECK(tracker.IsPeerRegistered(peer_id0));
64  tracker.ForgetPeer(peer_id0);
65  BOOST_CHECK(!tracker.IsPeerRegistered(peer_id0));
66 }
67 
68 BOOST_AUTO_TEST_CASE(IsPeerRegisteredTest)
69 {
71  NodeId peer_id0 = 0;
72 
73  BOOST_REQUIRE(!tracker.IsPeerRegistered(peer_id0));
74  tracker.PreRegisterPeer(peer_id0);
75  BOOST_REQUIRE(!tracker.IsPeerRegistered(peer_id0));
76 
77  BOOST_REQUIRE_EQUAL(tracker.RegisterPeer(peer_id0, true, 1, 1), ReconciliationRegisterResult::SUCCESS);
78  BOOST_CHECK(tracker.IsPeerRegistered(peer_id0));
79 
80  tracker.ForgetPeer(peer_id0);
81  BOOST_CHECK(!tracker.IsPeerRegistered(peer_id0));
82 }
83 
Transaction reconciliation is a way for nodes to efficiently announce transactions.
bool IsPeerRegistered(NodeId peer_id) const
Check if a peer is registered to reconcile transactions with us.
ReconciliationRegisterResult RegisterPeer(NodeId peer_id, bool is_peer_inbound, uint32_t peer_recon_version, uint64_t remote_salt)
Step 0.
uint64_t PreRegisterPeer(NodeId peer_id)
Step 0.
void ForgetPeer(NodeId peer_id)
Attempts to forget txreconciliation-related state of the peer (if we previously stored any).
BOOST_AUTO_TEST_SUITE_END()
int64_t NodeId
Definition: net.h:97
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
Basic testing setup.
Definition: setup_common.h:49
static constexpr uint32_t TXRECONCILIATION_VERSION
Supported transaction reconciliation protocol version.
BOOST_AUTO_TEST_CASE(RegisterPeerTest)