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

Commit 64799ea

Browse files
authored
Rework BeefyAPI (#110)
1 parent 80eaa52 commit 64799ea

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

client/beefy/src/lib.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,13 @@ pub async fn start_beefy_gadget<Block, Pair, Backend, Client, Network, SyncOracl
111111
);
112112

113113
let at = BlockId::hash(client.info().best_hash);
114-
let authorities = client
114+
let validator_set = client
115115
.runtime_api()
116-
.authorities(&at)
116+
.validator_set(&at)
117117
.expect("Failed to get BEEFY authorities");
118118

119-
let local_id = match authorities
119+
let local_id = match validator_set
120+
.validators
120121
.iter()
121122
.find(|id| SyncCryptoStore::has_keys(&*key_store, &[(id.to_raw_vec(), KEY_TYPE)]))
122123
{
@@ -136,7 +137,7 @@ pub async fn start_beefy_gadget<Block, Pair, Backend, Client, Network, SyncOracl
136137
let worker = worker::BeefyWorker::<_, Pair::Public, Pair::Signature, _>::new(
137138
local_id,
138139
key_store,
139-
authorities,
140+
validator_set.validators,
140141
client.finality_notification_stream(),
141142
gossip_engine,
142143
signed_commitment_sender,

frame/beefy/src/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ pub mod pallet {
9696
}
9797

9898
impl<T: Config> Pallet<T> {
99+
/// Return the current active BEEFY validator set.
100+
pub fn validator_set() -> ValidatorSet<T::AuthorityId> {
101+
ValidatorSet::<T::AuthorityId> {
102+
validators: Self::authorities(),
103+
id: Self::validator_set_id(),
104+
}
105+
}
106+
99107
fn change_authorities(new: Vec<T::AuthorityId>, queued: Vec<T::AuthorityId>) {
100108
// As in GRANDPA, we trigger a validator set change only if the the validator
101109
// set has actually changed.

frame/beefy/src/tests.rs

+36
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,39 @@ fn session_change_updates_next_authorities() {
103103
assert_eq!(want[3], next_authorities[1]);
104104
});
105105
}
106+
107+
#[test]
108+
fn validator_set_at_genesis() {
109+
let want = vec![mock_beefy_id(1), mock_beefy_id(2)];
110+
111+
new_test_ext(vec![1, 2, 3, 4]).execute_with(|| {
112+
let vs = Beefy::validator_set();
113+
114+
assert_eq!(vs.id, 0u64);
115+
assert_eq!(vs.validators[0], want[0]);
116+
assert_eq!(vs.validators[1], want[1]);
117+
});
118+
}
119+
120+
#[test]
121+
fn validator_set_updates_work() {
122+
let want = vec![mock_beefy_id(1), mock_beefy_id(2), mock_beefy_id(3), mock_beefy_id(4)];
123+
124+
new_test_ext(vec![1, 2, 3, 4]).execute_with(|| {
125+
init_block(1);
126+
127+
let vs = Beefy::validator_set();
128+
129+
assert_eq!(vs.id, 0u64);
130+
assert_eq!(want[0], vs.validators[0]);
131+
assert_eq!(want[1], vs.validators[1]);
132+
133+
init_block(2);
134+
135+
let vs = Beefy::validator_set();
136+
137+
assert_eq!(vs.id, 1u64);
138+
assert_eq!(want[2], vs.validators[0]);
139+
assert_eq!(want[3], vs.validators[1]);
140+
});
141+
}

primitives/beefy/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub const BEEFY_ENGINE_ID: sp_runtime::ConsensusEngineId = *b"BEEF";
7070
pub type ValidatorSetId = u64;
7171

7272
/// A set of BEEFY authorities, a.k.a. validators.
73-
#[derive(Decode, Encode, Debug)]
73+
#[derive(Decode, Encode, Debug, PartialEq)]
7474
pub struct ValidatorSet<AuthorityId> {
7575
/// Public keys of the validator set elements
7676
pub validators: Vec<AuthorityId>,
@@ -101,7 +101,7 @@ pub enum ConsensusLog<AuthorityId: Codec> {
101101
sp_api::decl_runtime_apis! {
102102
/// API necessary for BEEFY voters.
103103
pub trait BeefyApi<AuthorityId: Codec> {
104-
/// Return the current set of authorities.
105-
fn authorities() -> Vec<AuthorityId>;
104+
/// Return the current active BEEFY validator set
105+
fn validator_set() -> ValidatorSet<AuthorityId>;
106106
}
107107
}

0 commit comments

Comments
 (0)