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

simplify grandpa api #1763

Merged
merged 11 commits into from
Sep 7, 2023
3 changes: 0 additions & 3 deletions core/consensus/grandpa/impl/grandpa_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ namespace kagome::consensus::grandpa {
std::shared_ptr<crypto::Hasher> hasher,
std::shared_ptr<Environment> environment,
std::shared_ptr<crypto::Ed25519Provider> crypto_provider,
std::shared_ptr<runtime::GrandpaApi> grandpa_api,
std::shared_ptr<crypto::SessionKeys> session_keys,
const application::ChainSpec &chain_spec,
std::shared_ptr<AuthorityManager> authority_manager,
Expand All @@ -86,7 +85,6 @@ namespace kagome::consensus::grandpa {
hasher_{std::move(hasher)},
environment_{std::move(environment)},
crypto_provider_{std::move(crypto_provider)},
grandpa_api_{std::move(grandpa_api)},
session_keys_{std::move(session_keys)},
authority_manager_(std::move(authority_manager)),
synchronizer_(std::move(synchronizer)),
Expand All @@ -103,7 +101,6 @@ namespace kagome::consensus::grandpa {
libp2p::basic::Scheduler::Config{})} {
BOOST_ASSERT(environment_ != nullptr);
BOOST_ASSERT(crypto_provider_ != nullptr);
BOOST_ASSERT(grandpa_api_ != nullptr);
BOOST_ASSERT(scheduler_ != nullptr);
BOOST_ASSERT(authority_manager_ != nullptr);
BOOST_ASSERT(synchronizer_ != nullptr);
Expand Down
11 changes: 1 addition & 10 deletions core/consensus/grandpa/impl/grandpa_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef KAGOME_CONSENSUS_GRANDPA_GRANDPAIMPL
#define KAGOME_CONSENSUS_GRANDPA_GRANDPAIMPL
#pragma once

#include "consensus/grandpa/grandpa.hpp"
#include "consensus/grandpa/grandpa_observer.hpp"
Expand Down Expand Up @@ -46,10 +45,6 @@ namespace kagome::network {
class Synchronizer;
} // namespace kagome::network

namespace kagome::runtime {
class GrandpaApi;
}

namespace kagome::consensus::grandpa {

// clang-format off
Expand Down Expand Up @@ -98,7 +93,6 @@ namespace kagome::consensus::grandpa {
std::shared_ptr<crypto::Hasher> hasher,
std::shared_ptr<Environment> environment,
std::shared_ptr<crypto::Ed25519Provider> crypto_provider,
std::shared_ptr<runtime::GrandpaApi> grandpa_api,
std::shared_ptr<crypto::SessionKeys> session_keys,
const application::ChainSpec &chain_spec,
std::shared_ptr<AuthorityManager> authority_manager,
Expand Down Expand Up @@ -307,7 +301,6 @@ namespace kagome::consensus::grandpa {
std::shared_ptr<crypto::Hasher> hasher_;
std::shared_ptr<Environment> environment_;
std::shared_ptr<crypto::Ed25519Provider> crypto_provider_;
std::shared_ptr<runtime::GrandpaApi> grandpa_api_;
std::shared_ptr<crypto::SessionKeys> session_keys_;
std::shared_ptr<AuthorityManager> authority_manager_;
std::shared_ptr<network::Synchronizer> synchronizer_;
Expand Down Expand Up @@ -343,5 +336,3 @@ namespace kagome::consensus::grandpa {
};

} // namespace kagome::consensus::grandpa

#endif // KAGOME_CONSENSUS_GRANDPA_GRANDPAIMPL
14 changes: 8 additions & 6 deletions core/parachain/approval/approval_distribution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "clock/impl/basic_waitable_timer.hpp"
#include "common/visitor.hpp"
#include "consensus/babe/babe_config_repository.hpp"
#include "consensus/babe/impl/babe_digests_util.hpp"
#include "crypto/crypto_store.hpp"
#include "crypto/hasher.hpp"
Expand Down Expand Up @@ -463,7 +464,7 @@ namespace {
namespace kagome::parachain {

ApprovalDistribution::ApprovalDistribution(
std::shared_ptr<runtime::BabeApi> babe_api,
std::shared_ptr<consensus::babe::BabeConfigRepository> babe_config_repo,
std::shared_ptr<application::AppStateManager> app_state_manager,
std::shared_ptr<ThreadPool> thread_pool,
std::shared_ptr<runtime::ParachainHost> parachain_host,
Expand Down Expand Up @@ -494,7 +495,7 @@ namespace kagome::parachain {
crypto_provider_(std::move(crypto_provider)),
pm_(std::move(pm)),
router_(std::move(router)),
babe_api_(std::move(babe_api)),
babe_config_repo_(std::move(babe_config_repo)),
block_tree_(std::move(block_tree)),
pvf_(std::move(pvf)),
recovery_(std::move(recovery)),
Expand All @@ -510,7 +511,7 @@ namespace kagome::parachain {
BOOST_ASSERT(crypto_provider_);
BOOST_ASSERT(pm_);
BOOST_ASSERT(router_);
BOOST_ASSERT(babe_api_);
BOOST_ASSERT(babe_config_repo_);
BOOST_ASSERT(block_tree_);
BOOST_ASSERT(pvf_);
BOOST_ASSERT(recovery_);
Expand Down Expand Up @@ -895,15 +896,16 @@ namespace kagome::parachain {
const primitives::BlockHeader &block_header,
const primitives::BlockHash &block_hash) {
OUTCOME_TRY(babe_digests, consensus::babe::getBabeDigests(block_header));
OUTCOME_TRY(babe_config, babe_api_->configuration(block_hash));
OUTCOME_TRY(epoch,
babe_util_->slotToEpoch(*block_header.parentInfo(),
babe_digests.second.slot_number));
OUTCOME_TRY(babe_config,
babe_config_repo_->config(*block_header.parentInfo(), epoch));

return std::make_tuple(epoch,
std::move(babe_digests.second),
std::move(babe_config.authorities),
std::move(babe_config.randomness));
std::move(babe_config->authorities),
std::move(babe_config->randomness));
}

outcome::result<ApprovalDistribution::CandidateIncludedList>
Expand Down
14 changes: 7 additions & 7 deletions core/parachain/approval/approval_distribution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef KAGOME_APPROVAL_DISTRIBUTION_HPP
#define KAGOME_APPROVAL_DISTRIBUTION_HPP
#pragma once

#include <unordered_set>
#include <vector>
Expand Down Expand Up @@ -32,12 +31,15 @@
#include "parachain/approval/store.hpp"
#include "parachain/availability/recovery/recovery.hpp"
#include "parachain/validator/parachain_processor.hpp"
#include "runtime/runtime_api/babe_api.hpp"
#include "runtime/runtime_api/parachain_host.hpp"
#include "runtime/runtime_api/parachain_host_types.hpp"
#include "utils/safe_object.hpp"
#include "utils/thread_pool.hpp"

namespace kagome::consensus::babe {
class BabeConfigRepository;
} // namespace kagome::consensus::babe

namespace kagome::parachain {
using DistributeAssignment = network::Assignment;

Expand Down Expand Up @@ -257,7 +259,7 @@ namespace kagome::parachain {
};

ApprovalDistribution(
std::shared_ptr<runtime::BabeApi> babe_api,
std::shared_ptr<consensus::babe::BabeConfigRepository> babe_config_repo,
std::shared_ptr<application::AppStateManager> app_state_manager,
std::shared_ptr<ThreadPool> thread_pool,
std::shared_ptr<runtime::ParachainHost> parachain_host,
Expand Down Expand Up @@ -696,7 +698,7 @@ namespace kagome::parachain {
std::shared_ptr<crypto::Sr25519Provider> crypto_provider_;
std::shared_ptr<network::PeerManager> pm_;
std::shared_ptr<network::Router> router_;
std::shared_ptr<runtime::BabeApi> babe_api_;
std::shared_ptr<consensus::babe::BabeConfigRepository> babe_config_repo_;
std::shared_ptr<blockchain::BlockTree> block_tree_;
std::shared_ptr<parachain::Pvf> pvf_;
std::shared_ptr<parachain::Recovery> recovery_;
Expand Down Expand Up @@ -732,5 +734,3 @@ namespace kagome::parachain {
} // namespace kagome::parachain

OUTCOME_HPP_DECLARE_ERROR(kagome::parachain, ApprovalDistribution::Error);

#endif // KAGOME_APPROVAL_DISTRIBUTION_HPP
40 changes: 2 additions & 38 deletions core/runtime/runtime_api/grandpa_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,10 @@
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef KAGOME_RUNTIME_GRANDPAAPI
#define KAGOME_RUNTIME_GRANDPAAPI
#pragma once

#include <optional>

#include "common/buffer.hpp"
#include "outcome/outcome.hpp"
#include "primitives/authority.hpp"
#include "primitives/block_id.hpp"
#include "primitives/common.hpp"
#include "primitives/digest.hpp"
#include "primitives/scheduled_change.hpp"
#include "primitives/session_key.hpp"

namespace kagome::runtime {
// https://github.com/paritytech/substrate/blob/8bf08ca63491961fafe6adf414a7411cb3953dcf/core/finality-grandpa/primitives/src/lib.rs#L56
Expand All @@ -25,42 +16,17 @@ namespace kagome::runtime {
*/
class GrandpaApi {
protected:
using Digest = primitives::Digest;
using ScheduledChange = primitives::ScheduledChange;
using BlockNumber = primitives::BlockNumber;
using AuthorityList = primitives::AuthorityList;
using ForcedChange = primitives::ForcedChange;
using BlockId = primitives::BlockId;

public:
virtual ~GrandpaApi() = default;
/**
* @brief calls Grandpa_pending_change runtime api function,
* which checks a digest for pending changes.
* @param digest digest to check
* @return nullopt if there are no pending changes,
* scheduled change item if exists or error if error occured
*/
virtual outcome::result<std::optional<ScheduledChange>> pending_change(
primitives::BlockHash const &block, const Digest &digest) = 0;

/**
* @brief calls Grandpa_forced_change runtime api function
* which checks a digest for forced changes
* @param digest digest to check
* @return nullopt if no forced changes,
* forced change item if exists or error if error occured
*
*/
virtual outcome::result<std::optional<ForcedChange>> forced_change(
primitives::BlockHash const &block, const Digest &digest) = 0;

/**
* @brief calls Grandpa_authorities runtime api function
* @return collection of current grandpa authorities with their weights
*/
virtual outcome::result<AuthorityList> authorities(
const primitives::BlockId &block_id) = 0;
const primitives::BlockHash &block_hash) = 0;

/**
* @return the id of the current voter set at the provided block
Expand All @@ -70,5 +36,3 @@ namespace kagome::runtime {
};

} // namespace kagome::runtime

#endif // KAGOME_RUNTIME_GRANDPAAPI
1 change: 0 additions & 1 deletion core/runtime/runtime_api/impl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ add_library(grandpa_api
grandpa_api.cpp
)
target_link_libraries(grandpa_api
blockchain
executor
)

Expand Down
7 changes: 2 additions & 5 deletions core/runtime/runtime_api/impl/babe_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ namespace kagome::runtime {

outcome::result<primitives::BabeConfiguration> BabeApiImpl::configuration(
const primitives::BlockHash &block) {
OUTCOME_TRY(ref, cache_.get_else(block, [&] {
return executor_->callAt<primitives::BabeConfiguration>(
block, "BabeApi_configuration");
}));
return *ref;
return executor_->callAt<primitives::BabeConfiguration>(
block, "BabeApi_configuration");
}

outcome::result<primitives::Epoch> BabeApiImpl::next_epoch(
Expand Down
9 changes: 1 addition & 8 deletions core/runtime/runtime_api/impl/babe_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef KAGOME_CORE_RUNTIME_IMPL_BABE_API_HPP
#define KAGOME_CORE_RUNTIME_IMPL_BABE_API_HPP
#pragma once

#include "runtime/runtime_api/babe_api.hpp"

#include "common/lru_cache.hpp"

namespace kagome::runtime {

class Executor;
Expand All @@ -26,10 +23,6 @@ namespace kagome::runtime {

private:
std::shared_ptr<Executor> executor_;

LruCache<primitives::BlockHash, primitives::BabeConfiguration> cache_{10};
};

} // namespace kagome::runtime

#endif // KAGOME_CORE_RUNTIME_IMPL_BABE_API_HPP
40 changes: 7 additions & 33 deletions core/runtime/runtime_api/impl/grandpa_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,21 @@

namespace kagome::runtime {

GrandpaApiImpl::GrandpaApiImpl(
std::shared_ptr<blockchain::BlockHeaderRepository> block_header_repo,
std::shared_ptr<Executor> executor)
: block_header_repo_{std::move(block_header_repo)},
executor_{std::move(executor)} {
BOOST_ASSERT(block_header_repo_);
GrandpaApiImpl::GrandpaApiImpl(std::shared_ptr<Executor> executor)
: executor_{std::move(executor)} {
BOOST_ASSERT(executor_);
}

outcome::result<std::optional<GrandpaApi::ScheduledChange>>
GrandpaApiImpl::pending_change(const primitives::BlockHash &block,
const Digest &digest) {
return executor_->callAt<std::optional<ScheduledChange>>(
block, "GrandpaApi_pending_change", digest);
}

outcome::result<std::optional<GrandpaApi::ForcedChange>>
GrandpaApiImpl::forced_change(const primitives::BlockHash &block,
const Digest &digest) {
return executor_->callAt<std::optional<ForcedChange>>(
block, "GrandpaApi_forced_change", digest);
}

outcome::result<GrandpaApi::AuthorityList> GrandpaApiImpl::authorities(
const primitives::BlockId &block_id) {
OUTCOME_TRY(block_hash, block_header_repo_->getHashById(block_id));

OUTCOME_TRY(ref, authorities_.get_else(block_hash, [&] {
return executor_->callAt<AuthorityList>(block_hash,
"GrandpaApi_grandpa_authorities");
}));
return *ref;
const primitives::BlockHash &block_hash) {
return executor_->callAt<AuthorityList>(block_hash,
"GrandpaApi_grandpa_authorities");
}

outcome::result<primitives::AuthoritySetId> GrandpaApiImpl::current_set_id(
const primitives::BlockHash &block_hash) {
OUTCOME_TRY(ref, set_id_.get_else(block_hash, [&] {
return executor_->callAt<primitives::AuthoritySetId>(
block_hash, "GrandpaApi_current_set_id");
}));
return *ref;
return executor_->callAt<primitives::AuthoritySetId>(
block_hash, "GrandpaApi_current_set_id");
}

} // namespace kagome::runtime
24 changes: 3 additions & 21 deletions core/runtime/runtime_api/impl/grandpa_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,26 @@
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef KAGOME_RUNTIME_IMPL_GRANDPAAPI
#define KAGOME_RUNTIME_IMPL_GRANDPAAPI
#pragma once

#include "runtime/runtime_api/grandpa_api.hpp"

#include "blockchain/block_header_repository.hpp"
#include "common/lru_cache.hpp"

namespace kagome::runtime {

class Executor;

class GrandpaApiImpl final : public GrandpaApi {
public:
GrandpaApiImpl(
std::shared_ptr<blockchain::BlockHeaderRepository> block_header_repo,
std::shared_ptr<Executor> executor);

outcome::result<std::optional<ScheduledChange>> pending_change(
const primitives::BlockHash &block, const Digest &digest) override;

outcome::result<std::optional<ForcedChange>> forced_change(
const primitives::BlockHash &block, const Digest &digest) override;
GrandpaApiImpl(std::shared_ptr<Executor> executor);

outcome::result<AuthorityList> authorities(
const primitives::BlockId &block_id) override;
const primitives::BlockHash &block_hash) override;

outcome::result<primitives::AuthoritySetId> current_set_id(
const primitives::BlockHash &block) override;

private:
std::shared_ptr<blockchain::BlockHeaderRepository> block_header_repo_;
std::shared_ptr<Executor> executor_;

LruCache<primitives::BlockHash, AuthorityList> authorities_{10};
LruCache<primitives::BlockHash, primitives::AuthoritySetId> set_id_{10};
};

} // namespace kagome::runtime

#endif // KAGOME_RUNTIME_GRANDPAAPI
Loading
Loading