Beefy: Provide well-formed ValidatorSet (#10445)

* beefy: provide well-formed ValidatorSet

* pallet-beefy: use well-formed ValidatorSet

* pallet-beefy-mmr: use well-formed ValidatorSet

* beefy-gadget: fail votes early when ValidatorSet empty

* beefy: small efficiency improvements

* address review comments

Signed-off-by: acatangiu <adrian@parity.io>
This commit is contained in:
Adrian Catangiu
2021-12-21 16:34:32 +02:00
committed by GitHub
parent 2d347e68f2
commit 3d8ce67383
7 changed files with 146 additions and 99 deletions
+2 -2
View File
@@ -89,8 +89,8 @@ impl BeefyKeystore {
let store = self.0.clone().ok_or_else(|| error::Error::Keystore("no Keystore".into()))?;
let pk: Vec<Public> = SyncCryptoStore::ecdsa_public_keys(&*store, KEY_TYPE)
.iter()
.map(|k| Public::from(k.clone()))
.drain(..)
.map(Public::from)
.collect();
Ok(pk)
+22 -36
View File
@@ -74,15 +74,15 @@ where
N: Ord + AtLeast32BitUnsigned + MaybeDisplay + Clone,
{
pub(crate) fn validator_set_id(&self) -> ValidatorSetId {
self.validator_set.id
self.validator_set.id()
}
pub(crate) fn validators(&self) -> Vec<Public> {
self.validator_set.validators.clone()
pub(crate) fn validators(&self) -> &[Public] {
self.validator_set.validators()
}
pub(crate) fn add_vote(&mut self, round: &(H, N), vote: (Public, Signature)) -> bool {
if self.validator_set.validators.iter().any(|id| vote.0 == *id) {
if self.validator_set.validators().iter().any(|id| vote.0 == *id) {
self.rounds.entry(round.clone()).or_default().add_vote(vote)
} else {
false
@@ -93,7 +93,7 @@ where
let done = self
.rounds
.get(round)
.map(|tracker| tracker.is_done(threshold(self.validator_set.validators.len())))
.map(|tracker| tracker.is_done(threshold(self.validator_set.len())))
.unwrap_or(false);
debug!(target: "beefy", "🥩 Round #{} done: {}", round.1, done);
@@ -108,7 +108,7 @@ where
Some(
self.validator_set
.validators
.validators()
.iter()
.map(|authority_id| {
signatures.iter().find_map(|(id, sig)| {
@@ -139,26 +139,18 @@ mod tests {
fn new_rounds() {
sp_tracing::try_init_simple();
let rounds = Rounds::<H256, NumberFor<Block>>::new(ValidatorSet::<Public>::empty());
assert_eq!(0, rounds.validator_set_id());
assert!(rounds.validators().is_empty());
let validators = ValidatorSet::<Public> {
validators: vec![
Keyring::Alice.public(),
Keyring::Bob.public(),
Keyring::Charlie.public(),
],
id: 42,
};
let validators = ValidatorSet::<Public>::new(
vec![Keyring::Alice.public(), Keyring::Bob.public(), Keyring::Charlie.public()],
42,
)
.unwrap();
let rounds = Rounds::<H256, NumberFor<Block>>::new(validators);
assert_eq!(42, rounds.validator_set_id());
assert_eq!(
vec![Keyring::Alice.public(), Keyring::Bob.public(), Keyring::Charlie.public()],
&vec![Keyring::Alice.public(), Keyring::Bob.public(), Keyring::Charlie.public()],
rounds.validators()
);
}
@@ -167,14 +159,11 @@ mod tests {
fn add_vote() {
sp_tracing::try_init_simple();
let validators = ValidatorSet::<Public> {
validators: vec![
Keyring::Alice.public(),
Keyring::Bob.public(),
Keyring::Charlie.public(),
],
id: Default::default(),
};
let validators = ValidatorSet::<Public>::new(
vec![Keyring::Alice.public(), Keyring::Bob.public(), Keyring::Charlie.public()],
Default::default(),
)
.unwrap();
let mut rounds = Rounds::<H256, NumberFor<Block>>::new(validators);
@@ -212,14 +201,11 @@ mod tests {
fn drop() {
sp_tracing::try_init_simple();
let validators = ValidatorSet::<Public> {
validators: vec![
Keyring::Alice.public(),
Keyring::Bob.public(),
Keyring::Charlie.public(),
],
id: Default::default(),
};
let validators = ValidatorSet::<Public>::new(
vec![Keyring::Alice.public(), Keyring::Bob.public(), Keyring::Charlie.public()],
Default::default(),
)
.unwrap();
let mut rounds = Rounds::<H256, NumberFor<Block>>::new(validators);
+43 -26
View File
@@ -20,7 +20,7 @@ use std::{collections::BTreeSet, fmt::Debug, marker::PhantomData, sync::Arc};
use codec::{Codec, Decode, Encode};
use futures::{future, FutureExt, StreamExt};
use log::{debug, error, info, trace, warn};
use log::{debug, error, info, log_enabled, trace, warn};
use parking_lot::Mutex;
use sc_client_api::{Backend, FinalityNotification, FinalityNotifications};
@@ -79,7 +79,7 @@ where
/// Min delta in block numbers between two blocks, BEEFY should vote on
min_block_delta: u32,
metrics: Option<Metrics>,
rounds: round::Rounds<Payload, NumberFor<B>>,
rounds: Option<round::Rounds<Payload, NumberFor<B>>>,
finality_notifications: FinalityNotifications<B>,
/// Best block we received a GRANDPA notification for
best_grandpa_block: NumberFor<B>,
@@ -125,7 +125,7 @@ where
gossip_validator,
min_block_delta,
metrics,
rounds: round::Rounds::new(ValidatorSet::empty()),
rounds: None,
finality_notifications: client.finality_notification_stream(),
best_grandpa_block: client.info().finalized_number,
best_beefy_block: None,
@@ -172,7 +172,7 @@ where
Some(new)
} else {
let at = BlockId::hash(header.hash());
self.client.runtime_api().validator_set(&at).ok()
self.client.runtime_api().validator_set(&at).ok().flatten()
};
trace!(target: "beefy", "🥩 active validator set: {:?}", new);
@@ -190,11 +190,12 @@ where
fn verify_validator_set(
&self,
block: &NumberFor<B>,
mut active: ValidatorSet<Public>,
active: &ValidatorSet<Public>,
) -> Result<(), error::Error> {
let active: BTreeSet<Public> = active.validators.drain(..).collect();
let active: BTreeSet<&Public> = active.validators().iter().collect();
let store: BTreeSet<Public> = self.key_store.public_keys()?.drain(..).collect();
let public_keys = self.key_store.public_keys()?;
let store: BTreeSet<&Public> = public_keys.iter().collect();
let missing: Vec<_> = store.difference(&active).cloned().collect();
@@ -214,26 +215,31 @@ where
if let Some(active) = self.validator_set(&notification.header) {
// Authority set change or genesis set id triggers new voting rounds
//
// TODO: (adoerr) Enacting a new authority set will also implicitly 'conclude'
// the currently active BEEFY voting round by starting a new one. This is
// temporary and needs to be replaced by proper round life cycle handling.
if active.id != self.rounds.validator_set_id() ||
(active.id == GENESIS_AUTHORITY_SET_ID && self.best_beefy_block.is_none())
// TODO: (grandpa-bridge-gadget#366) Enacting a new authority set will also
// implicitly 'conclude' the currently active BEEFY voting round by starting a
// new one. This should be replaced by proper round life-cycle handling.
if self.rounds.is_none() ||
active.id() != self.rounds.as_ref().unwrap().validator_set_id() ||
(active.id() == GENESIS_AUTHORITY_SET_ID && self.best_beefy_block.is_none())
{
debug!(target: "beefy", "🥩 New active validator set id: {:?}", active);
metric_set!(self, beefy_validator_set_id, active.id);
metric_set!(self, beefy_validator_set_id, active.id());
// BEEFY should produce a signed commitment for each session
if active.id != self.last_signed_id + 1 && active.id != GENESIS_AUTHORITY_SET_ID {
if active.id() != self.last_signed_id + 1 && active.id() != GENESIS_AUTHORITY_SET_ID
{
metric_inc!(self, beefy_skipped_sessions);
}
// verify the new validator set
let _ = self.verify_validator_set(notification.header.number(), active.clone());
if log_enabled!(target: "beefy", log::Level::Debug) {
// verify the new validator set - only do it if we're also logging the warning
let _ = self.verify_validator_set(notification.header.number(), &active);
}
self.rounds = round::Rounds::new(active.clone());
let id = active.id();
self.rounds = Some(round::Rounds::new(active));
debug!(target: "beefy", "🥩 New Rounds for id: {:?}", active.id);
debug!(target: "beefy", "🥩 New Rounds for id: {:?}", id);
self.best_beefy_block = Some(*notification.header.number());
@@ -244,9 +250,13 @@ where
}
if self.should_vote_on(*notification.header.number()) {
let authority_id = if let Some(id) =
self.key_store.authority_id(self.rounds.validators().as_slice())
{
let (validators, validator_set_id) = if let Some(rounds) = &self.rounds {
(rounds.validators(), rounds.validator_set_id())
} else {
debug!(target: "beefy", "🥩 Missing validator set - can't vote for: {:?}", notification.header.hash());
return
};
let authority_id = if let Some(id) = self.key_store.authority_id(validators) {
debug!(target: "beefy", "🥩 Local authority id: {:?}", id);
id
} else {
@@ -266,7 +276,7 @@ where
let commitment = Commitment {
payload,
block_number: notification.header.number(),
validator_set_id: self.rounds.validator_set_id(),
validator_set_id,
};
let encoded_commitment = commitment.encode();
@@ -305,12 +315,19 @@ where
fn handle_vote(&mut self, round: (Payload, NumberFor<B>), vote: (Public, Signature)) {
self.gossip_validator.note_round(round.1);
let vote_added = self.rounds.add_vote(&round, vote);
let rounds = if let Some(rounds) = self.rounds.as_mut() {
rounds
} else {
debug!(target: "beefy", "🥩 Missing validator set - can't handle vote {:?}", vote);
return
};
if vote_added && self.rounds.is_done(&round) {
if let Some(signatures) = self.rounds.drop(&round) {
let vote_added = rounds.add_vote(&round, vote);
if vote_added && rounds.is_done(&round) {
if let Some(signatures) = rounds.drop(&round) {
// id is stored for skipped session metric calculation
self.last_signed_id = self.rounds.validator_set_id();
self.last_signed_id = rounds.validator_set_id();
let commitment = Commitment {
payload: round.0,