Skip to content
This repository has been archived by the owner on Nov 11, 2022. 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 2e1136f commit 0e11cfe
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 33 deletions.
9 changes: 5 additions & 4 deletions beefy-gadget/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
58 changes: 32 additions & 26 deletions beefy-node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,45 @@
#[cfg(feature = "std")]
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));

use beefy_primitives::ecdsa::AuthorityId as BeefyId;
use frame_system::limits;
use pallet_grandpa::fg_primitives;
use pallet_grandpa::{AuthorityId as GrandpaId, AuthorityList as GrandpaAuthorityList};
use sp_api::impl_runtime_apis;
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
use sp_core::{crypto::KeyTypeId, OpaqueMetadata};
use sp_runtime::traits::{BlakeTwo256, Block as BlockT, IdentifyAccount, IdentityLookup, Keccak256, NumberFor, Verify};
use sp_runtime::{
create_runtime_str, generic, impl_opaque_keys,
transaction_validity::{TransactionSource, TransactionValidity},
ApplyExtrinsicResult, MultiSignature,
use {
beefy_primitives::{ecdsa::AuthorityId as BeefyId, ValidatorSet},
frame_system::limits,
pallet_grandpa::fg_primitives,
pallet_grandpa::{AuthorityId as GrandpaId, AuthorityList as GrandpaAuthorityList},
sp_api::impl_runtime_apis,
sp_consensus_aura::sr25519::AuthorityId as AuraId,
sp_core::{crypto::KeyTypeId, OpaqueMetadata},
sp_runtime::traits::{BlakeTwo256, Block as BlockT, IdentifyAccount, IdentityLookup, Keccak256, NumberFor, Verify},
sp_runtime::{
create_runtime_str, generic, impl_opaque_keys,
transaction_validity::{TransactionSource, TransactionValidity},
ApplyExtrinsicResult, MultiSignature,
},
sp_std::prelude::*,
sp_version::RuntimeVersion,
};
use sp_std::prelude::*;

#[cfg(feature = "std")]
use sp_version::NativeVersion;
use sp_version::RuntimeVersion;

// A few exports that help ease life for downstream crates.
pub use frame_support::{
construct_runtime, parameter_types,
traits::{KeyOwnerProofSystem, Randomness},
weights::{
constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_PER_SECOND},
DispatchClass, IdentityFee, Weight,
pub use {
frame_support::{
construct_runtime, parameter_types,
traits::{KeyOwnerProofSystem, Randomness},
weights::{
constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_PER_SECOND},
DispatchClass, IdentityFee, Weight,
},
StorageValue,
},
StorageValue,
pallet_balances::Call as BalancesCall,
pallet_timestamp::Call as TimestampCall,
sp_runtime::{Perbill, Permill},
};
pub use pallet_balances::Call as BalancesCall;
pub use pallet_timestamp::Call as TimestampCall;

#[cfg(any(feature = "std", test))]
pub use sp_runtime::BuildStorage;
pub use sp_runtime::{Perbill, Permill};

/// An index to a block.
pub type BlockNumber = u32;
Expand Down Expand Up @@ -453,8 +459,8 @@ impl_runtime_apis! {
}

impl beefy_primitives::BeefyApi<Block, BeefyId> for Runtime {
fn authorities() -> Vec<BeefyId> {
Beefy::authorities()
fn validator_set() -> ValidatorSet<BeefyId> {
Beefy::validator_set()
}
}

Expand Down
8 changes: 8 additions & 0 deletions beefy-pallet/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 beefy-pallet/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 beefy-primitives/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 0e11cfe

Please sign in to comment.