referrerpolicy=no-referrer-when-downgrade

sc_consensus_babe/
migration.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19use crate::{
20	AuthorityId, BabeAuthorityWeight, BabeConfiguration, BabeEpochConfiguration, Epoch,
21	NextEpochDescriptor, Randomness,
22};
23use codec::{Decode, Encode};
24use sc_consensus_epochs::Epoch as EpochT;
25use sp_consensus_slots::Slot;
26
27/// BABE epoch information, version 0.
28#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)]
29pub struct EpochV0 {
30	/// The epoch index.
31	pub epoch_index: u64,
32	/// The starting slot of the epoch.
33	pub start_slot: Slot,
34	/// The duration of this epoch.
35	pub duration: u64,
36	/// The authorities and their weights.
37	pub authorities: Vec<(AuthorityId, BabeAuthorityWeight)>,
38	/// Randomness for this epoch.
39	pub randomness: Randomness,
40}
41
42impl EpochT for EpochV0 {
43	type NextEpochDescriptor = NextEpochDescriptor;
44	type Slot = Slot;
45
46	fn increment(&self, descriptor: NextEpochDescriptor) -> EpochV0 {
47		EpochV0 {
48			epoch_index: self.epoch_index + 1,
49			start_slot: self.start_slot + self.duration,
50			duration: self.duration,
51			authorities: descriptor.authorities,
52			randomness: descriptor.randomness,
53		}
54	}
55
56	fn start_slot(&self) -> Slot {
57		self.start_slot
58	}
59
60	fn end_slot(&self) -> Slot {
61		self.start_slot + self.duration
62	}
63}
64
65// Implement From<EpochV0> for Epoch
66impl EpochV0 {
67	/// Migrate the struct to current epoch version.
68	pub fn migrate(self, config: &BabeConfiguration) -> Epoch {
69		sp_consensus_babe::Epoch {
70			epoch_index: self.epoch_index,
71			start_slot: self.start_slot,
72			duration: self.duration,
73			authorities: self.authorities,
74			randomness: self.randomness,
75			config: BabeEpochConfiguration { c: config.c, allowed_slots: config.allowed_slots },
76		}
77		.into()
78	}
79}