Babe epoch newtype (#1596)

Removal of verbatim duplication of BABE's `Epoch` struct in the client.

I think is better to have one single definition and wrap the primitive
`Epoch` in a newtype (required because we need to implement the `Epoch`
trait).
This commit is contained in:
Davide Galassi
2023-09-17 22:06:19 +02:00
committed by GitHub
parent d787269cdf
commit c7dbfc21b6
6 changed files with 45 additions and 51 deletions
@@ -132,23 +132,22 @@ fn claim_secondary_slot(
keystore: &KeystorePtr,
author_secondary_vrf: bool,
) -> Option<(PreDigest, AuthorityId)> {
let Epoch { authorities, randomness, mut epoch_index, .. } = epoch;
if authorities.is_empty() {
if epoch.authorities.is_empty() {
return None
}
let mut epoch_index = epoch.epoch_index;
if epoch.end_slot() <= slot {
// Slot doesn't strictly belong to the epoch, create a clone with fixed values.
epoch_index = epoch.clone_for_slot(slot).epoch_index;
}
let expected_author = secondary_slot_author(slot, authorities, *randomness)?;
let expected_author = secondary_slot_author(slot, &epoch.authorities, epoch.randomness)?;
for (authority_id, authority_index) in keys {
if authority_id == expected_author {
let pre_digest = if author_secondary_vrf {
let data = make_vrf_sign_data(randomness, slot, epoch_index);
let data = make_vrf_sign_data(&epoch.randomness, slot, epoch_index);
let result =
keystore.sr25519_vrf_sign(AuthorityId::ID, authority_id.as_ref(), &data);
if let Ok(Some(vrf_signature)) = result {
@@ -232,19 +231,18 @@ fn claim_primary_slot(
keystore: &KeystorePtr,
keys: &[(AuthorityId, usize)],
) -> Option<(PreDigest, AuthorityId)> {
let Epoch { authorities, randomness, mut epoch_index, .. } = epoch;
let mut epoch_index = epoch.epoch_index;
if epoch.end_slot() <= slot {
// Slot doesn't strictly belong to the epoch, create a clone with fixed values.
epoch_index = epoch.clone_for_slot(slot).epoch_index;
}
let data = make_vrf_sign_data(randomness, slot, epoch_index);
let data = make_vrf_sign_data(&epoch.randomness, slot, epoch_index);
for (authority_id, authority_index) in keys {
let result = keystore.sr25519_vrf_sign(AuthorityId::ID, authority_id.as_ref(), &data);
if let Ok(Some(vrf_signature)) = result {
let threshold = calculate_primary_threshold(c, authorities, *authority_index);
let threshold = calculate_primary_threshold(c, &epoch.authorities, *authority_index);
let can_claim = authority_id
.as_inner_ref()
@@ -274,7 +272,7 @@ fn claim_primary_slot(
#[cfg(test)]
mod tests {
use super::*;
use sp_consensus_babe::{AllowedSlots, AuthorityId, BabeEpochConfiguration};
use sp_consensus_babe::{AllowedSlots, AuthorityId, BabeEpochConfiguration, Epoch};
use sp_core::{crypto::Pair as _, sr25519::Pair};
use sp_keystore::testing::MemoryKeystore;
@@ -300,7 +298,8 @@ mod tests {
c: (3, 10),
allowed_slots: AllowedSlots::PrimaryAndSecondaryPlainSlots,
},
};
}
.into();
assert!(claim_slot(10.into(), &epoch, &keystore).is_none());