Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions example/00-vcpkg-install/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
int main() {
auto ma_res = libp2p::multi::Multiaddress::create("/ip4/127.0.0.1/tcp/8080");
if (ma_res.has_value()) {
std::cout << "Created multiaddress: "
<< ma_res.value().getStringAddress() << std::endl;
std::cout << "Created multiaddress: " << ma_res.value().getStringAddress()
<< std::endl;
}
return 0;
}
2 changes: 1 addition & 1 deletion example/02-kademlia/rendezvous_chat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,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
8 changes: 6 additions & 2 deletions include/libp2p/common/metrics/instance_count.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@
})(); \
return count; \
} \
Libp2pMetricsInstanceCount(const type *) { ++count(); } \
Libp2pMetricsInstanceCount(const type *) { \
++count(); \
} \
Libp2pMetricsInstanceCount(const Libp2pMetricsInstanceCount &) { \
++count(); \
} \
~Libp2pMetricsInstanceCount() { --count(); } \
~Libp2pMetricsInstanceCount() { \
--count(); \
} \
} libp2p_metrics_instance_count { \
this \
}
Expand Down
58 changes: 29 additions & 29 deletions include/libp2p/common/metrics/instance_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,35 @@
#include <list>
#include <mutex>

#define _LIBP2P_METRICS_INSTANCE_LIST(type) \
struct Libp2pMetricsInstanceList { \
using List = std::list<const type *>; \
struct State { \
std::mutex mutex; \
List list; \
static auto &get() { \
static State state; \
return state; \
} \
}; \
List::const_iterator it; \
Libp2pMetricsInstanceList(const type *ptr) { \
auto &state{State::get()}; \
std::lock_guard lock{state.mutex}; \
state.list.emplace_front(ptr); \
it = state.list.begin(); \
} \
Libp2pMetricsInstanceList(const Libp2pMetricsInstanceList &list) \
: Libp2pMetricsInstanceList{ \
(const type *)((uintptr_t)*list.it + (this - &list))} {} \
~Libp2pMetricsInstanceList() { \
auto &state{State::get()}; \
std::lock_guard lock{state.mutex}; \
state.list.erase(it); \
} \
void operator=(const Libp2pMetricsInstanceList &hook) {} \
} libp2p_metrics_instance_list { \
this \
#define _LIBP2P_METRICS_INSTANCE_LIST(type) \
struct Libp2pMetricsInstanceList { \
using List = std::list<const type *>; \
struct State { \
std::mutex mutex; \
List list; \
static auto &get() { \
static State state; \
return state; \
} \
}; \
List::const_iterator it; \
Libp2pMetricsInstanceList(const type *ptr) { \
auto &state{State::get()}; \
std::lock_guard lock{state.mutex}; \
state.list.emplace_front(ptr); \
it = state.list.begin(); \
} \
Libp2pMetricsInstanceList(const Libp2pMetricsInstanceList &list) \
: Libp2pMetricsInstanceList{ \
(const type *)((uintptr_t) * list.it + (this - &list))} {} \
~Libp2pMetricsInstanceList() { \
auto &state{State::get()}; \
std::lock_guard lock{state.mutex}; \
state.list.erase(it); \
} \
void operator=(const Libp2pMetricsInstanceList &hook) {} \
} libp2p_metrics_instance_list { \
this \
}

#define LIBP2P_METRICS_INSTANCE_LIST(type) \
Expand Down
8 changes: 4 additions & 4 deletions include/libp2p/event/emitter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ namespace libp2p::event {
template <typename ExpectedEvent>
std::enable_if_t<
std::is_same_v<std::decay_t<ThisEvent>, std::decay_t<ExpectedEvent>>,
Signal<ExpectedEvent>>
&getSignal() {
Signal<ExpectedEvent>> &
getSignal() {
return signal_;
}

Expand All @@ -61,8 +61,8 @@ namespace libp2p::event {
template <typename ExpectedEvent>
std::enable_if_t<
!std::is_same_v<std::decay_t<ThisEvent>, std::decay_t<ExpectedEvent>>,
Signal<ExpectedEvent>>
&getSignal() {
Signal<ExpectedEvent>> &
getSignal() {
return Base::template getSignal<ExpectedEvent>();
}

Expand Down
8 changes: 4 additions & 4 deletions include/libp2p/multi/multiaddress_protocol_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

#pragma once

#include <unistd.h>
#include <array>
#include <functional>
#include <map>
#include <string_view>
#include <unistd.h>

namespace libp2p::multi {

Expand Down Expand Up @@ -58,8 +58,8 @@ namespace libp2p::multi {
// https://github.com/multiformats/rust-multiaddr/blob/3c7e813c3b1fdd4187a9ca9ff67e10af0e79231d/src/protocol.rs#L50-L53
X_PARITY_WS = 4770,
X_PARITY_WSS = 4780,
// Range for private use: 0x300000 – 0x3FFFFF
// Debug section
// Range for private use: 0x300000 – 0x3FFFFF
// Debug section
_DUMMY_PROTO_1 = 0x3DEAD1,
_DUMMY_PROTO_2 = 0x3DEAD2,
_DUMMY_PROTO_3 = 0x3DEAD3,
Expand Down Expand Up @@ -161,7 +161,7 @@ namespace libp2p::multi {
{Protocol::Code::P2P_CIRCUIT, 0, "p2p-circuit"},
{Protocol::Code::X_PARITY_WS, Protocol::kVarLen, "x-parity-ws"},
{Protocol::Code::X_PARITY_WSS, Protocol::kVarLen, "x-parity-wss"},
// Debug section
// Debug section
{Protocol::Code::_DUMMY_PROTO_1, 0, "_dummy_proto_1"},
{Protocol::Code::_DUMMY_PROTO_2, 0, "_dummy_proto_2"},
{Protocol::Code::_DUMMY_PROTO_3, Protocol::kVarLen, "_dummy_proto_3"},
Expand Down
4 changes: 2 additions & 2 deletions include/libp2p/peer/peer_id.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ struct fmt::formatter<libp2p::peer::PeerId> {
// Formats the PeerId using the parsed format specification (presentation)
// stored in this formatter.
template <typename FormatContext>
auto format(const libp2p::peer::PeerId &peer_id, FormatContext &ctx) const
-> decltype(ctx.out()) {
auto format(const libp2p::peer::PeerId &peer_id,
FormatContext &ctx) const -> decltype(ctx.out()) {
// ctx.out() is an output iterator to write to.

auto &&b58 = peer_id.toBase58();
Expand Down
6 changes: 4 additions & 2 deletions include/libp2p/protocol/kademlia/impl/kademlia_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,10 @@ namespace libp2p::protocol::kademlia {
void performRepublishing();

// Helper methods for periodic operations
std::vector<PeerId> getClosestPeers(const Key& key, size_t count);
void replicateRecord(const Key& key, const Value& value, bool extend_expiration);
std::vector<PeerId> getClosestPeers(const Key &key, size_t count);
void replicateRecord(const Key &key,
const Value &value,
bool extend_expiration);

log::SubLogger log_;
};
Expand Down
2 changes: 1 addition & 1 deletion src/basic/protobuf_message_read_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace libp2p::basic {
std::shared_ptr<ReadWriter>
conn) // NOLINT(performance-unnecessary-value-param)
: read_writer_{
std::make_shared<MessageReadWriterUvarint>(std::move(conn))} {
std::make_shared<MessageReadWriterUvarint>(std::move(conn))} {
BOOST_ASSERT(read_writer_);
}
} // namespace libp2p::basic
2 changes: 1 addition & 1 deletion src/multi/multibase_codec/codecs/base64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ namespace {
};

/// Returns max bytes needed to decode a base64 string
inline std::size_t constexpr decodedSize(std::size_t n) {
inline constexpr std::size_t decodedSize(std::size_t n) {
return n / 4 * 3; // requires n&3==0, smaller
}

Expand Down
40 changes: 23 additions & 17 deletions src/protocol/kademlia/impl/kademlia_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ namespace libp2p::protocol::kademlia {
log_.debug("Performing periodic replication");

auto records = storage_->getAllRecords();
for (const auto& [key, value]: records) {
for (const auto &[key, value] : records) {
replicateRecord(key, value, false); // false = don't extend expiration
}
}
Expand All @@ -707,43 +707,49 @@ namespace libp2p::protocol::kademlia {
log_.debug("Performing periodic republishing");

auto records = storage_->getAllRecords();
for (const auto& [key, value] : records) {
for (const auto &[key, value] : records) {
replicateRecord(key, value, true); // true = extend expiration
}
}

std::vector<PeerId> KademliaImpl::getClosestPeers(const Key& key, size_t count) {
std::vector<PeerId> KademliaImpl::getClosestPeers(const Key &key,
size_t count) {
std::vector<PeerId> closest_peers;

// Get peers from peer routing table
HashedKey hashed_key(key);
auto peers = peer_routing_table_->getNearestPeers(hashed_key.hash, count);
for (const auto& peer : peers) {
if (peer != self_id_) { // Don't include self

for (const auto &peer : peers) {
if (peer != self_id_) { // Don't include self
closest_peers.push_back(peer);
}
}

return closest_peers;
}

void KademliaImpl::replicateRecord(const Key& key, const Value& value, bool extend_expiration) {
// If republishing, extend local expiration by putting the value back to storage
void KademliaImpl::replicateRecord(const Key &key,
const Value &value,
bool extend_expiration) {
// If republishing, extend local expiration by putting the value back to
// storage
if (extend_expiration) {
auto put_res = storage_->putValue(key, value);
if (!put_res) {
log_.warn("Republish: failed to extend expiration for key: {}: {}",
multi::detail::encodeBase58(key), put_res.error());
multi::detail::encodeBase58(key),
put_res.error());
}
}

auto closest_peers = getClosestPeers(key,
extend_expiration ? config_.periodicRepublishing.peers_per_cycle
auto closest_peers = getClosestPeers(
key,
extend_expiration ? config_.periodicRepublishing.peers_per_cycle
: config_.periodicReplication.peers_per_cycle);

if (closest_peers.empty()) {
log_.debug("No peers available for replication/republishing of key: {}",
log_.debug("No peers available for replication/republishing of key: {}",
multi::detail::encodeBase58(key));
return;
}
Expand All @@ -752,9 +758,9 @@ namespace libp2p::protocol::kademlia {
auto executor = createPutValueExecutor(key, value, closest_peers);
if (executor) {
std::ignore = executor->start();
log_.debug("Started {} for key: {} to {} peers",
log_.debug("Started {} for key: {} to {} peers",
extend_expiration ? "republishing" : "replication",
multi::detail::encodeBase58(key),
multi::detail::encodeBase58(key),
closest_peers.size());
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/protocol/kademlia/impl/storage_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ namespace libp2p::protocol::kademlia {
std::vector<std::pair<Key, Value>> StorageImpl::getAllRecords() const {
std::vector<std::pair<Key, Value>> records;
auto now = scheduler_->now();

// Iterate through all records and get their values from backend
for (const auto& record : *table_) {
for (const auto &record : *table_) {
// Only include non-expired records
if (record.expire_time > now) {
auto value_result = backend_->getValue(record.key);
Expand All @@ -132,7 +132,7 @@ namespace libp2p::protocol::kademlia {
}
}
}

return records;
}
} // namespace libp2p::protocol::kademlia
4 changes: 2 additions & 2 deletions src/security/secio/secio_dialer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ namespace {
namespace libp2p::security::secio {
Dialer::Dialer(std::shared_ptr<connection::LayerConnection> connection)
: rw{std::make_shared<libp2p::basic::ProtobufMessageReadWriter>(
std::make_shared<libp2p::basic::MessageReadWriterBigEndian>(
std::move(connection)))} {}
std::make_shared<libp2p::basic::MessageReadWriterBigEndian>(
std::move(connection)))} {}

void Dialer::storeLocalPeerProposalBytes(
const std::shared_ptr<std::vector<uint8_t>> &bytes) {
Expand Down
2 changes: 1 addition & 1 deletion src/security/tls/tls_adaptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

#include <libp2p/peer/peer_id.hpp>
#include <libp2p/security/tls/ssl_context.hpp>
#include <libp2p/security/tls/tls_connection.hpp>
#include <libp2p/security/tls/tls_details.hpp>
#include <libp2p/security/tls/tls_errors.hpp>
#include <libp2p/security/tls/tls_connection.hpp>

namespace libp2p::security {

Expand Down
3 changes: 1 addition & 2 deletions test/acceptance/p2p/host/peer/tick_counter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ struct TickCounter {
auto actual_count = ticks_count.load();
ASSERT_EQ(actual_count, required_count) // NOLINT
<< "sending messages from <" << ci << "> client to <" << si
<< "> server "
<< "required messages count <" << required_count
<< "> server " << "required messages count <" << required_count
<< ">count doesn't match actual count <" << actual_count << ">";
}

Expand Down
5 changes: 2 additions & 3 deletions test/libp2p/multi/utils/protocol_list_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ TEST(ProtocolList, getByCode) {
TEST(ProtocolList, getProtocols) {
auto &protocols = ProtocolList::getProtocols();
static_assert(protocols.size() == ProtocolList::kProtocolsNum);
auto it = std::ranges::find_if(protocols, [](auto &p) {
return p.name == "ip4";
});
auto it =
std::ranges::find_if(protocols, [](auto &p) { return p.name == "ip4"; });
ASSERT_NE(it, protocols.end());
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,12 @@ struct InmemAddressRepository_Test : public ::testing::Test {

TEST_F(InmemAddressRepository_Test, GarbageCollection) {
// @given address repository that has 2 peers, and some addresses
ASSERT_OUTCOME_SUCCESS(db->addAddresses(p1, std::vector<Multiaddress>{ma1, ma2}, 10ms));
ASSERT_OUTCOME_SUCCESS(db->addAddresses(p1, std::vector<Multiaddress>{ma3, ma4}, 1000ms));
ASSERT_OUTCOME_SUCCESS(db->upsertAddresses(p2, std::vector<Multiaddress>{ma4}, 10ms));
ASSERT_OUTCOME_SUCCESS(
db->addAddresses(p1, std::vector<Multiaddress>{ma1, ma2}, 10ms));
ASSERT_OUTCOME_SUCCESS(
db->addAddresses(p1, std::vector<Multiaddress>{ma3, ma4}, 1000ms));
ASSERT_OUTCOME_SUCCESS(
db->upsertAddresses(p2, std::vector<Multiaddress>{ma4}, 10ms));

// @when no collectGarbage is called
{
Expand Down Expand Up @@ -135,8 +138,10 @@ TEST_F(InmemAddressRepository_Test, GarbageCollection) {
* @then ttl is updated, ma1 is not evicted
*/
TEST_F(InmemAddressRepository_Test, UpdateAddress) {
ASSERT_OUTCOME_SUCCESS(db->addAddresses(p1, std::vector<Multiaddress>{ma1}, 10ms));
ASSERT_OUTCOME_SUCCESS(db->upsertAddresses(p1, std::vector<Multiaddress>{ma1}, 1000ms));
ASSERT_OUTCOME_SUCCESS(
db->addAddresses(p1, std::vector<Multiaddress>{ma1}, 10ms));
ASSERT_OUTCOME_SUCCESS(
db->upsertAddresses(p1, std::vector<Multiaddress>{ma1}, 1000ms));

{
ASSERT_OUTCOME_SUCCESS(v, db->getAddresses(p1));
Expand All @@ -157,8 +162,10 @@ TEST_F(InmemAddressRepository_Test, UpdateAddress) {
* @then ttl of ma1 is not updated, ma1 is evicted. ma2 is inserted.
*/
TEST_F(InmemAddressRepository_Test, InsertAddress) {
ASSERT_OUTCOME_SUCCESS(db->addAddresses(p1, std::vector<Multiaddress>{ma1}, 10ms));
ASSERT_OUTCOME_SUCCESS(db->upsertAddresses(p1, std::vector<Multiaddress>{ma2}, 1000ms));
ASSERT_OUTCOME_SUCCESS(
db->addAddresses(p1, std::vector<Multiaddress>{ma1}, 10ms));
ASSERT_OUTCOME_SUCCESS(
db->upsertAddresses(p1, std::vector<Multiaddress>{ma2}, 1000ms));

{
ASSERT_OUTCOME_SUCCESS(v, db->getAddresses(p1));
Expand Down
Loading
Loading