Skip to content

Commit 26f2483

Browse files
authored
Merge pull request #989 from evoskuil/master
Move session_peer implementation to ipp.
2 parents 5fde476 + 8e5c95b commit 26f2483

File tree

6 files changed

+194
-132
lines changed

6 files changed

+194
-132
lines changed

Makefile.am

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ include_bitcoin_node_impl_chasersdir = ${includedir}/bitcoin/node/impl/chasers
151151
include_bitcoin_node_impl_chasers_HEADERS = \
152152
include/bitcoin/node/impl/chasers/chaser_organize.ipp
153153

154+
include_bitcoin_node_impl_sessionsdir = ${includedir}/bitcoin/node/impl/sessions
155+
include_bitcoin_node_impl_sessions_HEADERS = \
156+
include/bitcoin/node/impl/sessions/session_peer.ipp
157+
154158
include_bitcoin_node_messagesdir = ${includedir}/bitcoin/node/messages
155159
include_bitcoin_node_messages_HEADERS = \
156160
include/bitcoin/node/messages/block.hpp \

builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@
216216
</ItemGroup>
217217
<ItemGroup>
218218
<None Include="..\..\..\..\include\bitcoin\node\impl\chasers\chaser_organize.ipp" />
219+
<None Include="..\..\..\..\include\bitcoin\node\impl\sessions\session_peer.ipp" />
219220
<None Include="packages.config" />
220221
</ItemGroup>
221222
<ItemGroup>

builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj.filters

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
<Filter Include="include\bitcoin\node\impl\chasers">
2929
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-00000000000F}</UniqueIdentifier>
3030
</Filter>
31+
<Filter Include="include\bitcoin\node\impl\sessions">
32+
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-000000000001}</UniqueIdentifier>
33+
</Filter>
3134
<Filter Include="include\bitcoin\node\messages">
3235
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-00000000000C}</UniqueIdentifier>
3336
</Filter>
@@ -38,7 +41,7 @@
3841
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-00000000000E}</UniqueIdentifier>
3942
</Filter>
4043
<Filter Include="resource">
41-
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-000000000001}</UniqueIdentifier>
44+
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-000000000002}</UniqueIdentifier>
4245
</Filter>
4346
<Filter Include="src">
4447
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-000000000000}</UniqueIdentifier>
@@ -337,6 +340,9 @@
337340
<None Include="..\..\..\..\include\bitcoin\node\impl\chasers\chaser_organize.ipp">
338341
<Filter>include\bitcoin\node\impl\chasers</Filter>
339342
</None>
343+
<None Include="..\..\..\..\include\bitcoin\node\impl\sessions\session_peer.ipp">
344+
<Filter>include\bitcoin\node\impl\sessions</Filter>
345+
</None>
340346
<None Include="packages.config" />
341347
</ItemGroup>
342348
<ItemGroup>
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/**
2+
* Copyright (c) 2011-2026 libbitcoin developers (see AUTHORS)
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+
20+
#ifndef LIBBITCOIN_NODE_SESSIONS_SESSION_PEER_IPP
21+
#define LIBBITCOIN_NODE_SESSIONS_SESSION_PEER_IPP
22+
23+
namespace libbitcoin {
24+
namespace node {
25+
26+
// this-> is required for dependent base access in CRTP.
27+
28+
TEMPLATE
29+
inline CLASS::channel_ptr CLASS::create_channel(
30+
const socket_ptr& socket) NOEXCEPT
31+
{
32+
BC_ASSERT(this->stranded());
33+
34+
const auto channel = system::emplace_shared<channel_t>(
35+
this->get_memory(), this->log, socket, this->create_key(),
36+
this->node_config(), this->options());
37+
38+
return std::static_pointer_cast<network::channel>(channel);
39+
}
40+
41+
TEMPLATE
42+
inline void CLASS::attach_handshake(const channel_ptr& channel,
43+
network::result_handler&& handler) NOEXCEPT
44+
{
45+
BC_ASSERT(channel->stranded());
46+
BC_ASSERT(channel->paused());
47+
48+
// Set the current top for version protocol, before handshake.
49+
const auto top = this->archive().get_top_confirmed();
50+
const auto peer = std::dynamic_pointer_cast<channel_t>(channel);
51+
peer->set_start_height(top);
52+
53+
// Attach and execute appropriate version protocol.
54+
NetworkSession::attach_handshake(channel, std::move(handler));
55+
}
56+
57+
TEMPLATE
58+
inline void CLASS::attach_protocols(const channel_ptr& channel) NOEXCEPT
59+
{
60+
BC_ASSERT(channel->stranded());
61+
BC_ASSERT(channel->paused());
62+
63+
using namespace system;
64+
using namespace network;
65+
using namespace network::messages::peer;
66+
using base = session_peer<NetworkSession>;
67+
68+
const auto self = this->template shared_from_base<base>();
69+
const auto relay = this->network_settings().enable_relay;
70+
const auto delay = this->node_settings().delay_inbound;
71+
const auto headers = this->node_settings().headers_first;
72+
const auto node_network = to_bool(bit_and<uint64_t>
73+
(
74+
this->network_settings().services_maximum,
75+
service::node_network
76+
));
77+
const auto node_client_filters = to_bool(bit_and<uint64_t>
78+
(
79+
this->network_settings().services_maximum,
80+
service::node_client_filters
81+
));
82+
83+
// Attach appropriate alert, reject, ping, and/or address protocols.
84+
NetworkSession::attach_protocols(channel);
85+
86+
// Channel suspensions.
87+
channel->attach<protocol_observer>(self)->start();
88+
89+
// Ready to relay blocks or block filters.
90+
const auto blocks_out = !delay || this->is_recent();
91+
92+
///////////////////////////////////////////////////////////////////////
93+
// bip152: "Upon receipt of a `sendcmpct` message with the first and
94+
// second integers set to 1, the node SHOULD announce new blocks by
95+
// sending a cmpctblock message." IOW at 70014 bip152 is optional.
96+
// This allows the node to support bip157 without supporting bip152.
97+
///////////////////////////////////////////////////////////////////////
98+
99+
const auto peer = std::dynamic_pointer_cast<channel_t>(channel);
100+
101+
// Node must advertise node_client_filters or no out filters.
102+
if (node_client_filters && blocks_out &&
103+
peer->is_negotiated(level::bip157))
104+
channel->attach<protocol_filter_out_70015>(self)->start();
105+
106+
// Node must advertise node_network or no in|out blocks|txs.
107+
if (!node_network)
108+
return;
109+
110+
// Ready to relay transactions.
111+
const auto txs_in_out = relay && peer->is_negotiated(level::bip37) &&
112+
(!delay || is_current(true));
113+
114+
// Peer advertises chain (blocks in).
115+
if (peer->is_peer_service(service::node_network))
116+
{
117+
if (headers && peer->is_negotiated(level::bip130))
118+
{
119+
channel->attach<protocol_header_in_70012>(self)->start();
120+
channel->attach<protocol_block_in_31800>(self)->start();
121+
122+
}
123+
else if (headers && peer->is_negotiated(level::headers_protocol))
124+
{
125+
channel->attach<protocol_header_in_31800>(self)->start();
126+
channel->attach<protocol_block_in_31800>(self)->start();
127+
}
128+
else
129+
{
130+
// Very hard to find < 31800 peer to connect with.
131+
// Blocks-first synchronization (not base of block_in_31800).
132+
channel->attach<protocol_block_in_106>(self)->start();
133+
}
134+
}
135+
136+
// Blocks are ready (blocks out).
137+
if (blocks_out)
138+
{
139+
if (headers && peer->is_negotiated(level::bip130))
140+
{
141+
channel->attach<protocol_header_out_70012>(self)->start();
142+
channel->attach<protocol_block_out_70012>(self)->start();
143+
}
144+
else if (headers && peer->is_negotiated(level::headers_protocol))
145+
{
146+
channel->attach<protocol_header_out_31800>(self)->start();
147+
channel->attach<protocol_block_out_106>(self)->start();
148+
}
149+
else
150+
{
151+
channel->attach<protocol_block_out_106>(self)->start();
152+
}
153+
}
154+
155+
// Relay is configured, active, and txs are ready (txs in/out).
156+
if (txs_in_out)
157+
{
158+
if (peer->peer_version()->relay)
159+
channel->attach<protocol_transaction_out_106>(self)->start();
160+
}
161+
}
162+
163+
} // namespace node
164+
} // namespace libbitcoin
165+
166+
#endif

include/bitcoin/node/protocols/protocol_performer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class BCN_API protocol_performer
4242
: node::protocol_peer(session, channel),
4343
deviation_(session->node_settings().allowed_deviation > 0.0),
4444
enabled_(enabled && to_bool(session->node_settings().sample_period_seconds)),
45-
performance_timer_(std::make_shared<network::deadline>(session->log,
45+
performance_timer_(system::emplace_shared<network::deadline>(session->log,
4646
channel->strand(), session->node_settings().sample_period())),
4747
network::tracker<protocol_performer>(session->log)
4848
{

include/bitcoin/node/sessions/session_peer.hpp

Lines changed: 15 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -54,141 +54,26 @@ class session_peer
5454
using socket_ptr = network::socket::ptr;
5555
using channel_ptr = network::channel::ptr;
5656

57-
// this-> is required for dependent base access in CRTP.
58-
59-
channel_ptr create_channel(const socket_ptr& socket) NOEXCEPT override
60-
{
61-
BC_ASSERT(this->stranded());
62-
63-
const auto channel = std::make_shared<channel_t>(
64-
this->get_memory(), this->log, socket, this->create_key(),
65-
this->node_config(), this->options());
66-
67-
return std::static_pointer_cast<network::channel>(channel);
68-
}
69-
70-
void attach_handshake(const channel_ptr& channel,
71-
network::result_handler&& handler) NOEXCEPT override
72-
{
73-
BC_ASSERT(channel->stranded());
74-
BC_ASSERT(channel->paused());
75-
76-
// Set the current top for version protocol, before handshake.
77-
const auto top = this->archive().get_top_confirmed();
78-
const auto peer = std::dynamic_pointer_cast<channel_t>(channel);
79-
peer->set_start_height(top);
80-
81-
// Attach and execute appropriate version protocol.
82-
NetworkSession::attach_handshake(channel, std::move(handler));
83-
}
84-
85-
void attach_protocols(const channel_ptr& channel) NOEXCEPT override
86-
{
87-
BC_ASSERT(channel->stranded());
88-
BC_ASSERT(channel->paused());
89-
90-
using namespace system;
91-
using namespace network;
92-
using namespace network::messages::peer;
93-
using base = session_peer<NetworkSession>;
94-
95-
const auto self = this->template shared_from_base<base>();
96-
const auto relay = this->network_settings().enable_relay;
97-
const auto delay = this->node_settings().delay_inbound;
98-
const auto headers = this->node_settings().headers_first;
99-
const auto node_network = to_bool(bit_and<uint64_t>
100-
(
101-
this->network_settings().services_maximum,
102-
service::node_network
103-
));
104-
const auto node_client_filters = to_bool(bit_and<uint64_t>
105-
(
106-
this->network_settings().services_maximum,
107-
service::node_client_filters
108-
));
109-
110-
// Attach appropriate alert, reject, ping, and/or address protocols.
111-
NetworkSession::attach_protocols(channel);
112-
113-
// Channel suspensions.
114-
channel->attach<protocol_observer>(self)->start();
115-
116-
// Ready to relay blocks or block filters.
117-
const auto blocks_out = !delay || this->is_recent();
118-
119-
///////////////////////////////////////////////////////////////////////
120-
// bip152: "Upon receipt of a `sendcmpct` message with the first and
121-
// second integers set to 1, the node SHOULD announce new blocks by
122-
// sending a cmpctblock message." IOW at 70014 bip152 is optional.
123-
// This allows the node to support bip157 without supporting bip152.
124-
///////////////////////////////////////////////////////////////////////
125-
126-
const auto peer = std::dynamic_pointer_cast<channel_t>(channel);
127-
128-
// Node must advertise node_client_filters or no out filters.
129-
if (node_client_filters && blocks_out &&
130-
peer->is_negotiated(level::bip157))
131-
channel->attach<protocol_filter_out_70015>(self)->start();
132-
133-
// Node must advertise node_network or no in|out blocks|txs.
134-
if (!node_network)
135-
return;
57+
inline channel_ptr create_channel(
58+
const socket_ptr& socket) NOEXCEPT override;
59+
inline void attach_handshake(const channel_ptr& channel,
60+
network::result_handler&& handler) NOEXCEPT override;
61+
inline void attach_protocols(const channel_ptr& channel) NOEXCEPT override;
62+
};
13663

137-
// Ready to relay transactions.
138-
const auto txs_in_out = relay && peer->is_negotiated(level::bip37) &&
139-
(!delay || is_current(true));
64+
} // namespace node
65+
} // namespace libbitcoin
14066

141-
// Peer advertises chain (blocks in).
142-
if (peer->is_peer_service(service::node_network))
143-
{
144-
if (headers && peer->is_negotiated(level::bip130))
145-
{
146-
channel->attach<protocol_header_in_70012>(self)->start();
147-
channel->attach<protocol_block_in_31800>(self)->start();
67+
#define TEMPLATE template <typename NetworkSession>
68+
#define CLASS session_peer<NetworkSession>
14869

149-
}
150-
else if (headers && peer->is_negotiated(level::headers_protocol))
151-
{
152-
channel->attach<protocol_header_in_31800>(self)->start();
153-
channel->attach<protocol_block_in_31800>(self)->start();
154-
}
155-
else
156-
{
157-
// Very hard to find < 31800 peer to connect with.
158-
// Blocks-first synchronization (not base of block_in_31800).
159-
channel->attach<protocol_block_in_106>(self)->start();
160-
}
161-
}
70+
////BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
16271

163-
// Blocks are ready (blocks out).
164-
if (blocks_out)
165-
{
166-
if (headers && peer->is_negotiated(level::bip130))
167-
{
168-
channel->attach<protocol_header_out_70012>(self)->start();
169-
channel->attach<protocol_block_out_70012>(self)->start();
170-
}
171-
else if (headers && peer->is_negotiated(level::headers_protocol))
172-
{
173-
channel->attach<protocol_header_out_31800>(self)->start();
174-
channel->attach<protocol_block_out_106>(self)->start();
175-
}
176-
else
177-
{
178-
channel->attach<protocol_block_out_106>(self)->start();
179-
}
180-
}
72+
#include <bitcoin/node/impl/sessions/session_peer.ipp>
18173

182-
// Relay is configured, active, and txs are ready (txs in/out).
183-
if (txs_in_out)
184-
{
185-
if (peer->peer_version()->relay)
186-
channel->attach<protocol_transaction_out_106>(self)->start();
187-
}
188-
}
189-
};
74+
////BC_POP_WARNING()
19075

191-
} // namespace node
192-
} // namespace libbitcoin
76+
#undef CLASS
77+
#undef TEMPLATE
19378

19479
#endif

0 commit comments

Comments
 (0)