Skip to content

Commit

Permalink
Merge with master
Browse files Browse the repository at this point in the history
  • Loading branch information
Harrm committed Apr 27, 2022
2 parents 4ed0f9f + b85e77c commit df524ff
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 37 deletions.
13 changes: 6 additions & 7 deletions core/blockchain/impl/block_tree_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ namespace kagome::blockchain {
auto least_leaf = *block_tree_leaves.begin();
auto best_leaf = *block_tree_leaves.rbegin();

OUTCOME_TRY(last_finalized_block_info, storage->getLastFinalized());

std::optional<consensus::EpochNumber> curr_epoch_number;

// First, look up slot number of block number 1
Expand Down Expand Up @@ -172,18 +174,15 @@ namespace kagome::blockchain {
// Now we have all to calculate epoch number
auto epoch_number = (last_slot_number - first_slot_number)
/ babe_configuration->epoch_length;
consensus::EpochDescriptor epoch{
.epoch_number = epoch_number,
.start_slot = first_slot_number
+ epoch_number * babe_configuration->epoch_length};

babe_util->syncEpoch(epoch);
babe_util->syncEpoch([&] {
auto is_first_block_finalized = last_finalized_block_info.number > 0;
return std::tuple(first_slot_number, is_first_block_finalized);
});

curr_epoch_number.emplace(epoch_number);
}

OUTCOME_TRY(last_finalized_block_info, storage->getLastFinalized());

std::optional<consensus::EpochDigest> curr_epoch;
std::optional<consensus::EpochDigest> next_epoch;
auto hash_tmp = last_finalized_block_info.hash;
Expand Down
6 changes: 4 additions & 2 deletions core/consensus/babe/babe_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ namespace kagome::consensus {
virtual ~BabeUtil() = default;

/**
* Init inner state by {@param epoch_descriptor}
* Init inner state by call {@param f} returning first block slot and flag
* if first block is already finalized
*/
virtual void syncEpoch(EpochDescriptor epoch_descriptor) = 0;
virtual BabeSlotNumber syncEpoch(
std::function<std::tuple<BabeSlotNumber, bool>()> &&f) = 0;

/**
* @returns current unix time slot number
Expand Down
75 changes: 54 additions & 21 deletions core/consensus/babe/impl/babe_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,20 +193,31 @@ namespace kagome::consensus::babe {
}

void BabeImpl::adjustEpochDescriptor() {
if (current_epoch_.epoch_number > 1) {
return;
}

auto res = block_tree_->getBlockHeader(primitives::BlockNumber(1));
if (res.has_error()) {
return;
}
auto first_slot_number = babe_util_->syncEpoch([&]() {
auto res = block_tree_->getBlockHeader(primitives::BlockNumber(1));
if (res.has_error()) {
SL_TRACE(log_,
"First block slot is {}: no first block (at adjusting)",
babe_util_->getCurrentSlot());
return std::tuple(babe_util_->getCurrentSlot(), false);
}

auto &first_block_header = res.value();
auto babe_digest_res = consensus::getBabeDigests(first_block_header);
BOOST_ASSERT_MSG(babe_digest_res.has_value(),
"Any non genesis block must contain babe digest");
auto first_slot_number = babe_digest_res.value().second.slot_number;
auto &first_block_header = res.value();
auto babe_digest_res = consensus::getBabeDigests(first_block_header);
BOOST_ASSERT_MSG(babe_digest_res.has_value(),
"Any non genesis block must contain babe digest");
auto first_slot_number = babe_digest_res.value().second.slot_number;

auto is_first_block_finalized =
block_tree_->getLastFinalized().number > 0;

SL_TRACE(
log_,
"First block slot is {}: by {}finalized first block (at adjusting)",
first_slot_number,
is_first_block_finalized ? "" : "non-");
return std::tuple(first_slot_number, is_first_block_finalized);
});

auto current_epoch_start_slot =
first_slot_number
Expand All @@ -216,12 +227,10 @@ namespace kagome::consensus::babe {
SL_WARN(log_,
"Start-slot of current epoch {} has updated from {} to {}",
current_epoch_.epoch_number,
current_epoch_start_slot,
current_epoch_.start_slot);
current_epoch_.start_slot,
current_epoch_start_slot);

current_epoch_.start_slot =current_epoch_start_slot;

babe_util_->syncEpoch(current_epoch_);
current_epoch_.start_slot = current_epoch_start_slot;
}
}

Expand All @@ -233,6 +242,8 @@ namespace kagome::consensus::babe {

BOOST_ASSERT(keypair_ != nullptr);

adjustEpochDescriptor();

SL_DEBUG(
log_,
"Starting an epoch {}. Session key: {:l}. Secondary slots allowed={}",
Expand All @@ -242,8 +253,6 @@ namespace kagome::consensus::babe {
current_epoch_ = epoch;
current_slot_ = current_epoch_.start_slot;

babe_util_->syncEpoch(current_epoch_);

runSlot();
}

Expand Down Expand Up @@ -842,7 +851,31 @@ namespace kagome::consensus::babe {
++current_epoch_.epoch_number;
current_epoch_.start_slot = current_slot_;

babe_util_->syncEpoch(current_epoch_);
babe_util_->syncEpoch([&]() {
auto res = block_tree_->getBlockHeader(primitives::BlockNumber(1));
if (res.has_error()) {
SL_WARN(log_,
"First block slot is {}: no first block (at start next epoch)",
babe_util_->getCurrentSlot());
return std::tuple(babe_util_->getCurrentSlot(), false);
}

auto &first_block_header = res.value();
auto babe_digest_res = consensus::getBabeDigests(first_block_header);
BOOST_ASSERT_MSG(babe_digest_res.has_value(),
"Any non genesis block must contain babe digest");
auto first_slot_number = babe_digest_res.value().second.slot_number;

auto is_first_block_finalized =
block_tree_->getLastFinalized().number > 0;

SL_WARN(log_,
"First block slot is {}: by {}finalized first block (at start "
"next epoch)",
first_slot_number,
is_first_block_finalized ? "" : "non-");
return std::tuple(first_slot_number, is_first_block_finalized);
});
}

bool BabeImpl::isSecondarySlotsAllowed() const {
Expand Down
12 changes: 8 additions & 4 deletions core/consensus/babe/impl/babe_util_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ namespace kagome::consensus {
"Epoch length must be non zero");
}

void BabeUtilImpl::syncEpoch(EpochDescriptor epoch_descriptor) {
first_block_slot_number_.emplace(
epoch_descriptor.start_slot
- epoch_descriptor.epoch_number * babe_configuration_->epoch_length);
BabeSlotNumber BabeUtilImpl::syncEpoch(
std::function<std::tuple<BabeSlotNumber, bool>()> &&f) {
if (not is_first_block_finalized_) {
auto [first_block_slot_number, is_first_block_finalized] = f();
first_block_slot_number_.emplace(first_block_slot_number);
is_first_block_finalized_ = is_first_block_finalized;
}
return first_block_slot_number_.value();
}

BabeSlotNumber BabeUtilImpl::getCurrentSlot() const {
Expand Down
4 changes: 3 additions & 1 deletion core/consensus/babe/impl/babe_util_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ namespace kagome::consensus {
std::shared_ptr<primitives::BabeConfiguration> babe_configuration,
const BabeClock &clock);

void syncEpoch(EpochDescriptor epoch_descriptor) override;
BabeSlotNumber syncEpoch(
std::function<std::tuple<BabeSlotNumber, bool>()> &&f) override;

BabeSlotNumber getCurrentSlot() const override;

Expand All @@ -42,6 +43,7 @@ namespace kagome::consensus {
const BabeClock &clock_;

std::optional<BabeSlotNumber> first_block_slot_number_;
bool is_first_block_finalized_ = false;
};
} // namespace kagome::consensus
#endif // KAGOME_CONSENSUS_BABEUTILIMPL
34 changes: 34 additions & 0 deletions core/consensus/babe/impl/block_executor_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,40 @@ namespace kagome::consensus {
const auto &babe_header = babe_digests.second;

auto slot_number = babe_header.slot_number;

babe_util_->syncEpoch([&] {
auto res = block_tree_->getBlockHeader(primitives::BlockNumber(1));
if (res.has_error()) {
if (block.header.number == 1) {
SL_TRACE(logger_,
"First block slot is {}: it is first block (at executing)",
slot_number);
return std::tuple(slot_number, false);
} else {
SL_TRACE(logger_,
"First block slot is {}: no first block (at executing)",
babe_util_->getCurrentSlot());
return std::tuple(babe_util_->getCurrentSlot(), false);
}
}

auto &first_block_header = res.value();
auto babe_digest_res = consensus::getBabeDigests(first_block_header);
BOOST_ASSERT_MSG(babe_digest_res.has_value(),
"Any non genesis block must contain babe digest");
auto first_slot_number = babe_digest_res.value().second.slot_number;

auto is_first_block_finalized =
block_tree_->getLastFinalized().number > 0;

SL_TRACE(
logger_,
"First block slot is {}: by {}finalized first block (at executing)",
first_slot_number,
is_first_block_finalized ? "" : "non-");
return std::tuple(first_slot_number, is_first_block_finalized);
});

auto epoch_number = babe_util_->slotToEpoch(slot_number);

logger_->info(
Expand Down
2 changes: 1 addition & 1 deletion test/core/blockchain/block_tree_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ struct BlockTreeTest : public testing::Test {
babe_config_->epoch_length = 2;

babe_util_ = std::make_shared<BabeUtilMock>();
EXPECT_CALL(*babe_util_, syncEpoch(_)).WillRepeatedly(Return());
EXPECT_CALL(*babe_util_, syncEpoch(_)).WillRepeatedly(Return(1));
EXPECT_CALL(*babe_util_, slotToEpoch(_)).WillRepeatedly(Return(0));

block_tree_ = BlockTreeImpl::create(header_repo_,
Expand Down
8 changes: 7 additions & 1 deletion test/mock/core/consensus/babe/babe_util_mock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ namespace kagome::consensus {

class BabeUtilMock : public BabeUtil {
public:
MOCK_METHOD(void, syncEpoch, (EpochDescriptor), (override));
using SyncFunctor = std::function<std::tuple<BabeSlotNumber, bool>()>;

MOCK_METHOD(BabeSlotNumber, syncEpoch, (SyncFunctor &), ());

BabeSlotNumber syncEpoch(SyncFunctor &&func) override {
return syncEpoch(func);
}

MOCK_METHOD(BabeSlotNumber, getCurrentSlot, (), (const, override));

Expand Down

0 comments on commit df524ff

Please sign in to comment.