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

Fix runtime api #1464

Merged
merged 5 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion core/authorship/impl/block_builder_factory_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace kagome::authorship {
return res.error();
} else {
return std::make_unique<BlockBuilderImpl>(
header, res.value(), r_block_builder_);
header, std::move(res.value()), r_block_builder_);
}
}

Expand Down
22 changes: 6 additions & 16 deletions core/authorship/impl/block_builder_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,24 @@ namespace kagome::authorship {

BlockBuilderImpl::BlockBuilderImpl(
primitives::BlockHeader block_header,
const storage::trie::RootHash &storage_state,
std::unique_ptr<runtime::RuntimeEnvironment> env,
std::shared_ptr<runtime::BlockBuilder> block_builder_api)
: block_header_{std::move(block_header)},
block_builder_api_{std::move(block_builder_api)},
storage_state_{std::move(storage_state)},
env_{std::move(env)},
logger_{log::createLogger("BlockBuilder", "authorship")} {
BOOST_ASSERT(block_builder_api_ != nullptr);
}

outcome::result<std::vector<primitives::Extrinsic>>
BlockBuilderImpl::getInherentExtrinsics(
const primitives::InherentData &data) const {
return block_builder_api_->inherent_extrinsics(
{block_header_.number - 1, block_header_.parent_hash},
storage_state_,
data);
return block_builder_api_->inherent_extrinsics(*env_, data);
}

outcome::result<primitives::ExtrinsicIndex> BlockBuilderImpl::pushExtrinsic(
const primitives::Extrinsic &extrinsic) {
auto apply_res = block_builder_api_->apply_extrinsic(
{block_header_.number - 1, block_header_.parent_hash},
storage_state_,
extrinsic);
auto apply_res = block_builder_api_->apply_extrinsic(*env_, extrinsic);
if (not apply_res) {
// Takes place when API method execution fails for some technical kind of
// problem. This WON'T be executed when apply_extrinsic returned an error
Expand All @@ -52,11 +46,10 @@ namespace kagome::authorship {
apply_res.error());
return apply_res.error();
}
storage_state_ = apply_res.value().new_storage_root;

using return_type = outcome::result<primitives::ExtrinsicIndex>;
return visit_in_place(
apply_res.value().result,
apply_res.value(),
[this, &extrinsic](
const primitives::DispatchOutcome &outcome) -> return_type {
if (1 == outcome.which()) { // DispatchError
Expand Down Expand Up @@ -102,10 +95,7 @@ namespace kagome::authorship {
}

outcome::result<primitives::Block> BlockBuilderImpl::bake() const {
OUTCOME_TRY(finalized_header,
block_builder_api_->finalize_block(
{block_header_.number - 1, block_header_.parent_hash},
storage_state_));
OUTCOME_TRY(finalized_header, block_builder_api_->finalize_block(*env_));
return primitives::Block{finalized_header, extrinsics_};
}

Expand Down
5 changes: 3 additions & 2 deletions core/authorship/impl/block_builder_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "primitives/event_types.hpp"
#include "runtime/runtime_api/block_builder.hpp"
#include "runtime/runtime_api/core.hpp"
#include "runtime/runtime_environment_factory.hpp"

namespace kagome::authorship {

Expand All @@ -21,7 +22,7 @@ namespace kagome::authorship {
~BlockBuilderImpl() override = default;

BlockBuilderImpl(primitives::BlockHeader block_header,
const storage::trie::RootHash &storage_state,
std::unique_ptr<runtime::RuntimeEnvironment> env,
std::shared_ptr<runtime::BlockBuilder> block_builder_api);

outcome::result<std::vector<primitives::Extrinsic>> getInherentExtrinsics(
Expand All @@ -39,7 +40,7 @@ namespace kagome::authorship {

primitives::BlockHeader block_header_;
std::shared_ptr<runtime::BlockBuilder> block_builder_api_;
storage::trie::RootHash storage_state_;
std::unique_ptr<runtime::RuntimeEnvironment> env_;
log::Logger logger_;

std::vector<primitives::Extrinsic> extrinsics_{};
Expand Down
105 changes: 11 additions & 94 deletions core/runtime/common/executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "runtime/memory_provider.hpp"
#include "runtime/module_instance.hpp"
#include "runtime/module_repository.hpp"
#include "runtime/persistent_result.hpp"
#include "runtime/runtime_api/metadata.hpp"
#include "runtime/runtime_environment_factory.hpp"
#include "runtime/runtime_properties_cache.hpp"
Expand Down Expand Up @@ -52,84 +51,18 @@ namespace kagome::runtime {
std::shared_ptr<RuntimePropertiesCache> cache)
: env_factory_(std::move(env_factory)),
cache_(std::move(cache)),
logger_{log::createLogger("Executor", "runtime")} {
BOOST_ASSERT(env_factory_ != nullptr);
}

/**
* Call a runtime method in a persistent environment, e. g. the storage
* changes, made by this call, will persist in the node's Trie storage
* The call will be done with the runtime code from \param block_info state
* on \param storage_state storage state
*/
template <typename Result, typename... Args>
outcome::result<PersistentResult<Result>> persistentCallAt(
primitives::BlockInfo const &block_info,
storage::trie::RootHash const &storage_state,
std::string_view name,
Args &&...args) {
OUTCOME_TRY(
env,
env_factory_->start(block_info, storage_state)->persistent().make());
auto res = callInternal<Result>(*env, name, std::forward<Args>(args)...);
if (res) {
OUTCOME_TRY(new_state_root, commitState(*env));
if constexpr (std::is_void_v<Result>) {
return PersistentResult<Result>{new_state_root};
} else {
return PersistentResult<Result>{std::move(res.value()),
new_state_root};
}
}
return res.error();
}

/**
* Call a runtime method in a persistent environment, e. g. the storage
* changes, made by this call, will persist in the node's Trie storage
* The call will be done on the genesis block state
*/
template <typename Result, typename... Args>
outcome::result<PersistentResult<Result>> persistentCallAtGenesis(
std::string_view name, Args &&...args) {
OUTCOME_TRY(env_template, env_factory_->start());
OUTCOME_TRY(env, env_template->persistent().make());
auto res = callInternal<Result>(*env, name, std::forward<Args>(args)...);
if (res) {
OUTCOME_TRY(new_state_root, commitState(*env));
if constexpr (std::is_void_v<Result>) {
return PersistentResult<Result>{new_state_root};
} else {
return PersistentResult<Result>{std::move(res.value()),
new_state_root};
}
}
return res.error();
}
logger_{log::createLogger("Executor", "runtime")} {}

/**
* Call a runtime method in a persistent environment, e. g. the storage
* changes, made by this call, will persist in the node's Trie storage
* The call will be done on the \param block_info state
*/
template <typename Result, typename... Args>
outcome::result<PersistentResult<Result>> persistentCallAt(
primitives::BlockHash const &block_hash,
std::string_view name,
Args &&...args) {
outcome::result<std::unique_ptr<RuntimeEnvironment>> persistentAt(
primitives::BlockHash const &block_hash) {
OUTCOME_TRY(env_template, env_factory_->start(block_hash));
OUTCOME_TRY(env, env_template->persistent().make());
auto res = callInternal<Result>(*env, name, std::forward<Args>(args)...);
if (res) {
OUTCOME_TRY(new_state_root, commitState(*env));
if constexpr (std::is_void_v<Result>) {
return PersistentResult<Result>{new_state_root};
} else {
return PersistentResult<Result>{std::move(res.value()),
new_state_root};
}
}
return res.error();
return std::move(env);
}

/**
Expand Down Expand Up @@ -200,7 +133,6 @@ namespace kagome::runtime {
return result;
}

private:
/**
* Internal method for calling a Runtime API method
* Resets the runtime memory with the module's heap base,
Expand All @@ -215,20 +147,20 @@ namespace kagome::runtime {
if constexpr (std::is_same_v<Result, primitives::Version>) {
if (likely(name == "Core_version")) {
return cache_->getVersion(env.module_instance->getCodeHash(), [&] {
return callInternal<Result>(env, name, std::forward<Args>(args)...);
return call<Result>(env, name, std::forward<Args>(args)...);
});
}
}

if constexpr (std::is_same_v<Result, primitives::OpaqueMetadata>) {
if (likely(name == "Metadata_metadata")) {
return cache_->getMetadata(env.module_instance->getCodeHash(), [&] {
return callInternal<Result>(env, name, std::forward<Args>(args)...);
return call<Result>(env, name, std::forward<Args>(args)...);
});
}
}

return callInternal<Result>(env, name, std::forward<Args>(args)...);
return call<Result>(env, name, std::forward<Args>(args)...);
}

/**
Expand All @@ -239,9 +171,9 @@ namespace kagome::runtime {
* Changes, made to the Host API state, are reset after the call.
*/
template <typename Result, typename... Args>
outcome::result<Result> callInternal(RuntimeEnvironment &env,
std::string_view name,
Args &&...args) {
outcome::result<Result> call(RuntimeEnvironment &env,
std::string_view name,
Args &&...args) {
auto &memory = env.memory_provider->getCurrentMemory()->get();

Buffer encoded_args{};
Expand Down Expand Up @@ -283,22 +215,7 @@ namespace kagome::runtime {
}
}

outcome::result<storage::trie::RootHash> commitState(
const RuntimeEnvironment &env) {
KAGOME_PROFILE_START(state_commit)
BOOST_ASSERT_MSG(
env.storage_provider->tryGetPersistentBatch(),
"Current batch should always be persistent for a persistent call");
auto persistent_batch =
env.storage_provider->tryGetPersistentBatch().value();
OUTCOME_TRY(new_state_root, persistent_batch->commit());
SL_DEBUG(logger_,
"Runtime call committed new state with hash {}",
new_state_root.toHex());
KAGOME_PROFILE_END(state_commit)
return std::move(new_state_root);
}

private:
std::shared_ptr<RuntimeEnvironmentFactory> env_factory_;
std::shared_ptr<RuntimePropertiesCache> cache_;
log::Logger logger_;
Expand Down
24 changes: 0 additions & 24 deletions core/runtime/persistent_result.hpp

This file was deleted.

15 changes: 6 additions & 9 deletions core/runtime/runtime_api/block_builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
#include "primitives/check_inherents_result.hpp"
#include "primitives/extrinsic.hpp"
#include "primitives/inherent_data.hpp"
#include "runtime/persistent_result.hpp"

namespace kagome::runtime {
class RuntimeEnvironment;

/**
* Part of runtime API responsible for building a block for a runtime.
*/
Expand All @@ -28,25 +29,21 @@ namespace kagome::runtime {
/**
* Apply the given extrinsic.
*/
virtual outcome::result<PersistentResult<primitives::ApplyExtrinsicResult>>
apply_extrinsic(const primitives::BlockInfo &block,
storage::trie::RootHash const &storage_hash,
const primitives::Extrinsic &extrinsic) = 0;
virtual outcome::result<primitives::ApplyExtrinsicResult> apply_extrinsic(
RuntimeEnvironment &env, const primitives::Extrinsic &extrinsic) = 0;

/**
* Finish the current block.
*/
virtual outcome::result<primitives::BlockHeader> finalize_block(
const primitives::BlockInfo &block,
storage::trie::RootHash const &storage_hash) = 0;
RuntimeEnvironment &env) = 0;

/**
* Generate inherent extrinsics. The inherent data will vary from chain to
* chain.
*/
virtual outcome::result<std::vector<primitives::Extrinsic>>
inherent_extrinsics(const primitives::BlockInfo &block,
storage::trie::RootHash const &storage_hash,
inherent_extrinsics(RuntimeEnvironment &env,
const primitives::InherentData &data) = 0;

/**
Expand Down
12 changes: 10 additions & 2 deletions core/runtime/runtime_api/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "primitives/common.hpp"
#include "primitives/transaction_validity.hpp"
#include "primitives/version.hpp"
#include "runtime/runtime_environment_factory.hpp"

namespace kagome::runtime {
class RuntimeCodeProvider;
Expand All @@ -27,6 +28,13 @@ namespace kagome::runtime {
public:
virtual ~Core() = default;

/**
* @brief Returns the version of the runtime
* @return runtime version
*/
virtual outcome::result<primitives::Version> version(
RuntimeEnvironment &env) = 0;

/**
* @brief Returns the version of the runtime
* @return runtime version
Expand All @@ -52,8 +60,8 @@ namespace kagome::runtime {
* @brief Initialize a block with the given header.
* @param header header used for block initialization
*/
virtual outcome::result<storage::trie::RootHash> initialize_block(
const primitives::BlockHeader &header) = 0;
virtual outcome::result<std::unique_ptr<RuntimeEnvironment>>
initialize_block(const primitives::BlockHeader &header) = 0;
};

} // namespace kagome::runtime
Expand Down
Loading