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

Improve block-selection strategy #10727

Closed
Closed
Show file tree
Hide file tree
Changes from 2 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
10 changes: 3 additions & 7 deletions client/beefy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use prometheus::Registry;
use sc_client_api::{Backend, BlockchainEvents, Finalizer};
use sc_network_gossip::{GossipEngine, Network as GossipNetwork};

use sp_api::ProvideRuntimeApi;
use sp_api::{NumberFor, ProvideRuntimeApi};
use sp_blockchain::HeaderBackend;
use sp_keystore::SyncCryptoStorePtr;
use sp_runtime::traits::Block;
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 All @@ -126,8 +126,6 @@ where
pub signed_commitment_sender: BeefySignedCommitmentSender<B>,
/// BEEFY best block sender
pub beefy_best_block_sender: BeefyBestBlockSender<B>,
/// Minimal delta between blocks, BEEFY should vote for
pub min_block_delta: u32,
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's not remove this. You can still use it on top of your changes as described in beefy.md #L224.

/// Prometheus metric registry
pub prometheus_registry: Option<Registry>,
/// Chain specific GRANDPA protocol name. See [`beefy_protocol_name::standard_name`].
Expand All @@ -142,7 +140,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 All @@ -152,7 +150,6 @@ where
network,
signed_commitment_sender,
beefy_best_block_sender,
min_block_delta,
prometheus_registry,
protocol_name,
} = beefy_params;
Expand Down Expand Up @@ -182,7 +179,6 @@ where
beefy_best_block_sender,
gossip_engine,
gossip_validator,
min_block_delta,
metrics,
};

Expand Down
145 changes: 67 additions & 78 deletions client/beefy/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ where
pub beefy_best_block_sender: BeefyBestBlockSender<B>,
pub gossip_engine: GossipEngine<B>,
pub gossip_validator: Arc<GossipValidator<B>>,
pub min_block_delta: u32,
pub metrics: Option<Metrics>,
}

Expand All @@ -78,8 +77,6 @@ where
signed_commitment_sender: BeefySignedCommitmentSender<B>,
gossip_engine: Arc<Mutex<GossipEngine<B>>>,
gossip_validator: Arc<GossipValidator<B>>,
/// Min delta in block numbers between two blocks, BEEFY should vote on
min_block_delta: u32,
metrics: Option<Metrics>,
rounds: Option<round::Rounds<Payload, NumberFor<B>>>,
finality_notifications: FinalityNotifications<B>,
Expand All @@ -100,7 +97,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 All @@ -117,7 +114,6 @@ where
beefy_best_block_sender,
gossip_engine,
gossip_validator,
min_block_delta,
metrics,
} = worker_params;

Expand All @@ -128,7 +124,6 @@ where
signed_commitment_sender,
gossip_engine: Arc::new(Mutex::new(gossip_engine)),
gossip_validator,
min_block_delta,
metrics,
rounds: None,
finality_notifications: client.finality_notification_stream(),
Expand All @@ -146,18 +141,28 @@ 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`
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// Return `true`, if we should vote on block `number`
/// Return `true`, if we should vote on block with `header`

fn should_vote_on(&self, number: NumberFor<B>) -> bool {
fn should_vote_on(&self, header: &B::Header) -> bool {
let number = *header.number();
let at = BlockId::hash(header.hash());
let session_boundary = self.client.runtime_api().get_session_boundary(&at).ok();
Copy link
Contributor

Choose a reason for hiding this comment

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

We might want to open an enhancement issue to cache session boundary in the worker and not have to call runtime api for every block here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll keep that in mind.

let session_start = if let Some(session_boundary) = session_boundary {
session_boundary
} else {
debug!(target: "beefy", "🥩 Could not retrieve session boundary- won't vote for: {:?}", number);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think at least warning here:

Suggested change
debug!(target: "beefy", "🥩 Could not retrieve session boundary- won't vote for: {:?}", number);
warn!(target: "beefy", "🥩 Could not retrieve session boundary - won't vote for: {:?}", number);

return false
};

let best_beefy_block = if let Some(block) = self.best_beefy_block {
block
} else {
debug!(target: "beefy", "🥩 Missing best BEEFY block - won't vote for: {:?}", number);
return false
};

let target = vote_target(self.best_grandpa_block, best_beefy_block, self.min_block_delta);
let target = vote_target(self.best_grandpa_block, best_beefy_block, session_start);

trace!(target: "beefy", "🥩 should_vote_on: #{:?}, next_block_to_vote_on: #{:?}", number, target);

Expand Down Expand Up @@ -258,7 +263,7 @@ where
}
}

if self.should_vote_on(*notification.header.number()) {
if self.should_vote_on(&notification.header) {
let (validators, validator_set_id) = if let Some(rounds) = &self.rounds {
(rounds.validators(), rounds.validator_set_id())
} else {
Expand Down Expand Up @@ -461,13 +466,18 @@ where
}

/// Calculate next block number to vote on
fn vote_target<N>(best_grandpa: N, best_beefy: N, min_delta: u32) -> N
fn vote_target<N>(best_grandpa: N, best_beefy: N, session_start: N) -> N
where
N: AtLeast32Bit + Copy + Debug,
{
// if the session_start as a mandatory block does not have a beefy justification yet, we vote on
// it
Copy link
Contributor

Choose a reason for hiding this comment

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

nit:

Suggested change
// if the session_start as a mandatory block does not have a beefy justification yet, we vote on
// it
// if the session_start as a mandatory block does not have a beefy justification yet,
// we vote on it

let m: u32 = if session_start <= best_beefy { 1 } else { 0 };

let diff = best_grandpa.saturating_sub(best_beefy);
let diff = diff.saturated_into::<u32>();
let target = best_beefy + min_delta.max(diff.next_power_of_two()).into();
let diff = diff.saturated_into::<u32>() / 2;
let target = (session_start * (1u32 - m).into()) +
((best_beefy + diff.next_power_of_two().into()) * m.into());

trace!(
target: "beefy",
Expand All @@ -485,90 +495,69 @@ mod tests {
use super::vote_target;

#[test]
fn vote_on_min_block_delta() {
let t = vote_target(1u32, 0, 4);
assert_eq!(4, t);
let t = vote_target(2u32, 0, 4);
assert_eq!(4, t);
let t = vote_target(3u32, 0, 4);
assert_eq!(4, t);
let t = vote_target(4u32, 0, 4);
assert_eq!(4, t);

let t = vote_target(4u32, 4, 4);
assert_eq!(8, t);

let t = vote_target(10u32, 10, 4);
assert_eq!(14, t);
let t = vote_target(11u32, 10, 4);
assert_eq!(14, t);
let t = vote_target(12u32, 10, 4);
assert_eq!(14, t);
let t = vote_target(13u32, 10, 4);
assert_eq!(14, t);

let t = vote_target(10u32, 10, 8);
assert_eq!(18, t);
let t = vote_target(11u32, 10, 8);
assert_eq!(18, t);
let t = vote_target(12u32, 10, 8);
assert_eq!(18, t);
let t = vote_target(13u32, 10, 8);
assert_eq!(18, t);
fn vote_on_mandatory_block() {
let t = vote_target(1008u32, 1000, 1001);
assert_eq!(1001, t);

let t = vote_target(1016u32, 1000, 1008);
assert_eq!(1008, t);

let t = vote_target(105u32, 100, 101);
assert_eq!(101, t);
}

#[test]
fn vote_on_power_of_two() {
let t = vote_target(1008u32, 1000, 4);
let t = vote_target(1008u32, 1000, 999);
assert_eq!(1004, t);

let t = vote_target(1016u32, 1000, 999);
assert_eq!(1008, t);

let t = vote_target(1016u32, 1000, 4);
let t = vote_target(1032u32, 1000, 999);
assert_eq!(1016, t);

let t = vote_target(1032u32, 1000, 4);
let t = vote_target(1064u32, 1000, 999);
assert_eq!(1032, t);

let t = vote_target(1064u32, 1000, 4);
let t = vote_target(1128u32, 1000, 999);
assert_eq!(1064, t);

let t = vote_target(1128u32, 1000, 4);
let t = vote_target(1256u32, 1000, 999);
assert_eq!(1128, t);

let t = vote_target(1256u32, 1000, 4);
let t = vote_target(1512u32, 1000, 999);
assert_eq!(1256, t);

let t = vote_target(1512u32, 1000, 4);
assert_eq!(1512, t);

let t = vote_target(1024u32, 0, 4);
assert_eq!(1024, t);
let t = vote_target(1024u32, 0, 0);
assert_eq!(512, t);
}

#[test]
fn vote_on_target_block() {
let t = vote_target(1008u32, 1002, 4);
assert_eq!(1010, t);
let t = vote_target(1010u32, 1002, 4);
assert_eq!(1010, t);

let t = vote_target(1016u32, 1006, 4);
assert_eq!(1022, t);
let t = vote_target(1022u32, 1006, 4);
assert_eq!(1022, t);

let t = vote_target(1032u32, 1012, 4);
assert_eq!(1044, t);
let t = vote_target(1044u32, 1012, 4);
assert_eq!(1044, t);

let t = vote_target(1064u32, 1014, 4);
assert_eq!(1078, t);
let t = vote_target(1078u32, 1014, 4);
assert_eq!(1078, t);

let t = vote_target(1128u32, 1008, 4);
assert_eq!(1136, t);
let t = vote_target(1136u32, 1008, 4);
assert_eq!(1136, t);
let t = vote_target(1008u32, 1002, 1001);
assert_eq!(1006, t);
let t = vote_target(1010u32, 1002, 1001);
assert_eq!(1006, t);

let t = vote_target(1016u32, 1006, 1004);
assert_eq!(1014, t);
let t = vote_target(1022u32, 1006, 1004);
assert_eq!(1014, t);

let t = vote_target(1032u32, 1012, 1010);
assert_eq!(1028, t);
let t = vote_target(1044u32, 1012, 1010);
assert_eq!(1028, t);

let t = vote_target(1064u32, 1014, 1012);
assert_eq!(1046, t);
let t = vote_target(1078u32, 1014, 1012);
assert_eq!(1046, t);

let t = vote_target(1128u32, 1008, 1004);
assert_eq!(1072, t);
let t = vote_target(1136u32, 1008, 1004);
assert_eq!(1072, t);
}
}
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