mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-15 10:21:05 +00:00
Introduce a Slot type (#7997)
* Introduce a `Slot` type Instead of having some type definition that only was used in half of the code or directly using `u64`, this adds a new unit type wrapper `Slot`. This makes it especially easy for the outside api to know what type is expected/returned. * Change epoch duratioC * rename all instances of slot number to slot * Make the constructor private Co-authored-by: André Silva <andrerfosilva@gmail.com>
This commit is contained in:
@@ -20,11 +20,8 @@
|
||||
|
||||
use sp_application_crypto::AppKey;
|
||||
use sp_consensus_babe::{
|
||||
BABE_VRF_PREFIX,
|
||||
AuthorityId, BabeAuthorityWeight,
|
||||
SlotNumber,
|
||||
make_transcript,
|
||||
make_transcript_data,
|
||||
BABE_VRF_PREFIX, AuthorityId, BabeAuthorityWeight, make_transcript, make_transcript_data,
|
||||
Slot,
|
||||
};
|
||||
use sp_consensus_babe::digests::{
|
||||
PreDigest, PrimaryPreDigest, SecondaryPlainPreDigest, SecondaryVRFPreDigest,
|
||||
@@ -106,7 +103,7 @@ pub(super) fn check_primary_threshold(inout: &VRFInOut, threshold: u128) -> bool
|
||||
/// authorities. This should always assign the slot to some authority unless the
|
||||
/// authorities list is empty.
|
||||
pub(super) fn secondary_slot_author(
|
||||
slot_number: u64,
|
||||
slot: Slot,
|
||||
authorities: &[(AuthorityId, BabeAuthorityWeight)],
|
||||
randomness: [u8; 32],
|
||||
) -> Option<&AuthorityId> {
|
||||
@@ -114,7 +111,7 @@ pub(super) fn secondary_slot_author(
|
||||
return None;
|
||||
}
|
||||
|
||||
let rand = U256::from((randomness, slot_number).using_encoded(blake2_256));
|
||||
let rand = U256::from((randomness, slot).using_encoded(blake2_256));
|
||||
|
||||
let authorities_len = U256::from(authorities.len());
|
||||
let idx = rand % authorities_len;
|
||||
@@ -130,7 +127,7 @@ pub(super) fn secondary_slot_author(
|
||||
/// pre-digest to use when authoring the block, or `None` if it is not our turn
|
||||
/// to propose.
|
||||
fn claim_secondary_slot(
|
||||
slot_number: SlotNumber,
|
||||
slot: Slot,
|
||||
epoch: &Epoch,
|
||||
keys: &[(AuthorityId, usize)],
|
||||
keystore: &SyncCryptoStorePtr,
|
||||
@@ -143,7 +140,7 @@ fn claim_secondary_slot(
|
||||
}
|
||||
|
||||
let expected_author = super::authorship::secondary_slot_author(
|
||||
slot_number,
|
||||
slot,
|
||||
authorities,
|
||||
*randomness,
|
||||
)?;
|
||||
@@ -153,7 +150,7 @@ fn claim_secondary_slot(
|
||||
let pre_digest = if author_secondary_vrf {
|
||||
let transcript_data = super::authorship::make_transcript_data(
|
||||
randomness,
|
||||
slot_number,
|
||||
slot,
|
||||
*epoch_index,
|
||||
);
|
||||
let result = SyncCryptoStore::sr25519_vrf_sign(
|
||||
@@ -164,7 +161,7 @@ fn claim_secondary_slot(
|
||||
);
|
||||
if let Ok(signature) = result {
|
||||
Some(PreDigest::SecondaryVRF(SecondaryVRFPreDigest {
|
||||
slot_number,
|
||||
slot,
|
||||
vrf_output: VRFOutput(signature.output),
|
||||
vrf_proof: VRFProof(signature.proof),
|
||||
authority_index: *authority_index as u32,
|
||||
@@ -174,7 +171,7 @@ fn claim_secondary_slot(
|
||||
}
|
||||
} else if SyncCryptoStore::has_keys(&**keystore, &[(authority_id.to_raw_vec(), AuthorityId::ID)]) {
|
||||
Some(PreDigest::SecondaryPlain(SecondaryPlainPreDigest {
|
||||
slot_number,
|
||||
slot,
|
||||
authority_index: *authority_index as u32,
|
||||
}))
|
||||
} else {
|
||||
@@ -195,7 +192,7 @@ fn claim_secondary_slot(
|
||||
/// secondary slots enabled for the given epoch, we will fallback to trying to
|
||||
/// claim a secondary slot.
|
||||
pub fn claim_slot(
|
||||
slot_number: SlotNumber,
|
||||
slot: Slot,
|
||||
epoch: &Epoch,
|
||||
keystore: &SyncCryptoStorePtr,
|
||||
) -> Option<(PreDigest, AuthorityId)> {
|
||||
@@ -203,24 +200,24 @@ pub fn claim_slot(
|
||||
.enumerate()
|
||||
.map(|(index, a)| (a.0.clone(), index))
|
||||
.collect::<Vec<_>>();
|
||||
claim_slot_using_keys(slot_number, epoch, keystore, &authorities)
|
||||
claim_slot_using_keys(slot, epoch, keystore, &authorities)
|
||||
}
|
||||
|
||||
/// Like `claim_slot`, but allows passing an explicit set of key pairs. Useful if we intend
|
||||
/// to make repeated calls for different slots using the same key pairs.
|
||||
pub fn claim_slot_using_keys(
|
||||
slot_number: SlotNumber,
|
||||
slot: Slot,
|
||||
epoch: &Epoch,
|
||||
keystore: &SyncCryptoStorePtr,
|
||||
keys: &[(AuthorityId, usize)],
|
||||
) -> Option<(PreDigest, AuthorityId)> {
|
||||
claim_primary_slot(slot_number, epoch, epoch.config.c, keystore, &keys)
|
||||
claim_primary_slot(slot, epoch, epoch.config.c, keystore, &keys)
|
||||
.or_else(|| {
|
||||
if epoch.config.allowed_slots.is_secondary_plain_slots_allowed() ||
|
||||
epoch.config.allowed_slots.is_secondary_vrf_slots_allowed()
|
||||
{
|
||||
claim_secondary_slot(
|
||||
slot_number,
|
||||
slot,
|
||||
&epoch,
|
||||
keys,
|
||||
&keystore,
|
||||
@@ -237,7 +234,7 @@ pub fn claim_slot_using_keys(
|
||||
/// the VRF. If the VRF produces a value less than `threshold`, it is our turn,
|
||||
/// so it returns `Some(_)`. Otherwise, it returns `None`.
|
||||
fn claim_primary_slot(
|
||||
slot_number: SlotNumber,
|
||||
slot: Slot,
|
||||
epoch: &Epoch,
|
||||
c: (u64, u64),
|
||||
keystore: &SyncCryptoStorePtr,
|
||||
@@ -248,12 +245,12 @@ fn claim_primary_slot(
|
||||
for (authority_id, authority_index) in keys {
|
||||
let transcript = super::authorship::make_transcript(
|
||||
randomness,
|
||||
slot_number,
|
||||
slot,
|
||||
*epoch_index
|
||||
);
|
||||
let transcript_data = super::authorship::make_transcript_data(
|
||||
randomness,
|
||||
slot_number,
|
||||
slot,
|
||||
*epoch_index
|
||||
);
|
||||
// Compute the threshold we will use.
|
||||
@@ -276,7 +273,7 @@ fn claim_primary_slot(
|
||||
};
|
||||
if super::authorship::check_primary_threshold(&inout, threshold) {
|
||||
let pre_digest = PreDigest::Primary(PrimaryPreDigest {
|
||||
slot_number,
|
||||
slot,
|
||||
vrf_output: VRFOutput(signature.output),
|
||||
vrf_proof: VRFProof(signature.proof),
|
||||
authority_index: *authority_index as u32,
|
||||
@@ -314,7 +311,7 @@ mod tests {
|
||||
|
||||
let mut epoch = Epoch {
|
||||
epoch_index: 10,
|
||||
start_slot: 0,
|
||||
start_slot: 0.into(),
|
||||
duration: 20,
|
||||
authorities: authorities.clone(),
|
||||
randomness: Default::default(),
|
||||
@@ -324,9 +321,9 @@ mod tests {
|
||||
},
|
||||
};
|
||||
|
||||
assert!(claim_slot(10, &epoch, &keystore).is_none());
|
||||
assert!(claim_slot(10.into(), &epoch, &keystore).is_none());
|
||||
|
||||
epoch.authorities.push((valid_public_key.clone().into(), 10));
|
||||
assert_eq!(claim_slot(10, &epoch, &keystore).unwrap().1, valid_public_key.into());
|
||||
assert_eq!(claim_slot(10.into(), &epoch, &keystore).unwrap().1, valid_public_key.into());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user