cfxcore/pos/consensus/liveness/
round_proposer_election.rs

1// Copyright (c) The Diem Core Contributors
2// SPDX-License-Identifier: Apache-2.0
3
4// Copyright 2021 Conflux Foundation. All rights reserved.
5// Conflux is free software and distributed under GNU General Public License.
6// See http://www.gnu.org/licenses/
7
8use crate::pos::consensus::liveness::proposer_election::ProposerElection;
9use consensus_types::common::{Author, Round};
10
11use std::collections::HashMap;
12
13/// The round proposer maps a round to author
14pub struct RoundProposer {
15    // A pre-defined map specifying proposers per round
16    proposers: HashMap<Round, Author>,
17    // Default proposer to use if proposer for a round is unspecified.
18    // We hardcode this to the first proposer
19    default_proposer: Author,
20}
21
22impl RoundProposer {
23    pub fn new(
24        proposers: HashMap<Round, Author>, default_proposer: Author,
25    ) -> Self {
26        Self {
27            proposers,
28            default_proposer,
29        }
30    }
31}
32
33impl ProposerElection for RoundProposer {
34    fn get_valid_proposer(&self, round: Round) -> Author {
35        match self.proposers.get(&round) {
36            None => self.default_proposer,
37            Some(round_proposer) => *round_proposer,
38        }
39    }
40}