Repository: bitcoin/bitcoin
File: src/test/fuzz/txrequest.cpp (C++)

Code before:
        }
    }

    void Check()
    {
        // Compare CountTracked and CountLoad with naive structure.
        size_t total = 0;
        for (int peer = 0; peer < MAX_PEERS; ++peer) {
            size_t tracked = 0;
            size_t inflight = 0;
            size_t candidates = 0;
            for (int txhash = 0; txhash < MAX_TXHASHES; ++txhash) {
                tracked += m_announcements[txhash][peer].m_state != State::NOTHING;
                inflight += m_announcements[txhash][peer].m_state == State::REQUESTED;
                candidates += m_announcements[txhash][peer].m_state == State::CANDIDATE;

                std::vector<NodeId> candidate_peers;
                m_tracker.GetCandidatePeers(TXHASHES[txhash], candidate_peers);
                for (const auto& peer : candidate_peers) {
                    assert(m_announcements[txhash][peer].m_state == State::CANDIDATE || m_announcements[txhash][peer].m_state == State::REQUESTED);
                }
            }
            assert(m_tracker.Count(peer) == tracked);
            assert(m_tracker.CountInFlight(peer) == inflight);
            assert(m_tracker.CountCandidates(peer) == candidates);
            total += tracked;
        }
        // Compare Size.
        assert(m_tracker.Size() == total);

        // Invoke internal consistency check of TxRequestTracker object.
        m_tracker.SanityCheck();
    }
};
} // namespace

FUZZ_TARGET(txrequest)
{
    // Tester object (which encapsulates a TxRequestTracker).
    Tester tester;

    // Decode the input as a sequence of instructions with parameters
    auto it = buffer.begin();
    while (it != buffer.end()) {
        int cmd = *(it++) % 11;
        int peer, txidnum, delaynum;
        switch (cmd) {
        case 0: // Make time jump to the next event (m_time of CANDIDATE or REQUESTED)
            tester.AdvanceToEvent();
            break;
        case 1: // Change time

Review comment (suggestion):
In commit "[fuzz] GetCandidatePeers"

This tests that all values returned by `GetCandidatePeers` are in the set of expected results, but not that nothing is missing from the `GetCandidatePeers` result (so if `GetCandidatePeers()` never modified `candidate_peers`, no issue would be detected).

Code after:
        }
    }

    void Check()
    {
        // Compare CountTracked and CountLoad with naive structure.
        size_t total = 0;
        for (int peer = 0; peer < MAX_PEERS; ++peer) {
            size_t tracked = 0;
            size_t inflight = 0;
            size_t candidates = 0;
            for (int txhash = 0; txhash < MAX_TXHASHES; ++txhash) {
                tracked += m_announcements[txhash][peer].m_state != State::NOTHING;
                inflight += m_announcements[txhash][peer].m_state == State::REQUESTED;
                candidates += m_announcements[txhash][peer].m_state == State::CANDIDATE;

                std::bitset<MAX_PEERS> expected_announcers;
                for (int peer = 0; peer < MAX_PEERS; ++peer) {
                    if (m_announcements[txhash][peer].m_state == State::CANDIDATE || m_announcements[txhash][peer].m_state == State::REQUESTED) {
                        expected_announcers[peer] = true;
                    }
                }
                std::vector<NodeId> candidate_peers;
                m_tracker.GetCandidatePeers(TXHASHES[txhash], candidate_peers);
                assert(expected_announcers.count() == candidate_peers.size());
                for (const auto& peer : candidate_peers) {
                    assert(expected_announcers[peer]);
                }
            }
            assert(m_tracker.Count(peer) == tracked);
            assert(m_tracker.CountInFlight(peer) == inflight);
            assert(m_tracker.CountCandidates(peer) == candidates);
            total += tracked;
        }
        // Compare Size.
        assert(m_tracker.Size() == total);

        // Invoke internal consistency check of TxRequestTracker object.
        m_tracker.SanityCheck();
    }
};
} // namespace

FUZZ_TARGET(txrequest)
{
    // Tester object (which encapsulates a TxRequestTracker).
    Tester tester;

    // Decode the input as a sequence of instructions with parameters
    auto it = buffer.begin();
    while (it != buffer.end()) {