Skip to content

Commit

Permalink
Refactor: replace Microsoft.GSL (#1855)
Browse files Browse the repository at this point in the history
* refactor: gst to std

Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>

* refactor: gst to std

Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>

* refactor: gst to std

Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>

* refactor: gst to std

Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>

* update: hunter

Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>

* fix: review issues

Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>

* fix: review issues

Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>

* fix: review issues

Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>

* fix: review issues

Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>

* fix: review issues

Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>

* Switch to Macos-13

* fix: review issues

Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>

---------

Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
Co-authored-by: kamilsa <[email protected]>
  • Loading branch information
xDimon and kamilsa authored Oct 27, 2023
1 parent 6696e5e commit 6b3b59c
Show file tree
Hide file tree
Showing 188 changed files with 754 additions and 882 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ env:
jobs:
MacOS:
runs-on: macos-latest
runs-on: macos-13
steps:
- uses: actions/checkout@dc323e67f16fb5f7663d20ff7941f27f5809e9b6
- uses: actions/cache@937d24475381cd9c75ae6db12cb4e79714b926ed
Expand Down
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
14 changes: 6 additions & 8 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 Down Expand Up @@ -141,8 +139,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 (not startsWith(key.value(), 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 (not startsWith(key.value(), prefix)) {
break;
}
result.push_back(cursor->key().value());
Expand Down
6 changes: 4 additions & 2 deletions core/api/service/mmr/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include "api/service/mmr/rpc.hpp"

#include <libp2p/common/final_action.hpp>

#include "api/jrpc/jrpc_server_impl.hpp"
#include "api/service/jrpc_fn.hpp"
#include "blockchain/block_tree.hpp"
Expand Down Expand Up @@ -62,8 +64,8 @@ namespace kagome::api {

auto MmrRpc::withOffchain(const primitives::BlockHash &at) {
// TODO(turuslan): simplify offchain
auto remove =
gsl::finally([&] { offchain_worker_pool_.get()->removeWorker(); });
::libp2p::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
15 changes: 7 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,7 @@ namespace kagome::api {
BOOST_ASSERT(key.has_value());

// make sure our key begins with prefix
if (!boost::starts_with(key.value(), prefix)) {
if (not startsWith(key.value(), prefix)) {
break;
}
result.push_back(cursor->key().value());
Expand All @@ -113,13 +113,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 +130,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 +153,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 +182,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
11 changes: 5 additions & 6 deletions core/api/service/state/state_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ namespace kagome::api {
const std::optional<primitives::BlockHash> &block_hash_opt) const = 0;

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

struct StorageChangeSet {
primitives::BlockHash block;
Expand All @@ -54,16 +53,16 @@ namespace kagome::api {
};

virtual 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 = 0;

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

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

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

0 comments on commit 6b3b59c

Please sign in to comment.