Repository navigation
Expand file tree
/
Copy pathsession_seed.cpp
More file actions
305 lines (250 loc) · 9.39 KB
/
Copy pathsession_seed.cpp
File metadata and controls
305 lines (250 loc) · 9.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/**
* Copyright (c) 2011-2026 libbitcoin developers
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <bitcoin/network/sessions/session_seed.hpp>
#include <bitcoin/network/config/config.hpp>
#include <bitcoin/network/log/log.hpp>
#include <bitcoin/network/net.hpp>
#include <bitcoin/network/protocols/protocols.hpp>
#include <bitcoin/network/sessions/session_peer.hpp>
namespace libbitcoin {
namespace network {
#define CLASS session_seed
using namespace system;
using namespace std::placeholders;
// Bind throws (ok).
// Shared pointers required in handler parameters so closures control lifetime.
BC_PUSH_WARNING(NO_VALUE_OR_CONST_REF_SHARED_PTR)
BC_PUSH_WARNING(SMART_PTR_NOT_NEEDED)
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
session_seed::session_seed(net& network, uint64_t identifier) NOEXCEPT
: session_peer(network, identifier, network.network_settings().outbound),
tracker<session_seed>(network)
{
}
// Properties.
// ----------------------------------------------------------------------------
const session_seed::options_t& session_seed::options() const NOEXCEPT
{
return static_cast<const options_t&>(session_peer::options());
}
bool session_seed::seeding() const NOEXCEPT
{
BC_ASSERT(stranded());
return seeding_;
}
steady_clock::time_point session_seed::completed() const NOEXCEPT
{
BC_ASSERT(stranded());
return completed_;
}
// Start/stop sequence.
// ----------------------------------------------------------------------------
void session_seed::start(result_handler&& handler) NOEXCEPT
{
BC_ASSERT(stranded());
// Seeding is allowed even with !enable_address configured.
if (is_zero(network_settings().outbound.connections) ||
is_zero(network_settings().outbound.connect_batch_size))
{
LOGN("Bypassed seeding because outbound connections disabled.");
handler(error::success);
unsubscribe_close();
return;
}
if (address_count() >= network_settings().outbound.minimum_address_count())
{
LOGN("Bypassed seeding because of sufficient (" << address_count()
<< " of " << network_settings().outbound.minimum_address_count()
<< ") address quantity.");
handler(error::success);
unsubscribe_close();
return;
}
if (is_zero(network_settings().outbound.host_pool_capacity))
{
LOGN("Cannot seed because no address pool capacity configured.");
handler(error::seeding_unsuccessful);
unsubscribe_close();
return;
}
if (network_settings().outbound.seeds.empty())
{
LOGN("Cannot seed because no seeds configured");
handler(error::seeding_unsuccessful);
unsubscribe_close();
return;
}
session_peer::start(BIND(handle_started, _1, std::move(handler)));
}
void session_seed::handle_started(const code& ec,
const result_handler& handler) NOEXCEPT
{
BC_ASSERT(stranded());
if (ec)
{
handler(ec);
unsubscribe_close();
return;
}
const auto seeds = network_settings().outbound.seeds.size();
const auto required = network_settings().outbound.minimum_address_count();
LOGN("Seeding because of insufficient ("
<< address_count() << " of " << required << ") address quantity.");
// Bogus warning, this pointer is copied into std::bind().
BC_PUSH_WARNING(NO_UNUSED_LOCAL_SMART_PTR)
const auto racer = std::make_shared<race>(seeds, required);
BC_POP_WARNING()
seeding_ = true;
// Invoke sufficient on count, invoke complete with all seeds stopped.
racer->start(move_copy(handler), BIND(stop_seed, _1));
size_t slot{};
for (const auto& seed: network_settings().outbound.seeds)
{
const auto connector = create_seed_connector(slot++);
subscribe_stop([=](const code&) NOEXCEPT
{
connector->stop();
return false;
});
start_seed(error::success, seed, connector,
BIND(handle_connect, _1, _2, seed, racer));
}
}
// Seed sequence.
// ----------------------------------------------------------------------------
// Attempt to connect one seed.
void session_seed::start_seed(const code&, const config::endpoint& seed,
const connector::ptr& connector, const socket_handler& handler) NOEXCEPT
{
BC_ASSERT(stranded());
LOGN("Connecting to seed [" << seed << "]");
// Guard restartable connector (shutdown delay).
if (stopped())
{
handler(error::service_stopped, nullptr);
return;
}
connector->connect(seed, move_copy(handler));
}
void session_seed::handle_connect(const code& ec, const socket::ptr& socket,
const config::endpoint& LOG_ONLY(seed), const race::ptr& racer) NOEXCEPT
{
BC_ASSERT(stranded());
if (ec)
{
BC_ASSERT_MSG(!socket || socket->stopped(), "unexpected socket");
LOGN("Failed to connect seed address [" << seed << "] " << ec.message());
racer->finish(address_count());
return;
}
const auto channel = create_channel(socket);
std::dynamic_pointer_cast<channel_peer>(channel)->set_quiet();
start_channel(channel,
BIND(handle_channel_start, _1, channel),
BIND(handle_channel_stop, _1, channel, racer));
}
void session_seed::attach_handshake(const channel::ptr& channel,
result_handler&& handler) NOEXCEPT
{
BC_ASSERT_MSG(channel->stranded(), "channel strand");
BC_ASSERT_MSG(!channel->held(), "gate held before attach");
// Tx relay is always disabled for seeding.
using namespace messages::peer;
constexpr auto relay = false;
// Protocol must pause the channel after receiving version and verack.
const auto self = shared_from_this();
const auto reject = network_settings().enable_reject;
const auto address_v2 = network_settings().enable_address_v2;
// Address v2 can be disabled, independent of version.
if (is_configured(level::bip155) && address_v2)
channel->attach<protocol_version_70016>(self, relay,
reject)->shake(std::move(handler));
// Protocol versions are cumulative, but reject is deprecated.
else if (is_configured(level::bip61) && reject)
channel->attach<protocol_version_70002>(self, relay,
reject)->shake(std::move(handler));
else if (is_configured(level::bip37))
channel->attach<protocol_version_70001>(self,
relay)->shake(std::move(handler));
else if (is_configured(level::version_message))
channel->attach<protocol_version_106>(
self)->shake(std::move(handler));
}
void session_seed::handle_channel_start(const code& ec,
const channel::ptr& LOG_ONLY(channel)) NOEXCEPT
{
BC_ASSERT(stranded());
if (ec)
{
LOGN("Seed start [" << channel->endpoint() << "] " << ec.message());
}
}
void session_seed::attach_protocols(const channel::ptr& channel) NOEXCEPT
{
BC_ASSERT_MSG(channel->stranded(), "channel strand");
using namespace messages::peer;
const auto self = shared_from_this();
const auto alert = network_settings().enable_alert;
const auto reject = network_settings().enable_reject;
const auto address_v2 = network_settings().enable_address_v2;
const auto peer = std::dynamic_pointer_cast<channel_peer>(channel);
// Alert is deprecated, independent of version.
if (peer->is_negotiated(level::alert_message) && alert)
channel->attach<protocol_alert_311>(self)->start();
// Reject is deprecated, independent of version.
if (peer->is_negotiated(level::bip61) && reject)
channel->attach<protocol_reject_70002>(self)->start();
if (peer->is_negotiated(level::bip31))
channel->attach<protocol_ping_60001>(self)->start();
else if (peer->is_negotiated(level::version_message))
channel->attach<protocol_ping_106>(self)->start();
// Seed protocol stops upon completion, causing session removal.
if (peer->is_negotiated(level::bip155) && address_v2)
{
channel->attach<protocol_seed_70016>(self)->start();
}
else if (peer->is_negotiated(level::get_address_message))
{
channel->attach<protocol_seed_209>(self)->start();
}
else if (peer->is_negotiated(level::version_message))
{
////channel->attach<protocol_seed_106>(self)->start();
}
}
void session_seed::handle_channel_stop(const code& LOG_ONLY(ec),
const channel::ptr& LOG_ONLY(channel), const race::ptr& racer) NOEXCEPT
{
BC_ASSERT(stranded());
LOGN("Seed stop [" << channel->endpoint() << "] " << ec.message());
racer->finish(address_count());
}
void session_seed::stop_seed(const code&) NOEXCEPT
{
BC_ASSERT(stranded());
LOGN("Seed session complete.");
completed_ = steady_clock::now();
seeding_ = false;
unsubscribe_close();
}
BC_POP_WARNING()
BC_POP_WARNING()
BC_POP_WARNING()
} // namespace network
} // namespace libbitcoin