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

babe indexer #1661

Merged
merged 17 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from 12 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
3 changes: 3 additions & 0 deletions core/application/impl/kagome_application_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#include "telemetry/service.hpp"

namespace kagome::application {
KagomeApplicationImpl::~KagomeApplicationImpl() {
kagome::telemetry::setTelemetryService(nullptr);
}

KagomeApplicationImpl::KagomeApplicationImpl(
std::shared_ptr<AppConfiguration> app_config)
Expand Down
2 changes: 1 addition & 1 deletion core/application/impl/kagome_application_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace kagome::application {
using uptr = std::unique_ptr<T>;

public:
~KagomeApplicationImpl() override = default;
~KagomeApplicationImpl() override;

explicit KagomeApplicationImpl(std::shared_ptr<AppConfiguration> config);

Expand Down
7 changes: 7 additions & 0 deletions core/blockchain/block_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,13 @@ namespace kagome::blockchain {
const primitives::BlockHash &ancestor,
const primitives::BlockHash &descendant) const = 0;

bool hasDirectChain(const primitives::BlockInfo &ancestor,
const primitives::BlockInfo &descendant) const {
return hasDirectChain(ancestor.hash, descendant.hash);
}

virtual bool isFinalized(const primitives::BlockInfo &block) const = 0;

/**
* Get a best leaf of the tree
* @return best leaf
Expand Down
8 changes: 8 additions & 0 deletions core/blockchain/impl/block_tree_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,14 @@ namespace kagome::blockchain {
});
}

bool BlockTreeImpl::isFinalized(const primitives::BlockInfo &block) const {
return block_tree_data_.sharedAccess([&](const BlockTreeData &p) {
return block.number <= getLastFinalizedNoLock(p).number
and p.header_repo_->getHashByNumber(block.number)
== outcome::success(block.hash);
});
}

primitives::BlockInfo BlockTreeImpl::bestLeafNoLock(
const BlockTreeData &p) const {
auto leaf = p.tree_->getMetadata().best_leaf.lock();
Expand Down
2 changes: 2 additions & 0 deletions core/blockchain/impl/block_tree_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ namespace kagome::blockchain {
bool hasDirectChain(const primitives::BlockHash &ancestor,
const primitives::BlockHash &descendant) const override;

bool isFinalized(const primitives::BlockInfo &block) const override;

primitives::BlockInfo bestLeaf() const override;

outcome::result<primitives::BlockInfo> getBestContaining(
Expand Down
36 changes: 3 additions & 33 deletions core/blockchain/impl/digest_tracker_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,15 @@
#include "digest_tracker_impl.hpp"

#include "common/visitor.hpp"
#include "consensus/babe/babe_digest_observer.hpp"
#include "consensus/grandpa/grandpa_digest_observer.hpp"

namespace kagome::blockchain {

DigestTrackerImpl::DigestTrackerImpl(
std::shared_ptr<consensus::babe::BabeDigestObserver> babe_update_observer,
std::shared_ptr<consensus::grandpa::GrandpaDigestObserver>
grandpa_digest_observer)
: babe_digest_observer_(std::move(babe_update_observer)),
grandpa_digest_observer_(std::move(grandpa_digest_observer)),
: grandpa_digest_observer_(std::move(grandpa_digest_observer)),
logger_(log::createLogger("DigestTracker", "digest_tracker")) {
BOOST_ASSERT(babe_digest_observer_ != nullptr);
BOOST_ASSERT(grandpa_digest_observer_ != nullptr);
}

Expand All @@ -44,11 +40,7 @@ namespace kagome::blockchain {
return outcome::success(); // It does not processed by tracker
},
[&](const primitives::PreRuntime &item) {
SL_TRACE(logger_,
"PreRuntime-digest on block {}, engine '{}'",
context.block_info,
item.consensus_engine_id.toString());
return onPreRuntime(context, item);
return outcome::success();
},
[&](const primitives::RuntimeEnvironmentUpdated &item) {
SL_TRACE(logger_,
Expand All @@ -70,9 +62,6 @@ namespace kagome::blockchain {
}

void DigestTrackerImpl::cancel(const primitives::BlockInfo &block) {
// Cancel tracked babe digest
babe_digest_observer_->cancel(block);

// Cancel tracked grandpa digest
grandpa_digest_observer_->cancel(block);
}
Expand All @@ -83,7 +72,7 @@ namespace kagome::blockchain {
if (message.consensus_engine_id == primitives::kBabeEngineId) {
OUTCOME_TRY(digest, scale::decode<primitives::BabeDigest>(message.data));

return babe_digest_observer_->onDigest(context, digest);
return outcome::success();

} else if (message.consensus_engine_id == primitives::kGrandpaEngineId) {
OUTCOME_TRY(digest,
Expand All @@ -109,23 +98,4 @@ namespace kagome::blockchain {
return outcome::success();
}
}

outcome::result<void> DigestTrackerImpl::onPreRuntime(
const primitives::BlockContext &context,
const primitives::PreRuntime &message) {
if (message.consensus_engine_id == primitives::kBabeEngineId) {
OUTCOME_TRY(
digest,
scale::decode<consensus::babe::BabeBlockHeader>(message.data));

return babe_digest_observer_->onDigest(context, digest);
} else {
SL_WARN(logger_,
"Unknown consensus engine id in block {}: {}",
context.block_info,
message.consensus_engine_id.toString());
return outcome::success();
}
}

} // namespace kagome::blockchain
11 changes: 1 addition & 10 deletions core/blockchain/impl/digest_tracker_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,12 @@
namespace kagome::consensus::grandpa {
class GrandpaDigestObserver;
}
namespace kagome::consensus::babe {
class BabeDigestObserver;
}

namespace kagome::blockchain {

class DigestTrackerImpl final : public DigestTracker {
public:
DigestTrackerImpl(std::shared_ptr<consensus::babe::BabeDigestObserver>
babe_update_observer,
std::shared_ptr<consensus::grandpa::GrandpaDigestObserver>
DigestTrackerImpl(std::shared_ptr<consensus::grandpa::GrandpaDigestObserver>
grandpa_digest_observer);

outcome::result<void> onDigest(const primitives::BlockContext &context,
Expand All @@ -32,14 +27,10 @@ namespace kagome::blockchain {
void cancel(const primitives::BlockInfo &block) override;

private:
outcome::result<void> onPreRuntime(const primitives::BlockContext &context,
const primitives::PreRuntime &message);

outcome::result<void> onConsensus(
const primitives::BlockContext &context,
const primitives::Consensus &consensus_message);

std::shared_ptr<consensus::babe::BabeDigestObserver> babe_digest_observer_;
std::shared_ptr<consensus::grandpa::GrandpaDigestObserver>
grandpa_digest_observer_;

Expand Down
Loading