-
Notifications
You must be signed in to change notification settings - Fork 40
pvf precheck #1705
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
Merged
Merged
pvf precheck #1705
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| /** | ||
| * Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #include "parachain/pvf/precheck.hpp" | ||
|
|
||
| #include "offchain/offchain_worker_factory.hpp" | ||
| #include "offchain/offchain_worker_pool.hpp" | ||
| #include "runtime/common/uncompress_code_if_needed.hpp" | ||
| #include "runtime/module.hpp" | ||
| #include "runtime/module_factory.hpp" | ||
|
|
||
| namespace kagome::parachain { | ||
| PvfPrecheck::PvfPrecheck( | ||
| std::shared_ptr<crypto::Hasher> hasher, | ||
| std::shared_ptr<ValidatorSignerFactory> signer_factory, | ||
| std::shared_ptr<runtime::ParachainHost> parachain_api, | ||
| std::shared_ptr<runtime::ModuleFactory> module_factory, | ||
| std::shared_ptr<runtime::Executor> executor, | ||
| std::shared_ptr<offchain::OffchainWorkerFactory> offchain_worker_factory, | ||
| std::shared_ptr<offchain::OffchainWorkerPool> offchain_worker_pool) | ||
| : hasher_{std::move(hasher)}, | ||
| signer_factory_{std::move(signer_factory)}, | ||
| parachain_api_{std::move(parachain_api)}, | ||
| module_factory_{std::move(module_factory)}, | ||
| executor_{std::move(executor)}, | ||
| offchain_worker_factory_{std::move(offchain_worker_factory)}, | ||
| offchain_worker_pool_{std::move(offchain_worker_pool)} {} | ||
|
|
||
| void PvfPrecheck::start( | ||
| std::shared_ptr<primitives::events::ChainSubscriptionEngine> | ||
| chain_sub_engine) { | ||
| chain_sub_ = std::make_shared<primitives::events::ChainEventSubscriber>( | ||
| chain_sub_engine); | ||
| chain_sub_->subscribe(chain_sub_->generateSubscriptionSetId(), | ||
| primitives::events::ChainEventType::kNewHeads); | ||
| chain_sub_->setCallback( | ||
| [weak = weak_from_this()]( | ||
| subscription::SubscriptionSetId, | ||
| auto &&, | ||
| primitives::events::ChainEventType, | ||
| const primitives::events::ChainEventParams &event) { | ||
| if (auto self = weak.lock()) { | ||
| self->thread_.io_context()->post( | ||
| [weak, | ||
| header{boost::get<primitives::events::HeadsEventParams>(event) | ||
| .get()}] { | ||
| if (auto self = weak.lock()) { | ||
| auto block_hash = self->hasher_->blake2b_256( | ||
| scale::encode(header).value()); | ||
| auto r = self->onBlock(block_hash, header); | ||
| if (r.has_error()) { | ||
| SL_DEBUG(self->logger_, "onBlock error {}", r.error()); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| outcome::result<void> PvfPrecheck::onBlock( | ||
| const BlockHash &block_hash, const primitives::BlockHeader &header) { | ||
| OUTCOME_TRY(signer, signer_factory_->at(block_hash)); | ||
| if (not signer.has_value()) { | ||
| return outcome::success(); | ||
| } | ||
| OUTCOME_TRY(need, parachain_api_->pvfs_require_precheck(block_hash)); | ||
| for (auto &code_hash : need) { | ||
| if (not seen_.emplace(code_hash).second) { | ||
| continue; | ||
| } | ||
| auto code_zstd_res = | ||
| parachain_api_->validation_code_by_hash(block_hash, code_hash); | ||
| if (not code_zstd_res or not code_zstd_res.value()) { | ||
| seen_.erase(code_hash); | ||
| continue; | ||
| } | ||
| auto &code_zstd = *code_zstd_res.value(); | ||
| ParachainRuntime code; | ||
| auto res = [&]() -> outcome::result<void> { | ||
| OUTCOME_TRY(runtime::uncompressCodeIfNeeded(code_zstd, code)); | ||
| OUTCOME_TRY(module_factory_->make(code)); | ||
| return outcome::success(); | ||
| }(); | ||
| if (res) { | ||
| SL_VERBOSE(logger_, "approve {}", code_hash); | ||
| } else { | ||
| SL_WARN(logger_, "reject {}: {}", code_hash, res.error()); | ||
| } | ||
| PvfCheckStatement statement{ | ||
| res.has_value(), | ||
| code_hash, | ||
| signer->getSessionIndex(), | ||
| signer->validatorIndex(), | ||
| }; | ||
| OUTCOME_TRY(signature, signer->signRaw(statement.signable())); | ||
| offchain_worker_pool_->addWorker( | ||
| offchain_worker_factory_->make(executor_, header)); | ||
| auto remove = | ||
| gsl::finally([&] { offchain_worker_pool_->removeWorker(); }); | ||
| OUTCOME_TRY(parachain_api_->submit_pvf_check_statement( | ||
| block_hash, statement, signature)); | ||
| } | ||
| return outcome::success(); | ||
| } | ||
| } // namespace kagome::parachain | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /** | ||
| * Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #ifndef KAGOME_PARACHAIN_PVF_PRECHECK_HPP | ||
| #define KAGOME_PARACHAIN_PVF_PRECHECK_HPP | ||
|
|
||
| #include <unordered_set> | ||
|
|
||
| #include "crypto/hasher.hpp" | ||
| #include "log/logger.hpp" | ||
| #include "parachain/validator/signer.hpp" | ||
| #include "primitives/event_types.hpp" | ||
| #include "runtime/runtime_api/parachain_host.hpp" | ||
| #include "utils/thread_pool.hpp" | ||
|
|
||
| namespace kagome::offchain { | ||
| class OffchainWorkerFactory; | ||
| class OffchainWorkerPool; | ||
| } // namespace kagome::offchain | ||
|
|
||
| namespace kagome::runtime { | ||
| class Executor; | ||
| class ModuleFactory; | ||
| } // namespace kagome::runtime | ||
|
|
||
| namespace kagome::parachain { | ||
| /// Signs pvf check statement for every new head. | ||
| class PvfPrecheck : public std::enable_shared_from_this<PvfPrecheck> { | ||
| public: | ||
| using BroadcastCallback = std::function<void( | ||
| const primitives::BlockHash &, const network::SignedBitfield &)>; | ||
| using Candidates = std::vector<std::optional<network::CandidateHash>>; | ||
|
|
||
| PvfPrecheck( | ||
| std::shared_ptr<crypto::Hasher> hasher, | ||
| std::shared_ptr<ValidatorSignerFactory> signer_factory, | ||
| std::shared_ptr<runtime::ParachainHost> parachain_api, | ||
| std::shared_ptr<runtime::ModuleFactory> module_factory, | ||
| std::shared_ptr<runtime::Executor> executor, | ||
| std::shared_ptr<offchain::OffchainWorkerFactory> | ||
| offchain_worker_factory, | ||
| std::shared_ptr<offchain::OffchainWorkerPool> offchain_worker_pool); | ||
|
|
||
| /// Subscribes to new heads. | ||
| void start(std::shared_ptr<primitives::events::ChainSubscriptionEngine> | ||
| chain_sub_engine); | ||
|
|
||
| private: | ||
| using BlockHash = primitives::BlockHash; | ||
|
|
||
| outcome::result<void> onBlock(const BlockHash &block_hash, | ||
| const primitives::BlockHeader &header); | ||
|
|
||
| std::shared_ptr<crypto::Hasher> hasher_; | ||
| std::shared_ptr<ValidatorSignerFactory> signer_factory_; | ||
| std::shared_ptr<runtime::ParachainHost> parachain_api_; | ||
| std::shared_ptr<runtime::ModuleFactory> module_factory_; | ||
| std::shared_ptr<runtime::Executor> executor_; | ||
| std::shared_ptr<offchain::OffchainWorkerFactory> offchain_worker_factory_; | ||
| std::shared_ptr<offchain::OffchainWorkerPool> offchain_worker_pool_; | ||
| std::shared_ptr<primitives::events::ChainEventSubscriber> chain_sub_; | ||
| std::unordered_set<ValidationCodeHash> seen_; | ||
| ThreadPool thread_{"PvfPrecheck", 1}; | ||
| log::Logger logger_ = log::createLogger("PvfPrecheck", "parachain"); | ||
| }; | ||
| } // namespace kagome::parachain | ||
|
|
||
| #endif // KAGOME_PARACHAIN_PVF_PRECHECK_HPP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.