2022-10-16 03:29:55 -04:00
|
|
|
use std::{sync::Arc, collections::HashMap};
|
|
|
|
|
|
|
|
|
|
use crate::{ext::*, Round, Step, Data, Message, TendermintError};
|
|
|
|
|
|
2022-10-16 03:55:39 -04:00
|
|
|
pub(crate) struct MessageLog<N: Network> {
|
2022-10-16 03:29:55 -04:00
|
|
|
network: Arc<N>,
|
2022-10-16 03:55:39 -04:00
|
|
|
precommitted: HashMap<N::ValidatorId, <N::Block as Block>::Id>,
|
|
|
|
|
log: HashMap<Round, HashMap<N::ValidatorId, HashMap<Step, Data<N::Block>>>>,
|
2022-10-16 03:29:55 -04:00
|
|
|
}
|
|
|
|
|
|
2022-10-16 03:55:39 -04:00
|
|
|
impl<N: Network> MessageLog<N> {
|
|
|
|
|
pub(crate) fn new(network: Arc<N>) -> MessageLog<N> {
|
2022-10-16 03:29:55 -04:00
|
|
|
MessageLog { network, precommitted: HashMap::new(), log: HashMap::new() }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns true if it's a new message
|
2022-10-16 03:55:39 -04:00
|
|
|
pub(crate) fn log(
|
|
|
|
|
&mut self,
|
|
|
|
|
msg: Message<N::ValidatorId, N::Block>,
|
|
|
|
|
) -> Result<bool, TendermintError<N::ValidatorId>> {
|
2022-10-16 03:29:55 -04:00
|
|
|
let round = self.log.entry(msg.round).or_insert_with(HashMap::new);
|
|
|
|
|
let msgs = round.entry(msg.sender).or_insert_with(HashMap::new);
|
|
|
|
|
|
|
|
|
|
// Handle message replays without issue. It's only multiple messages which is malicious
|
|
|
|
|
let step = msg.data.step();
|
|
|
|
|
if let Some(existing) = msgs.get(&step) {
|
|
|
|
|
if existing != &msg.data {
|
|
|
|
|
Err(TendermintError::Malicious(msg.sender))?;
|
|
|
|
|
}
|
|
|
|
|
return Ok(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If they already precommitted to a distinct hash, error
|
|
|
|
|
if let Data::Precommit(Some(hash)) = msg.data {
|
|
|
|
|
if let Some(prev) = self.precommitted.get(&msg.sender) {
|
|
|
|
|
if hash != *prev {
|
|
|
|
|
Err(TendermintError::Malicious(msg.sender))?;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
self.precommitted.insert(msg.sender, hash);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
msgs.insert(step, msg.data);
|
|
|
|
|
Ok(true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// For a given round, return the participating weight for this step, and the weight agreeing with
|
|
|
|
|
// the data.
|
2022-10-16 03:55:39 -04:00
|
|
|
pub(crate) fn message_instances(&self, round: Round, data: Data<N::Block>) -> (u64, u64) {
|
2022-10-16 03:29:55 -04:00
|
|
|
let mut participating = 0;
|
|
|
|
|
let mut weight = 0;
|
|
|
|
|
for (participant, msgs) in &self.log[&round] {
|
|
|
|
|
if let Some(msg) = msgs.get(&data.step()) {
|
|
|
|
|
let validator_weight = self.network.weight(*participant);
|
|
|
|
|
participating += validator_weight;
|
|
|
|
|
if &data == msg {
|
|
|
|
|
weight += validator_weight;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
(participating, weight)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the participation in a given round for a given step.
|
|
|
|
|
pub(crate) fn participation(&self, round: Round, step: Step) -> u64 {
|
|
|
|
|
let (participating, _) = self.message_instances(
|
|
|
|
|
round,
|
|
|
|
|
match step {
|
|
|
|
|
Step::Propose => panic!("Checking for participation on Propose"),
|
|
|
|
|
Step::Prevote => Data::Prevote(None),
|
|
|
|
|
Step::Precommit => Data::Precommit(None),
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
participating
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if there's been a BFT level of participation
|
|
|
|
|
pub(crate) fn has_participation(&self, round: Round, step: Step) -> bool {
|
|
|
|
|
self.participation(round, step) >= self.network.threshold()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if consensus has been reached on a specific piece of data
|
2022-10-16 03:55:39 -04:00
|
|
|
pub(crate) fn has_consensus(&self, round: Round, data: Data<N::Block>) -> bool {
|
2022-10-16 03:29:55 -04:00
|
|
|
let (_, weight) = self.message_instances(round, data);
|
|
|
|
|
weight >= self.network.threshold()
|
|
|
|
|
}
|
2022-10-16 03:55:39 -04:00
|
|
|
|
|
|
|
|
pub(crate) fn get(
|
|
|
|
|
&self,
|
|
|
|
|
round: Round,
|
|
|
|
|
sender: N::ValidatorId,
|
|
|
|
|
step: Step,
|
|
|
|
|
) -> Option<&Data<N::Block>> {
|
|
|
|
|
self.log.get(&round).and_then(|round| round.get(&sender).and_then(|msgs| msgs.get(&step)))
|
|
|
|
|
}
|
2022-10-16 03:29:55 -04:00
|
|
|
}
|