• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

libbitcoin / libbitcoin-node / 35809535048

23 Sep 2026 02:14AM UTC coverage: 42.184% (+0.1%) from 42.039%
35809535048

push

github

web-flow
Merge pull request #1146 from evoskuil/master

Reduce outbound connections to node.current_outbound while current.

17 of 30 new or added lines in 3 files covered. (56.67%)

2847 of 6749 relevant lines covered (42.18%)

1092.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

26.76
/src/protocols/protocol_observer.cpp
1
/**
2
 * Copyright (c) 2011-2026 libbitcoin developers
3
 *
4
 * This file is part of libbitcoin.
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU Affero General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
#include <bitcoin/node/protocols/protocol_observer.hpp>
20

21
#include <bitcoin/node/define.hpp>
22

23
namespace libbitcoin {
24
namespace node {
25

26
#define CLASS protocol_observer
27

28
using namespace network::messages::peer;
29
using namespace std::placeholders;
30

31
// Shared pointers required for lifetime in handler parameters.
32
BC_PUSH_WARNING(SMART_PTR_NOT_NEEDED)
33
BC_PUSH_WARNING(NO_VALUE_OR_CONST_REF_SHARED_PTR)
34

35
// start/stop
36
// ----------------------------------------------------------------------------
37

38
void protocol_observer::start() NOEXCEPT
11 ✔
39
{
40
    BC_ASSERT(stranded());
11 ✔
41

42
    if (started())
11 ✔
43
        return;
44

45
    // Events subscription is asynchronous, events may be missed.
46
    subscribe_chase(BIND(handle_chase, _1, _2));
11 ✔
47

48
    if (relay_disallowed_)
11 ✔
49
    {
50
        SUBSCRIBE_CHANNEL(inventory, handle_receive_inventory, _1, _2);
9 ✔
51
    }
52

53
    SUBSCRIBE_BROADCAST(network::diagnostics, handle_broadcast_diagnostics, _1, _2, _3);
11 ✔
54
    SUBSCRIBE_BROADCAST(network::terminator, handle_broadcast_terminator, _1, _2, _3);
11 ✔
55
    protocol_peer::start();
11 ✔
56
}
57

58
void protocol_observer::stopping(const code& ec) NOEXCEPT
11 ✔
59
{
60
    // Unsubscriber race is ok.
61
    BC_ASSERT(stranded());
11 ✔
62
    unsubscribe_chase();
11 ✔
63
    protocol_peer::stopping(ec);
11 ✔
64
}
11 ✔
65

66
// handle events (suspend)
67
// ----------------------------------------------------------------------------
68

69
bool protocol_observer::handle_chase(const code& ec, event_value value) NOEXCEPT
11 ✔
70
{
71
    // Do not pass ec to stopped as it is not a call status.
72
    if (stopped())
11 ✔
73
        return false;
74

75
    switch (to_chase(value))
×
76
    {
77
        case chase::suspend:
×
78
        {
79
            // The code distinguishes a stop from a drop (manual removal).
80
            stop(ec);
×
81
            break;
×
82
        }
83
        case chase::stop:
84
        {
85
            return false;
86
        }
87
        default:
88
        {
89
            break;
90
        }
91
    }
92

93
    return true;
94
}
95

96
// Inbound (inv).
97
// ----------------------------------------------------------------------------
98
// Protocol hygiene for messages that may not be captured.
99

100
bool protocol_observer::handle_receive_inventory(const code& ec,
9 ✔
101
    const inventory::cptr& message) NOEXCEPT
102
{
103
    BC_ASSERT(stranded());
9 ✔
104

105
    if (stopped(ec))
9 ✔
106
        return false;
107

108
    //  Common with Satoshi 25.0 and 25.1.
109
    if (relay_disallowed_ && message->any_transaction())
×
110
    {
111
        LOGR("Unrequested tx relay from [" << opposite() << "] "
×
112
            << peer_version()->user_agent);
113

114
        stop(network::error::protocol_violation);
×
115
        return false;
×
116
    }
117

118
    return true;
119
}
120

121
// Diagnostics (capture).
122
// ----------------------------------------------------------------------------
123

124
network::diagnostics::target protocol_observer::group() const NOEXCEPT
×
125
{
126
    return group_;
×
127
}
128

129
bool protocol_observer::handle_broadcast_diagnostics(const code& ec,
×
130
    const network::diagnostics::cptr& message, uint64_t) NOEXCEPT
131
{
132
    BC_ASSERT(stranded());
×
133

134
    if (stopped(ec))
×
135
        return false;
136

137
    if (!message->targets(identifier()) && !message->targets(group()))
×
138
        return true;
139

140
    using namespace system;
141
    const auto peer = peer_version();
×
142
    uint64_t services{ service::node_none };
143
    network::config::address local{};
×
144
    int64_t time_offset{};
145
    std::string agent{};
146
    bool relay{};
147

148
    if (peer)
×
149
    {
150
        time_offset = subtract<int64_t>(peer->timestamp, created());
×
151
        local = { peer->address_receiver };
×
152
        services = peer->services;
×
153
        agent = peer->user_agent;
×
154
        relay = peer->relay;
×
155
    }
156

157
    message->add(
×
158
    {
159
        .group = group(),
×
160
        .identifier = identifier(),
×
161
        .endpoint = opposite(),
×
162
        .address = outbound(),
×
163
        .local = local,
164
        .binding = binding(),
×
165

166
        .encrypted = encrypted(),
×
167
        .peer_relay = relay,
168
        .peer_start_height = start_height(),
×
169
        .peer_version = negotiated_version(),
×
170
        .peer_services = services,
171
        .peer_minimum_fee = minimum_fee(),
×
172
        .peer_user_agent = agent,
173

174
        .created = created(),
×
175
        .last_read = last_read(),
×
176
        .last_write = last_write(),
×
177
        .time_offset = time_offset,
178
        .bytes_sent = sent(),
×
179
        .bytes_received = received(),
×
180
        .bytes_sent_by_message = sent_by_message(),
×
181
        .bytes_received_by_message = received_by_message(),
×
182

183
        .ping_time = ping_time(),
×
184
        .minimum_ping_time = minimum_ping_time(),
×
185
        .pending_ping_time = pending_ping_time()
×
186
    });
187

188
    return true;
189
}
×
190

191
// The first member stops, so the round completes without the other channels.
192
bool protocol_observer::handle_broadcast_terminator(const code& ec,
×
193
    const network::terminator::cptr& message, uint64_t) NOEXCEPT
194
{
195
    BC_ASSERT(stranded());
×
196

197
    if (stopped(ec))
×
198
        return false;
199

200
    using target = network::diagnostics::target;
NEW
201
    const auto slotted = (group() == target::outbound) &&
×
NEW
202
        is_terminal(message->slots());
×
203

NEW
204
    if (!slotted && !message->targets(identifier(), outbound(), opposite()))
×
205
        return true;
206

207
    message->stopped();
×
208
    stop(message->reason());
×
209
    return false;
×
210
}
211

212
BC_POP_WARNING()
213
BC_POP_WARNING()
214

215
} // namespace node
216
} // namespace libbitcoin
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc