From 0e11cfe87594228cb5d9e09a6f6d055c779e266a Mon Sep 17 00:00:00 2001 From: Andreas Doerr Date: Tue, 9 Mar 2021 13:12:57 +0100 Subject: [PATCH] Rework BeefyAPI (#110) --- beefy-gadget/src/lib.rs | 9 +++--- beefy-node/runtime/src/lib.rs | 58 +++++++++++++++++++---------------- beefy-pallet/src/lib.rs | 8 +++++ beefy-pallet/src/tests.rs | 36 ++++++++++++++++++++++ beefy-primitives/src/lib.rs | 6 ++-- 5 files changed, 84 insertions(+), 33 deletions(-) diff --git a/beefy-gadget/src/lib.rs b/beefy-gadget/src/lib.rs index 152d5745..dc107c06 100644 --- a/beefy-gadget/src/lib.rs +++ b/beefy-gadget/src/lib.rs @@ -111,12 +111,13 @@ pub async fn start_beefy_gadget::new( local_id, key_store, - authorities, + validator_set.validators, client.finality_notification_stream(), gossip_engine, signed_commitment_sender, diff --git a/beefy-node/runtime/src/lib.rs b/beefy-node/runtime/src/lib.rs index b409fde0..8b666ad1 100644 --- a/beefy-node/runtime/src/lib.rs +++ b/beefy-node/runtime/src/lib.rs @@ -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; @@ -453,8 +459,8 @@ impl_runtime_apis! { } impl beefy_primitives::BeefyApi for Runtime { - fn authorities() -> Vec { - Beefy::authorities() + fn validator_set() -> ValidatorSet { + Beefy::validator_set() } } diff --git a/beefy-pallet/src/lib.rs b/beefy-pallet/src/lib.rs index 7ecf93fc..68bfffde 100644 --- a/beefy-pallet/src/lib.rs +++ b/beefy-pallet/src/lib.rs @@ -96,6 +96,14 @@ pub mod pallet { } impl Pallet { + /// Return the current active BEEFY validator set. + pub fn validator_set() -> ValidatorSet { + ValidatorSet:: { + validators: Self::authorities(), + id: Self::validator_set_id(), + } + } + fn change_authorities(new: Vec, queued: Vec) { // As in GRANDPA, we trigger a validator set change only if the the validator // set has actually changed. diff --git a/beefy-pallet/src/tests.rs b/beefy-pallet/src/tests.rs index 186334f3..9ee30350 100644 --- a/beefy-pallet/src/tests.rs +++ b/beefy-pallet/src/tests.rs @@ -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]); + }); +} diff --git a/beefy-primitives/src/lib.rs b/beefy-primitives/src/lib.rs index fcc21590..cbd227da 100644 --- a/beefy-primitives/src/lib.rs +++ b/beefy-primitives/src/lib.rs @@ -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 { /// Public keys of the validator set elements pub validators: Vec, @@ -101,7 +101,7 @@ pub enum ConsensusLog { sp_api::decl_runtime_apis! { /// API necessary for BEEFY voters. pub trait BeefyApi { - /// Return the current set of authorities. - fn authorities() -> Vec; + /// Return the current active BEEFY validator set + fn validator_set() -> ValidatorSet; } }