Skip to content

Commit

Permalink
babe indexer (#1661)
Browse files Browse the repository at this point in the history
Signed-off-by: turuslan <turuslan.devbox@gmail.com>
  • Loading branch information
turuslan authored Jul 19, 2023
1 parent 80deb0f commit f0132ae
Show file tree
Hide file tree
Showing 42 changed files with 849 additions and 1,197 deletions.
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 @@ -1211,6 +1211,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 @@ -124,6 +124,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

0 comments on commit f0132ae

Please sign in to comment.