Last active
January 13, 2020 14:24
-
-
Save igor-egorov/176d75086a7bc4b614dd62cd3be83d9c to your computer and use it in GitHub Desktop.
libp2p secio debug files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | |
* SPDX-License-Identifier: Apache-2.0 | |
*/ | |
#include <chrono> | |
#include <iostream> | |
#include <memory> | |
#include <spdlog/sinks/stdout_color_sinks.h> | |
#include <libp2p/common/literals.hpp> | |
#include <libp2p/host/basic_host.hpp> | |
#include <libp2p/injector/host_injector.hpp> | |
#include <libp2p/protocol/echo.hpp> | |
#include <libp2p/security/plaintext.hpp> | |
#include <libp2p/security/secio.hpp> | |
int main(int argc, char *argv[]) { | |
// auto sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>(); | |
// auto logger = std::make_shared<spdlog::logger>("main", sink); | |
// spdlog::register_logger(logger); | |
// spdlog::set_default_logger(logger); | |
// spdlog::flush_on(spdlog::level::trace); | |
spdlog::set_level(spdlog::level::debug); | |
// logger->info("hello"); | |
using libp2p::crypto::Key; | |
using libp2p::crypto::KeyPair; | |
using libp2p::crypto::PrivateKey; | |
using libp2p::crypto::PublicKey; | |
using libp2p::common::operator""_unhex; | |
if (argc != 2) { | |
std::cerr << "please, provide an address of the server\n"; | |
std::exit(EXIT_FAILURE); | |
} | |
// create Echo protocol object - it implement the logic of both server and | |
// client, but in this example it's used as a client-only | |
libp2p::protocol::Echo echo{libp2p::protocol::EchoConfig{1}}; | |
// create a default Host via an injector | |
auto injector = libp2p::injector::makeHostInjector( | |
// libp2p::injector::useSecurityAdaptors<libp2p::security::Plaintext>() | |
libp2p::injector::useSecurityAdaptors<libp2p::security::Secio>()); | |
auto host = injector.create<std::shared_ptr<libp2p::Host>>(); | |
// create io_context - in fact, thing, which allows us to execute async | |
// operations | |
auto context = injector.create<std::shared_ptr<boost::asio::io_context>>(); | |
context->post([host{std::move(host)}, &echo, argv] { // NOLINT | |
auto server_ma_res = | |
libp2p::multi::Multiaddress::create(argv[1]); // NOLINT | |
if (!server_ma_res) { | |
std::cerr << "unable to create server multiaddress: " | |
<< server_ma_res.error().message() << std::endl; | |
std::exit(EXIT_FAILURE); | |
} | |
auto server_ma = std::move(server_ma_res.value()); | |
auto server_peer_id_str = server_ma.getPeerId(); | |
if (!server_peer_id_str) { | |
std::cerr << "unable to get peer id" << std::endl; | |
std::exit(EXIT_FAILURE); | |
} | |
auto server_peer_id_res = | |
libp2p::peer::PeerId::fromBase58(*server_peer_id_str); | |
if (!server_peer_id_res) { | |
std::cerr << "Unable to decode peer id from base 58: " | |
<< server_peer_id_res.error().message() << std::endl; | |
std::exit(EXIT_FAILURE); | |
} | |
auto server_peer_id = std::move(server_peer_id_res.value()); | |
auto peer_info = libp2p::peer::PeerInfo{server_peer_id, {server_ma}}; | |
// create Host object and open a stream through it | |
host->newStream( | |
peer_info, echo.getProtocolId(), [&echo](auto &&stream_res) { | |
if (!stream_res) { | |
std::cerr << "Cannot connect to server: " | |
<< stream_res.error().message() << std::endl; | |
std::exit(EXIT_FAILURE); | |
} | |
auto stream_p = std::move(stream_res.value()); | |
auto echo_client = echo.createClient(stream_p); | |
std::cout << "SENDING 'Hello from C++!'\n"; | |
echo_client->sendAnd( | |
"Hello from C++!\n", | |
[stream = std::move(stream_p)](auto &&response_result) { | |
std::cout << "RESPONSE " << response_result.value() | |
<< std::endl; | |
stream->close([](auto &&) { std::exit(EXIT_SUCCESS); }); | |
}); | |
}); | |
}); | |
// run the IO context | |
context->run_for(std::chrono::seconds(5)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace { | |
template <typename UIntVector> | |
void printBytesVector(std::string title, UIntVector vector) { | |
std::cout << std::endl << title << ":" << std::endl; | |
for (auto e : vector) { | |
std::cout << std::hex << std::setfill('0') << std::setw(2) << (unsigned)e | |
<< " "; | |
} | |
std::cout << std::dec << std::setw(0) << std::endl; | |
} | |
} // namespace |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | |
* SPDX-License-Identifier: Apache-2.0 | |
*/ | |
#include <chrono> | |
#include <iostream> | |
#include <memory> | |
#include <libp2p/common/literals.hpp> | |
#include <libp2p/host/basic_host.hpp> | |
#include <libp2p/injector/host_injector.hpp> | |
#include <libp2p/protocol/echo.hpp> | |
#include <libp2p/security/plaintext.hpp> | |
#include <libp2p/security/secio.hpp> | |
namespace { | |
template <typename UIntVector> | |
void printBytesVector(std::string title, UIntVector vector) { | |
std::cout << std::endl << title << ":" << std::endl; | |
for (auto e : vector) { | |
std::cout << std::hex << std::setfill('0') << std::setw(2) << (unsigned)e | |
<< " "; | |
} | |
std::cout << std::dec << std::setw(0) << std::endl; | |
} | |
} // namespace | |
int main() { | |
spdlog::set_level(spdlog::level::debug); | |
using libp2p::crypto::Key; | |
using libp2p::crypto::KeyPair; | |
using libp2p::crypto::PrivateKey; | |
using libp2p::crypto::PublicKey; | |
using libp2p::common::operator""_unhex; | |
// this keypair generates a PeerId | |
// "12D3KooWLs7RC93EGXZzn9YdKyZYYx3f9UjTLYNX1reThpCkFb83" | |
KeyPair keypair{PublicKey{{Key::Type::Ed25519, | |
"48453469c62f4885373099421a7365520b5ff" | |
"b0d93726c124166be4b81d852e6"_unhex}}, | |
PrivateKey{{Key::Type::Ed25519, | |
"4a9361c525840f7086b893d584ebbe475b4ec" | |
"7069951d2e897e8bceb0a3f35ce"_unhex}}}; | |
// create a default Host via an injector, overriding a random-generated | |
// keypair with ours | |
auto injector = libp2p::injector::makeHostInjector( | |
libp2p::injector::useKeyPair(keypair) | |
// libp2p::injector::useSecurityAdaptors<libp2p::security::Plaintext>() | |
// libp2p::injector::useSecurityAdaptors<libp2p::security::Secio>() | |
); | |
auto host = injector.create<std::shared_ptr<libp2p::Host>>(); | |
// auto crypto = | |
// injector.create<std::shared_ptr<libp2p::crypto::CryptoProvider>>(); | |
// | |
// auto pk = crypto->derivePublicKey(keypair.privateKey); | |
// printBytesVector("original public key", keypair.publicKey.data); | |
// printBytesVector("derived public key", pk.value().data); | |
// set a handler for Echo protocol | |
libp2p::protocol::Echo echo{libp2p::protocol::EchoConfig{1}}; | |
host->setProtocolHandler( | |
echo.getProtocolId(), | |
[&echo](std::shared_ptr<libp2p::connection::Stream> received_stream) { | |
echo.handle(std::move(received_stream)); | |
}); | |
// launch a Listener part of the Host | |
auto context = injector.create<std::shared_ptr<boost::asio::io_context>>(); | |
context->post([host{std::move(host)}] { | |
auto ma = | |
libp2p::multi::Multiaddress::create("/ip4/127.0.0.1/tcp/40010").value(); | |
auto listen_res = host->listen(ma); | |
if (!listen_res) { | |
std::cerr << "host cannot listen the given multiaddress: " | |
<< listen_res.error().message() << "\n"; | |
std::exit(EXIT_FAILURE); | |
} | |
host->start(); | |
std::cout << "Server started\nListening on: " << ma.getStringAddress() | |
<< "\nPeer id: " << host->getPeerInfo().id.toBase58() << "\n"; | |
}); | |
// run the IO context | |
context->run(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <typename String> | |
struct CallTracer { | |
CallTracer(libp2p::common::Logger logger, String name) | |
: log(std::move(logger)), function_name(std::move(name)) { | |
log->trace("{} {} {}", "//", function_name, "+"); | |
} | |
~CallTracer() { | |
log->trace("{} {} {}", "\\\\", function_name, "-"); | |
} | |
libp2p::common::Logger log; | |
String function_name; | |
}; | |
#define TRACE_L(logger) \ | |
CallTracer call_tracer_((logger), BOOST_CURRENT_FUNCTION) | |
#define TRACE TRACE_L(log_) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment