Skip to content

Commit

Permalink
Make key store optional in host api and fix wavm build (#2056)
Browse files Browse the repository at this point in the history
* Make key store optional in host api and fix wavm build
  • Loading branch information
Harrm authored Apr 23, 2024
1 parent 2160c7d commit 4fc15ff
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 59 deletions.
38 changes: 20 additions & 18 deletions core/host_api/impl/crypto_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace kagome::host_api {
std::shared_ptr<const crypto::Ed25519Provider> ed25519_provider,
std::shared_ptr<const crypto::Secp256k1Provider> secp256k1_provider,
std::shared_ptr<const crypto::Hasher> hasher,
std::shared_ptr<crypto::KeyStore> key_store)
std::optional<std::shared_ptr<crypto::KeyStore>> key_store)
: memory_provider_(std::move(memory_provider)),
sr25519_provider_(std::move(sr25519_provider)),
ecdsa_provider_(std::move(ecdsa_provider)),
Expand All @@ -81,7 +81,7 @@ namespace kagome::host_api {
BOOST_ASSERT(secp256k1_provider_ != nullptr);
BOOST_ASSERT(hasher_ != nullptr);
BOOST_ASSERT(logger_ != nullptr);
BOOST_ASSERT(key_store_ != nullptr);
BOOST_ASSERT(key_store_ == std::nullopt || *key_store_ != nullptr);
}

// ---------------------- hashing ----------------------
Expand Down Expand Up @@ -182,7 +182,7 @@ namespace kagome::host_api {
crypto::KeyType key_type = loadKeyType(key_type_ptr);
checkIfKeyIsSupported(key_type, logger_);

auto public_keys = key_store_->ed25519().getPublicKeys(key_type);
auto public_keys = key_store_.value()->ed25519().getPublicKeys(key_type);
if (not public_keys) {
throw_with_error(
logger_, "error loading public keys: {}", public_keys.error());
Expand All @@ -208,10 +208,10 @@ namespace kagome::host_api {

outcome::result<crypto::Ed25519Keypair> kp_res = [&] {
if (seed_opt.has_value()) {
return key_store_->ed25519().generateKeypair(key_type,
seed_opt.value());
return key_store_.value()->ed25519().generateKeypair(key_type,
seed_opt.value());
} else {
return key_store_->ed25519().generateKeypairOnDisk(key_type);
return key_store_.value()->ed25519().generateKeypairOnDisk(key_type);
}
}();
if (!kp_res) {
Expand Down Expand Up @@ -241,7 +241,8 @@ namespace kagome::host_api {
if (!pk) {
BOOST_UNREACHABLE_RETURN({});
}
auto key_pair_opt = key_store_->ed25519().findKeypair(key_type, pk.value());
auto key_pair_opt =
key_store_.value()->ed25519().findKeypair(key_type, pk.value());
if (!key_pair_opt) {
logger_->error("failed to find required key");
auto error_result = scale::encode(ResultType(std::nullopt)).value();
Expand Down Expand Up @@ -309,7 +310,7 @@ namespace kagome::host_api {
crypto::KeyType key_type = loadKeyType(key_type_ptr);
checkIfKeyIsSupported(key_type, logger_);

auto public_keys = key_store_->sr25519().getPublicKeys(key_type);
auto public_keys = key_store_.value()->sr25519().getPublicKeys(key_type);
if (not public_keys) {
throw_with_error(
logger_, "error loading public keys: {}", public_keys.error());
Expand All @@ -336,10 +337,10 @@ namespace kagome::host_api {
outcome::result<crypto::Sr25519Keypair> kp_res = [&]() {
auto bip39_seed = seed_res.value();
if (bip39_seed.has_value()) {
return key_store_->sr25519().generateKeypair(key_type,
bip39_seed.value());
return key_store_.value()->sr25519().generateKeypair(
key_type, bip39_seed.value());
} else {
return key_store_->sr25519().generateKeypairOnDisk(key_type);
return key_store_.value()->sr25519().generateKeypairOnDisk(key_type);
}
}();
if (!kp_res) {
Expand Down Expand Up @@ -376,7 +377,8 @@ namespace kagome::host_api {
// error is not possible, since we loaded correct number of bytes
BOOST_UNREACHABLE_RETURN({});
}
auto key_pair = key_store_->sr25519().findKeypair(key_type, pk.value());
auto key_pair =
key_store_.value()->sr25519().findKeypair(key_type, pk.value());
if (!key_pair) {
logger_->error(
"failed to find required key: {} {}", key_type, pk.value());
Expand Down Expand Up @@ -581,7 +583,7 @@ namespace kagome::host_api {
crypto::KeyType key_type = loadKeyType(key_type_ptr);
checkIfKeyIsSupported(key_type, logger_);

auto public_keys = key_store_->ecdsa().getPublicKeys(key_type);
auto public_keys = key_store_.value()->ecdsa().getPublicKeys(key_type);
if (not public_keys) {
throw_with_error(
logger_, "error loading public keys: {}", public_keys.error());
Expand All @@ -608,7 +610,7 @@ namespace kagome::host_api {

crypto::EcdsaPublicKey pk;
std::copy(public_buffer.begin(), public_buffer.end(), pk.begin());
auto key_pair = key_store_->ecdsa().findKeypair(key_type, pk);
auto key_pair = key_store_.value()->ecdsa().findKeypair(key_type, pk);
if (!key_pair) {
logger_->error("failed to find required key");
auto error_result = scale::encode(ResultType(std::nullopt)).value();
Expand Down Expand Up @@ -641,7 +643,7 @@ namespace kagome::host_api {

crypto::EcdsaPublicKey pk;
std::copy(public_buffer.begin(), public_buffer.end(), pk.begin());
auto key_pair = key_store_->ecdsa().findKeypair(key_type, pk);
auto key_pair = key_store_.value()->ecdsa().findKeypair(key_type, pk);
if (!key_pair) {
logger_->error("failed to find required key");
auto error_result = scale::encode(ResultType(std::nullopt)).value();
Expand Down Expand Up @@ -677,10 +679,10 @@ namespace kagome::host_api {
outcome::result<crypto::EcdsaKeypair> kp_res = [&]() {
auto bip39_seed = seed_res.value();
if (bip39_seed.has_value()) {
return key_store_->ecdsa().generateKeypair(key_type,
bip39_seed.value());
return key_store_.value()->ecdsa().generateKeypair(key_type,
bip39_seed.value());
} else {
return key_store_->ecdsa().generateKeypairOnDisk(key_type);
return key_store_.value()->ecdsa().generateKeypairOnDisk(key_type);
}
}();
if (!kp_res) {
Expand Down
5 changes: 3 additions & 2 deletions core/host_api/impl/crypto_extension.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace kagome::host_api {
std::shared_ptr<const crypto::Ed25519Provider> ed25519_provider,
std::shared_ptr<const crypto::Secp256k1Provider> secp256k1_provider,
std::shared_ptr<const crypto::Hasher> hasher,
std::shared_ptr<crypto::KeyStore> key_store);
std::optional<std::shared_ptr<crypto::KeyStore>> key_store);

void reset();

Expand Down Expand Up @@ -270,7 +270,8 @@ namespace kagome::host_api {
std::shared_ptr<const crypto::Ed25519Provider> ed25519_provider_;
std::shared_ptr<const crypto::Secp256k1Provider> secp256k1_provider_;
std::shared_ptr<const crypto::Hasher> hasher_;
std::shared_ptr<crypto::KeyStore> key_store_;
// not needed in PVF workers
std::optional<std::shared_ptr<crypto::KeyStore>> key_store_;
log::Logger logger_;
std::optional<runtime::WasmSize> batch_verify_;
};
Expand Down
6 changes: 4 additions & 2 deletions core/host_api/impl/host_api_factory_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ namespace kagome::host_api {
ed25519_provider_(std::move(ed25519_provider)),
secp256k1_provider_(std::move(secp256k1_provider)),
hasher_(std::move(hasher)),
key_store_(std::move(key_store)),
// we do this instead of passing key_store as an optional right away
// because boost.di doesn't like optional<shared_ptr>
key_store_(key_store ? std::optional(key_store) : std::nullopt),
offchain_persistent_storage_(std::move(offchain_persistent_storage)),
offchain_worker_pool_(std::move(offchain_worker_pool)) {
BOOST_ASSERT(sr25519_provider_ != nullptr);
BOOST_ASSERT(ed25519_provider_ != nullptr);
BOOST_ASSERT(secp256k1_provider_ != nullptr);
BOOST_ASSERT(hasher_ != nullptr);
BOOST_ASSERT(key_store_ != nullptr);
BOOST_ASSERT(key_store_ == std::nullopt || *key_store_ != nullptr);
}

std::unique_ptr<HostApi> HostApiFactoryImpl::make(
Expand Down
2 changes: 1 addition & 1 deletion core/host_api/impl/host_api_factory_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace kagome::host_api {
std::shared_ptr<crypto::Ed25519Provider> ed25519_provider_;
std::shared_ptr<crypto::Secp256k1Provider> secp256k1_provider_;
std::shared_ptr<crypto::Hasher> hasher_;
std::shared_ptr<crypto::KeyStore> key_store_;
std::optional<std::shared_ptr<crypto::KeyStore>> key_store_;
std::shared_ptr<offchain::OffchainPersistentStorage>
offchain_persistent_storage_;
std::shared_ptr<offchain::OffchainWorkerPool> offchain_worker_pool_;
Expand Down
2 changes: 1 addition & 1 deletion core/host_api/impl/host_api_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace kagome::host_api {
std::shared_ptr<const crypto::Ed25519Provider> ed25519_provider,
std::shared_ptr<const crypto::Secp256k1Provider> secp256k1_provider,
std::shared_ptr<const crypto::Hasher> hasher,
std::shared_ptr<crypto::KeyStore> key_store,
std::optional<std::shared_ptr<crypto::KeyStore>> key_store,
std::shared_ptr<offchain::OffchainPersistentStorage>
offchain_persistent_storage,
std::shared_ptr<offchain::OffchainWorkerPool> offchain_worker_pool)
Expand Down
2 changes: 1 addition & 1 deletion core/host_api/impl/host_api_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace kagome::host_api {
std::shared_ptr<const crypto::Ed25519Provider> ed25519_provider,
std::shared_ptr<const crypto::Secp256k1Provider> secp256k1_provider,
std::shared_ptr<const crypto::Hasher> hasher,
std::shared_ptr<crypto::KeyStore> key_store,
std::optional<std::shared_ptr<crypto::KeyStore>> key_store,
std::shared_ptr<offchain::OffchainPersistentStorage>
offchain_persistent_storage,
std::shared_ptr<offchain::OffchainWorkerPool> offchain_worker_pool);
Expand Down
74 changes: 40 additions & 34 deletions test/core/runtime/wavm/wavm_module_init_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

#include <filesystem>
#include <string_view>

#include <gtest/gtest.h>
Expand All @@ -12,20 +13,20 @@

#include "blockchain/impl/block_header_repository_impl.hpp" //header_repo
#include "crypto/bip39/impl/bip39_provider_impl.hpp" //bip39_provider
#include "crypto/crypto_store/crypto_store_impl.hpp" //crypto_store
#include "crypto/ecdsa/ecdsa_provider_impl.hpp" //ecdsa_provider
#include "crypto/ed25519/ed25519_provider_impl.hpp" //ed25519_provider
#include "crypto/hasher/hasher_impl.hpp" //hasher
#include "crypto/key_store/key_store_impl.hpp" //crypto_store
#include "crypto/pbkdf2/impl/pbkdf2_provider_impl.hpp" //pbkdf2_provider
#include "crypto/secp256k1/secp256k1_provider_impl.hpp" //secp256k1_provider
#include "crypto/sr25519/sr25519_provider_impl.hpp" //sr25519_provider
#include "host_api/impl/host_api_factory_impl.hpp" // host_api_factory
#include "mock/core/application/app_state_manager_mock.hpp"
#include "offchain/impl/offchain_persistent_storage.hpp" //offchain_persistent_store
#include "offchain/impl/offchain_worker_pool_impl.hpp" //offchain_worker_pool
#include "runtime/common/runtime_properties_cache_impl.hpp" // cache
#include "runtime/executor.hpp"
#include "runtime/memory_provider.hpp"
#include "runtime/module.hpp" // smc
#include "runtime/runtime_context.hpp"
#include "runtime/wavm/compartment_wrapper.hpp" // compartment
#include "runtime/wavm/instance_environment_factory.hpp" // instance_env_factory
Expand Down Expand Up @@ -53,6 +54,9 @@ class WavmModuleInitTest : public ::testing::TestWithParam<std::string_view> {
}

void SetUp() override {
std::filesystem::path base_path{std::filesystem::temp_directory_path()
/ "wasm_module_init_test"};

auto compartment =
std::make_shared<kagome::runtime::wavm::CompartmentWrapper>(
"WAVM Compartment");
Expand All @@ -79,39 +83,42 @@ class WavmModuleInitTest : public ::testing::TestWithParam<std::string_view> {
std::make_shared<kagome::runtime::wavm::IntrinsicModule>(
compartment, module_params->intrinsicMemoryType);

auto hasher = std::make_shared<kagome::crypto::HasherImpl>();
auto sr25519_provider =
std::make_shared<kagome::crypto::Sr25519ProviderImpl>();
auto ecdsa_provider =
std::make_shared<kagome::crypto::EcdsaProviderImpl>(hasher);
auto ed25519_provider =
std::make_shared<kagome::crypto::Ed25519ProviderImpl>(hasher);
auto secp256k1_provider =
std::make_shared<kagome::crypto::Secp256k1ProviderImpl>();
auto pbkdf2_provider =
std::make_shared<kagome::crypto::Pbkdf2ProviderImpl>();
auto bip39_provider = std::make_shared<kagome::crypto::Bip39ProviderImpl>(
pbkdf2_provider, hasher);
auto ecdsa_suite =
std::make_shared<kagome::crypto::EcdsaSuite>(ecdsa_provider);
auto ed_suite =
std::make_shared<kagome::crypto::Ed25519Suite>(ed25519_provider);
auto sr_suite =
std::make_shared<kagome::crypto::Sr25519Suite>(sr25519_provider);
std::shared_ptr<kagome::crypto::KeyFileStorage> key_fs =
kagome::crypto::KeyFileStorage::createAt(
"/tmp/kagome_vawm_tmp_key_storage")
.value();
using namespace kagome::crypto;
auto hasher = std::make_shared<HasherImpl>();
auto csprng =
std::make_shared<libp2p::crypto::random::BoostRandomGenerator>();
auto crypto_store = std::make_shared<kagome::crypto::KeyStoreImpl>(
ecdsa_suite, ed_suite, sr_suite, bip39_provider, csprng, key_fs);
auto ecdsa_provider = std::make_shared<EcdsaProviderImpl>(hasher);
auto ed25519_provider = std::make_shared<Ed25519ProviderImpl>(hasher);
auto sr25519_provider = std::make_shared<Sr25519ProviderImpl>();

auto secp256k1_provider = std::make_shared<Secp256k1ProviderImpl>();
auto pbkdf2_provider = std::make_shared<Pbkdf2ProviderImpl>();
auto bip39_provider =
std::make_shared<Bip39ProviderImpl>(std::move(pbkdf2_provider), hasher);
std::shared_ptr key_file_storage =
kagome::crypto::KeyFileStorage::createAt(base_path).value();
KeyStore::Config config{base_path};
auto key_store = std::make_shared<KeyStore>(
std::make_unique<KeySuiteStoreImpl<Sr25519Provider>>(
std::move(sr25519_provider),
bip39_provider,
csprng,
key_file_storage),
std::make_unique<KeySuiteStoreImpl<Ed25519Provider>>(
ed25519_provider, bip39_provider, csprng, key_file_storage),
std::make_unique<KeySuiteStoreImpl<EcdsaProvider>>(
std::move(ecdsa_provider),
bip39_provider,
csprng,
key_file_storage),
ed25519_provider,
std::make_shared<kagome::application::AppStateManagerMock>(),
config);

rocksdb::Options db_options{};
db_options.create_if_missing = true;
std::shared_ptr<kagome::storage::RocksDb> database =
kagome::storage::RocksDb::create("/tmp/kagome_tmp_db", db_options)
.value();
kagome::storage::RocksDb::create(base_path / "db", db_options).value();

auto header_repo =
std::make_shared<kagome::blockchain::BlockHeaderRepositoryImpl>(
Expand All @@ -130,7 +137,7 @@ class WavmModuleInitTest : public ::testing::TestWithParam<std::string_view> {
ed25519_provider,
secp256k1_provider,
hasher,
crypto_store,
key_store,
offchain_persistent_storage,
offchain_worker_pool);

Expand All @@ -145,7 +152,6 @@ class WavmModuleInitTest : public ::testing::TestWithParam<std::string_view> {
trie_storage,
serializer,
intrinsic_module,
smc,
std::nullopt,
hasher);

Expand All @@ -164,9 +170,9 @@ TEST_P(WavmModuleInitTest, DISABLED_SingleModule) {
auto code_provider = std::make_shared<kagome::runtime::BasicCodeProvider>(
std::string(kBasePath) + std::string(wasm));
EXPECT_OUTCOME_TRUE(code, code_provider->getCodeAt({}));
EXPECT_OUTCOME_TRUE(
runtime_context,
kagome::runtime::RuntimeContextFactory::fromCode(*module_factory_, code));
EXPECT_OUTCOME_TRUE(runtime_context,
kagome::runtime::RuntimeContextFactory::fromCode(
*module_factory_, *code, {}));
EXPECT_OUTCOME_TRUE(response,
runtime_context.module_instance->callExportFunction(
runtime_context, "Core_version", {}));
Expand Down

0 comments on commit 4fc15ff

Please sign in to comment.