Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Change intial best beefy block #10669

Closed
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions client/beefy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use sc_network_gossip::{GossipEngine, Network as GossipNetwork};
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use sp_keystore::SyncCryptoStorePtr;
use sp_runtime::traits::Block;
use sp_runtime::traits::{Block, NumberFor};

use beefy_primitives::BeefyApi;

Expand Down Expand Up @@ -111,7 +111,7 @@ where
B: Block,
BE: Backend<B>,
C: Client<B, BE>,
C::Api: BeefyApi<B>,
C::Api: BeefyApi<B, NumberFor<B>>,
N: GossipNetwork<B> + Clone + Send + 'static,
{
/// BEEFY client
Expand Down Expand Up @@ -142,7 +142,7 @@ where
B: Block,
BE: Backend<B>,
C: Client<B, BE>,
C::Api: BeefyApi<B>,
C::Api: BeefyApi<B, NumberFor<B>>,
N: GossipNetwork<B> + Clone + Send + 'static,
{
let BeefyParams {
Expand Down
17 changes: 11 additions & 6 deletions client/beefy/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ where
B: Block + Codec,
BE: Backend<B>,
C: Client<B, BE>,
C::Api: BeefyApi<B>,
C::Api: BeefyApi<B, NumberFor<B>>,
{
/// Return a new BEEFY worker instance.
///
Expand Down Expand Up @@ -146,7 +146,7 @@ where
B: Block,
BE: Backend<B>,
C: Client<B, BE>,
C::Api: BeefyApi<B>,
C::Api: BeefyApi<B, NumberFor<B>>,
{
/// Return `true`, if we should vote on block `number`
fn should_vote_on(&self, number: NumberFor<B>) -> bool {
Expand Down Expand Up @@ -247,14 +247,19 @@ where

debug!(target: "beefy", "🥩 New Rounds for id: {:?}", id);

self.best_beefy_block = Some(*notification.header.number());
let at = BlockId::hash(notification.header.hash());
let session_boundary = self.client.runtime_api().get_session_boundary(&at).ok();
self.best_beefy_block = session_boundary.clone();
self.beefy_best_block_sender
.notify(|| Ok::<_, ()>(notification.hash.clone()))
.expect("forwards closure result; the closure always returns Ok; qed.");

// this metric is kind of 'fake'. Best BEEFY block should only be updated once we
// have a signed commitment for the block. Remove once the above TODO is done.
metric_set!(self, beefy_best_block, *notification.header.number());
// this metric is kind of 'fake'. Best BEEFY block should only be updated once
// we have a signed commitment for the block. Remove once the above TODO is
// done.
if let Some(session_boundary) = session_boundary {
metric_set!(self, beefy_best_block, session_boundary);
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions frame/session/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ pub mod pallet {

Validators::<T>::put(initial_validators_0);
<QueuedKeys<T>>::put(queued_keys);
SessionStart::<T>::put(T::BlockNumber::default());

T::SessionManager::start_session(0);
}
Expand All @@ -509,6 +510,11 @@ pub mod pallet {
#[pallet::getter(fn current_index)]
pub type CurrentIndex<T> = StorageValue<_, SessionIndex, ValueQuery>;

/// Block number at which the current session began
#[pallet::storage]
#[pallet::getter(fn session_start)]
pub type SessionStart<T: Config> = StorageValue<_, T::BlockNumber, ValueQuery>;

/// True if the underlying economic identities or weighting behind the validators
/// has changed in the queued validator set.
#[pallet::storage]
Expand Down Expand Up @@ -573,6 +579,7 @@ pub mod pallet {
fn on_initialize(n: T::BlockNumber) -> Weight {
if T::ShouldEndSession::should_end_session(n) {
Self::rotate_session();
SessionStart::<T>::put(n);
T::BlockWeights::get().max_block
} else {
// NOTE: the non-database part of the weight for `should_end_session(n)` is
Expand Down Expand Up @@ -899,6 +906,11 @@ impl<T: Config> Pallet<T> {
fn clear_key_owner(id: KeyTypeId, key_data: &[u8]) {
<KeyOwner<T>>::remove((id, key_data));
}

/// Returns the block number at which the session began
pub fn get_session_boundary() -> T::BlockNumber {
Pallet::<T>::session_start()
}
}

impl<T: Config> ValidatorRegistration<T::ValidatorId> for Pallet<T> {
Expand Down
4 changes: 3 additions & 1 deletion primitives/beefy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,12 @@ pub struct VoteMessage<Number, Id, Signature> {

sp_api::decl_runtime_apis! {
/// API necessary for BEEFY voters.
pub trait BeefyApi
pub trait BeefyApi<BlockNumber: codec::Codec>
{
/// Return the current active BEEFY validator set
fn validator_set() -> Option<ValidatorSet<crypto::AuthorityId>>;
/// Returns the block number at which the current session began
fn get_session_boundary() -> BlockNumber;
}
}

Expand Down