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

explicit compile #2140

Merged
merged 11 commits into from
Jul 4, 2024
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
28 changes: 11 additions & 17 deletions core/application/impl/app_configuration_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include <boost/program_options/value_semantic.hpp>
#include <charconv>
#include <filesystem>
#include <limits>
#include <regex>
#include <string>
Expand All @@ -31,9 +30,10 @@
#include "common/hexutil.hpp"
#include "common/uri.hpp"
#include "filesystem/common.hpp"
#include "filesystem/directories.hpp"
#include "log/formatters/filepath.hpp"
#include "utils/mkdirs.hpp"
#include "utils/read_file.hpp"
#include "utils/write_file.hpp"

namespace {
namespace fs = kagome::filesystem;
Expand Down Expand Up @@ -507,7 +507,7 @@ namespace kagome::application {
return false;
}

if (base_path_.empty() or !fs::createDirectoryRecursive(base_path_)) {
if (not mkdirs(base_path_)) {
SL_ERROR(logger_,
"Base path {} does not exist, "
"please specify a valid path with -d option",
Expand Down Expand Up @@ -988,13 +988,10 @@ namespace kagome::application {
}

if (not kagome::filesystem::exists(chain_spec_path_)) {
kagome::filesystem::create_directories(
chain_spec_path_.parent_path());
mkdirs(chain_spec_path_.parent_path()).value();

std::ofstream ofs;
ofs.open(chain_spec_path_.native(), std::ios::ate);
ofs << kagome::assets::embedded_chainspec;
ofs.close();
writeFile(chain_spec_path_, kagome::assets::embedded_chainspec)
.value();

auto chain_spec = ChainSpecImpl::loadFrom(chain_spec_path_.native());
auto path = keystorePath(chain_spec.value()->id());
Expand All @@ -1015,12 +1012,10 @@ namespace kagome::application {
auto ma_res = chain_spec.value()->bootNodes()[0];
listen_addresses_.emplace_back(ma_res);

kagome::filesystem::create_directories(path);
mkdirs(path).value();

for (auto key_descr : kagome::assets::embedded_keys) {
ofs.open((path / key_descr.first).native(), std::ios::ate);
ofs << key_descr.second;
ofs.close();
writeFile(path / key_descr.first, key_descr.second).value();
}
}

Expand Down Expand Up @@ -1476,13 +1471,12 @@ namespace kagome::application {
}
}
{
std::error_code ec;
kagome::filesystem::create_directories(runtimeCacheDirPath(), ec);
if (ec) {
auto r = mkdirs(runtimeCacheDirPath());
if (not r) {
SL_ERROR(logger_,
"Failed to create runtime cache dir {}: {}",
runtimeCacheDirPath(),
ec);
r.error());
return false;
}
}
Expand Down
17 changes: 8 additions & 9 deletions core/application/modes/precompile_wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@

#include "blockchain/block_tree.hpp"
#include "common/bytestr.hpp"
#include "parachain/pvf/pool.hpp"
#include "parachain/pvf/session_params.hpp"
#include "runtime/common/uncompress_code_if_needed.hpp"
#include "runtime/module_factory.hpp"
#include "runtime/wabt/instrument.hpp"
#include "runtime/common/runtime_instances_pool.hpp"
#include "utils/read_file.hpp"

namespace kagome::application::mode {
PrecompileWasmMode::PrecompileWasmMode(
const application::AppConfiguration &app_config,
std::shared_ptr<blockchain::BlockTree> block_tree,
std::shared_ptr<runtime::ParachainHost> parachain_api,
std::shared_ptr<runtime::ModuleFactory> module_factory)
std::shared_ptr<crypto::Hasher> hasher,
std::shared_ptr<parachain::PvfPool> module_factory)
: log_{log::createLogger("PrecompileWasm")},
config_{*app_config.precompileWasm()},
block_tree_{std::move(block_tree)},
parachain_api_{std::move(parachain_api)},
hasher_{std::move(hasher)},
module_factory_{std::move(module_factory)} {}

int PrecompileWasmMode::run() const {
Expand Down Expand Up @@ -55,13 +56,11 @@ namespace kagome::application::mode {
if (text.starts_with("0x")) {
BOOST_OUTCOME_TRY(bytes, common::unhexWith0x(text));
}
Buffer code;
OUTCOME_TRY(runtime::uncompressCodeIfNeeded(bytes, code));
// https://github.com/paritytech/polkadot-sdk/blob/b4ae5b01da280f754ccc00b94314a30b658182a1/polkadot/parachain/src/primitives.rs#L74-L81
auto code_hash = hasher_->blake2b_256(bytes);
OUTCOME_TRY(config,
parachain::sessionParams(*parachain_api_, block.hash));
BOOST_OUTCOME_TRY(
code, runtime::prepareBlobForCompilation(code, config.memory_limits));
OUTCOME_TRY(module_factory_->make(code));
OUTCOME_TRY(module_factory_->precompile(code_hash, bytes, config));
}
return outcome::success();
}
Expand Down
15 changes: 12 additions & 3 deletions core/application/modes/precompile_wasm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ namespace kagome::blockchain {
class BlockTree;
} // namespace kagome::blockchain

namespace kagome::crypto {
class Hasher;
} // namespace kagome::crypto

namespace kagome::parachain {
class PvfPool;
} // namespace kagome::parachain

namespace kagome::runtime {
class ModuleFactory;
class ParachainHost;
} // namespace kagome::runtime

Expand All @@ -28,7 +35,8 @@ namespace kagome::application::mode {
PrecompileWasmMode(const application::AppConfiguration &app_config,
std::shared_ptr<blockchain::BlockTree> block_tree,
std::shared_ptr<runtime::ParachainHost> parachain_api,
std::shared_ptr<runtime::ModuleFactory> module_factory);
std::shared_ptr<crypto::Hasher> hasher,
std::shared_ptr<parachain::PvfPool> module_factory);

int run() const override;

Expand All @@ -39,6 +47,7 @@ namespace kagome::application::mode {
application::PrecompileWasmConfig config_;
std::shared_ptr<blockchain::BlockTree> block_tree_;
std::shared_ptr<runtime::ParachainHost> parachain_api_;
std::shared_ptr<runtime::ModuleFactory> module_factory_;
std::shared_ptr<crypto::Hasher> hasher_;
std::shared_ptr<parachain::PvfPool> module_factory_;
};
} // namespace kagome::application::mode
4 changes: 3 additions & 1 deletion core/consensus/timeline/impl/block_executor_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "storage/changes_trie/impl/storage_changes_tracker_impl.hpp"
#include "transaction_pool/transaction_pool.hpp"
#include "transaction_pool/transaction_pool_error.hpp"
#include "utils/pool_handler_ready_make.hpp"

namespace kagome::consensus {

Expand All @@ -43,7 +44,8 @@ namespace kagome::consensus {
std::unique_ptr<BlockAppenderBase> appender)
: block_tree_{std::move(block_tree)},
main_pool_handler_{main_thread_pool.handler(app_state_manager)},
worker_pool_handler_{worker_thread_pool.handler(app_state_manager)},
worker_pool_handler_{
poolHandlerReadyMake(app_state_manager, worker_thread_pool)},
core_{std::move(core)},
tx_pool_{std::move(tx_pool)},
hasher_{std::move(hasher)},
Expand Down
3 changes: 2 additions & 1 deletion core/consensus/timeline/impl/block_executor_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace kagome {
class PoolHandler;
class PoolHandlerReady;
} // namespace kagome

namespace kagome::application {
Expand Down Expand Up @@ -86,7 +87,7 @@ namespace kagome::consensus {
private:
std::shared_ptr<blockchain::BlockTree> block_tree_;
std::shared_ptr<PoolHandler> main_pool_handler_;
std::shared_ptr<PoolHandler> worker_pool_handler_;
std::shared_ptr<PoolHandlerReady> worker_pool_handler_;
std::shared_ptr<runtime::Core> core_;
std::shared_ptr<transaction_pool::TransactionPool> tx_pool_;
std::shared_ptr<crypto::Hasher> hasher_;
Expand Down
11 changes: 10 additions & 1 deletion core/consensus/timeline/impl/timeline_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "application/app_state_manager.hpp"
#include "blockchain/block_tree.hpp"
#include "clock/impl/clock_impl.hpp"
#include "common/main_thread_pool.hpp"
#include "consensus/consensus_selector.hpp"
#include "consensus/grandpa/justification_observer.hpp"
#include "consensus/timeline/impl/slot_leadership_error.hpp"
Expand Down Expand Up @@ -37,6 +38,7 @@ namespace kagome::consensus {
const application::AppConfiguration &app_config,
std::shared_ptr<application::AppStateManager> app_state_manager,
const clock::SystemClock &clock,
common::MainThreadPool &main_thread_pool,
std::shared_ptr<SlotsUtil> slots_util,
std::shared_ptr<blockchain::BlockTree> block_tree,
std::shared_ptr<ConsensusSelector> consensus_selector,
Expand All @@ -57,6 +59,7 @@ namespace kagome::consensus {
: log_(log::createLogger("Timeline", "timeline")),
app_state_manager_(std::move(app_state_manager)),
clock_(clock),
main_pool_handler_{main_thread_pool.handlerStarted()},
slots_util_(std::move(slots_util)),
block_tree_(std::move(block_tree)),
consensus_selector_(std::move(consensus_selector)),
Expand Down Expand Up @@ -272,7 +275,13 @@ namespace kagome::consensus {

if (validator_status == ValidatorStatus::SingleValidator) {
SL_INFO(log_, "Starting single validating node.");
onSynchronized();
main_pool_handler_->execute([weak_self{weak_from_this()}] {
auto self = weak_self.lock();
if (not self) {
return;
}
self->onSynchronized();
});
return true;
}
}
Expand Down
10 changes: 10 additions & 0 deletions core/consensus/timeline/impl/timeline_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
#include "primitives/event_types.hpp"
#include "telemetry/service.hpp"

namespace kagome {
class PoolHandler;
} // namespace kagome

namespace kagome::application {
class AppStateManager;
class AppConfiguration;
Expand All @@ -27,6 +31,10 @@ namespace kagome::blockchain {
class BlockTree;
}

namespace kagome::common {
class MainThreadPool;
} // namespace kagome::common

namespace kagome::consensus {
class SlotsUtil;
class ConsensusSelector;
Expand Down Expand Up @@ -71,6 +79,7 @@ namespace kagome::consensus {
const application::AppConfiguration &app_config,
std::shared_ptr<application::AppStateManager> app_state_manager,
const clock::SystemClock &clock,
common::MainThreadPool &main_thread_pool,
std::shared_ptr<SlotsUtil> slots_util,
std::shared_ptr<blockchain::BlockTree> block_tree,
std::shared_ptr<ConsensusSelector> consensus_selector,
Expand Down Expand Up @@ -138,6 +147,7 @@ namespace kagome::consensus {

std::shared_ptr<application::AppStateManager> app_state_manager_;
const clock::SystemClock &clock_;
std::shared_ptr<PoolHandler> main_pool_handler_;
std::shared_ptr<SlotsUtil> slots_util_;
std::shared_ptr<blockchain::BlockTree> block_tree_;
std::shared_ptr<ConsensusSelector> consensus_selector_;
Expand Down
43 changes: 4 additions & 39 deletions core/crypto/key_store/key_file_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

#include "crypto/key_store/key_file_storage.hpp"

#include <fstream>

#include "common/hexutil.hpp"
#include "crypto/key_store/key_type.hpp"
#include "filesystem/common.hpp"
#include "utils/json_unquote.hpp"
#include "utils/mkdirs.hpp"
#include "utils/read_file.hpp"
#include "utils/write_file.hpp"

OUTCOME_CPP_DEFINE_CATEGORY(kagome::crypto, KeyFileStorage::Error, e) {
using E = kagome::crypto::KeyFileStorage::Error;
Expand All @@ -29,10 +29,6 @@ OUTCOME_CPP_DEFINE_CATEGORY(kagome::crypto, KeyFileStorage::Error, e) {
return "specified key file is invalid";
case E::INCONSISTENT_KEYFILE:
return "key file is inconsistent, public key != derived public key";
case E::KEYS_PATH_IS_NOT_DIRECTORY:
return "specified key storage directory path is not a directory";
case E::FAILED_CREATE_KEYS_DIRECTORY:
return "failed to create key storage directory";
}
return "unknown KeyFileStorage error";
}
Expand Down Expand Up @@ -80,44 +76,13 @@ namespace kagome::crypto {
}

outcome::result<void> KeyFileStorage::initialize() {
std::error_code ec{};
bool does_exist = filesystem::exists(keystore_path_, ec);
if (ec and ec != std::errc::no_such_file_or_directory) {
logger_->error("Error initializing key storage: {}", ec);
return outcome::failure(ec);
}
if (does_exist) {
// check whether specified path is a directory
if (not filesystem::is_directory(keystore_path_, ec)) {
return Error::KEYS_PATH_IS_NOT_DIRECTORY;
}
if (ec) {
logger_->error("Error scanning key storage: {}", ec);
return outcome::failure(ec);
}
} else {
// try create directory
if (not filesystem::create_directories(keystore_path_, ec)) {
return Error::FAILED_CREATE_KEYS_DIRECTORY;
}
if (ec) {
logger_->error("Error creating keystore dir: {}", ec);
return outcome::failure(ec);
}
}

OUTCOME_TRY(mkdirs(keystore_path_));
return outcome::success();
}

outcome::result<void> KeyFileStorage::saveKeyHexAtPath(
common::BufferView private_key, const KeyFileStorage::Path &path) const {
std::ofstream file;
file.open(path.native(), std::ios::out | std::ios::trunc);
if (!file.is_open()) {
return Error::FAILED_OPEN_FILE;
}
auto hex = common::hex_lower_0x(private_key);
file << hex;
OUTCOME_TRY(writeFile(path, common::hex_lower_0x(private_key)));
SL_TRACE(logger_, "Saving key to {}", path.native());
return outcome::success();
}
Expand Down
2 changes: 0 additions & 2 deletions core/crypto/key_store/key_file_storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ namespace kagome::crypto {
FILE_DOESNT_EXIST,
INVALID_FILE_FORMAT,
INCONSISTENT_KEYFILE,
KEYS_PATH_IS_NOT_DIRECTORY,
FAILED_CREATE_KEYS_DIRECTORY
};

using Buffer = common::Buffer;
Expand Down
22 changes: 0 additions & 22 deletions core/filesystem/directories.hpp

This file was deleted.

1 change: 1 addition & 0 deletions core/host_api/impl/host_api_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ namespace kagome::host_api {
misc_ext_{DEFAULT_CHAIN_ID,
hasher,
memory_provider_,
storage_provider_,
std::move(core_provider)},
storage_ext_(storage_provider_, memory_provider_, hasher),
child_storage_ext_(storage_provider_, memory_provider_),
Expand Down
Loading
Loading