|
| 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 |
0 commit comments