Coverage Report

Created: 2026-04-29 19:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/private_broadcast.cpp
Line
Count
Source
1
// Copyright (c) 2023-present The Bitcoin Core developers
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or https://opensource.org/license/mit/.
4
5
#include <private_broadcast.h>
6
#include <util/check.h>
7
8
#include <algorithm>
9
10
11
bool PrivateBroadcast::Add(const CTransactionRef& tx)
12
    EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
13
11
{
14
11
    LOCK(m_mutex);
15
11
    const bool inserted{m_transactions.try_emplace(tx).second};
16
11
    return inserted;
17
11
}
18
19
std::optional<size_t> PrivateBroadcast::Remove(const CTransactionRef& tx)
20
    EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
21
16.2k
{
22
16.2k
    LOCK(m_mutex);
23
16.2k
    const auto handle{m_transactions.extract(tx)};
24
16.2k
    if (handle) {
25
5
        const auto p{DerivePriority(handle.mapped().send_statuses)};
26
5
        return p.num_confirmed;
27
5
    }
28
16.2k
    return std::nullopt;
29
16.2k
}
30
31
std::optional<CTransactionRef> PrivateBroadcast::PickTxForSend(const NodeId& will_send_to_nodeid, const CService& will_send_to_address)
32
    EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
33
14
{
34
14
    LOCK(m_mutex);
35
36
14
    const auto it{std::ranges::max_element(
37
14
            m_transactions,
38
14
            [](const auto& a, const auto& b) { return a < b; },
39
14
            [](const auto& el) { return DerivePriority(el.second.send_statuses); })};
40
41
14
    if (it != m_transactions.end()) {
42
12
        auto& [tx, state]{*it};
43
12
        state.send_statuses.emplace_back(will_send_to_nodeid, will_send_to_address, NodeClock::now());
44
12
        return tx;
45
12
    }
46
47
2
    return std::nullopt;
48
14
}
49
50
std::optional<CTransactionRef> PrivateBroadcast::GetTxForNode(const NodeId& nodeid)
51
    EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
52
13
{
53
13
    LOCK(m_mutex);
54
13
    const auto tx_and_status{GetSendStatusByNode(nodeid)};
55
13
    if (tx_and_status.has_value()) {
56
12
        return tx_and_status.value().tx;
57
12
    }
58
1
    return std::nullopt;
59
13
}
60
61
void PrivateBroadcast::NodeConfirmedReception(const NodeId& nodeid)
62
    EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
63
12
{
64
12
    LOCK(m_mutex);
65
12
    const auto tx_and_status{GetSendStatusByNode(nodeid)};
66
12
    if (tx_and_status.has_value()) {
67
11
        tx_and_status.value().send_status.confirmed = NodeClock::now();
68
11
    }
69
12
}
70
71
bool PrivateBroadcast::DidNodeConfirmReception(const NodeId& nodeid)
72
    EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
73
16
{
74
16
    LOCK(m_mutex);
75
16
    const auto tx_and_status{GetSendStatusByNode(nodeid)};
76
16
    if (tx_and_status.has_value()) {
77
14
        return tx_and_status.value().send_status.confirmed.has_value();
78
14
    }
79
2
    return false;
80
16
}
81
82
bool PrivateBroadcast::HavePendingTransactions()
83
    EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
84
2
{
85
2
    LOCK(m_mutex);
86
2
    return !m_transactions.empty();
87
2
}
88
89
std::vector<CTransactionRef> PrivateBroadcast::GetStale() const
90
    EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
91
12
{
92
12
    LOCK(m_mutex);
93
12
    const auto now{NodeClock::now()};
94
12
    std::vector<CTransactionRef> stale;
95
13
    for (const auto& [tx, state] : m_transactions) {
96
13
        const Priority p{DerivePriority(state.send_statuses)};
97
13
        if (p.num_confirmed == 0) {
98
9
            if (state.time_added < now - INITIAL_STALE_DURATION) stale.push_back(tx);
99
9
        } else {
100
4
            if (p.last_confirmed < now - STALE_DURATION) stale.push_back(tx);
101
4
        }
102
13
    }
103
12
    return stale;
104
12
}
105
106
std::vector<PrivateBroadcast::TxBroadcastInfo> PrivateBroadcast::GetBroadcastInfo() const
107
    EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
108
14
{
109
14
    LOCK(m_mutex);
110
14
    std::vector<TxBroadcastInfo> entries;
111
14
    entries.reserve(m_transactions.size());
112
113
25
    for (const auto& [tx, state] : m_transactions) {
114
25
        std::vector<PeerSendInfo> peers;
115
25
        peers.reserve(state.send_statuses.size());
116
36
        for (const auto& status : state.send_statuses) {
117
36
            peers.emplace_back(PeerSendInfo{.address = status.address, .sent = status.picked, .received = status.confirmed});
118
36
        }
119
25
        entries.emplace_back(TxBroadcastInfo{.tx = tx, .time_added = state.time_added, .peers = std::move(peers)});
120
25
    }
121
122
14
    return entries;
123
14
}
124
125
PrivateBroadcast::Priority PrivateBroadcast::DerivePriority(const std::vector<SendStatus>& sent_to)
126
28
{
127
28
    Priority p;
128
28
    p.num_picked = sent_to.size();
129
35
    for (const auto& send_status : sent_to) {
130
35
        p.last_picked = std::max(p.last_picked, send_status.picked);
131
35
        if (send_status.confirmed.has_value()) {
132
26
            ++p.num_confirmed;
133
26
            p.last_confirmed = std::max(p.last_confirmed, send_status.confirmed.value());
134
26
        }
135
35
    }
136
28
    return p;
137
28
}
138
139
std::optional<PrivateBroadcast::TxAndSendStatusForNode> PrivateBroadcast::GetSendStatusByNode(const NodeId& nodeid)
140
    EXCLUSIVE_LOCKS_REQUIRED(m_mutex)
141
41
{
142
41
    AssertLockHeld(m_mutex);
143
46
    for (auto& [tx, state] : m_transactions) {
144
82
        for (auto& send_status : state.send_statuses) {
145
82
            if (send_status.nodeid == nodeid) {
146
37
                return TxAndSendStatusForNode{.tx = tx, .send_status = send_status};
147
37
            }
148
82
        }
149
46
    }
150
4
    return std::nullopt;
151
41
}