Skip to content

Commit 33f2b06

Browse files
committed
Implement PriorityCollector
1 parent be94fc0 commit 33f2b06

File tree

4 files changed

+100
-1
lines changed

4 files changed

+100
-1
lines changed

core/src/consensus/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub use self::cuckoo::Cuckoo;
3131
pub use self::null_engine::NullEngine;
3232
pub use self::simple_poa::SimplePoA;
3333
pub use self::solo::Solo;
34+
pub use self::sortition::vrf_sortition::{Priority, PriorityInfo, VRFSortition};
3435
pub use self::tendermint::{
3536
ConsensusMessage, Height, Step, Tendermint, TendermintParams, TimeGapParams, View, VoteOn, VoteStep,
3637
};

core/src/consensus/tendermint/message.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ pub use super::super::sortition::PriorityMessage;
2727
use super::super::BitSet;
2828
use super::{Height, Step, View};
2929

30+
/// Step for the sortition round.
31+
/// FIXME: It has a large overlap with the previous VoteStep.
32+
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, RlpDecodable, RlpEncodable)]
33+
pub struct SortitionRound {
34+
pub height: Height,
35+
pub view: View,
36+
}
37+
3038
/// Complete step of the consensus process.
3139
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, RlpDecodable, RlpEncodable)]
3240
pub struct VoteStep {
@@ -73,6 +81,18 @@ impl Ord for VoteStep {
7381
}
7482
}
7583

84+
impl PartialOrd for SortitionRound {
85+
fn partial_cmp(&self, other: &SortitionRound) -> Option<cmp::Ordering> {
86+
Some(self.cmp(other))
87+
}
88+
}
89+
90+
impl Ord for SortitionRound {
91+
fn cmp(&self, other: &SortitionRound) -> cmp::Ordering {
92+
(self.height, self.view).cmp(&(other.height, other.view))
93+
}
94+
}
95+
7696
const MESSAGE_ID_CONSENSUS_MESSAGE: u8 = 0x01;
7797
const MESSAGE_ID_PROPOSAL_BLOCK: u8 = 0x02;
7898
const MESSAGE_ID_STEP_STATE: u8 = 0x03;

core/src/consensus/tendermint/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ mod engine;
2020
mod message;
2121
mod network;
2222
mod params;
23+
pub mod priority_collector;
2324
pub mod types;
2425
pub mod vote_collector;
2526
mod vote_regression_checker;
@@ -35,7 +36,7 @@ use ctimer::TimerToken;
3536
use parking_lot::RwLock;
3637

3738
use self::chain_notify::TendermintChainNotify;
38-
pub use self::message::{ConsensusMessage, VoteOn, VoteStep};
39+
pub use self::message::{ConsensusMessage, PriorityMessage, SortitionRound, VoteOn, VoteStep};
3940
pub use self::params::{TendermintParams, TimeGapParams, TimeoutParams};
4041
pub use self::types::{Height, Step, View};
4142
use super::{stake, ValidatorSet};
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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

Comments
 (0)