use crate::{Epoch, EpochChanges, PersistedEpoch, PersistedEpochHeader};
use codec::{Decode, Encode};
use fork_tree::ForkTree;
use sp_runtime::traits::{Block as BlockT, NumberFor};
use std::collections::BTreeMap;
#[derive(Clone, Encode, Decode)]
pub struct EpochChangesV0<Hash, Number, E: Epoch> {
inner: ForkTree<Hash, Number, PersistedEpoch<E>>,
}
#[derive(Clone, Encode, Decode)]
pub struct EpochChangesV1<Hash, Number, E: Epoch> {
inner: ForkTree<Hash, Number, PersistedEpochHeader<E>>,
epochs: BTreeMap<(Hash, Number), PersistedEpoch<E>>,
}
pub type EpochChangesV0For<Block, Epoch> =
EpochChangesV0<<Block as BlockT>::Hash, NumberFor<Block>, Epoch>;
pub type EpochChangesV1For<Block, Epoch> =
EpochChangesV1<<Block as BlockT>::Hash, NumberFor<Block>, Epoch>;
impl<Hash, Number, E: Epoch> EpochChangesV0<Hash, Number, E>
where
Hash: PartialEq + Ord + Copy,
Number: Ord + Copy,
{
pub fn from_raw(inner: ForkTree<Hash, Number, PersistedEpoch<E>>) -> Self {
Self { inner }
}
pub fn migrate(self) -> EpochChanges<Hash, Number, E> {
let mut epochs = BTreeMap::new();
let inner = self.inner.map(&mut |hash, number, data| {
let header = PersistedEpochHeader::from(&data);
epochs.insert((*hash, *number), data);
header
});
EpochChanges { inner, epochs }
}
}
impl<Hash, Number, E: Epoch> EpochChangesV1<Hash, Number, E>
where
Hash: PartialEq + Ord + Copy,
Number: Ord + Copy,
{
pub fn migrate(self) -> EpochChanges<Hash, Number, E> {
EpochChanges { inner: self.inner, epochs: self.epochs }
}
}