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 07:30:11 -04:00
|
|
|
weights: Arc<N::Weights>,
|
2022-10-17 02:32:45 -04:00
|
|
|
pub(crate) precommitted: HashMap<
|
|
|
|
|
N::ValidatorId,
|
|
|
|
|
(<N::Block as Block>::Id, <N::SignatureScheme as SignatureScheme>::Signature),
|
|
|
|
|
>,
|
|
|
|
|
log: HashMap<
|
|
|
|
|
Round,
|
|
|
|
|
HashMap<
|
|
|
|
|
N::ValidatorId,
|
|
|
|
|
HashMap<Step, Data<N::Block, <N::SignatureScheme as SignatureScheme>::Signature>>,
|
|
|
|
|
>,
|
|
|
|
|
>,
|
2022-10-16 03:29:55 -04:00
|
|
|
}
|
|
|
|
|
|
2022-10-16 03:55:39 -04:00
|
|
|
impl<N: Network> MessageLog<N> {
|
2022-10-16 07:30:11 -04:00
|
|
|
pub(crate) fn new(weights: Arc<N::Weights>) -> MessageLog<N> {
|
|
|
|
|
MessageLog { weights, precommitted: HashMap::new(), log: HashMap::new() }
|
2022-10-16 03:29:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns true if it's a new message
|
2022-10-16 03:55:39 -04:00
|
|
|
pub(crate) fn log(
|
|
|
|
|
&mut self,
|
2022-10-17 02:32:45 -04:00
|
|
|
msg: Message<N::ValidatorId, N::Block, <N::SignatureScheme as SignatureScheme>::Signature>,
|
2022-10-16 03:55:39 -04:00
|
|
|
) -> 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
|
2022-10-17 02:32:45 -04:00
|
|
|
if let Data::Precommit(Some((hash, sig))) = &msg.data {
|
|
|
|
|
if let Some((prev, _)) = self.precommitted.get(&msg.sender) {
|
|
|
|
|
if hash != prev {
|
2022-10-16 03:29:55 -04:00
|
|
|
Err(TendermintError::Malicious(msg.sender))?;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-17 02:32:45 -04:00
|
|
|
self.precommitted.insert(msg.sender, (*hash, sig.clone()));
|
2022-10-16 03:29:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-17 02:32:45 -04:00
|
|
|
pub(crate) fn message_instances(
|
|
|
|
|
&self,
|
|
|
|
|
round: Round,
|
|
|
|
|
data: Data<N::Block, <N::SignatureScheme as SignatureScheme>::Signature>,
|
|
|
|
|
) -> (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()) {
|
2022-10-16 07:30:11 -04:00
|
|
|
let validator_weight = self.weights.weight(*participant);
|
2022-10-16 03:29:55 -04:00
|
|
|
participating += validator_weight;
|
|
|
|
|
if &data == msg {
|
|
|
|
|
weight += validator_weight;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
(participating, weight)
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-16 07:30:11 -04:00
|
|
|
// Get the participation in a given round
|
|
|
|
|
pub(crate) fn round_participation(&self, round: Round) -> u64 {
|
|
|
|
|
let mut weight = 0;
|
|
|
|
|
if let Some(round) = self.log.get(&round) {
|
|
|
|
|
for participant in round.keys() {
|
|
|
|
|
weight += self.weights.weight(*participant);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
weight
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-17 10:37:30 -04:00
|
|
|
// Check if a supermajority of nodes have participated on a specific step
|
|
|
|
|
pub(crate) fn has_participation(&self, round: Round, step: Step) -> bool {
|
|
|
|
|
let mut participating = 0;
|
|
|
|
|
for (participant, msgs) in &self.log[&round] {
|
|
|
|
|
if msgs.get(&step).is_some() {
|
|
|
|
|
participating += self.weights.weight(*participant);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
participating >= self.weights.threshold()
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-16 03:29:55 -04:00
|
|
|
// Check if consensus has been reached on a specific piece of data
|
2022-10-17 02:32:45 -04:00
|
|
|
pub(crate) fn has_consensus(
|
|
|
|
|
&self,
|
|
|
|
|
round: Round,
|
|
|
|
|
data: Data<N::Block, <N::SignatureScheme as SignatureScheme>::Signature>,
|
|
|
|
|
) -> bool {
|
2022-10-16 03:29:55 -04:00
|
|
|
let (_, weight) = self.message_instances(round, data);
|
2022-10-16 07:30:11 -04:00
|
|
|
weight >= self.weights.threshold()
|
2022-10-16 03:29:55 -04:00
|
|
|
}
|
2022-10-16 03:55:39 -04:00
|
|
|
|
|
|
|
|
pub(crate) fn get(
|
|
|
|
|
&self,
|
|
|
|
|
round: Round,
|
|
|
|
|
sender: N::ValidatorId,
|
|
|
|
|
step: Step,
|
2022-10-17 02:32:45 -04:00
|
|
|
) -> Option<&Data<N::Block, <N::SignatureScheme as SignatureScheme>::Signature>> {
|
2022-10-16 03:55:39 -04:00
|
|
|
self.log.get(&round).and_then(|round| round.get(&sender).and_then(|msgs| msgs.get(&step)))
|
|
|
|
|
}
|
2022-10-16 03:29:55 -04:00
|
|
|
}
|