#![allow(clippy::result_unit_err)]
use super::*;
use frame_support::{pallet_prelude::*, DefaultNoBound};
pub type RevisionIndex = u32;
pub type PageIndex = u32;
pub type KeyCount = u64;
pub type MemberOf<T> = <<T as Config>::Crypto as GenerateVerifiable>::Member;
pub type MembersOf<T> = <<T as Config>::Crypto as GenerateVerifiable>::Members;
pub type IntermediateOf<T> = <<T as Config>::Crypto as GenerateVerifiable>::Intermediate;
pub type SecretOf<T> = <<T as Config>::Crypto as GenerateVerifiable>::Secret;
pub type SignatureOf<T> = <<T as Config>::Crypto as GenerateVerifiable>::Signature;
pub type ChunksOf<T> = BoundedVec<
<<T as Config>::Crypto as GenerateVerifiable>::StaticChunk,
<T as Config>::ChunkPageSize,
>;
#[derive(
Clone,
PartialEq,
Eq,
Debug,
Default,
Encode,
Decode,
MaxEncodedLen,
TypeInfo,
DecodeWithMemTracking,
)]
pub enum RingMembersState {
#[default]
AppendOnly,
Mutating(u8),
KeyMigration,
}
impl RingMembersState {
pub fn append_only(&self) -> bool {
matches!(self, Self::AppendOnly)
}
pub fn mutating(&self) -> bool {
matches!(self, Self::Mutating(_))
}
pub fn key_migration(&self) -> bool {
matches!(self, Self::KeyMigration)
}
pub fn start_mutation_session(self) -> Result<Self, ()> {
match self {
Self::AppendOnly => Ok(Self::Mutating(1)),
Self::Mutating(n) => Ok(Self::Mutating(n.checked_add(1).ok_or(())?)),
Self::KeyMigration => Err(()),
}
}
pub fn end_mutation_session(self) -> Result<Self, ()> {
match self {
Self::AppendOnly => Err(()),
Self::Mutating(1) => Ok(Self::KeyMigration),
Self::Mutating(n) => Ok(Self::Mutating(n.saturating_sub(1))),
Self::KeyMigration => Err(()),
}
}
pub fn end_key_migration(self) -> Result<Self, ()> {
match self {
Self::KeyMigration => Ok(Self::AppendOnly),
_ => Err(()),
}
}
}
#[derive(
Clone, PartialEq, Eq, Debug, Encode, Decode, MaxEncodedLen, TypeInfo, DecodeWithMemTracking,
)]
pub struct RevisedContextualAlias {
pub revision: RevisionIndex,
pub ring: RingIndex,
pub ca: ContextualAlias,
}
#[derive(Clone, PartialEq, Eq, Debug, Encode, Decode, MaxEncodedLen, TypeInfo)]
pub struct RevisedAlias {
pub revision: RevisionIndex,
pub ring: RingIndex,
pub alias: Alias,
}
#[derive(PartialEq, Eq, Clone, Encode, Decode, Debug, TypeInfo, MaxEncodedLen)]
#[scale_info(skip_type_params(T))]
pub struct RingRoot<T: Config> {
pub root: MembersOf<T>,
pub revision: RevisionIndex,
pub intermediate: IntermediateOf<T>,
}
#[derive(PartialEq, Eq, Clone, Encode, Decode, Debug, TypeInfo, MaxEncodedLen, DefaultNoBound)]
#[scale_info(skip_type_params(T))]
pub struct RingStatus {
pub total: u32,
pub included: u32,
}
#[derive(PartialEq, Eq, Clone, Encode, Decode, Debug, TypeInfo, MaxEncodedLen)]
pub enum RingPosition {
Onboarding { queue_page: PageIndex },
Included { ring_index: RingIndex, ring_position: u32, scheduled_for_removal: bool },
Suspended,
}
impl RingPosition {
pub fn suspended(&self) -> bool {
matches!(self, Self::Suspended)
}
pub fn scheduled_for_removal(&self) -> bool {
match &self {
Self::Included { scheduled_for_removal, .. } => *scheduled_for_removal,
_ => false,
}
}
pub fn ring_index(&self) -> Option<RingIndex> {
match &self {
Self::Included { ring_index, .. } => Some(*ring_index),
_ => None,
}
}
}
#[derive(PartialEq, Eq, Clone, Encode, Decode, Debug, TypeInfo, MaxEncodedLen)]
pub struct PersonRecord<Member, AccountId> {
pub key: Member,
pub position: RingPosition,
pub account: Option<AccountId>,
}
#[derive(PartialEq, Eq, Clone, Encode, Decode, Debug, TypeInfo, MaxEncodedLen)]
#[scale_info(skip_type_params(T))]
pub(crate) enum QueueMergeAction<T: Config> {
Merge {
initial_head: PageIndex,
new_head: PageIndex,
first_key_page: BoundedVec<MemberOf<T>, T::OnboardingQueuePageSize>,
second_key_page: BoundedVec<MemberOf<T>, T::OnboardingQueuePageSize>,
},
NoAction,
}