Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into babe/time
Browse files Browse the repository at this point in the history
Signed-off-by: turuslan <turuslan.devbox@gmail.com>
  • Loading branch information
turuslan committed Apr 18, 2023
2 parents e6b8e48 + 0fa0010 commit 55d09fb
Show file tree
Hide file tree
Showing 139 changed files with 1,701 additions and 2,163 deletions.
5 changes: 5 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ IncludeBlocks: Preserve
InsertBraces: true
InsertTrailingCommas: Wrapped
SortIncludes: CaseSensitive
QualifierAlignment: Custom
QualifierOrder: ['inline', 'static', 'constexpr', 'const', 'volatile', 'type', 'restrict']
ReferenceAlignment: Right
# uncomment in clang-format-16
#RemoveSemicolon: true
#InsertNewlineAtEOF: true


3 changes: 0 additions & 3 deletions core/api/service/author/author_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ namespace kagome::api {
public:
virtual ~AuthorApi() = default;

virtual void setApiService(
std::shared_ptr<api::ApiService> const &api_service) = 0;

/**
* @brief validates and sends extrinsic to transaction pool
* @param source how extrinsic was received (for example external or
Expand Down
24 changes: 10 additions & 14 deletions core/api/service/author/impl/author_api_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,24 @@ namespace kagome::api {
sptr<crypto::CryptoStore> store,
sptr<crypto::SessionKeys> keys,
sptr<crypto::KeyFileStorage> key_store,
sptr<blockchain::BlockTree> block_tree)
LazySPtr<blockchain::BlockTree> block_tree,
LazySPtr<api::ApiService> api_service)
: keys_api_(std::move(key_api)),
pool_{std::move(pool)},
store_{std::move(store)},
keys_{std::move(keys)},
key_store_{std::move(key_store)},
api_service_{std::move(api_service)},
block_tree_{std::move(block_tree)},
logger_{log::createLogger("AuthorApi", "author_api")} {
BOOST_ASSERT_MSG(keys_api_ != nullptr, "session keys api is nullptr");
BOOST_ASSERT_MSG(pool_ != nullptr, "transaction pool is nullptr");
BOOST_ASSERT_MSG(store_ != nullptr, "crypto store is nullptr");
BOOST_ASSERT_MSG(keys_ != nullptr, "session keys store is nullptr");
BOOST_ASSERT_MSG(key_store_ != nullptr, "key store is nullptr");
BOOST_ASSERT_MSG(block_tree_ != nullptr, "block tree is nullptr");
BOOST_ASSERT_MSG(logger_ != nullptr, "logger is nullptr");
}

void AuthorApiImpl::setApiService(
std::shared_ptr<api::ApiService> const &api_service) {
BOOST_ASSERT(api_service != nullptr);
api_service_ = api_service;
}

outcome::result<common::Hash256> AuthorApiImpl::submitExtrinsic(
primitives::TransactionSource source,
const primitives::Extrinsic &extrinsic) {
Expand Down Expand Up @@ -125,8 +120,8 @@ namespace kagome::api {

outcome::result<common::Buffer> AuthorApiImpl::rotateKeys() {
OUTCOME_TRY(encoded_session_keys,
keys_api_->generate_session_keys(block_tree_->bestLeaf().hash,
std::nullopt));
keys_api_->generate_session_keys(
block_tree_.get()->bestLeaf().hash, std::nullopt));
return std::move(encoded_session_keys);
}

Expand Down Expand Up @@ -165,10 +160,11 @@ namespace kagome::api {
outcome::result<bool> AuthorApiImpl::hasKey(
const gsl::span<const uint8_t> &public_key, crypto::KeyTypeId key_type) {
auto res = key_store_->searchForSeed(key_type, public_key);
if (not res)
if (not res) {
return res.error();
else
} else {
return res.value() ? true : false;
}
}

outcome::result<std::vector<primitives::Extrinsic>>
Expand All @@ -195,7 +191,7 @@ namespace kagome::api {

outcome::result<AuthorApi::SubscriptionId>
AuthorApiImpl::submitAndWatchExtrinsic(Extrinsic extrinsic) {
if (auto service = api_service_.lock()) {
if (auto service = api_service_.get()) {
OUTCOME_TRY(
tx,
pool_->constructTransaction(TransactionSource::External, extrinsic));
Expand All @@ -218,7 +214,7 @@ namespace kagome::api {
}

outcome::result<bool> AuthorApiImpl::unwatchExtrinsic(SubscriptionId sub_id) {
if (auto service = api_service_.lock()) {
if (auto service = api_service_.get()) {
return service->unsubscribeFromExtrinsicLifecycle(sub_id);
}
throw jsonrpc::InternalErrorFault(
Expand Down
10 changes: 5 additions & 5 deletions core/api/service/author/impl/author_api_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <libp2p/peer/peer_id.hpp>

#include "common/blob.hpp"
#include "injector/lazy.hpp"
#include "log/logger.hpp"
#include "primitives/author_api_primitives.hpp"
#include "primitives/transaction.hpp"
Expand Down Expand Up @@ -72,12 +73,11 @@ namespace kagome::api {
sptr<crypto::CryptoStore> store,
sptr<crypto::SessionKeys> keys,
sptr<crypto::KeyFileStorage> key_store,
sptr<blockchain::BlockTree> block_tree);
LazySPtr<blockchain::BlockTree> block_tree,
LazySPtr<api::ApiService> api_service);

~AuthorApiImpl() override = default;

void setApiService(sptr<api::ApiService> const &api_service) override;

outcome::result<common::Hash256> submitExtrinsic(
TransactionSource source,
const primitives::Extrinsic &extrinsic) override;
Expand Down Expand Up @@ -113,8 +113,8 @@ namespace kagome::api {
sptr<crypto::CryptoStore> store_;
sptr<crypto::SessionKeys> keys_;
sptr<crypto::KeyFileStorage> key_store_;
std::weak_ptr<api::ApiService> api_service_;
sptr<blockchain::BlockTree> block_tree_;
LazySPtr<api::ApiService> api_service_;
LazySPtr<blockchain::BlockTree> block_tree_;

log::Logger logger_;
};
Expand Down
3 changes: 0 additions & 3 deletions core/api/service/chain/chain_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ namespace kagome::api {
using BlockHash = kagome::primitives::BlockHash;
using ValueType = boost::variant<BlockNumber, std::string>;

virtual void setApiService(
const std::shared_ptr<api::ApiService> &api_service) = 0;

/**
* @return last finalized block hash
*/
Expand Down
28 changes: 15 additions & 13 deletions core/api/service/chain/impl/chain_api_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ namespace kagome::api {
ChainApiImpl::ChainApiImpl(
std::shared_ptr<blockchain::BlockHeaderRepository> block_repo,
std::shared_ptr<blockchain::BlockTree> block_tree,
std::shared_ptr<blockchain::BlockStorage> block_storage)
std::shared_ptr<blockchain::BlockStorage> block_storage,
LazySPtr<api::ApiService> api_service)
: header_repo_{std::move(block_repo)},
block_tree_{std::move(block_tree)},
api_service_{std::move(api_service)},
block_storage_{std::move(block_storage)} {
BOOST_ASSERT_MSG(header_repo_ != nullptr,
"block repo parameter is nullptr");
Expand All @@ -48,12 +50,6 @@ namespace kagome::api {
return header_repo_->getHashByNumber(value);
}

void ChainApiImpl::setApiService(
std::shared_ptr<api::ApiService> const &api_service) {
BOOST_ASSERT(api_service != nullptr);
api_service_ = api_service;
}

outcome::result<BlockHash> ChainApiImpl::getBlockHash(
std::string_view value) const {
// despite w3f specification says, that request contains 32-bit
Expand Down Expand Up @@ -87,14 +83,18 @@ namespace kagome::api {
std::string_view hash) {
OUTCOME_TRY(h, primitives::BlockHash::fromHexWithPrefix(hash));
OUTCOME_TRY(block, block_storage_->getBlockData(h));
if (block) return block.value();
if (block) {
return block.value();
}
return Error::BLOCK_NOT_FOUND;
}

outcome::result<primitives::BlockData> ChainApiImpl::getBlock() {
auto last_finalized_info = block_tree_->getLastFinalized();
OUTCOME_TRY(block, block_storage_->getBlockData(last_finalized_info.hash));
if (block) return block.value();
if (block) {
return block.value();
}
return Error::BLOCK_NOT_FOUND;
}

Expand All @@ -104,16 +104,17 @@ namespace kagome::api {
}

outcome::result<uint32_t> ChainApiImpl::subscribeFinalizedHeads() {
if (auto api_service = api_service_.lock())
if (auto api_service = api_service_.get()) {
return api_service->subscribeFinalizedHeads();
}

throw jsonrpc::InternalErrorFault(
"Internal error. Api service not initialized.");
}

outcome::result<void> ChainApiImpl::unsubscribeFinalizedHeads(
uint32_t subscription_id) {
if (auto api_service = api_service_.lock()) {
if (auto api_service = api_service_.get()) {
OUTCOME_TRY(api_service->unsubscribeFinalizedHeads(subscription_id));
// any non-error result is considered as success
return outcome::success();
Expand All @@ -124,16 +125,17 @@ namespace kagome::api {
}

outcome::result<uint32_t> ChainApiImpl::subscribeNewHeads() {
if (auto api_service = api_service_.lock())
if (auto api_service = api_service_.get()) {
return api_service->subscribeNewHeads();
}

throw jsonrpc::InternalErrorFault(
"Internal error. Api service not initialized.");
}

outcome::result<void> ChainApiImpl::unsubscribeNewHeads(
uint32_t subscription_id) {
if (auto api_service = api_service_.lock()) {
if (auto api_service = api_service_.get()) {
OUTCOME_TRY(api_service->unsubscribeNewHeads(subscription_id));
return outcome::success();
}
Expand Down
12 changes: 6 additions & 6 deletions core/api/service/chain/impl/chain_api_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
#ifndef KAGOME_CHAIN_API_IMPL_HPP
#define KAGOME_CHAIN_API_IMPL_HPP

#include "api/service/chain/chain_api.hpp"

#include <memory>

#include "api/service/chain/chain_api.hpp"
#include "blockchain/block_header_repository.hpp"
#include "blockchain/block_storage.hpp"
#include "blockchain/block_tree.hpp"
#include "injector/lazy.hpp"

namespace kagome::api {

Expand All @@ -26,10 +28,8 @@ namespace kagome::api {

ChainApiImpl(std::shared_ptr<blockchain::BlockHeaderRepository> block_repo,
std::shared_ptr<blockchain::BlockTree> block_tree,
std::shared_ptr<blockchain::BlockStorage> block_storage);

void setApiService(
std::shared_ptr<api::ApiService> const &api_service) override;
std::shared_ptr<blockchain::BlockStorage> block_storage,
LazySPtr<api::ApiService> api_service);

outcome::result<BlockHash> getBlockHash() const override;

Expand Down Expand Up @@ -69,7 +69,7 @@ namespace kagome::api {
private:
std::shared_ptr<blockchain::BlockHeaderRepository> header_repo_;
std::shared_ptr<blockchain::BlockTree> block_tree_;
std::weak_ptr<api::ApiService> api_service_;
LazySPtr<api::ApiService> api_service_;
std::shared_ptr<blockchain::BlockStorage> block_storage_;
};
} // namespace kagome::api
Expand Down
3 changes: 0 additions & 3 deletions core/api/service/child_state/child_state_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ namespace kagome::api {
public:
virtual ~ChildStateApi() = default;

virtual void setApiService(
const std::shared_ptr<api::ApiService> &api_service) = 0;

/**
* @brief Warning: This method is UNSAFE.
* Returns the keys from the specified child storage.
Expand Down
6 changes: 0 additions & 6 deletions core/api/service/child_state/impl/child_state_api_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ namespace kagome::api {
BOOST_ASSERT(nullptr != metadata_);
}

void ChildStateApiImpl::setApiService(
const std::shared_ptr<api::ApiService> &api_service) {
BOOST_ASSERT(api_service != nullptr);
api_service_ = api_service;
}

outcome::result<std::vector<common::Buffer>> ChildStateApiImpl::getKeys(
const common::Buffer &child_storage_key,
const std::optional<common::Buffer> &prefix_opt,
Expand Down
6 changes: 2 additions & 4 deletions core/api/service/child_state/impl/child_state_api_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
#define KAGOME_CHILD_STATE_API_IMPL_HPP

#include "api/service/child_state/child_state_api.hpp"

#include "blockchain/block_header_repository.hpp"
#include "blockchain/block_tree.hpp"
#include "injector/lazy.hpp"
#include "runtime/runtime_api/core.hpp"
#include "runtime/runtime_api/metadata.hpp"
#include "storage/trie/trie_storage.hpp"
Expand All @@ -24,9 +26,6 @@ namespace kagome::api {
std::shared_ptr<runtime::Core> runtime_core,
std::shared_ptr<runtime::Metadata> metadata);

void setApiService(
const std::shared_ptr<api::ApiService> &api_service) override;

outcome::result<std::vector<common::Buffer>> getKeys(
const common::Buffer &child_storage_key,
const std::optional<common::Buffer> &prefix_opt,
Expand Down Expand Up @@ -65,7 +64,6 @@ namespace kagome::api {
std::shared_ptr<blockchain::BlockTree> block_tree_;
std::shared_ptr<runtime::Core> runtime_core_;

std::weak_ptr<api::ApiService> api_service_;
std::shared_ptr<runtime::Metadata> metadata_;
};

Expand Down
18 changes: 9 additions & 9 deletions core/api/service/impl/api_service_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ namespace kagome::api {
ApiServiceImpl::ApiServiceImpl(
application::AppStateManager &app_state_manager,
std::shared_ptr<api::RpcThreadPool> thread_pool,
ListenerList listeners,
std::vector<std::shared_ptr<Listener>> listeners,
std::shared_ptr<JRpcServer> server,
const ProcessorSpan &processors,
std::vector<std::shared_ptr<JRpcProcessor>> processors,
StorageSubscriptionEnginePtr storage_sub_engine,
ChainSubscriptionEnginePtr chain_sub_engine,
ExtrinsicSubscriptionEnginePtr ext_sub_engine,
Expand All @@ -136,7 +136,7 @@ namespace kagome::api {
std::shared_ptr<storage::trie::TrieStorage> trie_storage,
std::shared_ptr<runtime::Core> core)
: thread_pool_(std::move(thread_pool)),
listeners_(std::move(listeners.listeners)),
listeners_(std::move(listeners)),
server_(std::move(server)),
logger_{log::createLogger("ApiService", "api")},
block_tree_{std::move(block_tree)},
Expand All @@ -154,7 +154,8 @@ namespace kagome::api {
std::all_of(listeners_.cbegin(), listeners_.cend(), [](auto &listener) {
return listener != nullptr;
}));
for (auto &processor : processors.processors) {
BOOST_ASSERT(server_);
for (auto &processor : processors) {
BOOST_ASSERT(processor != nullptr);
processor->registerHandlers();
}
Expand All @@ -172,11 +173,10 @@ namespace kagome::api {
std::pair<common::Buffer, std::optional<common::Buffer>>>
&key_value_pairs,
const primitives::BlockHash &block) {
/// TODO(iceseer): PRE-475 make event notification depending
/// in packs blocks, to batch them in a single message Because
/// of a spec, we can send an array of changes in a single
/// message. We can receive here a pack of events and format
/// them in a single json message.
/// TODO(iceseer): PRE-475 make event notification depending in packs
/// blocks, to batch them in a single message Because of a spec, we can send
/// an array of changes in a single message. We can receive here a pack of
/// events and format them in a single json message.

jsonrpc::Value::Array changes;
changes.reserve(key_value_pairs.size());
Expand Down
Loading

0 comments on commit 55d09fb

Please sign in to comment.