Skip to content

Commit 56df7f4

Browse files
authored
Kad (#32)
* cmake hunter config fix for gtest and gcc-9 * kademlia DHT PoC * kademlia DHT PoC-trying to fix dead streams * kad dirs restructure * kademlia POC v2 * todos and ci fixes
1 parent 9109752 commit 56df7f4

File tree

73 files changed

+3316
-509
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+3316
-509
lines changed

.clang-format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ BinPackArguments: true
88
BinPackParameters: true
99
AllowShortFunctionsOnASingleLine: Empty
1010
AllowShortIfStatementsOnASingleLine: false
11+
IncludeBlocks: Preserve

.clang-tidy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
Checks: '-*,clang-analyzer-*,clang-diagnostic-*,readability-*,modernize-*,-modernize-use-trailing-return-type,boost-*,bugprone-*,cppcoreguidelines-*,google-*,hicpp-*,performance-*,readability-*,-google-readability-namespace-comments,-readability-inconsistent-declaration-parameter-name,-readability-braces-around-statements,-hicpp-signed-bitwise,-google-runtime-references,-cppcoreguidelines-avoid-magic-numbers,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-readability-magic-numbers,-hicpp-explicit-conversions,-hicpp-uppercase-literal-suffix,-readability-uppercase-literal-suffix,-hicpp-no-array-decay,-hicpp-special-member-functions,-bugprone-narrowing-conversions,-modernize-use-nodiscard,-google-readability-braces-around-statements,-hicpp-braces-around-statements,-bugprone-suspicious-semicolon,-readability-named-parameter,-hicpp-named-parameter,-readability-identifier-naming'
3-
WarningsAsErrors: 'modernize-*,cppcoreguidelines-*,boost-*,performance-*,google-build-using-namespace,readability-else-after-return,google-readability-todo'
3+
WarningsAsErrors: 'modernize-*,cppcoreguidelines-*,boost-*,google-build-using-namespace,readability-else-after-return,google-readability-todo'
44
HeaderFilterRegex: 'libp2p/.*\.hpp'
55
AnalyzeTemporaryDtors: false
66
FormatStyle: .clang-format

example/02-kad/CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
6+
add_executable(kad_peer_discovery_example
7+
kad_peer_discovery_example.cpp
8+
factory.cpp
9+
)
10+
11+
target_link_libraries(kad_peer_discovery_example
12+
p2p_basic_host
13+
p2p_default_network
14+
p2p_peer_repository
15+
p2p_inmem_address_repository
16+
p2p_inmem_key_repository
17+
p2p_inmem_protocol_repository
18+
p2p_literals
19+
p2p_kad
20+
)

example/02-kad/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Example Libp2p Kad
2+
## General description
3+
4+
kademlia

example/02-kad/factory.cpp

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include <boost/di.hpp>
7+
#include "boost/di/extension/scopes/shared.hpp"
8+
9+
// implementations
10+
#include <libp2p/crypto/crypto_provider/crypto_provider_impl.hpp>
11+
#include <libp2p/crypto/ed25519_provider/ed25519_provider_impl.hpp>
12+
#include <libp2p/crypto/key_marshaller/key_marshaller_impl.hpp>
13+
#include <libp2p/crypto/key_validator/key_validator_impl.hpp>
14+
#include <libp2p/crypto/random_generator/boost_generator.hpp>
15+
#include <libp2p/host/basic_host.hpp>
16+
#include <libp2p/muxer/mplex.hpp>
17+
#include <libp2p/muxer/yamux.hpp>
18+
#include <libp2p/network/impl/connection_manager_impl.hpp>
19+
#include <libp2p/network/impl/dialer_impl.hpp>
20+
#include <libp2p/network/impl/listener_manager_impl.hpp>
21+
#include <libp2p/network/impl/network_impl.hpp>
22+
#include <libp2p/network/impl/router_impl.hpp>
23+
#include <libp2p/network/impl/transport_manager_impl.hpp>
24+
#include <libp2p/peer/address_repository/inmem_address_repository.hpp>
25+
#include <libp2p/peer/impl/identity_manager_impl.hpp>
26+
#include <libp2p/peer/impl/peer_repository_impl.hpp>
27+
#include <libp2p/peer/key_repository/inmem_key_repository.hpp>
28+
#include <libp2p/peer/protocol_repository/inmem_protocol_repository.hpp>
29+
#include <libp2p/protocol_muxer/multiselect.hpp>
30+
#include <libp2p/security/plaintext.hpp>
31+
#include <libp2p/security/plaintext/exchange_message_marshaller_impl.hpp>
32+
#include <libp2p/transport/impl/upgrader_impl.hpp>
33+
#include <libp2p/transport/tcp.hpp>
34+
35+
#include <libp2p/protocol/kademlia/impl/routing_table_impl.hpp>
36+
37+
#include <iostream>
38+
39+
#include "factory.hpp"
40+
41+
namespace libp2p::protocol::kademlia::example {
42+
43+
std::optional<libp2p::peer::PeerInfo> str2peerInfo(const std::string &str) {
44+
using R = std::optional<libp2p::peer::PeerInfo>;
45+
46+
auto server_ma_res = libp2p::multi::Multiaddress::create(str);
47+
if (!server_ma_res) {
48+
std::cerr << "unable to create server multiaddress: "
49+
<< server_ma_res.error().message() << std::endl;
50+
return R();
51+
}
52+
auto server_ma = std::move(server_ma_res.value());
53+
54+
auto server_peer_id_str = server_ma.getPeerId();
55+
if (!server_peer_id_str) {
56+
std::cerr << "unable to get peer id" << std::endl;
57+
return R();
58+
}
59+
60+
auto server_peer_id_res =
61+
libp2p::peer::PeerId::fromBase58(*server_peer_id_str);
62+
if (!server_peer_id_res) {
63+
std::cerr << "Unable to decode peer id from base 58: "
64+
<< server_peer_id_res.error().message() << std::endl;
65+
return R();
66+
}
67+
68+
return libp2p::peer::PeerInfo{server_peer_id_res.value(), {server_ma}};
69+
}
70+
71+
namespace {
72+
template <typename... Ts>
73+
auto makeInjector(Ts &&... args) {
74+
using namespace boost; // NOLINT
75+
76+
auto csprng = std::make_shared<crypto::random::BoostRandomGenerator>();
77+
auto ed25519_provider =
78+
std::make_shared<crypto::ed25519::Ed25519ProviderImpl>();
79+
auto crypto_provider = std::make_shared<crypto::CryptoProviderImpl>(
80+
csprng, ed25519_provider);
81+
auto validator = std::make_shared<crypto::validator::KeyValidatorImpl>(
82+
crypto_provider);
83+
84+
// assume no error here. otherwise... just blow up executable
85+
auto keypair =
86+
crypto_provider->generateKeys(crypto::Key::Type::Ed25519).value();
87+
88+
// clang-format off
89+
return di::make_injector<boost::di::extension::shared_config>(
90+
di::bind<crypto::CryptoProvider>().to(crypto_provider)[boost::di::override],
91+
di::bind<crypto::KeyPair>().template to(std::move(keypair)),
92+
di::bind<crypto::random::CSPRNG>().template to(std::move(csprng)),
93+
di::bind<crypto::marshaller::KeyMarshaller>().template to<crypto::marshaller::KeyMarshallerImpl>(),
94+
di::bind<peer::IdentityManager>().template to<peer::IdentityManagerImpl>(),
95+
di::bind<crypto::validator::KeyValidator>().template to<crypto::validator::KeyValidatorImpl>(),
96+
di::bind<security::plaintext::ExchangeMessageMarshaller>().template to<security::plaintext::ExchangeMessageMarshallerImpl>(),
97+
98+
// internal
99+
di::bind<network::Router>().template to<network::RouterImpl>(),
100+
di::bind<network::ConnectionManager>().template to<network::ConnectionManagerImpl>(),
101+
di::bind<network::ListenerManager>().template to<network::ListenerManagerImpl>(),
102+
di::bind<network::Dialer>().template to<network::DialerImpl>(),
103+
di::bind<network::Network>().template to<network::NetworkImpl>(),
104+
di::bind<network::TransportManager>().template to<network::TransportManagerImpl>(),
105+
di::bind<transport::Upgrader>().template to<transport::UpgraderImpl>(),
106+
di::bind<protocol_muxer::ProtocolMuxer>().template to<protocol_muxer::Multiselect>(),
107+
108+
// default adaptors
109+
di::bind<security::SecurityAdaptor *[]>().template to<security::Plaintext>(), // NOLINT
110+
di::bind<muxer::MuxerAdaptor *[]>().template to<muxer::Yamux, muxer::Mplex>(), // NOLINT
111+
di::bind<transport::TransportAdaptor *[]>().template to<transport::TcpTransport>(), // NOLINT
112+
113+
di::bind<event::Bus>.template to<event::Bus>(),
114+
di::bind<Host>.template to<host::BasicHost>(),
115+
116+
di::bind<muxer::MuxedConnectionConfig>.to(muxer::MuxedConnectionConfig()),
117+
118+
// repositories
119+
di::bind<peer::PeerRepository>.template to<peer::PeerRepositoryImpl>(),
120+
di::bind<peer::AddressRepository>.template to<peer::InmemAddressRepository>(),
121+
di::bind<peer::KeyRepository>.template to<peer::InmemKeyRepository>(),
122+
di::bind<peer::ProtocolRepository>.template to<peer::InmemProtocolRepository>(),
123+
124+
// user-defined overrides...
125+
std::forward<decltype(args)>(args)...
126+
);
127+
// clang-format on
128+
}
129+
} // namespace
130+
131+
void createPerHostObjects(PerHostObjects &objects, const KademliaConfig& conf) {
132+
auto injector = makeInjector(boost::di::bind<boost::asio::io_context>.to(
133+
createIOContext())[boost::di::override]);
134+
135+
objects.host = injector.create<std::shared_ptr<libp2p::Host>>();
136+
objects.key_gen =
137+
injector.create<std::shared_ptr<libp2p::crypto::CryptoProvider>>();
138+
objects.key_marshaller = injector.create<
139+
std::shared_ptr<libp2p::crypto::marshaller::KeyMarshaller>>();
140+
objects.routing_table =
141+
std::make_shared<RoutingTableImpl>(
142+
injector.create<std::shared_ptr<peer::IdentityManager>>(),
143+
injector.create<std::shared_ptr<event::Bus>>(),
144+
conf
145+
);
146+
}
147+
148+
std::shared_ptr<boost::asio::io_context> createIOContext() {
149+
static std::shared_ptr<boost::asio::io_context> c =
150+
std::make_shared<boost::asio::io_context>();
151+
return c;
152+
}
153+
154+
} // namespace libp2p::kad_example

example/02-kad/factory.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef LIBP2P_KAD_EXAMPLE_FACTORY_HPP
7+
#define LIBP2P_KAD_EXAMPLE_FACTORY_HPP
8+
9+
#include <libp2p/crypto/crypto_provider.hpp>
10+
#include <libp2p/crypto/key_marshaller.hpp>
11+
#include <libp2p/host/host.hpp>
12+
#include <memory>
13+
14+
namespace libp2p::protocol::kademlia::example {
15+
std::shared_ptr<boost::asio::io_context> createIOContext();
16+
17+
struct PerHostObjects {
18+
std::shared_ptr<libp2p::Host> host;
19+
std::shared_ptr<libp2p::protocol::kademlia::RoutingTable> routing_table;
20+
std::shared_ptr<libp2p::crypto::CryptoProvider> key_gen;
21+
std::shared_ptr<crypto::marshaller::KeyMarshaller> key_marshaller;
22+
};
23+
24+
void createPerHostObjects(PerHostObjects &objects, const KademliaConfig& conf);
25+
26+
std::optional<libp2p::peer::PeerInfo> str2peerInfo(const std::string &str);
27+
28+
} // namespace libp2p::protocol::kademlia::example
29+
30+
#endif // LIBP2P_KAD_EXAMPLE_FACTORY_HPP

0 commit comments

Comments
 (0)