|
| 1 | +// Copyright 2019 Kodebox, Inc. |
| 2 | +// This file is part of CodeChain. |
| 3 | +// |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as |
| 6 | +// published by the Free Software Foundation, either version 3 of the |
| 7 | +// License, or (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +use std::cmp::Ordering; |
| 18 | +use std::collections::{BTreeMap, BinaryHeap}; |
| 19 | + |
| 20 | +use rug::{integer::Order, Integer}; |
| 21 | + |
| 22 | +use super::{PriorityMessage, SortitionRound}; |
| 23 | +use crate::consensus::Priority; |
| 24 | + |
| 25 | +/// Storing Priorities |
| 26 | +#[derive(Default)] |
| 27 | +pub struct PriorityCollector { |
| 28 | + priorities: BTreeMap<SortitionRound, BinaryHeap<PriorityInfoSummary>>, |
| 29 | +} |
| 30 | + |
| 31 | +#[derive(Eq, PartialEq)] |
| 32 | +pub struct PriorityInfoSummary { |
| 33 | + priority: Priority, |
| 34 | + signer_idx: usize, |
| 35 | +} |
| 36 | + |
| 37 | +impl Ord for PriorityInfoSummary { |
| 38 | + fn cmp(&self, other: &Self) -> Ordering { |
| 39 | + let self_as_int = Integer::from_digits(&self.priority, Order::MsfBe); |
| 40 | + let other_as_int = Integer::from_digits(&other.priority, Order::MsfBe); |
| 41 | + |
| 42 | + self_as_int.cmp(&other_as_int) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl PartialOrd for PriorityInfoSummary { |
| 47 | + fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 48 | + Some(self.cmp(other)) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +impl PriorityInfoSummary { |
| 53 | + pub fn from_message_and_idx(message: &PriorityMessage, signer_idx: usize) -> Self { |
| 54 | + Self { |
| 55 | + priority: message.info.priority(), |
| 56 | + signer_idx, |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +impl PriorityCollector { |
| 62 | + pub fn insert(&mut self, message: &PriorityMessage, signer_idx: usize, round: SortitionRound) { |
| 63 | + let info_summary = PriorityInfoSummary::from_message_and_idx(message, signer_idx); |
| 64 | + self.priorities.entry(round).or_insert_with(Default::default).push(info_summary); |
| 65 | + } |
| 66 | + |
| 67 | + pub fn get_highest_priority(&mut self, round: SortitionRound) -> Option<PriorityInfoSummary> { |
| 68 | + self.priorities.entry(round).or_default().pop() |
| 69 | + } |
| 70 | + |
| 71 | + /// Throw away priorities older than the given round. |
| 72 | + pub fn throw_away_old(&mut self, round: &SortitionRound) { |
| 73 | + let new_collector = self.priorities.split_off(round); |
| 74 | + assert!(!new_collector.is_empty()); |
| 75 | + self.priorities = new_collector; |
| 76 | + } |
| 77 | +} |
0 commit comments