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

Refactor: replace Microsoft.GSL #1855

Merged
merged 14 commits into from
Oct 27, 2023
8 changes: 7 additions & 1 deletion cmake/Hunter/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ hunter_config(

hunter_config(
libp2p
VERSION 0.1.16
VERSION 0.1.17
KEEP_PACKAGE_SOURCES
)

Expand All @@ -64,6 +64,12 @@ hunter_config(
KEEP_PACKAGE_SOURCES
)

hunter_config(
scale
VERSION 1.1.0
KEEP_PACKAGE_SOURCES
)

# Fix for Apple clang (or clang from brew) of versions 15 and higher
if(APPLE AND (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang") AND CMAKE_CXX_COMPILER_VERSION GREATER_EQUAL "15.0.0")
hunter_config(
Expand Down
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/qdrvm/hunter/archive/refs/tags/v0.23.257-qdrvm10.tar.gz
SHA1 72b446a4424ba28ea90f9a68a9134b0f8e44b5b2
URL https://github.com/qdrvm/hunter/archive/refs/tags/v0.23.257-qdrvm11.tar.gz
SHA1 b2a69ae501bdc99006fb86e55930640004468556
LOCAL
)
4 changes: 0 additions & 4 deletions cmake/dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ find_package(Protobuf CONFIG REQUIRED)
hunter_add_package(RapidJSON)
find_package(RapidJSON CONFIG REQUIRED)

# https://docs.hunter.sh/en/latest/packages/pkg/Microsoft.GSL.html
hunter_add_package(Microsoft.GSL)
find_package(Microsoft.GSL CONFIG REQUIRED)

hunter_add_package(erasure_coding_crust)
find_package(erasure_coding_crust CONFIG REQUIRED)

Expand Down
4 changes: 2 additions & 2 deletions core/api/jrpc/value_converter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,8 @@ namespace kagome::api {
jArray peers;
peers.resize(params.peers.size());
std::transform(
params.peers.cbegin(),
params.peers.cend(),
params.peers.begin(),
params.peers.end(),
peers.begin(),
[](const auto &peer_id) { return makeValue(peer_id.toHex()); });
return jStruct{std::pair{"broadcast", std::move(peers)}};
Expand Down
17 changes: 8 additions & 9 deletions core/api/service/author/author_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "common/blob.hpp"
#include "common/buffer.hpp"
#include "common/buffer_view.hpp"
#include "crypto/crypto_store/key_type.hpp"
#include "primitives/author_api_primitives.hpp"
#include "primitives/transaction_validity.hpp"
Expand All @@ -25,6 +26,7 @@ namespace kagome::api {
protected:
using Hash256 = common::Hash256;
using Buffer = common::Buffer;
using BufferView = common::BufferView;
using Extrinsic = primitives::Extrinsic;
using SubscriptionId = primitives::SubscriptionId;
using ExtrinsicKey = primitives::ExtrinsicKey;
Expand All @@ -50,10 +52,9 @@ namespace kagome::api {
* @param seed The seed (suri) in binary
* @param public_key The public key in binary
*/
virtual outcome::result<void> insertKey(
crypto::KeyType key_type,
const gsl::span<const uint8_t> &seed,
const gsl::span<const uint8_t> &public_key) = 0;
virtual outcome::result<void> insertKey(crypto::KeyType key_type,
const BufferView &seed,
const BufferView &public_key) = 0;

/**
* @brief Generate new session keys and
Expand All @@ -69,18 +70,16 @@ namespace kagome::api {
* @return returns true if all private keys could be found, false if
* otherwise
*/
virtual outcome::result<bool> hasSessionKeys(
const gsl::span<const uint8_t> &keys) = 0;
virtual outcome::result<bool> hasSessionKeys(const BufferView &keys) = 0;

/**
* @brief checks if the keystore has private keys for the given public
* key and key type
* @param public_key The public key in binary
* @param key_type The key type
*/
virtual outcome::result<bool> hasKey(
const gsl::span<const uint8_t> &public_key,
crypto::KeyType key_type) = 0;
virtual outcome::result<bool> hasKey(const BufferView &public_key,
crypto::KeyType key_type) = 0;

/**
* @return collection of pending extrinsics
Expand Down
17 changes: 8 additions & 9 deletions core/api/service/author/impl/author_api_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ namespace kagome::api {
return pool_->submitExtrinsic(source, extrinsic);
}

outcome::result<void> AuthorApiImpl::insertKey(
crypto::KeyType key_type_id,
const gsl::span<const uint8_t> &seed,
const gsl::span<const uint8_t> &public_key) {
outcome::result<void> AuthorApiImpl::insertKey(crypto::KeyType key_type_id,
const BufferView &seed,
const BufferView &public_key) {
if (std::find(kKeyTypes.begin(), kKeyTypes.end(), key_type_id)
== kKeyTypes.end()) {
std::string types;
Expand Down Expand Up @@ -112,8 +111,7 @@ namespace kagome::api {
// logic here is polkadot specific only!
// it could be extended by reading config from chainspec palletSession/keys
// value
outcome::result<bool> AuthorApiImpl::hasSessionKeys(
const gsl::span<const uint8_t> &keys) {
outcome::result<bool> AuthorApiImpl::hasSessionKeys(const BufferView &keys) {
scale::ScaleDecoderStream stream(keys);
std::array<uint8_t, 32> key;
if (keys.size() < 32 || keys.size() > 32 * 6 || (keys.size() % 32) != 0) {
Expand All @@ -128,7 +126,8 @@ namespace kagome::api {
crypto::KeyTypes::GRANDPA,
crypto::Ed25519PublicKey(common::Blob<32>(key)))) {
unsigned count = 1;
while (stream.currentIndex() < keys.size()) {
while (stream.currentIndex()
< static_cast<scale::ScaleDecoderStream::SizeType>(keys.size())) {
stream >> key;
if (not store_->findSr25519Keypair(
crypto::polkadot_key_order[count++],
Expand All @@ -141,8 +140,8 @@ namespace kagome::api {
return false;
}

outcome::result<bool> AuthorApiImpl::hasKey(
const gsl::span<const uint8_t> &public_key, crypto::KeyType key_type) {
outcome::result<bool> AuthorApiImpl::hasKey(const BufferView &public_key,
crypto::KeyType key_type) {
auto res = key_store_->searchForPhrase(key_type, public_key);
if (not res) {
return res.error();
Expand Down
12 changes: 5 additions & 7 deletions core/api/service/author/impl/author_api_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,15 @@ namespace kagome::api {
TransactionSource source,
const primitives::Extrinsic &extrinsic) override;

outcome::result<void> insertKey(
crypto::KeyType key_type,
const gsl::span<const uint8_t> &seed,
const gsl::span<const uint8_t> &public_key) override;
outcome::result<void> insertKey(crypto::KeyType key_type,
const BufferView &seed,
const BufferView &public_key) override;

outcome::result<common::Buffer> rotateKeys() override;

outcome::result<bool> hasSessionKeys(
const gsl::span<const uint8_t> &keys) override;
outcome::result<bool> hasSessionKeys(const BufferView &keys) override;

outcome::result<bool> hasKey(const gsl::span<const uint8_t> &public_key,
outcome::result<bool> hasKey(const BufferView &public_key,
crypto::KeyType key_type) override;

outcome::result<std::vector<primitives::Extrinsic>> pendingExtrinsics()
Expand Down
2 changes: 1 addition & 1 deletion core/api/service/author/requests/has_key.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace kagome::api::author::request {

outcome::result<Return> execute() override {
OUTCOME_TRY(public_key, common::unhexWith0x(getParam<0>()));
return api_->hasKey(gsl::span(public_key.data(), public_key.size()),
return api_->hasKey(public_key,
crypto::decodeKeyTypeFromStr(getParam<1>()));
}

Expand Down
2 changes: 1 addition & 1 deletion core/api/service/author/requests/has_session_keys.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace kagome::api::author::request {

outcome::result<Return> execute() override {
OUTCOME_TRY(keys, common::unhexWith0x(getParam<0>()));
return api_->hasSessionKeys(gsl::span(keys.data(), keys.size()));
return api_->hasSessionKeys(keys);
}

private:
Expand Down
5 changes: 2 additions & 3 deletions core/api/service/author/requests/insert_key.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ namespace kagome::api::author::request {
outcome::result<Return> execute() override {
OUTCOME_TRY(seed, common::unhexWith0x(getParam<1>()));
OUTCOME_TRY(public_key, common::unhexWith0x(getParam<2>()));
return api_->insertKey(crypto::decodeKeyTypeFromStr(getParam<0>()),
gsl::span(seed.data(), seed.size()),
gsl::span(public_key.data(), public_key.size()));
return api_->insertKey(
crypto::decodeKeyTypeFromStr(getParam<0>()), seed, public_key);
}

private:
Expand Down
2 changes: 1 addition & 1 deletion core/api/service/chain/chain_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace kagome::api {
* @return array of block hashes for numbers
*/
virtual outcome::result<std::vector<BlockHash>> getBlockHash(
gsl::span<const ValueType> values) const = 0;
std::span<const ValueType> values) const = 0;

/**
* @param hash hex-string of a block to retrieve
Expand Down
2 changes: 1 addition & 1 deletion core/api/service/chain/impl/chain_api_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ namespace kagome::api {
}

outcome::result<std::vector<BlockHash>> ChainApiImpl::getBlockHash(
gsl::span<const ValueType> values) const {
std::span<const ValueType> values) const {
std::vector<BlockHash> results;
results.reserve(values.size());

Expand Down
2 changes: 1 addition & 1 deletion core/api/service/chain/impl/chain_api_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace kagome::api {
std::string_view value) const override;

outcome::result<std::vector<BlockHash>> getBlockHash(
gsl::span<const ValueType> values) const override;
std::span<const ValueType> values) const override;

outcome::result<primitives::BlockHeader> getHeader(
std::string_view hash) override {
Expand Down
2 changes: 1 addition & 1 deletion core/api/service/chain/requests/get_block_hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ namespace kagome::api::chain::request {
},
[this](
const std::vector<VectorParam> &v) -> outcome::result<ResultType> {
OUTCOME_TRY(rr, api_->getBlockHash(gsl::make_span(v)));
OUTCOME_TRY(rr, api_->getBlockHash(v));
std::vector<std::string> results{};
results.reserve(v.size());
for (const auto &it : rr) {
Expand Down
4 changes: 2 additions & 2 deletions core/api/service/child_state/impl/child_state_api_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ namespace kagome::api {
BOOST_ASSERT(key.has_value());

// make sure our key begins with prefix
if (!boost::starts_with(key.value(), prefix)) {
if (!key->startsWith(prefix)) {
break;
}
result.push_back(cursor->key().value());
Expand Down Expand Up @@ -106,7 +106,7 @@ namespace kagome::api {
BOOST_ASSERT(key.has_value());

// make sure our key begins with prefix
if (!boost::starts_with(key.value(), prefix)) {
if (!key->startsWith(prefix)) {
break;
}
result.push_back(cursor->key().value());
Expand Down
10 changes: 6 additions & 4 deletions core/api/service/mmr/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "api/jrpc/jrpc_server_impl.hpp"
#include "api/service/jrpc_fn.hpp"
#include "blockchain/block_tree.hpp"
#include "common/final_action.hpp"
#include "offchain/offchain_worker_factory.hpp"
#include "offchain/offchain_worker_pool.hpp"
#include "runtime/runtime_api/mmr.hpp"
Expand Down Expand Up @@ -62,8 +63,8 @@ namespace kagome::api {

auto MmrRpc::withOffchain(const primitives::BlockHash &at) {
// TODO(turuslan): simplify offchain
auto remove =
gsl::finally([&] { offchain_worker_pool_.get()->removeWorker(); });
common::MovableFinalAction remove(
[&] { offchain_worker_pool_.get()->removeWorker(); });
outcome::result<decltype(remove)> result{std::move(remove)};
if (auto r = block_tree_.get()->getBlockHeader(at)) {
offchain_worker_pool_.get()->addWorker(
Expand Down Expand Up @@ -142,8 +143,9 @@ namespace kagome::api {
OUTCOME_TRY(
leaves,
scale::decode<primitives::MmrLeaves>(proof_raw.leaves));
OUTCOME_TRY(proof,
scale::decode<primitives::MmrProof>(proof_raw.proof));
OUTCOME_TRY(
proof,
scale::decode<primitives::MmrProof>(proof_raw.proof.view()));
OUTCOME_TRY(r,
self->mmr_api_.get()->verifyProofStateless(
at, mmr_root, leaves, proof));
Expand Down
19 changes: 11 additions & 8 deletions core/api/service/state/impl/state_api_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ namespace kagome::api {
BOOST_ASSERT(key.has_value());

// make sure our key begins with prefix
if (!boost::starts_with(key.value(), prefix)) {
if (!std::equal(
key.value().begin(),
key.value().begin() + std::min(key.value().size(), prefix.size()),
prefix.begin(),
prefix.end())) {
break;
}
result.push_back(cursor->key().value());
Expand All @@ -113,13 +117,13 @@ namespace kagome::api {
}

outcome::result<std::optional<common::Buffer>> StateApiImpl::getStorage(
const common::BufferView &key) const {
common::BufferView key) const {
auto last_finalized = block_tree_->getLastFinalized();
return getStorageAt(key, last_finalized.hash);
}

outcome::result<std::optional<common::Buffer>> StateApiImpl::getStorageAt(
const common::BufferView &key, const primitives::BlockHash &at) const {
common::BufferView key, const primitives::BlockHash &at) const {
OUTCOME_TRY(header, header_repo_->getBlockHeader(at));
OUTCOME_TRY(trie_reader, storage_->getEphemeralBatchAt(header.state_root));
auto res = trie_reader->tryGet(key);
Expand All @@ -130,7 +134,7 @@ namespace kagome::api {

outcome::result<std::vector<StateApiImpl::StorageChangeSet>>
StateApiImpl::queryStorage(
gsl::span<const common::Buffer> keys,
std::span<const common::Buffer> keys,
const primitives::BlockHash &from,
std::optional<primitives::BlockHash> opt_to) const {
// TODO(Harrm): Optimize once changes trie is enabled (and a warning/assert
Expand All @@ -153,8 +157,7 @@ namespace kagome::api {
}

std::vector<StorageChangeSet> changes;
std::map<gsl::span<const uint8_t>, std::optional<common::Buffer>>
last_values;
std::map<common::BufferView, std::optional<common::Buffer>> last_values;

// TODO(Harrm): optimize it to use a lazy generator instead of returning the
// whole vector with block ids
Expand Down Expand Up @@ -183,15 +186,15 @@ namespace kagome::api {

outcome::result<std::vector<StateApiImpl::StorageChangeSet>>
StateApiImpl::queryStorageAt(
gsl::span<const common::Buffer> keys,
std::span<const common::Buffer> keys,
std::optional<primitives::BlockHash> opt_at) const {
auto at =
opt_at.has_value() ? opt_at.value() : block_tree_->bestBlock().hash;
return queryStorage(keys, at, at);
}

outcome::result<StateApi::ReadProof> StateApiImpl::getReadProof(
gsl::span<const common::Buffer> keys,
std::span<const common::Buffer> keys,
std::optional<primitives::BlockHash> opt_at) const {
auto at =
opt_at.has_value() ? opt_at.value() : block_tree_->bestBlock().hash;
Expand Down
11 changes: 5 additions & 6 deletions core/api/service/state/impl/state_api_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,22 @@ namespace kagome::api {
const override;

outcome::result<std::optional<common::Buffer>> getStorage(
const common::BufferView &key) const override;
common::BufferView key) const override;

outcome::result<std::optional<common::Buffer>> getStorageAt(
const common::BufferView &key,
const primitives::BlockHash &at) const override;
common::BufferView key, const primitives::BlockHash &at) const override;

outcome::result<std::vector<StorageChangeSet>> queryStorage(
gsl::span<const common::Buffer> keys,
std::span<const common::Buffer> keys,
const primitives::BlockHash &from,
std::optional<primitives::BlockHash> to) const override;

outcome::result<std::vector<StorageChangeSet>> queryStorageAt(
gsl::span<const common::Buffer> keys,
std::span<const common::Buffer> keys,
std::optional<primitives::BlockHash> at) const override;

outcome::result<ReadProof> getReadProof(
gsl::span<const common::Buffer> keys,
std::span<const common::Buffer> keys,
std::optional<primitives::BlockHash> at) const override;

outcome::result<uint32_t> subscribeStorage(
Expand Down
Loading