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

Commit

Permalink
Rework BeefyAPI (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
adoerr authored Mar 9, 2021
1 parent 80eaa52 commit 64799ea
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
9 changes: 5 additions & 4 deletions client/beefy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,13 @@ pub async fn start_beefy_gadget<Block, Pair, Backend, Client, Network, SyncOracl
);

let at = BlockId::hash(client.info().best_hash);
let authorities = client
let validator_set = client
.runtime_api()
.authorities(&at)
.validator_set(&at)
.expect("Failed to get BEEFY authorities");

let local_id = match authorities
let local_id = match validator_set
.validators
.iter()
.find(|id| SyncCryptoStore::has_keys(&*key_store, &[(id.to_raw_vec(), KEY_TYPE)]))
{
Expand All @@ -136,7 +137,7 @@ pub async fn start_beefy_gadget<Block, Pair, Backend, Client, Network, SyncOracl
let worker = worker::BeefyWorker::<_, Pair::Public, Pair::Signature, _>::new(
local_id,
key_store,
authorities,
validator_set.validators,
client.finality_notification_stream(),
gossip_engine,
signed_commitment_sender,
Expand Down
8 changes: 8 additions & 0 deletions frame/beefy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ pub mod pallet {
}

impl<T: Config> Pallet<T> {
/// Return the current active BEEFY validator set.
pub fn validator_set() -> ValidatorSet<T::AuthorityId> {
ValidatorSet::<T::AuthorityId> {
validators: Self::authorities(),
id: Self::validator_set_id(),
}
}

fn change_authorities(new: Vec<T::AuthorityId>, queued: Vec<T::AuthorityId>) {
// As in GRANDPA, we trigger a validator set change only if the the validator
// set has actually changed.
Expand Down
36 changes: 36 additions & 0 deletions frame/beefy/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,39 @@ fn session_change_updates_next_authorities() {
assert_eq!(want[3], next_authorities[1]);
});
}

#[test]
fn validator_set_at_genesis() {
let want = vec![mock_beefy_id(1), mock_beefy_id(2)];

new_test_ext(vec![1, 2, 3, 4]).execute_with(|| {
let vs = Beefy::validator_set();

assert_eq!(vs.id, 0u64);
assert_eq!(vs.validators[0], want[0]);
assert_eq!(vs.validators[1], want[1]);
});
}

#[test]
fn validator_set_updates_work() {
let want = vec![mock_beefy_id(1), mock_beefy_id(2), mock_beefy_id(3), mock_beefy_id(4)];

new_test_ext(vec![1, 2, 3, 4]).execute_with(|| {
init_block(1);

let vs = Beefy::validator_set();

assert_eq!(vs.id, 0u64);
assert_eq!(want[0], vs.validators[0]);
assert_eq!(want[1], vs.validators[1]);

init_block(2);

let vs = Beefy::validator_set();

assert_eq!(vs.id, 1u64);
assert_eq!(want[2], vs.validators[0]);
assert_eq!(want[3], vs.validators[1]);
});
}
6 changes: 3 additions & 3 deletions primitives/beefy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub const BEEFY_ENGINE_ID: sp_runtime::ConsensusEngineId = *b"BEEF";
pub type ValidatorSetId = u64;

/// A set of BEEFY authorities, a.k.a. validators.
#[derive(Decode, Encode, Debug)]
#[derive(Decode, Encode, Debug, PartialEq)]
pub struct ValidatorSet<AuthorityId> {
/// Public keys of the validator set elements
pub validators: Vec<AuthorityId>,
Expand Down Expand Up @@ -101,7 +101,7 @@ pub enum ConsensusLog<AuthorityId: Codec> {
sp_api::decl_runtime_apis! {
/// API necessary for BEEFY voters.
pub trait BeefyApi<AuthorityId: Codec> {
/// Return the current set of authorities.
fn authorities() -> Vec<AuthorityId>;
/// Return the current active BEEFY validator set
fn validator_set() -> ValidatorSet<AuthorityId>;
}
}

0 comments on commit 64799ea

Please sign in to comment.