1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright 2019-2022 PureStake Inc.
// This file is part of Moonbeam.

// Moonbeam is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Moonbeam is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Moonbeam.  If not, see <http://www.gnu.org/licenses/>

use scale_info::TypeInfo;
use sp_runtime::{
	codec::{Decode, Encode},
	RuntimeDebug,
};
use sp_std::vec::Vec;

#[derive(Decode, Encode, RuntimeDebug, TypeInfo)]
pub struct CurrentOrbiter<AccountId> {
	pub account_id: AccountId,
	pub removed: bool,
}
impl<AccountId: Clone> Clone for CurrentOrbiter<AccountId> {
	fn clone(&self) -> Self {
		Self {
			account_id: self.account_id.clone(),
			removed: self.removed,
		}
	}
}

pub(super) enum RemoveOrbiterResult {
	OrbiterNotFound,
	OrbiterRemoved,
	OrbiterRemoveScheduled,
}

#[derive(Decode, Encode, RuntimeDebug, TypeInfo)]
pub(super) struct RotateOrbiterResult<AccountId> {
	pub maybe_old_orbiter: Option<CurrentOrbiter<AccountId>>,
	pub maybe_next_orbiter: Option<AccountId>,
}

#[derive(Decode, Encode, RuntimeDebug, TypeInfo)]
pub struct CollatorPoolInfo<AccountId> {
	orbiters: Vec<AccountId>,
	maybe_current_orbiter: Option<CurrentOrbiter<AccountId>>,
	next_orbiter: u32,
}

impl<AccountId> Default for CollatorPoolInfo<AccountId> {
	fn default() -> Self {
		Self {
			orbiters: Vec::new(),
			maybe_current_orbiter: None,
			next_orbiter: 0,
		}
	}
}

impl<AccountId: Clone + PartialEq> CollatorPoolInfo<AccountId> {
	pub(super) fn add_orbiter(&mut self, orbiter: AccountId) {
		if self.next_orbiter > self.orbiters.len() as u32 {
			self.next_orbiter = 0;
		}
		self.orbiters.insert(self.next_orbiter as usize, orbiter);
		self.next_orbiter += 1;
	}
	pub(super) fn contains_orbiter(&self, orbiter: &AccountId) -> bool {
		if let Some(CurrentOrbiter { ref account_id, .. }) = self.maybe_current_orbiter {
			if account_id == orbiter {
				return true;
			}
		}
		for orbiter_ in self.orbiters.iter() {
			if orbiter_ == orbiter {
				return true;
			}
		}
		false
	}
	pub(super) fn get_orbiters(&self) -> &[AccountId] {
		&self.orbiters
	}
	pub(super) fn remove_orbiter(&mut self, orbiter: &AccountId) -> RemoveOrbiterResult {
		let mut found = false;
		for (index, orbiter_) in self.orbiters.iter().enumerate() {
			if orbiter_ == orbiter {
				self.orbiters.remove(index);
				found = true;
				break;
			}
		}

		if found {
			if let Some(CurrentOrbiter {
				account_id: ref current_orbiter,
				ref mut removed,
			}) = self.maybe_current_orbiter
			{
				if current_orbiter == orbiter {
					*removed = true;
					return RemoveOrbiterResult::OrbiterRemoveScheduled;
				}
			}
			RemoveOrbiterResult::OrbiterRemoved
		} else {
			RemoveOrbiterResult::OrbiterNotFound
		}
	}
	pub(super) fn rotate_orbiter(&mut self) -> RotateOrbiterResult<AccountId> {
		let maybe_old_orbiter = self.maybe_current_orbiter.clone();
		if self.next_orbiter >= self.orbiters.len() as u32 {
			self.next_orbiter = 0;
		}
		let maybe_next_orbiter =
			if let Some(next_orbiter) = self.orbiters.get(self.next_orbiter as usize) {
				self.maybe_current_orbiter = Some(CurrentOrbiter {
					account_id: next_orbiter.clone(),
					removed: false,
				});
				self.next_orbiter += 1;
				Some(next_orbiter.clone())
			} else {
				None
			};

		RotateOrbiterResult {
			maybe_old_orbiter,
			maybe_next_orbiter,
		}
	}
}

#[derive(Decode, Encode, RuntimeDebug, TypeInfo)]
pub struct RoundAuthors<AccountId> {
	data: Vec<(AccountId, u32)>,
	blocks_count: u32,
}

impl<AccountId> Default for RoundAuthors<AccountId> {
	fn default() -> Self {
		Self {
			data: Vec::new(),
			blocks_count: 0,
		}
	}
}

impl<AccountId: Ord> RoundAuthors<AccountId> {
	pub fn add_author(&mut self, author: AccountId) {
		match self
			.data
			.binary_search_by(|(account, _counter)| account.cmp(&author))
		{
			Ok(index) => self.data[index].1 = self.data[index].1.saturating_add(1),
			Err(index) => self.data.insert(index, (author, 1)),
		};
		self.blocks_count += 1;
	}
	pub fn into_data(self) -> (Vec<(AccountId, u32)>, u32) {
		let Self { data, blocks_count } = self;
		(data, blocks_count)
	}
}