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 babe genesis slot #1700

Merged
merged 19 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from 16 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
16 changes: 0 additions & 16 deletions core/consensus/babe/babe.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,6 @@ namespace kagome::consensus::babe {
// peer doing block production
};

/**
* Start a Babe production
* @param epoch - epoch, which is going to be run
* epoch.starting_slot_finish_time - when the slot, from which the BABE
* starts, ends; for example, we start from
* 5th slot of the some epoch. Then, we need to set time when 5th slot
* finishes; most probably, that time will be calculated using Median
* algorithm
*
* @note the function will automatically continue launching all further
* epochs of the Babe production
* @note in fact, it is an implementation of "Invoke-Block-Authoring" from
* the spec
*/
virtual void runEpoch(EpochDescriptor epoch) = 0;

/**
* @returns current state
*/
Expand Down
2 changes: 2 additions & 0 deletions core/consensus/babe/babe_error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ OUTCOME_CPP_DEFINE_CATEGORY(kagome::consensus::babe, BabeError, e) {
return "bad order of digest item; PreRuntime must be first";
case E::UNKNOWN_DIGEST_TYPE:
return "unknown type of digest";
case E::SLOT_BEFORE_GENESIS:
return "slot before genesis";
}
return "unknown error";
}
Expand Down
1 change: 1 addition & 0 deletions core/consensus/babe/babe_error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace kagome::consensus::babe {
MISSING_PROOF = 1,
BAD_ORDER_OF_DIGEST_ITEM,
UNKNOWN_DIGEST_TYPE,
SLOT_BEFORE_GENESIS,
};

enum class BlockAdditionError {
Expand Down
34 changes: 14 additions & 20 deletions core/consensus/babe/babe_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "consensus/babe/common.hpp"
#include "consensus/babe/types/epoch_descriptor.hpp"
#include "primitives/common.hpp"

namespace kagome::consensus::babe {

Expand All @@ -20,44 +21,37 @@ namespace kagome::consensus::babe {
public:
virtual ~BabeUtil() = default;

// TODO(turuslan): removed in https://github.com/soramitsu/kagome/pull/1700
virtual BabeSlotNumber getFirstBlockSlotNumber() = 0;

/**
* @returns current unix time slot number
* @returns slot for time
*/
virtual BabeSlotNumber getCurrentSlot() const = 0;
virtual BabeSlotNumber timeToSlot(BabeTimePoint time) const = 0;

/**
* @returns timepoint of start of slot #{@param slot}
*/
virtual BabeTimePoint slotStartTime(BabeSlotNumber slot) const = 0;

/**
* @returns duration to start of slot #{@param slot}
*/
virtual BabeDuration remainToStartOfSlot(BabeSlotNumber slot) const = 0;

/**
* @returns timepoint of finish of slot #{@param slot}
*/
virtual BabeTimePoint slotFinishTime(BabeSlotNumber slot) const = 0;

/**
* @returns duration to finish of slot #{@param slot}
*/
virtual BabeDuration remainToFinishOfSlot(BabeSlotNumber slot) const = 0;

/**
* @returns number of epoch by provided {@param slot_number}
* @returns epoch descriptor for given parent and slot
*/
virtual EpochNumber slotToEpoch(BabeSlotNumber slot_number) const = 0;
virtual outcome::result<EpochDescriptor> slotToEpochDescriptor(
const primitives::BlockInfo &parent_info,
BabeSlotNumber slot_number) const = 0;

/**
* @returns ordinal number of the slot in the corresponding epoch by
* provided {@param slot_number}
* @returns epoch number for given parent and slot
*/
virtual BabeSlotNumber slotInEpoch(BabeSlotNumber slot_number) const = 0;
outcome::result<EpochNumber> slotToEpoch(
const primitives::BlockInfo &parent_info,
BabeSlotNumber slot_number) const {
OUTCOME_TRY(epoch, slotToEpochDescriptor(parent_info, slot_number));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe for consistency and in case we want to mock this method, it makes sense to move implementation to babe_config_repository_impl?

return epoch.epoch_number;
}
};

} // namespace kagome::consensus::babe
Expand Down
169 changes: 90 additions & 79 deletions core/consensus/babe/impl/babe_config_repository_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ namespace kagome::consensus::babe {
std::shared_ptr<runtime::BabeApi> babe_api,
std::shared_ptr<crypto::Hasher> hasher,
std::shared_ptr<storage::trie::TrieStorage> trie_storage,
primitives::events::ChainSubscriptionEnginePtr chain_events_engine,
const BabeClock &clock)
primitives::events::ChainSubscriptionEnginePtr chain_events_engine)
: persistent_storage_(
persistent_storage->getSpace(storage::Space::kDefault)),
config_warp_sync_{app_config.syncMethod()
Expand All @@ -77,7 +76,6 @@ namespace kagome::consensus::babe {
return std::make_shared<primitives::events::ChainEventSubscriber>(
chain_events_engine);
}()),
clock_(clock),
logger_(log::createLogger("BabeConfigRepo", "babe_config_repo")) {
BOOST_ASSERT(persistent_storage_ != nullptr);
BOOST_ASSERT(block_tree_ != nullptr);
Expand All @@ -93,6 +91,21 @@ namespace kagome::consensus::babe {
}

bool BabeConfigRepositoryImpl::prepare() {
if (auto res = persistent_storage_->tryGet(
storage::kBabeConfigRepositoryImplGenesisSlot)) {
if (auto &genesis_slot_raw = res.value()) {
if (auto res = scale::decode<BabeSlotNumber>(*genesis_slot_raw)) {
first_block_slot_number_ = res.value();
} else {
SL_ERROR(logger_, "genesis slot decode error: {}", res.error());
std::ignore = persistent_storage_->remove(
storage::kBabeConfigRepositoryImplGenesisSlot);
}
}
} else {
SL_ERROR(logger_, "genesis slot db read error: {}", res.error());
return false;
}
std::unique_lock lock{indexer_mutex_};
auto finalized = block_tree_->getLastFinalized();
auto finalized_header = block_tree_->getBlockHeader(finalized.hash).value();
Expand Down Expand Up @@ -148,8 +161,8 @@ namespace kagome::consensus::babe {
auto epoch_changed = true;
if (parent_info.number != 0) {
OUTCOME_TRY(parent_header, block_tree_->getBlockHeader(parent_info.hash));
OUTCOME_TRY(parent_digest, getBabeDigests(parent_header));
auto parent_epoch = slotToEpoch(parent_digest.second.slot_number);
OUTCOME_TRY(parent_slot, getBabeSlot(parent_header));
OUTCOME_TRY(parent_epoch, slotToEpoch(parent_info, parent_slot));
epoch_changed = epoch_number != parent_epoch;
}
std::unique_lock lock{indexer_mutex_};
Expand All @@ -167,75 +180,73 @@ namespace kagome::consensus::babe {
return epoch_length_;
}

BabeSlotNumber BabeConfigRepositoryImpl::getCurrentSlot() const {
return static_cast<BabeSlotNumber>(clock_.now().time_since_epoch()
BabeSlotNumber BabeConfigRepositoryImpl::timeToSlot(
BabeTimePoint time) const {
return static_cast<BabeSlotNumber>(time.time_since_epoch()
/ slotDuration());
}

BabeTimePoint BabeConfigRepositoryImpl::slotStartTime(
BabeSlotNumber slot) const {
return clock_.zero() + slot * slotDuration();
}

BabeDuration BabeConfigRepositoryImpl::remainToStartOfSlot(
BabeSlotNumber slot) const {
auto deadline = slotStartTime(slot);
auto now = clock_.now();
if (deadline > now) {
return deadline - now;
}
return BabeDuration{};
return BabeTimePoint{} + slot * slotDuration();
}

BabeTimePoint BabeConfigRepositoryImpl::slotFinishTime(
BabeSlotNumber slot) const {
return slotStartTime(slot + 1);
}

BabeDuration BabeConfigRepositoryImpl::remainToFinishOfSlot(
BabeSlotNumber slot) const {
return remainToStartOfSlot(slot + 1);
}

BabeSlotNumber BabeConfigRepositoryImpl::getFirstBlockSlotNumber() {
if (first_block_slot_number_.has_value()) {
return first_block_slot_number_.value();
}

auto r = [&]() -> outcome::result<BabeSlotNumber> {
OUTCOME_TRY(hash, header_repo_->getHashByNumber(1));
OUTCOME_TRY(header, header_repo_->getBlockHeader(hash));
OUTCOME_TRY(digest, getBabeDigests(header));
auto &slot = digest.second.slot_number;
if (block_tree_->getLastFinalized().number != 0) {
first_block_slot_number_ = slot;
outcome::result<BabeSlotNumber>
BabeConfigRepositoryImpl::getFirstBlockSlotNumber(
const primitives::BlockInfo &parent_info) const {
auto slot1 = first_block_slot_number_;
if (not slot1) {
auto finalized = block_tree_->getLastFinalized();
OUTCOME_TRY(parent, block_tree_->getBlockHeader(parent_info.hash));
if (parent.number == 1) {
BOOST_OUTCOME_TRY(slot1, getBabeSlot(parent));
} else if (finalized.number != 0) {
OUTCOME_TRY(hash1, block_tree_->getBlockHash(1));
if (hash1) {
OUTCOME_TRY(header1, block_tree_->getBlockHeader(*hash1));
BOOST_OUTCOME_TRY(slot1, getBabeSlot(header1));
}
}
if (not slot1 and trie_storage_->getEphemeralBatchAt(parent.state_root)) {
OUTCOME_TRY(epoch, babe_api_->next_epoch(parent_info.hash));
slot1 = epoch.start_slot - epoch.epoch_index * epoch.duration;
}
if (not slot1) {
auto header1 = parent;
while (header1.number != 1) {
BOOST_OUTCOME_TRY(header1,
block_tree_->getBlockHeader(header1.parent_hash));
}
BOOST_OUTCOME_TRY(slot1, getBabeSlot(header1));
}
if (finalized.number != 0
and block_tree_->hasDirectChain(finalized, parent_info)) {
first_block_slot_number_ = slot1;
OUTCOME_TRY(persistent_storage_->put(
storage::kBabeConfigRepositoryImplGenesisSlot,
scale::encode(*slot1).value()));
}
return slot;
}();
if (r) {
return r.value();
}

return getCurrentSlot();
return slot1.value();
}

EpochNumber BabeConfigRepositoryImpl::slotToEpoch(BabeSlotNumber slot) const {
auto genesis_slot_number =
const_cast<BabeConfigRepositoryImpl &>(*this).getFirstBlockSlotNumber();
if (slot > genesis_slot_number) {
return (slot - genesis_slot_number) / epochLength();
outcome::result<EpochDescriptor>
BabeConfigRepositoryImpl::slotToEpochDescriptor(
const primitives::BlockInfo &parent_info, BabeSlotNumber slot) const {
if (parent_info.number == 0) {
return EpochDescriptor{0, slot};
}
return 0;
}

BabeSlotNumber BabeConfigRepositoryImpl::slotInEpoch(
BabeSlotNumber slot) const {
auto genesis_slot_number =
const_cast<BabeConfigRepositoryImpl &>(*this).getFirstBlockSlotNumber();
if (slot > genesis_slot_number) {
return (slot - genesis_slot_number) % epochLength();
OUTCOME_TRY(slot1, getFirstBlockSlotNumber(parent_info));
if (slot < slot1) {
return BabeError::SLOT_BEFORE_GENESIS;
}
return 0;
auto slots = slot - slot1;
return EpochDescriptor{slots / epochLength(), slots % epochLength()};
}

void BabeConfigRepositoryImpl::warp(std::unique_lock<std::mutex> &lock,
Expand Down Expand Up @@ -264,29 +275,22 @@ namespace kagome::consensus::babe {
OUTCOME_TRY(_state, babe_api_->configuration(info.hash));
auto state = std::make_shared<primitives::BabeConfiguration>(
std::move(_state));
BabeIndexedValue value{getConfig(*state), state, state};
if (info.number == 0) {
indexer_.put(info, {value, std::nullopt}, true);
} else {
std::vector<primitives::BlockInfo> refs;
while (true) {
OUTCOME_TRY(header, block_tree_->getBlockHeader(info.hash));
if (HasBabeConsensusDigest digests{header}) {
value.next_state = applyDigests(value.config, digests);
indexer_.put(info, {value, std::nullopt}, true);
if (not refs.empty()) {
indexer_.remove(refs.front());
}
break;
}
refs.emplace_back(info);
info = *header.parentInfo();
}
std::reverse(refs.begin(), refs.end());
for (auto &block : refs) {
indexer_.put(block, {std::nullopt, info, true}, false);
}
BabeIndexedValue value{getConfig(*state), state, std::nullopt, state};
if (info.number != 0) {
OUTCOME_TRY(next, babe_api_->next_epoch(info.hash));
value.next_state_warp =
std::make_shared<primitives::BabeConfiguration>(
primitives::BabeConfiguration{
state->slot_duration,
state->epoch_length,
next.leadership_rate,
std::move(next.authorities),
next.randomness,
next.allowed_slots,
});
value.next_state = value.next_state_warp;
}
indexer_.put(info, {value, std::nullopt}, true);
if (i_first == i_last) {
return outcome::success();
}
Expand All @@ -302,7 +306,12 @@ namespace kagome::consensus::babe {
BOOST_OUTCOME_TRY(prev_state, loadPrev(prev));
}
auto state = applyDigests(getConfig(*prev_state), digests);
BabeIndexedValue value{getConfig(*state), std::nullopt, state};
BabeIndexedValue value{
getConfig(*state),
std::nullopt,
std::nullopt,
state,
};
indexer_.put(info, {value, prev}, block_tree_->isFinalized(info));
prev = info;
prev_state = state;
Expand Down Expand Up @@ -362,6 +371,8 @@ namespace kagome::consensus::babe {
if (block.number == 0) {
BOOST_ASSERT(item.value->state);
item.value->next_state = item.value->state;
} else if (item.value->next_state_warp) {
item.value->next_state = item.value->next_state_warp;
} else {
OUTCOME_TRY(header, block_tree_->getBlockHeader(block.hash));
item.value->next_state = applyDigests(item.value->config, {header});
Expand Down
Loading
Loading