Skip to content

Commit 4673d4c

Browse files
art-gorturuslan
andauthored
identify protocol signals (#69)
* identify protocol signals * clang-patch Co-authored-by: turuslan <[email protected]>
1 parent ba8aa59 commit 4673d4c

File tree

10 files changed

+61
-9
lines changed

10 files changed

+61
-9
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ jobs:
5353
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # has to be included to access other secrets
5454
GITHUB_HUNTER_USERNAME: ${{ secrets.GITHUB_HUNTER_USERNAME }}
5555
GITHUB_HUNTER_TOKEN: ${{ secrets.GITHUB_HUNTER_TOKEN }}
56-
run: cmake . -GNinja -Bbuild
56+
run: cmake . -GNinja -Bbuild `housekeeping/clang-patch/apply.sh`
5757
- name: build
5858
run: cmake --build build -- -j4
5959
- name: test

.github/workflows/clang-tidy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
LLVM_VER=$(ls -1 ${LLVM_DIR})
3535
export LLVM_ROOT=${LLVM_DIR}/${LLVM_VER}
3636
export PATH=${LLVM_ROOT}/bin:${LLVM_ROOT}/share/clang:${PATH}
37-
cmake . -GNinja -Bbuild
37+
cmake . -GNinja -Bbuild `housekeeping/clang-patch/apply.sh`
3838
cmake --build build --target generated
3939
housekeeping/clang-tidy.sh build
4040

CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ include(cmake/san.cmake)
3232
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
3333

3434
option(TESTING "Build tests" ON)
35+
option(EXAMPLES "Build examples" ON)
3536
option(CLANG_FORMAT "Enable clang-format target" ON)
3637
option(CLANG_TIDY "Enable clang-tidy checks during compilation" OFF)
3738
option(COVERAGE "Enable generation of coverage info" OFF)
@@ -93,7 +94,10 @@ include_directories(
9394
)
9495

9596
add_subdirectory(src)
96-
add_subdirectory(example)
97+
98+
if(EXAMPLES)
99+
add_subdirectory(example)
100+
endif()
97101

98102
if(TESTING)
99103
enable_testing()

housekeeping/clang-patch/apply.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
set -e
3+
if ([ "$RUNNER_OS" = "macOS" ] && which clang++ && ! clang++ -fcoalesce-templates --version) >> null; then
4+
dir=$(cd `dirname $0` && pwd)
5+
exe="$dir/clang-patch"
6+
cc -o $exe "$dir/clang-patch.c"
7+
echo "-DCMAKE_CXX_COMPILER=$exe"
8+
fi
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <unistd.h>
2+
#include <string.h>
3+
4+
int main(int argc, char *argv[]) {
5+
char **oa = argv;
6+
for (char **ia = argv; *ia; ++ia) {
7+
if (strcmp(*ia, "-fcoalesce-templates") != 0) {
8+
*oa = *ia;
9+
++oa;
10+
}
11+
}
12+
*oa = NULL;
13+
execv("/usr/bin/clang++", argv);
14+
}

include/libp2p/protocol/identify/identify.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ namespace libp2p::protocol {
3737

3838
~Identify() override = default;
3939

40+
boost::signals2::connection onIdentifyReceived(
41+
const std::function<IdentifyMessageProcessor::IdentifyCallback> &cb);
42+
4043
/**
4144
* Get addresses other peers reported we have dialed from
4245
* @return set of addresses

include/libp2p/protocol/identify/identify_msg_processor.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
#include <libp2p/host/host.hpp>
1818
#include <libp2p/multi/multiaddress.hpp>
1919
#include <libp2p/network/connection_manager.hpp>
20+
#include <libp2p/outcome/outcome.hpp>
2021
#include <libp2p/peer/identity_manager.hpp>
2122
#include <libp2p/peer/peer_id.hpp>
2223
#include <libp2p/protocol/identify/observed_addresses.hpp>
23-
#include <libp2p/outcome/outcome.hpp>
2424

2525
namespace identify::pb {
2626
class Identify;
@@ -35,11 +35,16 @@ namespace libp2p::protocol {
3535
using StreamSPtr = std::shared_ptr<connection::Stream>;
3636

3737
public:
38+
using IdentifyCallback = void(const peer::PeerId &);
39+
3840
IdentifyMessageProcessor(
3941
Host &host, network::ConnectionManager &conn_manager,
4042
peer::IdentityManager &identity_manager,
4143
std::shared_ptr<crypto::marshaller::KeyMarshaller> key_marshaller);
4244

45+
boost::signals2::connection onIdentifyReceived(
46+
const std::function<IdentifyCallback> &cb);
47+
4348
/**
4449
* Send an Identify message over the provided stream
4550
* @param stream to be identified over
@@ -131,6 +136,7 @@ namespace libp2p::protocol {
131136
peer::IdentityManager &identity_manager_;
132137
std::shared_ptr<crypto::marshaller::KeyMarshaller> key_marshaller_;
133138
ObservedAddresses observed_addresses_;
139+
boost::signals2::signal<IdentifyCallback> signal_identify_received_;
134140

135141
common::Logger log_ = common::createLogger("IdentifyMsgProcessor");
136142
};

src/peer/address_repository/inmem_address_repository.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace libp2p::peer {
1919
it = db_.find(p);
2020
}
2121

22-
for (auto &m : ma) {
22+
for (const auto &m : ma) {
2323
it->second->insert({m, Clock::now() + ttl});
2424
signal_added_(p, m);
2525
}
@@ -38,7 +38,13 @@ namespace libp2p::peer {
3838

3939
auto expires_in = Clock::now() + ttl;
4040
for (const auto &m : ma) {
41-
(*it->second)[m] = expires_in;
41+
auto item_it = it->second->find(m);
42+
if (item_it == it->second->end()) {
43+
it->second->insert({m, expires_in});
44+
signal_added_(p, m);
45+
} else {
46+
item_it->second = expires_in;
47+
}
4248
}
4349

4450
return outcome::success();

src/protocol/identify/identify.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ namespace libp2p::protocol {
2121
BOOST_ASSERT(msg_processor_);
2222
}
2323

24+
boost::signals2::connection Identify::onIdentifyReceived(
25+
const std::function<IdentifyMessageProcessor::IdentifyCallback> &cb) {
26+
return msg_processor_->onIdentifyReceived(cb);
27+
}
28+
2429
std::vector<multi::Multiaddress> Identify::getAllObservedAddresses() const {
2530
return msg_processor_->getObservedAddresses().getAllAddresses();
2631
}

src/protocol/identify/identify_msg_processor.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ namespace libp2p::protocol {
2626
BOOST_ASSERT(key_marshaller_);
2727
}
2828

29+
boost::signals2::connection IdentifyMessageProcessor::onIdentifyReceived(
30+
const std::function<IdentifyCallback> &cb) {
31+
return signal_identify_received_.connect(cb);
32+
}
33+
2934
void IdentifyMessageProcessor::sendIdentify(StreamSPtr stream) {
3035
identify::pb::Identify msg;
3136

@@ -170,6 +175,8 @@ namespace libp2p::protocol {
170175
addresses.push_back(addr);
171176
}
172177
consumeListenAddresses(addresses, peer_id);
178+
179+
signal_identify_received_(peer_id);
173180
}
174181

175182
boost::optional<peer::PeerId> IdentifyMessageProcessor::consumePublicKey(
@@ -326,15 +333,14 @@ namespace libp2p::protocol {
326333
// invalidate previously known addresses of that peer
327334
auto add_res = addr_repo.updateAddresses(peer_id, peer::ttl::kTransient);
328335
if (!add_res) {
329-
log_->error("cannot update listen addresses of the peer {}: {}",
336+
log_->debug("cannot update listen addresses of the peer {}: {}",
330337
peer_id.toBase58(), add_res.error().message());
331338
}
332339

333340
// memorize the addresses
334341
auto addresses = addr_repo.getAddresses(peer_id);
335342
if (!addresses) {
336-
log_->error("can not get addresses for peer {}", peer_id.toBase58());
337-
return;
343+
log_->debug("can not get addresses for peer {}", peer_id.toBase58());
338344
}
339345

340346
switch (conn_manager_.connectedness({peer_id, addresses.value()})) {

0 commit comments

Comments
 (0)