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: core version and metadata cache #1391

Merged
merged 13 commits into from
Nov 11, 2022
13 changes: 9 additions & 4 deletions core/api/service/impl/api_service_impl.cpp
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@
#include "common/hexutil.hpp"
#include "primitives/common.hpp"
#include "primitives/transaction.hpp"
#include "runtime/runtime_api/core.hpp"
#include "storage/trie/trie_storage.hpp"
#include "subscription/extrinsic_event_key_repository.hpp"
#include "subscription/subscriber.hpp"
@@ -127,20 +128,23 @@ namespace kagome::api {
std::shared_ptr<subscription::ExtrinsicEventKeyRepository>
extrinsic_event_key_repo,
std::shared_ptr<blockchain::BlockTree> block_tree,
std::shared_ptr<storage::trie::TrieStorage> trie_storage)
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)),
server_(std::move(server)),
logger_{log::createLogger("ApiService", "api")},
block_tree_{std::move(block_tree)},
trie_storage_{std::move(trie_storage)},
core_(std::move(core)),
subscription_engines_{.storage = std::move(storage_sub_engine),
.chain = std::move(chain_sub_engine),
.ext = std::move(ext_sub_engine)},
extrinsic_event_key_repo_{std::move(extrinsic_event_key_repo)} {
BOOST_ASSERT(thread_pool_);
BOOST_ASSERT(block_tree_);
BOOST_ASSERT(trie_storage_);
BOOST_ASSERT(core_);
BOOST_ASSERT(
std::all_of(listeners_.cbegin(), listeners_.cend(), [](auto &listener) {
return listener != nullptr;
@@ -402,14 +406,15 @@ namespace kagome::api {
session->subscribe(
id, primitives::events::ChainEventType::kFinalizedRuntimeVersion);

auto ver = block_tree_->runtimeVersion();
if (ver) {
auto version_res = core_->version(block_tree_->getLastFinalized().hash);
if (version_res.has_value()) {
const auto &version = version_res.value();
session_context.messages = uploadMessagesListFromCache();
forJsonData(server_,
logger_,
id,
kRpcEventRuntimeVersion,
makeValue(*ver),
makeValue(version),
[&](const auto &result) {
session_context.messages->emplace_back(
uploadFromCache(result.data()));
7 changes: 6 additions & 1 deletion core/api/service/impl/api_service_impl.hpp
Original file line number Diff line number Diff line change
@@ -41,6 +41,9 @@ namespace kagome::primitives {
namespace kagome::storage::trie {
class TrieStorage;
}
namespace kagome::runtime {
class Core;
}
namespace kagome::subscription {
class ExtrinsicEventKeyRepository;
}
@@ -135,7 +138,8 @@ namespace kagome::api {
std::shared_ptr<subscription::ExtrinsicEventKeyRepository>
extrinsic_event_key_repo,
std::shared_ptr<blockchain::BlockTree> block_tree,
std::shared_ptr<storage::trie::TrieStorage> trie_storage);
std::shared_ptr<storage::trie::TrieStorage> trie_storage,
std::shared_ptr<runtime::Core> core);

~ApiServiceImpl() override = default;

@@ -241,6 +245,7 @@ namespace kagome::api {
log::Logger logger_;
std::shared_ptr<blockchain::BlockTree> block_tree_;
std::shared_ptr<storage::trie::TrieStorage> trie_storage_;
std::shared_ptr<runtime::Core> core_;

std::mutex subscribed_sessions_cs_;
std::unordered_map<Session::SessionId,
6 changes: 0 additions & 6 deletions core/blockchain/block_tree.hpp
Original file line number Diff line number Diff line change
@@ -72,12 +72,6 @@ namespace kagome::blockchain {
virtual outcome::result<primitives::Justification> getBlockJustification(
const primitives::BlockId &block) const = 0;

/**
* Method to get actual runtime version.
* @return runtime version.
*/
virtual std::optional<primitives::Version> runtimeVersion() const = 0;

/**
* Adds header to the storage
* @param header that we are adding
5 changes: 0 additions & 5 deletions core/blockchain/impl/block_tree_impl.hpp
Original file line number Diff line number Diff line change
@@ -113,10 +113,6 @@ namespace kagome::blockchain {
const primitives::BlockHash &ancestor,
const primitives::BlockHash &descendant) const override;

std::optional<primitives::Version> runtimeVersion() const override {
return actual_runtime_version_;
}

bool hasDirectChain(const primitives::BlockHash &ancestor,
const primitives::BlockHash &descendant) const override;

@@ -190,7 +186,6 @@ namespace kagome::blockchain {
justification_storage_policy_;

std::optional<primitives::BlockHash> genesis_block_hash_;
std::optional<primitives::Version> actual_runtime_version_;

log::Logger log_ = log::createLogger("BlockTree", "blockchain");

1 change: 1 addition & 0 deletions core/injector/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -82,6 +82,7 @@ target_link_libraries(application_injector
rpc_thread_pool
runtime_upgrade_tracker
runtime_environment_factory
runtime_properties_cache
executor
secp256k1_provider
soralog::fallback_configurator
23 changes: 7 additions & 16 deletions core/injector/application_injector.cpp
Original file line number Diff line number Diff line change
@@ -134,6 +134,7 @@
#include "runtime/runtime_api/impl/metadata.hpp"
#include "runtime/runtime_api/impl/offchain_worker_api.hpp"
#include "runtime/runtime_api/impl/parachain_host.hpp"
#include "runtime/runtime_api/impl/runtime_properties_cache_impl.hpp"
#include "runtime/runtime_api/impl/session_keys_api.hpp"
#include "runtime/runtime_api/impl/tagged_transaction_queue.hpp"
#include "runtime/runtime_api/impl/transaction_payment_api.hpp"
@@ -533,6 +534,7 @@ namespace {
auto block_tree = injector.template create<sptr<blockchain::BlockTree>>();
auto trie_storage =
injector.template create<sptr<storage::trie::TrieStorage>>();
auto core = injector.template create<sptr<runtime::Core>>();

auto api_service =
std::make_shared<api::ApiServiceImpl>(asmgr,
@@ -545,7 +547,8 @@ namespace {
ext_sub_engine,
extrinsic_event_key_repo,
block_tree,
trie_storage);
trie_storage,
core);

auto child_state_api =
injector.template create<std::shared_ptr<api::ChildStateApi>>();
@@ -900,7 +903,8 @@ namespace {
sptr<runtime::wavm::InstanceEnvironmentFactory>>(),
injector
.template create<sptr<runtime::wavm::IntrinsicModule>>(),
module_cache_opt);
module_cache_opt,
injector.template create<sptr<crypto::Hasher>>());
}),
di::bind<runtime::ModuleFactory>.template to(
[method](const auto &injector) {
@@ -909,20 +913,6 @@ namespace {
runtime::binaryen::ModuleFactoryImpl,
runtime::wavm::ModuleFactoryImpl>(injector, method);
}),
di::bind<runtime::Executor>.template to([](const auto &injector) {
static std::optional<std::shared_ptr<runtime::Executor>> initialized;
if (!initialized) {
auto env_factory = injector.template create<
std::shared_ptr<runtime::RuntimeEnvironmentFactory>>();
auto header_repo = injector.template create<
std::shared_ptr<blockchain::BlockHeaderRepository>>();
auto storage = injector.template create<
std::shared_ptr<storage::trie::TrieStorage>>();
initialized =
std::make_shared<runtime::Executor>(std::move(env_factory));
}
return initialized.value();
}),
di::bind<runtime::RawExecutor>.template to<runtime::Executor>(),
di::bind<runtime::TaggedTransactionQueue>.template to<runtime::TaggedTransactionQueueImpl>(),
di::bind<runtime::ParachainHost>.template to<runtime::ParachainHostImpl>(),
@@ -942,6 +932,7 @@ namespace {
di::bind<runtime::AccountNonceApi>.template to<runtime::AccountNonceApiImpl>(),
di::bind<runtime::AuthorityDiscoveryApi>.template to<runtime::AuthorityDiscoveryApiImpl>(),
di::bind<runtime::SingleModuleCache>.template to<runtime::SingleModuleCache>(),
di::bind<runtime::RuntimePropertiesCache>.template to<runtime::RuntimePropertiesCacheImpl>(),
std::forward<Ts>(args)...);
}

27 changes: 19 additions & 8 deletions core/runtime/binaryen/core_api_factory_impl.cpp
Original file line number Diff line number Diff line change
@@ -12,15 +12,19 @@
#include "runtime/common/executor.hpp"
#include "runtime/common/trie_storage_provider_impl.hpp"
#include "runtime/runtime_api/impl/core.hpp"
#include "runtime/runtime_api/impl/runtime_properties_cache_impl.hpp"

namespace kagome::runtime::binaryen {

class OneModuleRepository final : public ModuleRepository {
public:
OneModuleRepository(
const std::vector<uint8_t> &code,
std::shared_ptr<const InstanceEnvironmentFactory> env_factory)
: env_factory_{std::move(env_factory)}, code_{code} {
std::shared_ptr<const InstanceEnvironmentFactory> env_factory,
const common::Hash256 &code_hash)
: env_factory_{std::move(env_factory)},
code_{code},
code_hash_(code_hash) {
BOOST_ASSERT(env_factory_);
}

@@ -29,7 +33,9 @@ namespace kagome::runtime::binaryen {
const primitives::BlockInfo &,
const primitives::BlockHeader &) override {
if (instance_ == nullptr) {
OUTCOME_TRY(module, ModuleImpl::createFromCode(code_, env_factory_));
OUTCOME_TRY(
module,
ModuleImpl::createFromCode(code_, env_factory_, code_hash_));
OUTCOME_TRY(inst, module->instantiate());
instance_ = std::move(inst);
}
@@ -40,6 +46,7 @@ namespace kagome::runtime::binaryen {
std::shared_ptr<ModuleInstance> instance_;
std::shared_ptr<const InstanceEnvironmentFactory> env_factory_;
const std::vector<uint8_t> &code_;
const common::Hash256 code_hash_;
};

class OneCodeProvider final : public RuntimeCodeProvider {
@@ -58,24 +65,28 @@ namespace kagome::runtime::binaryen {
CoreApiFactoryImpl::CoreApiFactoryImpl(
std::shared_ptr<const InstanceEnvironmentFactory> instance_env_factory,
std::shared_ptr<const blockchain::BlockHeaderRepository> header_repo,
std::shared_ptr<storage::changes_trie::ChangesTracker> changes_tracker)
std::shared_ptr<storage::changes_trie::ChangesTracker> changes_tracker,
std::shared_ptr<runtime::RuntimePropertiesCache> cache)
: instance_env_factory_{std::move(instance_env_factory)},
header_repo_{std::move(header_repo)},
changes_tracker_{std::move(changes_tracker)} {
changes_tracker_{std::move(changes_tracker)},
cache_(std::move(cache)) {
BOOST_ASSERT(instance_env_factory_ != nullptr);
BOOST_ASSERT(header_repo_ != nullptr);
BOOST_ASSERT(changes_tracker_ != nullptr);
BOOST_ASSERT(cache_ != nullptr);
}

std::unique_ptr<Core> CoreApiFactoryImpl::make(
std::shared_ptr<const crypto::Hasher> hasher,
const std::vector<uint8_t> &runtime_code) const {
auto code_hash = hasher->sha2_256(runtime_code);
auto env_factory = std::make_shared<runtime::RuntimeEnvironmentFactory>(
std::make_shared<OneCodeProvider>(runtime_code),
std::make_shared<OneModuleRepository>(runtime_code,
instance_env_factory_),
std::make_shared<OneModuleRepository>(
runtime_code, instance_env_factory_, code_hash),
header_repo_);
auto executor = std::make_unique<Executor>(env_factory);
auto executor = std::make_unique<Executor>(env_factory, cache_);
return std::make_unique<CoreImpl>(
std::move(executor), changes_tracker_, header_repo_);
}
5 changes: 4 additions & 1 deletion core/runtime/binaryen/core_api_factory_impl.hpp
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@ namespace kagome::runtime {
class TrieStorageProvider;
class Memory;
class RuntimeEnvironmentFactory;
class RuntimePropertiesCache;
} // namespace kagome::runtime

namespace kagome::runtime::binaryen {
@@ -39,7 +40,8 @@ namespace kagome::runtime::binaryen {
CoreApiFactoryImpl(
std::shared_ptr<const InstanceEnvironmentFactory> instance_env_factory,
std::shared_ptr<const blockchain::BlockHeaderRepository> header_repo,
std::shared_ptr<storage::changes_trie::ChangesTracker> changes_tracker);
std::shared_ptr<storage::changes_trie::ChangesTracker> changes_tracker,
std::shared_ptr<runtime::RuntimePropertiesCache> cache);

std::unique_ptr<Core> make(
std::shared_ptr<const crypto::Hasher> hasher,
@@ -49,6 +51,7 @@ namespace kagome::runtime::binaryen {
std::shared_ptr<const InstanceEnvironmentFactory> instance_env_factory_;
std::shared_ptr<const blockchain::BlockHeaderRepository> header_repo_;
std::shared_ptr<storage::changes_trie::ChangesTracker> changes_tracker_;
std::shared_ptr<runtime::RuntimePropertiesCache> cache_;
};

} // namespace kagome::runtime::binaryen
9 changes: 6 additions & 3 deletions core/runtime/binaryen/instance_environment_factory.cpp
Original file line number Diff line number Diff line change
@@ -17,17 +17,20 @@ namespace kagome::runtime::binaryen {
std::shared_ptr<storage::trie::TrieSerializer> serializer,
std::shared_ptr<host_api::HostApiFactory> host_api_factory,
std::shared_ptr<blockchain::BlockHeaderRepository> block_header_repo,
std::shared_ptr<storage::changes_trie::ChangesTracker> changes_tracker)
std::shared_ptr<storage::changes_trie::ChangesTracker> changes_tracker,
std::shared_ptr<runtime::RuntimePropertiesCache> cache)
: storage_{std::move(storage)},
serializer_{std::move(serializer)},
host_api_factory_{std::move(host_api_factory)},
block_header_repo_{std::move(block_header_repo)},
changes_tracker_{std::move(changes_tracker)} {
changes_tracker_{std::move(changes_tracker)},
cache_(std::move(cache)) {
BOOST_ASSERT(storage_);
BOOST_ASSERT(serializer_);
BOOST_ASSERT(host_api_factory_);
BOOST_ASSERT(block_header_repo_);
BOOST_ASSERT(changes_tracker_);
BOOST_ASSERT(cache_);
}

BinaryenInstanceEnvironment InstanceEnvironmentFactory::make() const {
@@ -37,7 +40,7 @@ namespace kagome::runtime::binaryen {
auto new_storage_provider =
std::make_shared<TrieStorageProviderImpl>(storage_, serializer_);
auto core_factory = std::make_shared<CoreApiFactoryImpl>(
shared_from_this(), block_header_repo_, changes_tracker_);
shared_from_this(), block_header_repo_, changes_tracker_, cache_);
auto host_api = std::shared_ptr<host_api::HostApi>(host_api_factory_->make(
core_factory, new_memory_provider, new_storage_provider));
auto rei = std::make_shared<RuntimeExternalInterface>(host_api);
8 changes: 7 additions & 1 deletion core/runtime/binaryen/instance_environment_factory.hpp
Original file line number Diff line number Diff line change
@@ -25,6 +25,10 @@ namespace kagome::blockchain {
class BlockHeaderRepository;
}

namespace kagome::runtime {
class RuntimePropertiesCache;
}

namespace kagome::runtime::binaryen {

class RuntimeExternalInterface;
@@ -42,7 +46,8 @@ namespace kagome::runtime::binaryen {
std::shared_ptr<storage::trie::TrieSerializer> serializer,
std::shared_ptr<host_api::HostApiFactory> host_api_factory,
std::shared_ptr<blockchain::BlockHeaderRepository> block_header_repo,
std::shared_ptr<storage::changes_trie::ChangesTracker> changes_tracker);
std::shared_ptr<storage::changes_trie::ChangesTracker> changes_tracker,
std::shared_ptr<RuntimePropertiesCache> cache);

[[nodiscard]] BinaryenInstanceEnvironment make() const;

@@ -52,6 +57,7 @@ namespace kagome::runtime::binaryen {
std::shared_ptr<host_api::HostApiFactory> host_api_factory_;
std::shared_ptr<blockchain::BlockHeaderRepository> block_header_repo_;
std::shared_ptr<storage::changes_trie::ChangesTracker> changes_tracker_;
std::shared_ptr<RuntimePropertiesCache> cache_;
};

} // namespace kagome::runtime::binaryen
10 changes: 7 additions & 3 deletions core/runtime/binaryen/module/module_factory_impl.cpp
Original file line number Diff line number Diff line change
@@ -17,16 +17,20 @@ namespace kagome::runtime::binaryen {

ModuleFactoryImpl::ModuleFactoryImpl(
std::shared_ptr<InstanceEnvironmentFactory> env_factory,
std::shared_ptr<storage::trie::TrieStorage> storage)
: env_factory_{std::move(env_factory)}, storage_{std::move(storage)} {
std::shared_ptr<storage::trie::TrieStorage> storage,
std::shared_ptr<crypto::Hasher> hasher)
: env_factory_{std::move(env_factory)},
storage_{std::move(storage)},
hasher_(std::move(hasher)) {
BOOST_ASSERT(env_factory_ != nullptr);
BOOST_ASSERT(storage_ != nullptr);
}

outcome::result<std::unique_ptr<Module>> ModuleFactoryImpl::make(
gsl::span<const uint8_t> code) const {
std::vector<uint8_t> code_vec{code.begin(), code.end()};
auto res = ModuleImpl::createFromCode(code_vec, env_factory_);
auto res = ModuleImpl::createFromCode(
code_vec, env_factory_, hasher_->sha2_256(code));
if (res.has_value()) {
return std::unique_ptr<Module>(std::move(res.value()));
}
8 changes: 7 additions & 1 deletion core/runtime/binaryen/module/module_factory_impl.hpp
Original file line number Diff line number Diff line change
@@ -12,6 +12,10 @@ namespace kagome::runtime {
class TrieStorageProvider;
}

namespace kagome::crypto {
class Hasher;
}

namespace kagome::host_api {
class HostApiFactory;
}
@@ -35,14 +39,16 @@ namespace kagome::runtime::binaryen {
class ModuleFactoryImpl final : public ModuleFactory {
public:
ModuleFactoryImpl(std::shared_ptr<InstanceEnvironmentFactory> env_factory,
std::shared_ptr<storage::trie::TrieStorage> storage);
std::shared_ptr<storage::trie::TrieStorage> storage,
std::shared_ptr<crypto::Hasher> hasher);

outcome::result<std::unique_ptr<Module>> make(
gsl::span<const uint8_t> code) const override;

private:
std::shared_ptr<InstanceEnvironmentFactory> env_factory_;
std::shared_ptr<storage::trie::TrieStorage> storage_;
std::shared_ptr<crypto::Hasher> hasher_;
};

} // namespace kagome::runtime::binaryen
Loading