use parity_scale_codec::{Decode, Encode};
use serde::{Deserialize, Serialize};
use crate::phron_primitives::BlockNumber;
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct SessionBoundaries {
first_block: BlockNumber,
last_block: BlockNumber,
}
impl SessionBoundaries {
pub fn first_block(&self) -> BlockNumber {
self.first_block
}
pub fn last_block(&self) -> BlockNumber {
self.last_block
}
}
#[derive(Clone, Debug)]
pub struct SessionBoundaryInfo {
session_period: SessionPeriod,
}
impl SessionBoundaryInfo {
pub const fn new(session_period: SessionPeriod) -> Self {
Self { session_period }
}
pub fn boundaries_for_session(&self, session_id: SessionId) -> SessionBoundaries {
SessionBoundaries {
first_block: self.first_block_of_session(session_id),
last_block: self.last_block_of_session(session_id),
}
}
pub fn session_id_from_block_num(&self, n: BlockNumber) -> SessionId {
SessionId(n / self.session_period.0)
}
pub fn last_block_of_session(&self, session_id: SessionId) -> BlockNumber {
(session_id.0 + 1) * self.session_period.0 - 1
}
pub fn first_block_of_session(&self, session_id: SessionId) -> BlockNumber {
session_id.0 * self.session_period.0
}
}
#[cfg(test)]
pub mod testing {
use sp_runtime::testing::UintAuthorityId;
use crate::phron_primitives::SessionAuthorityData;
pub fn authority_data(from: u32, to: u32) -> SessionAuthorityData {
SessionAuthorityData::new(
(from..to)
.map(|id| UintAuthorityId(id.into()).to_public_key())
.collect(),
None,
)
}
}
#[derive(
Copy, Clone, Debug, Default, Eq, PartialEq, Hash, Ord, PartialOrd,
Encode, Decode, Serialize, Deserialize
)]
pub struct SessionId(pub u32);
impl SessionId {
pub fn next(&self) -> Self {
SessionId(self.0 + 1)
}
}
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Hash, Ord, PartialOrd, Encode, Decode)]
pub struct SessionPeriod(pub u32);