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

light client protocol #1511

Merged
merged 5 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions core/common/bytestr.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef KAGOME_COMMON_BYTESTR_HPP
#define KAGOME_COMMON_BYTESTR_HPP

#include <libp2p/common/bytestr.hpp>
#include <libp2p/common/span_size.hpp>
#include <string_view>

namespace kagome {
using libp2p::bytestr;

inline std::string_view bytestr(const gsl::span<const uint8_t> &s) {
turuslan marked this conversation as resolved.
Show resolved Hide resolved
// NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
return {reinterpret_cast<const char *>(s.data()), libp2p::spanSize(s)};
}
} // namespace kagome

#endif // KAGOME_COMMON_BYTESTR_HPP
8 changes: 6 additions & 2 deletions core/consensus/babe/impl/babe_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1033,8 +1033,12 @@ namespace kagome::consensus::babe {
}

// finally, broadcast the sealed block
block_announce_transmitter_->blockAnnounce(
network::BlockAnnounce{block.header});
block_announce_transmitter_->blockAnnounce(network::BlockAnnounce{
block.header,
block_info == block_tree_->bestLeaf() ? network::BlockState::Best
: network::BlockState::Normal,
common::Buffer{},
});
SL_DEBUG(
log_,
"Announced block number {} in slot {} (epoch {}) with timestamp {}",
Expand Down
2 changes: 2 additions & 0 deletions core/network/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ add_subdirectory(protobuf)


add_library(network
impl/protocols/light.cpp
impl/state_protocol_observer_impl.cpp
impl/state_sync_request_flow.cpp
impl/synchronizer_impl.cpp
Expand Down Expand Up @@ -34,6 +35,7 @@ add_library(network
warp/cache.cpp
)
target_link_libraries(network
light_api_proto
p2p::p2p_basic_scheduler
runtime_environment_factory
scale_libp2p_types
Expand Down
163 changes: 163 additions & 0 deletions core/network/adapters/light.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef KAGOME_NETWORK_ADAPTERS_LIGHT_HPP
#define KAGOME_NETWORK_ADAPTERS_LIGHT_HPP

#include "common/bytestr.hpp"
#include "network/adapters/protobuf.hpp"
#include "network/protobuf/light.v1.pb.h"
#include "primitives/common.hpp"

namespace kagome::network {
struct LightProtocolRequest {
struct Read {
std::optional<common::Buffer> child;
std::vector<common::Buffer> keys;
};
struct Call {
std::string method;
common::Buffer args;
};

primitives::BlockHash block;
boost::variant<Read, Call> op;
};

struct LightProtocolResponse {
std::vector<common::Buffer> proof;
bool call = false;
};

template <>
struct ProtobufMessageAdapter<LightProtocolRequest> {
static size_t size(const LightProtocolRequest &t) {
return 0;
turuslan marked this conversation as resolved.
Show resolved Hide resolved
}

static std::vector<uint8_t>::iterator write(
const LightProtocolRequest &t,
std::vector<uint8_t> &out,
std::vector<uint8_t>::iterator loaded) {
::protobuf_generated::api::v1::light::Request msg;
if (auto call = boost::get<LightProtocolRequest::Call>(&t.op)) {
auto &pb = *msg.mutable_remote_call_request();
pb.set_block(std::string{bytestr(t.block)});
pb.set_method(call->method);
pb.set_data(std::string{bytestr(call->args)});
} else {
auto &read = boost::get<LightProtocolRequest::Read>(t.op);
if (read.child) {
auto &pb = *msg.mutable_remote_read_child_request();
pb.set_block(std::string{bytestr(t.block)});
pb.set_storage_key(std::string{bytestr(*read.child)});
for (auto &key : read.keys) {
pb.add_keys(std::string{bytestr(key)});
}
} else {
auto &pb = *msg.mutable_remote_read_request();
pb.set_block(std::string{bytestr(t.block)});
for (auto &key : read.keys) {
pb.add_keys(std::string{bytestr(key)});
}
}
}
return appendToVec(msg, out, loaded);
}

static outcome::result<std::vector<uint8_t>::const_iterator> read(
LightProtocolRequest &out,
const std::vector<uint8_t> &src,
std::vector<uint8_t>::const_iterator from) {
const auto remains = src.size() - std::distance(src.begin(), from);
assert(remains >= size(out));
turuslan marked this conversation as resolved.
Show resolved Hide resolved

::protobuf_generated::api::v1::light::Request msg;
if (not msg.ParseFromArray(from.base(), remains)) {
return AdaptersError::PARSE_FAILED;
}

auto get_block = [&](auto &pb) {
auto r = primitives::BlockHash::fromSpan(bytestr(pb.block()));
if (r) {
out.block = r.value();
}
return r;
};
if (msg.has_remote_call_request()) {
auto &pb = msg.remote_call_request();
OUTCOME_TRY(get_block(pb));
out.op = LightProtocolRequest::Call{pb.method(),
common::Buffer{bytestr(pb.data())}};
} else {
LightProtocolRequest::Read read;
auto get_keys = [&](auto &pb) {
for (auto &key : pb.keys()) {
read.keys.emplace_back(bytestr(key));
}
};
if (msg.has_remote_read_child_request()) {
auto &pb = msg.remote_read_child_request();
OUTCOME_TRY(get_block(pb));
read.child = common::Buffer{bytestr(pb.storage_key())};
get_keys(pb);
} else {
auto &pb = msg.remote_read_request();
OUTCOME_TRY(get_block(pb));
get_keys(pb);
}
out.op = std::move(read);
}

std::advance(from, msg.ByteSizeLong());
return from;
}
};

template <>
struct ProtobufMessageAdapter<LightProtocolResponse> {
static size_t size(const LightProtocolResponse &t) {
return 0;
}

static std::vector<uint8_t>::iterator write(
const LightProtocolResponse &t,
std::vector<uint8_t> &out,
std::vector<uint8_t>::iterator loaded) {
::protobuf_generated::api::v1::light::Response msg;
auto &proof = t.call
? *msg.mutable_remote_call_response()->mutable_proof()
: *msg.mutable_remote_read_response()->mutable_proof();
proof = bytestr(scale::encode(t.proof).value());
return appendToVec(msg, out, loaded);
}

static outcome::result<std::vector<uint8_t>::const_iterator> read(
LightProtocolResponse &out,
const std::vector<uint8_t> &src,
std::vector<uint8_t>::const_iterator from) {
const auto remains = src.size() - std::distance(src.begin(), from);
assert(remains >= size(out));

::protobuf_generated::api::v1::light::Response msg;
if (not msg.ParseFromArray(from.base(), remains)) {
return AdaptersError::PARSE_FAILED;
}

out.call = msg.has_remote_call_response();
OUTCOME_TRY(proof,
scale::decode<std::vector<common::Buffer>>(libp2p::bytestr(
out.call ? msg.remote_call_response().proof()
: msg.remote_read_response().proof())));
out.proof = std::move(proof);

std::advance(from, msg.ByteSizeLong());
return from;
}
};

} // namespace kagome::network

#endif // KAGOME_NETWORK_ADAPTERS_LIGHT_HPP
2 changes: 1 addition & 1 deletion core/network/adapters/protobuf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#include <functional>
#include <gsl/span>
#include <memory>
#include <vector>

#include "network/adapters/adapter_errors.hpp"
#include "network/protobuf/api.v1.pb.h"
#include "outcome/outcome.hpp"

namespace kagome::network {
Expand Down
1 change: 1 addition & 0 deletions core/network/adapters/protobuf_block_request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "common/visitor.hpp"
#include "macro/endianness_utils.hpp"
#include "network/protobuf/api.v1.pb.h"
#include "network/types/blocks_request.hpp"

namespace kagome::network {
Expand Down
23 changes: 16 additions & 7 deletions core/network/adapters/protobuf_block_response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "network/adapters/protobuf.hpp"

#include "network/protobuf/api.v1.pb.h"
#include "network/types/blocks_response.hpp"
#include "scale/scale.hpp"

Expand All @@ -28,20 +29,25 @@ namespace kagome::network {
auto *dst_block = msg.add_blocks();
dst_block->set_hash(src_block.hash.toString());

if (src_block.header)
if (src_block.header) {
dst_block->set_header(
vector_to_string(scale::encode(*src_block.header).value()));
}

if (src_block.body)
for (const auto &ext_body : *src_block.body)
if (src_block.body) {
for (const auto &ext_body : *src_block.body) {
dst_block->add_body(
vector_to_string(scale::encode(ext_body).value()));
}
}

if (src_block.receipt)
if (src_block.receipt) {
dst_block->set_receipt(src_block.receipt->toString());
}

if (src_block.message_queue)
if (src_block.message_queue) {
dst_block->set_message_queue(src_block.message_queue->toString());
}

if (src_block.justification) {
dst_block->set_justification(
Expand All @@ -63,8 +69,9 @@ namespace kagome::network {
assert(remains >= size(out));

::api::v1::BlockResponse msg;
if (!msg.ParseFromArray(from.base(), remains))
if (!msg.ParseFromArray(from.base(), remains)) {
return AdaptersError::PARSE_FAILED;
}

auto &dst_blocks = out.blocks;
dst_blocks.reserve(msg.blocks().size());
Expand All @@ -78,7 +85,9 @@ namespace kagome::network {

std::optional<primitives::BlockBody> bodies;
for (const auto &b : src_block_data.body()) {
if (!bodies) bodies = primitives::BlockBody{};
if (!bodies) {
bodies = primitives::BlockBody{};
}

OUTCOME_TRY(
body, extract_value<primitives::Extrinsic>([&]() { return b; }));
Expand Down
1 change: 1 addition & 0 deletions core/network/adapters/protobuf_state_request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "common/visitor.hpp"
#include "macro/endianness_utils.hpp"
#include "network/protobuf/api.v1.pb.h"
#include "network/types/state_request.hpp"
#include "primitives/common.hpp"

Expand Down
4 changes: 3 additions & 1 deletion core/network/adapters/protobuf_state_response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "network/adapters/protobuf.hpp"

#include "network/protobuf/api.v1.pb.h"
#include "network/types/state_response.hpp"
#include "scale/scale.hpp"

Expand Down Expand Up @@ -49,8 +50,9 @@ namespace kagome::network {
assert(remains >= size(out));

::api::v1::StateResponse msg;
if (!msg.ParseFromArray(from.base(), remains))
if (!msg.ParseFromArray(from.base(), remains)) {
return AdaptersError::PARSE_FAILED;
}

for (const auto &kvEntry : msg.entries()) {
KeyValueStateEntry kv;
Expand Down
1 change: 1 addition & 0 deletions core/network/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace kagome::network {
"/{}/block-announces/1";
const libp2p::peer::ProtocolName kGrandpaProtocol = "/{}/grandpa/1";
const libp2p::peer::ProtocolName kWarpProtocol = "/{}/sync/warp";
const libp2p::peer::ProtocolName kLightProtocol = "/{}/light/2";
const libp2p::peer::ProtocolName kCollationProtocol{"/{}/collation/1"};
const libp2p::peer::ProtocolName kValidationProtocol{"/{}/validation/1"};
const libp2p::peer::ProtocolName kReqCollationProtocol{"/{}/req_collation/1"};
Expand Down
60 changes: 60 additions & 0 deletions core/network/impl/protocols/light.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#include "network/impl/protocols/light.hpp"

#include "network/common.hpp"
#include "runtime/common/trie_storage_provider_impl.hpp"
#include "runtime/runtime_environment_factory.hpp"

namespace kagome::network {
LightProtocol::LightProtocol(
libp2p::Host &host,
const application::ChainSpec &chain_spec,
const primitives::GenesisBlockHeader &genesis,
std::shared_ptr<blockchain::BlockHeaderRepository> repository,
std::shared_ptr<storage::trie::TrieStorage> storage,
std::shared_ptr<runtime::RuntimeEnvironmentFactory> env_factory)
: RequestResponseProtocolType{
kName,
host,
make_protocols(kLightProtocol, chain_spec, genesis),
log::createLogger(kName),
},
repository_{std::move(repository)},
storage_{std::move(storage)},
env_factory_{std::move(env_factory)} {}

outcome::result<LightProtocol::ResponseType> LightProtocol::onRxRequest(
RequestType req, std::shared_ptr<Stream>) {
std::unordered_set<common::Buffer> proof;
auto prove = [&](common::BufferView raw) { proof.emplace(raw); };
OUTCOME_TRY(header, repository_->getBlockHeader(req.block));
OUTCOME_TRY(batch,
storage_->getProofReaderBatchAt(header.state_root, prove));
auto call = boost::get<LightProtocolRequest::Call>(&req.op);
if (call) {
OUTCOME_TRY(factory, env_factory_->start(req.block));
OUTCOME_TRY(env, factory->withStorageBatch(std::move(batch)).make());
OUTCOME_TRY(
env->module_instance->callExportFunction(call->method, call->args));
turuslan marked this conversation as resolved.
Show resolved Hide resolved
OUTCOME_TRY(env->module_instance->resetEnvironment());
} else {
auto &read = boost::get<LightProtocolRequest::Read>(req.op);
runtime::TrieStorageProviderImpl provider{storage_, nullptr};
provider.setTo(std::move(batch));
std::reference_wrapper<const storage::trie::TrieBatch> trie =
*provider.getCurrentBatch();
if (read.child) {
OUTCOME_TRY(child, provider.getChildBatchAt(*read.child));
trie = child;
}
for (auto &key : read.keys) {
OUTCOME_TRY(trie.get().tryGet(key));
}
}
return LightProtocolResponse{{proof.begin(), proof.end()}, call != nullptr};
}
} // namespace kagome::network
Loading