LCOV - code coverage report
Current view: top level - src/test - txreconciliation_tests.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 100.0 % 44 44
Test Date: 2026-03-16 05:20:51 Functions: 100.0 % 6 6
Branches: 50.0 % 390 195

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2021-present 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/common.h>
       8                 :             : #include <test/util/setup_common.h>
       9                 :             : 
      10                 :             : #include <boost/test/unit_test.hpp>
      11                 :             : 
      12                 :             : BOOST_FIXTURE_TEST_SUITE(txreconciliation_tests, BasicTestingSetup)
      13                 :             : 
      14   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(RegisterPeerTest)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      15                 :             : {
      16                 :           1 :     TxReconciliationTracker tracker(TXRECONCILIATION_VERSION);
      17                 :           1 :     const uint64_t salt = 0;
      18                 :             : 
      19                 :             :     // Prepare a peer for reconciliation.
      20         [ +  - ]:           1 :     tracker.PreRegisterPeer(0);
      21                 :             : 
      22                 :             :     // Invalid version.
      23   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(tracker.RegisterPeer(/*peer_id=*/0, /*is_peer_inbound=*/true,
                   +  - ]
      24                 :             :                                            /*peer_recon_version=*/0, salt),
      25                 :             :                       ReconciliationRegisterResult::PROTOCOL_VIOLATION);
      26                 :             : 
      27                 :             :     // Valid registration (inbound and outbound peers).
      28   [ +  -  +  -  :           2 :     BOOST_REQUIRE(!tracker.IsPeerRegistered(0));
             +  -  +  - ]
      29   [ +  -  +  -  :           1 :     BOOST_REQUIRE_EQUAL(tracker.RegisterPeer(0, true, 1, salt), ReconciliationRegisterResult::SUCCESS);
                   +  - ]
      30   [ +  -  +  -  :           2 :     BOOST_CHECK(tracker.IsPeerRegistered(0));
             +  -  +  - ]
      31   [ +  -  +  -  :           2 :     BOOST_REQUIRE(!tracker.IsPeerRegistered(1));
             +  -  +  - ]
      32         [ +  - ]:           1 :     tracker.PreRegisterPeer(1);
      33   [ +  -  +  -  :           2 :     BOOST_REQUIRE(tracker.RegisterPeer(1, false, 1, salt) == ReconciliationRegisterResult::SUCCESS);
             +  -  +  - ]
      34   [ +  -  +  -  :           2 :     BOOST_CHECK(tracker.IsPeerRegistered(1));
                   +  - ]
      35                 :             : 
      36                 :             :     // Reconciliation version is higher than ours, should be able to register.
      37   [ +  -  +  -  :           2 :     BOOST_REQUIRE(!tracker.IsPeerRegistered(2));
             +  -  +  - ]
      38         [ +  - ]:           1 :     tracker.PreRegisterPeer(2);
      39   [ +  -  +  -  :           1 :     BOOST_REQUIRE(tracker.RegisterPeer(2, true, 2, salt) == ReconciliationRegisterResult::SUCCESS);
                   +  - ]
      40   [ +  -  +  -  :           2 :     BOOST_CHECK(tracker.IsPeerRegistered(2));
             +  -  +  - ]
      41                 :             : 
      42                 :             :     // Try registering for the second time.
      43   [ +  -  +  -  :           2 :     BOOST_REQUIRE(tracker.RegisterPeer(1, false, 1, salt) == ReconciliationRegisterResult::ALREADY_REGISTERED);
             +  -  +  - ]
      44                 :             : 
      45                 :             :     // Do not register if there were no pre-registration for the peer.
      46   [ +  -  +  -  :           1 :     BOOST_REQUIRE_EQUAL(tracker.RegisterPeer(100, true, 1, salt), ReconciliationRegisterResult::NOT_FOUND);
                   +  - ]
      47   [ +  -  +  -  :           2 :     BOOST_CHECK(!tracker.IsPeerRegistered(100));
                   +  - ]
      48                 :           1 : }
      49                 :             : 
      50   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(ForgetPeerTest)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      51                 :             : {
      52                 :           1 :     TxReconciliationTracker tracker(TXRECONCILIATION_VERSION);
      53                 :           1 :     NodeId peer_id0 = 0;
      54                 :             : 
      55                 :             :     // Removing peer after pre-registering works and does not let to register the peer.
      56         [ +  - ]:           1 :     tracker.PreRegisterPeer(peer_id0);
      57         [ +  - ]:           1 :     tracker.ForgetPeer(peer_id0);
      58   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(tracker.RegisterPeer(peer_id0, true, 1, 1), ReconciliationRegisterResult::NOT_FOUND);
                   +  - ]
      59                 :             : 
      60                 :             :     // Removing peer after it is registered works.
      61         [ +  - ]:           1 :     tracker.PreRegisterPeer(peer_id0);
      62   [ +  -  +  -  :           2 :     BOOST_REQUIRE(!tracker.IsPeerRegistered(peer_id0));
             +  -  +  - ]
      63   [ +  -  +  -  :           1 :     BOOST_REQUIRE_EQUAL(tracker.RegisterPeer(peer_id0, true, 1, 1), ReconciliationRegisterResult::SUCCESS);
                   +  - ]
      64   [ +  -  +  -  :           2 :     BOOST_CHECK(tracker.IsPeerRegistered(peer_id0));
             +  -  +  - ]
      65         [ +  - ]:           1 :     tracker.ForgetPeer(peer_id0);
      66   [ +  -  +  -  :           2 :     BOOST_CHECK(!tracker.IsPeerRegistered(peer_id0));
                   +  - ]
      67                 :           1 : }
      68                 :             : 
      69   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(IsPeerRegisteredTest)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      70                 :             : {
      71                 :           1 :     TxReconciliationTracker tracker(TXRECONCILIATION_VERSION);
      72                 :           1 :     NodeId peer_id0 = 0;
      73                 :             : 
      74   [ +  -  +  -  :           2 :     BOOST_REQUIRE(!tracker.IsPeerRegistered(peer_id0));
             +  -  +  - ]
      75         [ +  - ]:           1 :     tracker.PreRegisterPeer(peer_id0);
      76   [ +  -  +  -  :           2 :     BOOST_REQUIRE(!tracker.IsPeerRegistered(peer_id0));
             +  -  +  - ]
      77                 :             : 
      78   [ +  -  +  -  :           1 :     BOOST_REQUIRE_EQUAL(tracker.RegisterPeer(peer_id0, true, 1, 1), ReconciliationRegisterResult::SUCCESS);
                   +  - ]
      79   [ +  -  +  -  :           2 :     BOOST_CHECK(tracker.IsPeerRegistered(peer_id0));
             +  -  +  - ]
      80                 :             : 
      81         [ +  - ]:           1 :     tracker.ForgetPeer(peer_id0);
      82   [ +  -  +  -  :           2 :     BOOST_CHECK(!tracker.IsPeerRegistered(peer_id0));
                   +  - ]
      83                 :           1 : }
      84                 :             : 
      85                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1