tp_consensus_slots/
lib.rs

1// This file is part of Tetcore.
2
3// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Primitives for slots-based consensus engines.
19
20#![cfg_attr(not(feature = "std"), no_std)]
21
22use codec::{Decode, Encode};
23
24/// Unit type wrapper that represents a slot.
25#[derive(Debug, Encode, Decode, Eq, Clone, Copy, Default, Ord)]
26pub struct Slot(u64);
27
28impl core::ops::Deref for Slot {
29	type Target = u64;
30
31	fn deref(&self) -> &u64 {
32		&self.0
33	}
34}
35
36impl core::ops::Add for Slot {
37	type Output = Self;
38
39	fn add(self, other: Self) -> Self {
40		Self(self.0 + other.0)
41	}
42}
43
44impl core::ops::Add<u64> for Slot {
45	type Output = Self;
46
47	fn add(self, other: u64) -> Self {
48		Self(self.0 + other)
49	}
50}
51
52impl<T: Into<u64> + Copy> core::cmp::PartialEq<T> for Slot {
53	fn eq(&self, eq: &T) -> bool {
54		self.0 == (*eq).into()
55	}
56}
57
58impl<T: Into<u64> + Copy> core::cmp::PartialOrd<T> for Slot {
59	fn partial_cmp(&self, other: &T) -> Option<core::cmp::Ordering> {
60		self.0.partial_cmp(&(*other).into())
61	}
62}
63
64impl Slot {
65	/// Saturating addition.
66	pub fn saturating_add<T: Into<u64>>(self, rhs: T) -> Self {
67		Self(self.0.saturating_add(rhs.into()))
68	}
69
70	/// Saturating subtraction.
71	pub fn saturating_sub<T: Into<u64>>(self, rhs: T) -> Self {
72		Self(self.0.saturating_sub(rhs.into()))
73	}
74}
75
76#[cfg(feature = "std")]
77impl std::fmt::Display for Slot {
78	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
79		write!(f, "{}", self.0)
80	}
81}
82
83impl From<u64> for Slot {
84	fn from(slot: u64) -> Slot {
85		Slot(slot)
86	}
87}
88
89impl From<Slot> for u64 {
90	fn from(slot: Slot) -> u64 {
91		slot.0
92	}
93}
94
95/// Represents an equivocation proof. An equivocation happens when a validator
96/// produces more than one block on the same slot. The proof of equivocation
97/// are the given distinct headers that were signed by the validator and which
98/// include the slot number.
99#[derive(Clone, Debug, Decode, Encode, PartialEq)]
100pub struct EquivocationProof<Header, Id> {
101	/// Returns the authority id of the equivocator.
102	pub offender: Id,
103	/// The slot at which the equivocation happened.
104	pub slot: Slot,
105	/// The first header involved in the equivocation.
106	pub first_header: Header,
107	/// The second header involved in the equivocation.
108	pub second_header: Header,
109}