referrerpolicy=no-referrer-when-downgrade

sc_consensus_epochs/
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
19//! Migration types for epoch changes.
20
21use crate::{Epoch, EpochChanges, PersistedEpoch, PersistedEpochHeader};
22use codec::{Decode, Encode};
23use fork_tree::ForkTree;
24use sp_runtime::traits::{Block as BlockT, NumberFor};
25use std::collections::BTreeMap;
26
27/// Legacy definition of epoch changes.
28#[derive(Clone, Encode, Decode)]
29pub struct EpochChangesV0<Hash, Number, E: Epoch> {
30	inner: ForkTree<Hash, Number, PersistedEpoch<E>>,
31}
32
33/// Legacy definition of epoch changes.
34#[derive(Clone, Encode, Decode)]
35pub struct EpochChangesV1<Hash, Number, E: Epoch> {
36	inner: ForkTree<Hash, Number, PersistedEpochHeader<E>>,
37	epochs: BTreeMap<(Hash, Number), PersistedEpoch<E>>,
38}
39
40/// Type alias for v0 definition of epoch changes.
41pub type EpochChangesV0For<Block, Epoch> =
42	EpochChangesV0<<Block as BlockT>::Hash, NumberFor<Block>, Epoch>;
43/// Type alias for v1 and v2 definition of epoch changes.
44pub type EpochChangesV1For<Block, Epoch> =
45	EpochChangesV1<<Block as BlockT>::Hash, NumberFor<Block>, Epoch>;
46
47impl<Hash, Number, E: Epoch> EpochChangesV0<Hash, Number, E>
48where
49	Hash: PartialEq + Ord + Copy,
50	Number: Ord + Copy,
51{
52	/// Create a new value of this type from raw.
53	pub fn from_raw(inner: ForkTree<Hash, Number, PersistedEpoch<E>>) -> Self {
54		Self { inner }
55	}
56
57	/// Migrate the type into current epoch changes definition.
58	pub fn migrate(self) -> EpochChanges<Hash, Number, E> {
59		let mut epochs = BTreeMap::new();
60
61		let inner = self.inner.map(&mut |hash, number, data| {
62			let header = PersistedEpochHeader::from(&data);
63			epochs.insert((*hash, *number), data);
64			header
65		});
66
67		EpochChanges { inner, epochs }
68	}
69}
70
71impl<Hash, Number, E: Epoch> EpochChangesV1<Hash, Number, E>
72where
73	Hash: PartialEq + Ord + Copy,
74	Number: Ord + Copy,
75{
76	/// Migrate the type into current epoch changes definition.
77	pub fn migrate(self) -> EpochChanges<Hash, Number, E> {
78		EpochChanges { inner: self.inner, epochs: self.epochs }
79	}
80}