Skip to content

Commit

Permalink
Refactor: slot leadership lottery (#1867)
Browse files Browse the repository at this point in the history
* refactor: babe
* fix: consensus selector
* refactor: block production consensus (prepare for common code extraction)
* fix: tests
* fix: mock object leaks

Signed-off-by: Dmitriy Khaustov aka xDimon <khaustov.dm@gmail.com>
  • Loading branch information
xDimon authored Nov 20, 2023
1 parent 053b5c0 commit df38521
Show file tree
Hide file tree
Showing 65 changed files with 908 additions and 866 deletions.
2 changes: 1 addition & 1 deletion core/blockchain/impl/block_tree_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
#include "blockchain/block_header_repository.hpp"
#include "blockchain/block_storage.hpp"
#include "blockchain/block_tree_error.hpp"
#include "consensus/babe/types/babe_configuration.hpp"
#include "consensus/timeline/types.hpp"
#include "crypto/hasher.hpp"
#include "log/logger.hpp"
#include "metrics/metrics.hpp"
#include "network/extrinsic_observer.hpp"
#include "primitives/babe_configuration.hpp"
#include "primitives/event_types.hpp"
#include "storage/trie/trie_storage.hpp"
#include "subscription/extrinsic_event_key_repository.hpp"
Expand Down
15 changes: 11 additions & 4 deletions core/consensus/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,26 @@
# SPDX-License-Identifier: Apache-2.0
#

add_library(consensus_common
block_production_error.cpp
)
target_link_libraries(consensus_common
logger
metrics
)
kagome_install(consensus_common)

add_subdirectory(timeline)
add_subdirectory(babe)
add_subdirectory(grandpa)
#add_subdirectory(sassafras)

add_library(consensus
validation/babe_block_validator.cpp
)
)
target_link_libraries(consensus
timeline
babe
# sassafras
grandpa
)
)
kagome_install(consensus)
kagome_clear_objects(consensus)
8 changes: 3 additions & 5 deletions core/consensus/babe/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ add_library(babe
impl/threshold_util.cpp
impl/babe_lottery_impl.cpp
impl/babe_config_repository_impl.cpp
impl/babe_error.cpp
)
)
target_link_libraries(babe
logger
metrics
)
consensus_common
)
kagome_install(babe)
16 changes: 4 additions & 12 deletions core/consensus/babe/babe_config_repository.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#pragma once

#include "primitives/babe_configuration.hpp"
#include "consensus/babe/types/babe_configuration.hpp"
#include "primitives/block_data.hpp"

namespace kagome::consensus::babe {
Expand All @@ -16,19 +16,11 @@ namespace kagome::consensus::babe {
public:
virtual ~BabeConfigRepository() = default;

/// Returns the duration of a slot in milliseconds
/// @return the duration of a slot in milliseconds
virtual Duration slotDuration() const = 0;

/// @return the epoch length in slots
virtual EpochLength epochLength() const = 0;

/// Returns the actual babe configuration
/// @return the actual babe configuration
virtual outcome::result<
std::shared_ptr<const primitives::BabeConfiguration>>
config(const primitives::BlockInfo &parent_info,
EpochNumber epoch_number) const = 0;
virtual outcome::result<std::shared_ptr<const BabeConfiguration>> config(
const primitives::BlockInfo &parent_info,
EpochNumber epoch_number) const = 0;

virtual void warp(const primitives::BlockInfo &block) = 0;
};
Expand Down
77 changes: 18 additions & 59 deletions core/consensus/babe/babe_lottery.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,74 +8,33 @@

#include <optional>

#include "consensus/babe/types/babe_configuration.hpp"
#include "consensus/babe/types/slot_leadership.hpp"
#include "consensus/timeline/types.hpp"
#include "crypto/sr25519_types.hpp"
#include "primitives/babe_configuration.hpp"

namespace kagome::consensus::babe {
/**
* Interface for acquiring leadership information for the current Babe epoch.
* It is expected to be used as follows:
* - first epoch is started, r (randomness) = 0
* - blocks are being accepted
* - when a block is finalized, submitVRFValue function is called for each
* block in the finalized chain up to the last finalized one (in the
* chronological order). The chain can be retrieved via calling
* BlockTree::longestPath() firstly and only then BlockTree::finalize(..)
* - at the end of the epoch, computeRandomness(..) is called, providing a
* randomness value for the new epoch
* - that value is then is used in slotsLeadership(..)
*
*/
/// Interface for acquiring leadership information for the current Babe epoch.
class BabeLottery {
public:
virtual ~BabeLottery() = default;

/**
* Set new epoch and corresponding randomness, threshold and keypair values
* @param epoch is an number of epoch where we calculate leadership
* @param randomness is an epoch random byte sequence
* @param threshold is a maximum value that is considered valid by vrf
* @param keypair is a current babe sign pair
*/
virtual void changeEpoch(EpochNumber epoch,
const Randomness &randomness,
const Threshold &threshold,
const crypto::Sr25519Keypair &keypair) = 0;

/**
* Return lottery current epoch
*/
/// Return lottery current epoch
virtual EpochNumber getEpoch() const = 0;

/**
* Compute leadership for the slot
* @param slot is a slot number
* @return none means the peer was not chosen as a leader
* for that slot, value contains VRF value and proof
*/
virtual std::optional<crypto::VRFOutput> getSlotLeadership(
SlotNumber slot) const = 0;

/**
* Computes VRF proof for the slot regardless threshold.
* Used when secondary VRF slots are enabled
* @param slot is a slot number
* @return VRF output and proof
*/
virtual crypto::VRFOutput slotVrfSignature(SlotNumber slot) const = 0;

/**
* Compute the expected author for secondary slot
* @param slot - slot to have secondary block produced
* @param authorities_count - quantity of authorities in current epoch
* @param randomness - current randomness
* @return - should always return some authority unless authorities list is
* empty
*/
virtual std::optional<primitives::AuthorityIndex> secondarySlotAuthor(
SlotNumber slot,
primitives::AuthorityListSize authorities_count,
const Randomness &randomness) const = 0;
/// Changes epoch
/// @param epoch epoch that switch to
/// @param block that epoch data based on
/// @return true if epoch successfully switched and node validator in epoch
virtual bool changeEpoch(EpochNumber epoch,
const primitives::BlockInfo &best_block) = 0;

/// Check slot leadership
/// @param block parent of block for which will produced new one if node is
/// slot-leader
/// @param slot for which leadership is checked
/// @return data needed for slot leadership or none
virtual std::optional<SlotLeadership> getSlotLeadership(
const primitives::BlockHash &block, SlotNumber slot) const = 0;
};
} // namespace kagome::consensus::babe
4 changes: 2 additions & 2 deletions core/consensus/babe/has_babe_consensus_digest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace kagome::consensus::babe {
if (not babe) {
continue;
}
if (auto item = boost::get<primitives::NextEpochData>(babe)) {
if (auto item = boost::get<consensus::babe::EpochData>(babe)) {
epoch = std::move(*item);
continue;
}
Expand All @@ -52,7 +52,7 @@ namespace kagome::consensus::babe {
return epoch.has_value();
}

std::optional<primitives::NextEpochData> epoch;
std::optional<consensus::babe::EpochData> epoch;
std::optional<primitives::NextConfigDataV1> config;
};
} // namespace kagome::consensus::babe
Loading

0 comments on commit df38521

Please sign in to comment.