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:
Bastian Köcher
2021-01-28 20:44:22 +01:00
committed by GitHub
parent 6c2dd28dfb
commit b6294418f8
34 changed files with 549 additions and 445 deletions
@@ -21,7 +21,7 @@
use codec::{Encode, Decode};
use sc_client_api::backend::AuxStore;
use sp_blockchain::{Result as ClientResult, Error as ClientError};
use sp_consensus_slots::EquivocationProof;
use sp_consensus_slots::{EquivocationProof, Slot};
use sp_runtime::traits::Header;
const SLOT_HEADER_MAP_KEY: &[u8] = b"slot_header_map";
@@ -52,8 +52,8 @@ fn load_decode<C, T>(backend: &C, key: &[u8]) -> ClientResult<Option<T>>
/// Note: it detects equivocations only when slot_now - slot <= MAX_SLOT_CAPACITY.
pub fn check_equivocation<C, H, P>(
backend: &C,
slot_now: u64,
slot: u64,
slot_now: Slot,
slot: Slot,
header: &H,
signer: &P,
) -> ClientResult<Option<EquivocationProof<H, P>>>
@@ -63,7 +63,7 @@ pub fn check_equivocation<C, H, P>(
P: Clone + Encode + Decode + PartialEq,
{
// We don't check equivocations for old headers out of our capacity.
if slot_now.saturating_sub(slot) > MAX_SLOT_CAPACITY {
if slot_now.saturating_sub(*slot) > Slot::from(MAX_SLOT_CAPACITY) {
return Ok(None);
}
@@ -77,7 +77,7 @@ pub fn check_equivocation<C, H, P>(
// Get first slot saved.
let slot_header_start = SLOT_HEADER_START.to_vec();
let first_saved_slot = load_decode::<_, u64>(backend, &slot_header_start[..])?
let first_saved_slot = load_decode::<_, Slot>(backend, &slot_header_start[..])?
.unwrap_or(slot);
if slot_now < first_saved_slot {
@@ -92,7 +92,7 @@ pub fn check_equivocation<C, H, P>(
// 2) with different hash
if header.hash() != prev_header.hash() {
return Ok(Some(EquivocationProof {
slot_number: slot,
slot,
offender: signer.clone(),
first_header: prev_header.clone(),
second_header: header.clone(),
@@ -109,11 +109,11 @@ pub fn check_equivocation<C, H, P>(
let mut keys_to_delete = vec![];
let mut new_first_saved_slot = first_saved_slot;
if slot_now - first_saved_slot >= PRUNING_BOUND {
if *slot_now - *first_saved_slot >= PRUNING_BOUND {
let prefix = SLOT_HEADER_MAP_KEY.to_vec();
new_first_saved_slot = slot_now.saturating_sub(MAX_SLOT_CAPACITY);
for s in first_saved_slot..new_first_saved_slot {
for s in u64::from(first_saved_slot)..new_first_saved_slot.into() {
let mut p = prefix.clone();
s.using_encoded(|s| p.extend(s));
keys_to_delete.push(p);
@@ -174,8 +174,8 @@ mod test {
assert!(
check_equivocation(
&client,
2,
2,
2.into(),
2.into(),
&header1,
&public,
).unwrap().is_none(),
@@ -184,8 +184,8 @@ mod test {
assert!(
check_equivocation(
&client,
3,
2,
3.into(),
2.into(),
&header1,
&public,
).unwrap().is_none(),
@@ -195,8 +195,8 @@ mod test {
assert!(
check_equivocation(
&client,
4,
2,
4.into(),
2.into(),
&header2,
&public,
).unwrap().is_some(),
@@ -206,8 +206,8 @@ mod test {
assert!(
check_equivocation(
&client,
5,
4,
5.into(),
4.into(),
&header3,
&public,
).unwrap().is_none(),
@@ -217,8 +217,8 @@ mod test {
assert!(
check_equivocation(
&client,
PRUNING_BOUND + 2,
MAX_SLOT_CAPACITY + 4,
(PRUNING_BOUND + 2).into(),
(MAX_SLOT_CAPACITY + 4).into(),
&header4,
&public,
).unwrap().is_none(),
@@ -228,8 +228,8 @@ mod test {
assert!(
check_equivocation(
&client,
PRUNING_BOUND + 3,
MAX_SLOT_CAPACITY + 4,
(PRUNING_BOUND + 3).into(),
(MAX_SLOT_CAPACITY + 4).into(),
&header5,
&public,
).unwrap().is_some(),
@@ -239,8 +239,8 @@ mod test {
assert!(
check_equivocation(
&client,
PRUNING_BOUND + 4,
4,
(PRUNING_BOUND + 4).into(),
4.into(),
&header6,
&public,
).unwrap().is_none(),