Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: ws/wss connection support #1483

Merged
merged 10 commits into from
Feb 13, 2023
76 changes: 30 additions & 46 deletions cmake/Hunter/config.cmake
Original file line number Diff line number Diff line change
@@ -1,55 +1,39 @@
## Template for a custom hunter configuration
# Useful when there is a need for a non-default version or arguments of a dependency,
# or when a project not registered in soramitsu-hunter should be added.
# Template for a custom hunter configuration Useful when there is a need for a
# non-default version or arguments of a dependency, or when a project not
# registered in soramitsu-hunter should be added.
#
# hunter_config(
# package-name
# VERSION 0.0.0-package-version
# CMAKE_ARGS "CMAKE_VARIABLE=value"
# )
# hunter_config( package-name VERSION 0.0.0-package-version CMAKE_ARGS
# "CMAKE_VARIABLE=value" )
#
# hunter_config(
# package-name
# URL https://repo/archive.zip
# SHA1 1234567890abcdef1234567890abcdef12345678
# CMAKE_ARGS "CMAKE_VARIABLE=value"
# )


hunter_config(
backward-cpp
URL https://github.com/bombela/backward-cpp/archive/refs/tags/v1.6.zip
SHA1 93c4c843fc9308e62ac462459077d87dc6dd9885
CMAKE_ARGS BACKWARD_TESTS=OFF
KEEP_PACKAGE_SOURCES
)
# hunter_config( package-name URL https://repo/archive.zip SHA1
# 1234567890abcdef1234567890abcdef12345678 CMAKE_ARGS "CMAKE_VARIABLE=value" )

hunter_config(
soralog
VERSION 0.1.5
KEEP_PACKAGE_SOURCES
)
backward-cpp
URL
https://github.com/bombela/backward-cpp/archive/refs/tags/v1.6.zip
SHA1
93c4c843fc9308e62ac462459077d87dc6dd9885
CMAKE_ARGS
BACKWARD_TESTS=OFF
KEEP_PACKAGE_SOURCES)

hunter_config(soralog VERSION 0.1.5 KEEP_PACKAGE_SOURCES)
hunter_config(
libp2p
VERSION 0.1.8
KEEP_PACKAGE_SOURCES
)
libp2p
VERSION 0.1.10
KEEP_PACKAGE_SOURCES)

hunter_config(
rocksdb
VERSION 7.2.2
CMAKE_ARGS WITH_GFLAGS=OFF
)
hunter_config(rocksdb VERSION 7.2.2 CMAKE_ARGS WITH_GFLAGS=OFF)

hunter_config(
wavm
VERSION 1.0.6
CMAKE_ARGS
TESTING=OFF
WAVM_ENABLE_FUZZ_TARGETS=OFF
WAVM_ENABLE_STATIC_LINKING=ON
WAVM_BUILD_EXAMPLES=OFF
WAVM_BUILD_TESTS=OFF
KEEP_PACKAGE_SOURCES
)
wavm
VERSION
1.0.6
CMAKE_ARGS
TESTING=OFF
WAVM_ENABLE_FUZZ_TARGETS=OFF
WAVM_ENABLE_STATIC_LINKING=ON
WAVM_BUILD_EXAMPLES=OFF
WAVM_BUILD_TESTS=OFF
KEEP_PACKAGE_SOURCES)
4 changes: 2 additions & 2 deletions cmake/Hunter/hunter-gate-url.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
HunterGate(
URL "https://github.com/soramitsu/soramitsu-hunter/archive/refs/tags/v0.23.257-soramitsu38.zip"
SHA1 "750757012e366941c9c677d8acbe5eebc6f71a63"
URL "https://github.com/soramitsu/soramitsu-hunter/archive/refs/tags/v0.23.257-soramitsu40.zip"
SHA1 "5e27e3d5ef155b895fd38b97936a87fdd3ea0529"
LOCAL
)
16 changes: 14 additions & 2 deletions core/application/impl/app_configuration_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ namespace kagome::application {
save_node_key_(false),
is_telemetry_enabled_(true),
p2p_port_(def_p2p_port),
p2p_port_explicitly_defined_(false),
max_blocks_in_response_(kAbsolutMaxBlocksInResponse),
rpc_http_host_(def_rpc_http_host),
rpc_ws_host_(def_rpc_ws_host),
Expand Down Expand Up @@ -1001,7 +1002,10 @@ namespace kagome::application {
find_argument<bool>(
vm, "save-node-key", [&](bool val) { save_node_key_ = val; });

find_argument<uint16_t>(vm, "port", [&](uint16_t val) { p2p_port_ = val; });
find_argument<uint16_t>(vm, "port", [&](uint16_t val) {
p2p_port_ = val;
p2p_port_explicitly_defined_ = true;
});

auto parse_multiaddrs =
[&](const std::string &param_name,
Expand Down Expand Up @@ -1031,6 +1035,14 @@ namespace kagome::application {
if (not parse_multiaddrs("listen-addr", listen_addresses_)) {
return false; // just proxy erroneous case to the top level
}

if (p2p_port_explicitly_defined_ and not listen_addresses_.empty()) {
SL_ERROR(logger_,
"Port and listen address must not be defined simultaneously; "
"Leave only one of them");
return false;
}

if (not parse_multiaddrs("public-addr", public_addresses_)) {
return false; // just proxy erroneous case to the top level
}
Expand All @@ -1046,7 +1058,7 @@ namespace kagome::application {
public_addresses_ = listen_addresses_;
}

if (0 != p2p_port_ and listen_addresses_.empty()) {
if (p2p_port_explicitly_defined_ and listen_addresses_.empty()) {
// IPv6
{
auto ma_res = libp2p::multi::Multiaddress::create(
Expand Down
1 change: 1 addition & 0 deletions core/application/impl/app_configuration_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ namespace kagome::application {
std::vector<telemetry::TelemetryEndpoint> telemetry_endpoints_;
bool is_telemetry_enabled_;
uint16_t p2p_port_;
bool p2p_port_explicitly_defined_ = false;
boost::asio::ip::tcp::endpoint rpc_http_endpoint_;
boost::asio::ip::tcp::endpoint rpc_ws_endpoint_;
boost::asio::ip::tcp::endpoint openmetrics_http_endpoint_;
Expand Down
16 changes: 8 additions & 8 deletions core/network/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ namespace kagome::network {
/// Lowest version we support
static constexpr uint32_t MIN_VERSION = 3;

const libp2p::peer::Protocol kStateProtocol = "/{}/state/2";
const libp2p::peer::Protocol kSyncProtocol = "/{}/sync/2";
const libp2p::peer::Protocol kPropagateTransactionsProtocol =
const libp2p::peer::ProtocolName kStateProtocol = "/{}/state/2";
const libp2p::peer::ProtocolName kSyncProtocol = "/{}/sync/2";
const libp2p::peer::ProtocolName kPropagateTransactionsProtocol =
"/{}/transactions/1";
const libp2p::peer::Protocol kBlockAnnouncesProtocol =
const libp2p::peer::ProtocolName kBlockAnnouncesProtocol =
"/{}/block-announces/1";
const libp2p::peer::Protocol kGrandpaProtocol = "/{}/grandpa/1";
const libp2p::peer::Protocol kCollationProtocol{"/{}/collation/1"};
const libp2p::peer::Protocol kValidationProtocol{"/{}/validation/1"};
const libp2p::peer::Protocol kReqCollationProtocol{"/{}/req_collation/1"};
const libp2p::peer::ProtocolName kGrandpaProtocol = "/{}/grandpa/1";
const libp2p::peer::ProtocolName kCollationProtocol{"/{}/collation/1"};
const libp2p::peer::ProtocolName kValidationProtocol{"/{}/validation/1"};
const libp2p::peer::ProtocolName kReqCollationProtocol{"/{}/req_collation/1"};

template <typename... Args>
libp2p::StreamProtocols make_protocols(std::string_view format,
Expand Down
2 changes: 1 addition & 1 deletion core/network/impl/protocols/protocol_base_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace kagome::network {
using Stream = libp2p::connection::Stream;
using Protocol = libp2p::peer::Protocol;
using Protocol = libp2p::peer::ProtocolName;
using Protocols = libp2p::StreamProtocols;
using PeerId = libp2p::peer::PeerId;
using PeerInfo = libp2p::peer::PeerInfo;
Expand Down
2 changes: 1 addition & 1 deletion core/network/impl/protocols/state_protocol_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
namespace kagome::network {

using Stream = libp2p::connection::Stream;
using Protocol = libp2p::peer::Protocol;
using Protocol = libp2p::peer::ProtocolName;
using PeerId = libp2p::peer::PeerId;
using PeerInfo = libp2p::peer::PeerInfo;

Expand Down
30 changes: 19 additions & 11 deletions core/network/impl/stream_engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ namespace kagome::network {

template <typename Rng = std::mt19937>
struct RandomGossipStrategy {
RandomGossipStrategy(const size_t candidates_num, const size_t lucky_peers_num)
RandomGossipStrategy(const size_t candidates_num,
const size_t lucky_peers_num)
: candidates_num_{candidates_num} {
auto lucky_rate = lucky_peers_num > 0
? static_cast<double>(lucky_peers_num)
Expand Down Expand Up @@ -61,7 +62,7 @@ namespace kagome::network {
struct StreamEngine final : std::enable_shared_from_this<StreamEngine> {
using PeerInfo = libp2p::peer::PeerInfo;
using PeerId = libp2p::peer::PeerId;
using Protocol = libp2p::peer::Protocol;
using Protocol = libp2p::peer::ProtocolName;
using Stream = libp2p::connection::Stream;
using StreamEnginePtr = std::shared_ptr<StreamEngine>;

Expand All @@ -82,7 +83,8 @@ namespace kagome::network {
StreamEngine &operator=(StreamEngine &&) = delete;

~StreamEngine() = default;
explicit StreamEngine(std::shared_ptr<ReputationRepository> reputation_repository)
explicit StreamEngine(
std::shared_ptr<ReputationRepository> reputation_repository)
: reputation_repository_(std::move(reputation_repository)),
logger_{log::createLogger("StreamEngine", "network")} {}

Expand Down Expand Up @@ -155,7 +157,8 @@ namespace kagome::network {
const std::shared_ptr<ProtocolBase> &protocol,
const std::shared_ptr<T> &msg,
const std::function<bool(const PeerId &peer_id)> &predicate,
const std::function<void(libp2p::connection::Stream&)> on_send = [](auto&){}) {
const std::function<void(libp2p::connection::Stream &)> on_send =
[](auto &) {}) {
BOOST_ASSERT(msg != nullptr);
BOOST_ASSERT(protocol != nullptr);

Expand All @@ -166,7 +169,11 @@ namespace kagome::network {
send(peer_id, protocol, descr.outgoing.stream, msg);
on_send(*descr.outgoing.stream);
} else {
descr.deferred_messages.push_back([weak_self = weak_from_this(), msg, peer_id, protocol, on_send](auto stream) {
descr.deferred_messages.push_back([weak_self = weak_from_this(),
msg,
peer_id,
protocol,
on_send](auto stream) {
if (auto self = weak_self.lock()) {
self->send(peer_id, protocol, stream, msg);
on_send(*stream);
Expand Down Expand Up @@ -295,7 +302,8 @@ namespace kagome::network {
}
};

using ProtocolMap = std::map<std::shared_ptr<ProtocolBase>, struct ProtocolDescr>;
using ProtocolMap =
std::map<std::shared_ptr<ProtocolBase>, struct ProtocolDescr>;
using PeerMap = std::map<PeerId, ProtocolMap>;

void uploadStream(std::shared_ptr<Stream> &dst,
Expand Down Expand Up @@ -344,9 +352,9 @@ namespace kagome::network {

template <typename PM, typename F>
static void forPeerProtocol(PeerId const &peer_id,
PM &streams,
std::shared_ptr<ProtocolBase> const &protocol,
F &&f) {
PM &streams,
std::shared_ptr<ProtocolBase> const &protocol,
F &&f) {
if (auto it = streams.find(peer_id); it != streams.end()) {
forProtocol(it->second, protocol, [&](auto &descr) {
std::forward<F>(f)(it->second, descr);
Expand All @@ -357,8 +365,8 @@ namespace kagome::network {
[[maybe_unused]] void dump(std::string_view msg);

void openOutgoingStream(PeerId const &peer_id,
std::shared_ptr<ProtocolBase> const &protocol,
ProtocolDescr &descr);
std::shared_ptr<ProtocolBase> const &protocol,
ProtocolDescr &descr);

template <typename T>
void updateStream(const PeerId &peer_id,
Expand Down
2 changes: 1 addition & 1 deletion core/network/protocol_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace kagome::network {

using Stream = libp2p::connection::Stream;
using PeerInfo = libp2p::peer::PeerInfo;
using Protocol = libp2p::peer::Protocol;
using Protocol = libp2p::peer::ProtocolName;
using PeerId = libp2p::peer::PeerId;
using Protocols = libp2p::StreamProtocols;
using ProtocolName = std::string;
Expand Down
2 changes: 1 addition & 1 deletion core/network/protocols/state_protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
namespace kagome::network {

using Stream = libp2p::connection::Stream;
using Protocol = libp2p::peer::Protocol;
using Protocol = libp2p::peer::ProtocolName;
using PeerId = libp2p::peer::PeerId;
using PeerInfo = libp2p::peer::PeerInfo;

Expand Down
2 changes: 1 addition & 1 deletion core/network/protocols/sync_protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
namespace kagome::network {

using Stream = libp2p::connection::Stream;
using Protocol = libp2p::peer::Protocol;
using Protocol = libp2p::peer::ProtocolName;
using PeerId = libp2p::peer::PeerId;
using PeerInfo = libp2p::peer::PeerInfo;

Expand Down
4 changes: 2 additions & 2 deletions core/network/rpc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ namespace kagome::network {
template <typename Request, typename Response>
static void write(libp2p::Host &host,
const libp2p::peer::PeerInfo &peer_info,
const libp2p::peer::Protocol &protocol,
const libp2p::peer::ProtocolName &protocol,
Request request,
std::function<void(outcome::result<Response>)> cb) {
host.newStream(
Expand Down Expand Up @@ -160,7 +160,7 @@ namespace kagome::network {
template <typename Request>
static void write(libp2p::Host &host,
const libp2p::peer::PeerInfo &peer_info,
const libp2p::peer::Protocol &protocol,
const libp2p::peer::ProtocolName &protocol,
Request request,
std::function<void(outcome::result<void>)> cb) {
host.newStream(peer_info.id,
Expand Down
4 changes: 2 additions & 2 deletions examples/network/testchain.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"id": "local_testnet",
"chainType": "Local",
"bootNodes": [
"/ip4/127.0.0.1/tcp/11122/p2p/12D3KooWEgUjBV5FJAuBSoNMRYFRHjV7PjZwRQ7b43EKX9g7D6xV",
"/ip4/127.0.0.1/tcp/11222/p2p/12D3KooWKzGMnzpdQQFGvxVEBYKQuyttE3KjigvBAwXy7hZtdHqs"
"/ip4/127.0.0.1/tcp/11122/ws/p2p/12D3KooWEgUjBV5FJAuBSoNMRYFRHjV7PjZwRQ7b43EKX9g7D6xV",
"/ip4/127.0.0.1/tcp/11222/ws/p2p/12D3KooWKzGMnzpdQQFGvxVEBYKQuyttE3KjigvBAwXy7hZtdHqs"
],
"telemetryEndpoints": null,
"protocolId": "dot",
Expand Down
2 changes: 1 addition & 1 deletion test/core/network/rpc_libp2p_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class RpcLibp2pTest : public testing::Test {

HostMock host_;
PeerInfo peer_info_{"my_peer"_peerid, {}};
Protocol protocol_{"/test/2.2.8"};
ProtocolName protocol_{"/test/2.2.8"};

// we are not interested in the exact semantics, so let BlocksResponse be both
// request and response
Expand Down