Skip to content

Commit 81ff25c

Browse files
authored
Merge pull request #685 from evoskuil/master
Move request size settings to tcp_server.
2 parents 5c7806c + cbf5873 commit 81ff25c

File tree

10 files changed

+45
-27
lines changed

10 files changed

+45
-27
lines changed

include/bitcoin/network/channels/channel.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ class BCT_API channel
8989
/// Configuration settings.
9090
const settings_t& settings() const NOEXCEPT;
9191

92+
/// TCP server options.
93+
const options_t& options() const NOEXCEPT;
94+
9295
protected:
9396
/// Construct a channel to encapsulated and communicate on the socket.
9497
channel(const logger& log, const socket::ptr& socket, uint64_t identifier,
@@ -112,6 +115,7 @@ class BCT_API channel
112115
void handle_monitor(const code& ec) NOEXCEPT;
113116

114117
// These are thread safe (const).
118+
const options_t& options_;
115119
const settings_t& settings_;
116120
const uint64_t identifier_;
117121
const uint64_t nonce_

include/bitcoin/network/channels/channel_http.hpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,28 +52,27 @@ class BCT_API channel_http
5252
dispatcher_.subscribe(std::forward<signature>(handler));
5353
}
5454

55-
// TODO: network.minimum_buffer is being overloaded here.
5655
/// Construct http channel to encapsulate and communicate on the socket.
5756
inline channel_http(const logger& log, const socket::ptr& socket,
5857
uint64_t identifier, const settings_t& settings,
5958
const options_t& options) NOEXCEPT
6059
: channel(log, socket, identifier, settings, options),
6160
response_buffer_(system::to_shared<http::flat_buffer>()),
62-
request_buffer_(settings.minimum_buffer)
61+
request_buffer_(options.minimum_buffer)
6362
{
6463
}
6564

65+
/// Serialize and write http message to client (requires strand).
66+
/// Completion handler is always invoked on the channel strand.
67+
void send(http::response&& response,
68+
result_handler&& handler) NOEXCEPT;
69+
6670
/// Resume reading from the socket (requires strand).
6771
void resume() NOEXCEPT override;
6872

6973
/// Must call after successful message handling if no stop.
7074
virtual void receive() NOEXCEPT;
7175

72-
/// Serialize and write http message to client (requires strand).
73-
/// Completion handler is always invoked on the channel strand.
74-
virtual void send(http::response&& response,
75-
result_handler&& handler) NOEXCEPT;
76-
7776
protected:
7877
/// Stranded handler invoked from stop().
7978
void stopping(const code& ec) NOEXCEPT override;

include/bitcoin/network/channels/channel_rpc.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,13 @@ class BCT_API channel_rpc
5050
dispatcher_.subscribe(std::forward<Handler>(handler));
5151
}
5252

53-
// TODO: network.minimum_buffer is being overloaded here.
5453
/// Construct rpc channel to encapsulate and communicate on the socket.
5554
inline channel_rpc(const logger& log, const socket::ptr& socket,
5655
uint64_t identifier, const settings_t& settings,
5756
const options_t& options) NOEXCEPT
5857
: channel(log, socket, identifier, settings, options),
5958
response_buffer_(system::to_shared<http::flat_buffer>()),
60-
request_buffer_(settings.minimum_buffer)
59+
request_buffer_(options.minimum_buffer)
6160
{
6261
}
6362

include/bitcoin/network/net/socket.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ class BCT_API socket
289289
std::atomic_bool stopped_{};
290290

291291
// These are protected by strand (see also handle_accept).
292+
size_t maximum_;
292293
asio::socket socket_;
293294
config::address address_;
294295
config::authority authority_{};

include/bitcoin/network/settings.hpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@
2828
namespace libbitcoin {
2929
namespace network {
3030

31+
/// The largest p2p payload request when configured for witness blocks.
32+
constexpr uint32_t maximum_request_
33+
{
34+
system::possible_narrow_cast<uint32_t>(
35+
messages::peer::heading::maximum_payload(
36+
messages::peer::level::canonical, true))
37+
};
38+
3139
/// Common network configuration settings, properties not thread safe.
3240
struct BCT_API settings
3341
{
@@ -44,6 +52,8 @@ struct BCT_API settings
4452
uint16_t connections{ 0 };
4553
uint32_t inactivity_minutes{ 10 };
4654
uint32_t expiration_minutes{ 60 };
55+
uint32_t maximum_request{ maximum_request_ };
56+
uint32_t minimum_buffer{ maximum_request_ };
4757

4858
/// Helpers.
4959
virtual bool enabled() const NOEXCEPT;
@@ -220,12 +230,6 @@ struct BCT_API settings
220230
uint32_t handshake_timeout_seconds{ 15 };
221231
uint32_t channel_heartbeat_minutes{ 5 };
222232
uint32_t maximum_skew_minutes{ 120 };
223-
uint32_t minimum_buffer
224-
{
225-
system::possible_narrow_cast<uint32_t>(
226-
messages::peer::heading::maximum_payload(
227-
messages::peer::level::canonical, true))
228-
};
229233
uint32_t rate_limit{ 1024 };
230234
std::string user_agent{ BC_USER_AGENT };
231235
std::filesystem::path path{};

src/channels/channel.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ inline deadline::ptr make_timer(const logger& log, asio::strand& strand,
4747
channel::channel(const logger& log, const socket::ptr& socket,
4848
uint64_t identifier, const settings_t& settings,
4949
const options_t& options) NOEXCEPT
50-
: proxy(socket), settings_(settings), identifier_(identifier),
50+
: proxy(socket),
51+
options_(options),
52+
settings_(settings),
53+
identifier_(identifier),
5154
inactivity_(make_timer(log, socket->strand(), options.inactivity())),
5255
expiration_(make_timer(log, socket->strand(), options.expiration()))
5356
{
@@ -221,6 +224,11 @@ const network::settings& channel::settings() const NOEXCEPT
221224
return settings_;
222225
}
223226

227+
const channel::options_t& channel::options() const NOEXCEPT
228+
{
229+
return options_;
230+
}
231+
224232
BC_POP_WARNING()
225233

226234
} // namespace network

src/channels/channel_peer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,9 @@ void channel_peer::handle_read_payload(const code& ec, size_t payload_size,
304304
///////////////////////////////////////////////////////////////////////////
305305

306306
// Don't retain larger than configured (time-space tradeoff).
307-
if (settings().minimum_buffer < payload_buffer_.capacity())
307+
if (options().minimum_buffer < payload_buffer_.capacity())
308308
{
309-
payload_buffer_.resize(settings().minimum_buffer);
309+
payload_buffer_.resize(options().minimum_buffer);
310310
payload_buffer_.shrink_to_fit();
311311
}
312312

src/net/socket.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ using namespace network::rpc;
3434
using namespace std::placeholders;
3535
namespace beast = boost::beast;
3636

37-
// TODO: move to config.
38-
constexpr size_t client_request_limit = 5u * 1024u * 1024u;
39-
4037
// Shared pointers required in handler parameters so closures control lifetime.
4138
BC_PUSH_WARNING(NO_VALUE_OR_CONST_REF_SHARED_PTR)
4239
BC_PUSH_WARNING(SMART_PTR_NOT_NEEDED)
@@ -59,6 +56,7 @@ socket::socket(const logger& log, asio::io_context& service) NOEXCEPT
5956
socket::socket(const logger& log, asio::io_context& service,
6057
const config::address& address) NOEXCEPT
6158
: strand_(service.get_executor()),
59+
maximum_(5u * 1024u * 1024u),
6260
socket_(strand_),
6361
service_(service),
6462
address_(address),
@@ -551,10 +549,10 @@ void socket::do_http_read(std::reference_wrapper<http::flat_buffer> buffer,
551549
auto parser = to_shared<http_parser>();
552550

553551
// Causes http::error::body_limit on completion.
554-
parser->body_limit(client_request_limit);
552+
parser->body_limit(maximum_);
555553

556554
// Causes http::error::header_limit on completion.
557-
parser->header_limit(client_request_limit);
555+
parser->header_limit(limit<uint32_t>(maximum_));
558556

559557
// This operation posts handler to the strand.
560558
beast::http::async_read(socket_, buffer.get(), *parser,
@@ -675,7 +673,7 @@ void socket::handle_rpc_read(boost_code ec, size_t size, size_t total,
675673
return;
676674
}
677675

678-
if (total > client_request_limit)
676+
if (total > maximum_)
679677
{
680678
handler(error::message_overflow, total);
681679
return;
@@ -894,7 +892,7 @@ code socket::set_websocket(const http::request& request) NOEXCEPT
894892
websocket_.emplace(std::move(socket_));
895893

896894
// Causes websocket::error::message_too_big on completion.
897-
websocket_->read_message_max(client_request_limit);
895+
websocket_->read_message_max(maximum_);
898896
websocket_->set_option(ws::decorator
899897
{
900898
[](http::fields& header) NOEXCEPT

test/channels/channel_peer.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ BOOST_AUTO_TEST_CASE(channel_peer__properties__default__expected)
102102
BOOST_REQUIRE_EQUAL(channel_ptr->settings().maximum_payload(), payload_maximum(set));
103103
BOOST_REQUIRE_EQUAL(channel_ptr->settings().identifier, set.identifier);
104104
BOOST_REQUIRE_EQUAL(channel_ptr->settings().validate_checksum, set.validate_checksum);
105-
BOOST_REQUIRE_EQUAL(channel_ptr->settings().minimum_buffer, set.minimum_buffer);
106105

107106
// Stop is asynchronous, threadpool destruct blocks until all complete.
108107
// Calling stop here sets channel.stopped and prevents destructor assertion.

test/settings.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ BOOST_AUTO_TEST_CASE(settings__construct__default__expected)
5353
BOOST_REQUIRE_EQUAL(instance.handshake_timeout_seconds, 15u);
5454
BOOST_REQUIRE_EQUAL(instance.channel_heartbeat_minutes, 5u);
5555
BOOST_REQUIRE_EQUAL(instance.maximum_skew_minutes, 120u);
56-
BOOST_REQUIRE_EQUAL(instance.minimum_buffer, 4'000'000u);
5756
BOOST_REQUIRE_EQUAL(instance.rate_limit, 1024u);
5857
BOOST_REQUIRE_EQUAL(instance.user_agent, BC_USER_AGENT);
5958
BOOST_REQUIRE(instance.path.empty());
@@ -344,6 +343,7 @@ BOOST_AUTO_TEST_CASE(settings__excluded__default__true)
344343

345344
// services
346345
// ----------------------------------------------------------------------------
346+
constexpr auto maximum_request = system::chain::max_block_weight;
347347

348348
BOOST_AUTO_TEST_CASE(settings__tcp_server__defaults__expected)
349349
{
@@ -357,6 +357,7 @@ BOOST_AUTO_TEST_CASE(settings__tcp_server__defaults__expected)
357357
BOOST_REQUIRE_EQUAL(instance.connections, 0u);
358358
BOOST_REQUIRE_EQUAL(instance.inactivity_minutes, 10u);
359359
BOOST_REQUIRE_EQUAL(instance.expiration_minutes, 60u);
360+
BOOST_REQUIRE_EQUAL(instance.maximum_request, maximum_request);
360361
BOOST_REQUIRE(!instance.enabled());
361362
BOOST_REQUIRE(instance.inactivity() == minutes(10));
362363
BOOST_REQUIRE(instance.expiration() == minutes(60));
@@ -374,6 +375,7 @@ BOOST_AUTO_TEST_CASE(settings__http_server__defaults__expected)
374375
BOOST_REQUIRE_EQUAL(instance.connections, 0u);
375376
BOOST_REQUIRE_EQUAL(instance.inactivity_minutes, 10u);
376377
BOOST_REQUIRE_EQUAL(instance.expiration_minutes, 60u);
378+
BOOST_REQUIRE_EQUAL(instance.maximum_request, maximum_request);
377379
BOOST_REQUIRE(!instance.enabled());
378380
BOOST_REQUIRE(instance.inactivity() == minutes(10));
379381
BOOST_REQUIRE(instance.expiration() == minutes(60));
@@ -399,6 +401,7 @@ BOOST_AUTO_TEST_CASE(settings__websocket_server__defaults__expected)
399401
BOOST_REQUIRE_EQUAL(instance.connections, 0u);
400402
BOOST_REQUIRE_EQUAL(instance.inactivity_minutes, 10u);
401403
BOOST_REQUIRE_EQUAL(instance.expiration_minutes, 60u);
404+
BOOST_REQUIRE_EQUAL(instance.maximum_request, maximum_request);
402405
BOOST_REQUIRE(!instance.enabled());
403406
BOOST_REQUIRE(instance.inactivity() == minutes(10));
404407
BOOST_REQUIRE(instance.expiration() == minutes(60));
@@ -425,6 +428,7 @@ BOOST_AUTO_TEST_CASE(settings__peer_outbound__mainnet__expected)
425428
BOOST_REQUIRE_EQUAL(instance.connections, 10u);
426429
BOOST_REQUIRE_EQUAL(instance.inactivity_minutes, 10u);
427430
BOOST_REQUIRE_EQUAL(instance.expiration_minutes, 60u);
431+
BOOST_REQUIRE_EQUAL(instance.maximum_request, maximum_request);
428432
BOOST_REQUIRE(!instance.enabled());
429433
BOOST_REQUIRE(instance.inactivity() == minutes(10));
430434
BOOST_REQUIRE(instance.expiration() == minutes(60));
@@ -545,6 +549,7 @@ BOOST_AUTO_TEST_CASE(settings__peer_inbound__mainnet__expected)
545549
BOOST_REQUIRE_EQUAL(instance.connections, 0u);
546550
BOOST_REQUIRE_EQUAL(instance.inactivity_minutes, 10u);
547551
BOOST_REQUIRE_EQUAL(instance.expiration_minutes, 60u);
552+
BOOST_REQUIRE_EQUAL(instance.maximum_request, maximum_request);
548553
BOOST_REQUIRE(!instance.enabled());
549554
BOOST_REQUIRE(instance.inactivity() == minutes(10));
550555
BOOST_REQUIRE(instance.expiration() == minutes(60));
@@ -691,6 +696,7 @@ BOOST_AUTO_TEST_CASE(settings__peer_manual__mainnet__expected)
691696
BOOST_REQUIRE_EQUAL(instance.connections, 0u);
692697
BOOST_REQUIRE_EQUAL(instance.inactivity_minutes, 10u);
693698
BOOST_REQUIRE_EQUAL(instance.expiration_minutes, 60u);
699+
BOOST_REQUIRE_EQUAL(instance.maximum_request, maximum_request);
694700
BOOST_REQUIRE(!instance.enabled());
695701
BOOST_REQUIRE(instance.inactivity() == minutes(10));
696702
BOOST_REQUIRE(instance.expiration() == minutes(60));

0 commit comments

Comments
 (0)