Skip to content

Commit

Permalink
boost 1.87.0 (#290)
Browse files Browse the repository at this point in the history
Signed-off-by: turuslan <[email protected]>
  • Loading branch information
turuslan authored Jan 23, 2025
1 parent c3e6cce commit ef43be4
Show file tree
Hide file tree
Showing 17 changed files with 91 additions and 78 deletions.
4 changes: 2 additions & 2 deletions cmake/Hunter/init.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ set(
include(${CMAKE_CURRENT_LIST_DIR}/HunterGate.cmake)

HunterGate(
URL https://github.com/qdrvm/hunter/archive/refs/tags/v0.25.3-qdrvm23.zip
SHA1 3fb58496e599fa3d35e94d1810324c6b379029f1
URL https://github.com/qdrvm/hunter/archive/refs/tags/v0.25.3-qdrvm28.tar.gz
SHA1 a4f1b0f42464e07790b7f90b783a822d71be6c6d
LOCAL
)
3 changes: 2 additions & 1 deletion example/01-echo/libp2p_echo_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ int main(int argc, char *argv[]) {
auto context = injector.create<std::shared_ptr<boost::asio::io_context>>();
auto sch = injector.create<std::shared_ptr<libp2p::basic::Scheduler>>();

context->post(
post(
*context,
[log,
host{std::move(host)},
&echo,
Expand Down
2 changes: 1 addition & 1 deletion example/01-echo/libp2p_echo_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ dPtse4GVRA2swbXcZX5iFVi/V8poIpdVrgn5iMadkQnYf9APWJuGcebK
auto ma = libp2p::multi::Multiaddress::create(_ma).value();

// launch a Listener part of the Host
io_context->post([&] {
post(*io_context, [&] {
auto listen_res = host->listen(ma);
if (!listen_res) {
log->error("host cannot listen the given multiaddress: {}",
Expand Down
4 changes: 2 additions & 2 deletions example/02-kademlia/rendezvous_chat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Session : public std::enable_shared_from_this<Session> {
public:
explicit Session(std::shared_ptr<libp2p::connection::Stream> stream)
: stream_(std::move(stream)),
incoming_(std::make_shared<std::vector<uint8_t>>(1 << 12)){};
incoming_(std::make_shared<std::vector<uint8_t>>(1 << 12)) {};

bool read() {
if (stream_->isClosedForRead()) {
Expand Down Expand Up @@ -316,7 +316,7 @@ int main(int argc, char *argv[]) {
kademlia_config.randomWalk.interval);
};

io->post([&] {
post(*io, [&] {
auto listen = host->listen(ma);
if (not listen) {
fmt::println(std::cerr,
Expand Down
2 changes: 1 addition & 1 deletion example/03-gossip/gossip_chat_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ int main(int argc, char *argv[]) {
}

// start the node as soon as async engine starts
io->post([&] {
post(*io, [&] {
auto listen_res = host->listen(peer_info->addresses[0]);
if (!listen_res) {
fmt::println(std::cerr,
Expand Down
17 changes: 5 additions & 12 deletions example/03-gossip/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,14 @@ namespace libp2p::protocol::example::utility {

std::string getLocalIP(boost::asio::io_context &io) {
boost::asio::ip::tcp::resolver resolver(io);
boost::asio::ip::tcp::resolver::query query(boost::asio::ip::host_name(),
"");
boost::system::error_code ec;
boost::asio::ip::tcp::resolver::iterator it = resolver.resolve(query, ec);
boost::asio::ip::tcp::resolver::iterator end;
std::string addr("127.0.0.1");
while (it != end) {
auto ep = it->endpoint();
if (ep.address().is_v4()) {
addr = ep.address().to_string();
break;
for (auto &entry : resolver.resolve(boost::asio::ip::host_name(), "", ec)) {
auto address = entry.endpoint().address();
if (address.is_v4()) {
return address.to_string();
}
++it;
}
return addr;
return "127.0.0.1";
}

boost::optional<libp2p::peer::PeerInfo> str2peerInfo(const std::string &str) {
Expand Down
2 changes: 1 addition & 1 deletion example/04-dnstxt/ares_resolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ int main(int argc, char *argv[]) {
auto context = injector.create<std::shared_ptr<boost::asio::io_context>>();
// the guard to preserve context's running state when tasks queue is empty
auto work_guard = boost::asio::make_work_guard(*context);
context->post([&] {
post(*context, [&] {
ares.resolveTxt(
"_dnsaddr.bootstrap.libp2p.io",
context,
Expand Down
28 changes: 28 additions & 0 deletions include/libp2p/boost/outcome.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include <qtils/outcome.hpp>

/**
* Boost 1.87.0 (1.86.0?) removed overloads with `error_code` and only kept
* throwing overload.
*/
namespace boost_outcome {
template <typename F>
outcome::result<std::invoke_result_t<F>> tryCatch(const F &f) {
try {
return f();
} catch (boost::system::system_error &e) {
return e.code();
}
}

outcome::result<std::string> to_string(const auto &x) {
return tryCatch([&] { return x.to_string(); });
}
} // namespace boost_outcome
7 changes: 2 additions & 5 deletions include/libp2p/transport/tcp/tcp_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/ip/udp.hpp>
#include <charconv>
#include <libp2p/boost/outcome.hpp>
#include <libp2p/multi/multiaddress.hpp>
#include <variant>

Expand Down Expand Up @@ -172,11 +173,7 @@ namespace libp2p::transport::detail {
constexpr auto udp = std::is_same_v<T, boost::asio::ip::udp::endpoint>;
static_assert(tcp or udp);
auto ip = endpoint.address();
boost::system::error_code ec;
auto ip_str = ip.to_string(ec);
if (ec) {
return ec;
}
OUTCOME_TRY(ip_str, boost_outcome::to_string(ip));
return fmt::format("/{}/{}/{}/{}",
ip.is_v4() ? "ip4" : "ip6",
ip_str,
Expand Down
15 changes: 4 additions & 11 deletions src/basic/scheduler/asio_scheduler_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include <libp2p/basic/scheduler/asio_scheduler_backend.hpp>

#include <boost/asio/post.hpp>

#include <libp2p/log/logger.hpp>
#include <libp2p/outcome/outcome.hpp>

Expand All @@ -15,7 +17,7 @@ namespace libp2p::basic {
: io_context_(std::move(io_context)), timer_(*io_context_) {}

void AsioSchedulerBackend::post(std::function<void()> &&cb) {
io_context_->post(std::move(cb));
boost::asio::post(*io_context_, std::move(cb));
}

std::chrono::milliseconds AsioSchedulerBackend::now() const {
Expand All @@ -25,16 +27,7 @@ namespace libp2p::basic {
void AsioSchedulerBackend::setTimer(
std::chrono::milliseconds abs_time,
std::weak_ptr<SchedulerBackendFeedback> scheduler) {
boost::system::error_code ec;
timer_.expires_at(decltype(timer_)::clock_type::time_point(abs_time), ec);

if (ec) {
// this should never happen
auto log = log::createLogger("Scheduler", "scheduler");
log->critical("cannot set timer: {}", ec);
boost::asio::detail::throw_error(ec, "setTimer");
}

timer_.expires_at(decltype(timer_)::clock_type::time_point(abs_time));
timer_.async_wait([scheduler = std::move(scheduler)](
const boost::system::error_code &error) {
if (!error) {
Expand Down
7 changes: 4 additions & 3 deletions src/network/cares/cares.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ namespace libp2p::network::c_ares {
Ares::TxtCallback callback,
Ares::Error error) {
if (auto ctx = io_context.lock()) {
ctx->post([callback{std::move(callback)}, error] { callback(error); });
post(*ctx, [callback{std::move(callback)}, error] { callback(error); });
return;
}
SL_DEBUG(log(), "IO context has expired");
Expand Down Expand Up @@ -186,8 +186,9 @@ namespace libp2p::network::c_ares {
}
::ares_free_data(reply);
if (auto ctx = request_ptr->io_context.lock()) {
ctx->post([callback{std::move(request_ptr->callback)},
reply{std::move(result)}] { callback(reply); });
post(*ctx,
[callback{std::move(request_ptr->callback)},
reply{std::move(result)}] { callback(reply); });
} else {
SL_DEBUG(log(), "IO context has expired");
}
Expand Down
2 changes: 1 addition & 1 deletion src/transport/quic/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace libp2p::transport {

void QuicConnection::deferReadCallback(outcome::result<size_t> res,
ReadCallbackFunc cb) {
io_context_->post([cb{std::move(cb)}, res] { cb(res); });
post(*io_context_, [cb{std::move(cb)}, res] { cb(res); });
}

void QuicConnection::writeSome(BytesIn in,
Expand Down
4 changes: 2 additions & 2 deletions src/transport/quic/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ namespace libp2p::transport::lsquic {
if (n > 0) {
r = n;
}
stream_ctx->engine->io_context_->post(
[cb{std::move(op.cb)}, r] { cb(r); });
post(*stream_ctx->engine->io_context_,
[cb{std::move(op.cb)}, r] { cb(r); });
};

lsquic_engine_api api{};
Expand Down
65 changes: 33 additions & 32 deletions test/acceptance/p2p/host/peer/test_peer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Peer::Peer(Peer::Duration timeout, bool secure)

void Peer::startServer(const multi::Multiaddress &address,
std::shared_ptr<std::promise<peer::PeerInfo>> promise) {
context_->post([this, address, p = std::move(promise)] {
post(*context_, [this, address, p = std::move(promise)] {
EXPECT_OK(host_->listen(address));
host_->start();
p->set_value(host_->getPeerInfo());
Expand All @@ -75,38 +75,39 @@ void Peer::startServer(const multi::Multiaddress &address,
void Peer::startClient(const peer::PeerInfo &pinfo,
size_t message_count,
Peer::sptr<TickCounter> counter) {
context_->post([this,
server_id = pinfo.id.toBase58(),
pinfo,
message_count,
counter = std::move(counter)]() mutable {
this->host_->newStream(
post(*context_,
[this,
server_id = pinfo.id.toBase58(),
pinfo,
{echo_->getProtocolId()},
[server_id = std::move(server_id),
ping_times = message_count,
counter =
std::move(counter)](StreamAndProtocolOrError rstream) mutable {
// get stream
auto stream = EXPECT_OK(rstream);
// make client session
auto client = std::make_shared<protocol::ClientTestSession>(
stream.stream, ping_times);
// handle session
client->handle(
[server_id = std::move(server_id),
client,
counter = std::move(counter)](
outcome::result<std::vector<uint8_t>> res) mutable {
// count message exchange
counter->tick();
// ensure message returned
auto vec = EXPECT_OK(res);
// ensure message is correct
ASSERT_EQ(vec.size(), client->bufferSize()); // NOLINT
});
});
});
message_count,
counter = std::move(counter)]() mutable {
this->host_->newStream(
pinfo,
{echo_->getProtocolId()},
[server_id = std::move(server_id),
ping_times = message_count,
counter = std::move(counter)](
StreamAndProtocolOrError rstream) mutable {
// get stream
auto stream = EXPECT_OK(rstream);
// make client session
auto client = std::make_shared<protocol::ClientTestSession>(
stream.stream, ping_times);
// handle session
client->handle(
[server_id = std::move(server_id),
client,
counter = std::move(counter)](
outcome::result<std::vector<uint8_t>> res) mutable {
// count message exchange
counter->tick();
// ensure message returned
auto vec = EXPECT_OK(res);
// ensure message is correct
ASSERT_EQ(vec.size(), client->bufferSize()); // NOLINT
});
});
});
}

void Peer::wait() {
Expand Down
2 changes: 1 addition & 1 deletion test/acceptance/p2p/muxer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ TEST_P(MuxerAcceptanceTest, ParallelEcho) {
std::vector<std::thread> clients;
std::atomic<int> clients_running(totalClients);

server_context->post([&]() {
post(*server_context, [&]() {
clients.reserve(totalClients);
for (int i = 0; i < totalClients; i++) {
auto localSeed = randomEngine();
Expand Down
4 changes: 2 additions & 2 deletions test/libp2p/muxer/muxers_and_streams_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ void testStreamsGetNotifiedAboutEOF(bool jumbo_msg, InjectorArgs &&...args) {
io,
std::forward<decltype(args)>(args)...);

io->post([&]() {
post(*io, [&]() {
server->listen(listen_to);
libp2p::peer::PeerInfo peer_info{server->getId(), {listen_to}};
client->connect(peer_info);
Expand Down Expand Up @@ -499,7 +499,7 @@ void testOutboundConnectionAcceptsStreams(InjectorArgs &&...args) {
io,
std::forward<decltype(args)>(args)...);

io->post([&]() {
post(*io, [&]() {
server->listen(listen_to);
libp2p::peer::PeerInfo peer_info{server->getId(), {listen_to}};
client->connect(peer_info);
Expand Down
1 change: 0 additions & 1 deletion test/libp2p/protocol/ping_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include <libp2p/protocol/ping.hpp>

#include <boost/asio/io_service.hpp>
#include <boost/optional.hpp>

#include <libp2p/common/literals.hpp>
Expand Down

0 comments on commit ef43be4

Please sign in to comment.