diff --git a/coordinator/tributary/tendermint/src/lib.rs b/coordinator/tributary/tendermint/src/lib.rs index 777b2f00..eeb46845 100644 --- a/coordinator/tributary/tendermint/src/lib.rs +++ b/coordinator/tributary/tendermint/src/lib.rs @@ -69,6 +69,17 @@ impl PartialEq for Data { } } +impl core::hash::Hash for Data { + fn hash(&self, state: &mut H) { + match self { + Data::Proposal(valid_round, block) => (0, valid_round, block.id().as_ref()).hash(state), + Data::Prevote(id) => (1, id.as_ref().map(AsRef::<[u8]>::as_ref)).hash(state), + Data::Precommit(None) => (2, 0).hash(state), + Data::Precommit(Some((id, _))) => (2, 1, id.as_ref()).hash(state), + } + } +} + impl Data { pub fn step(&self) -> Step { match self { diff --git a/coordinator/tributary/tendermint/src/message_log.rs b/coordinator/tributary/tendermint/src/message_log.rs index f39a45ca..c6d172c4 100644 --- a/coordinator/tributary/tendermint/src/message_log.rs +++ b/coordinator/tributary/tendermint/src/message_log.rs @@ -9,6 +9,7 @@ pub(crate) struct MessageLog { weights: Arc, round_participation: HashMap, participation: HashMap<(RoundNumber, Step), u64>, + message_instances: HashMap<(RoundNumber, DataFor), u64>, pub(crate) log: HashMap>, } @@ -18,6 +19,7 @@ impl MessageLog { weights, round_participation: HashMap::new(), participation: HashMap::new(), + message_instances: HashMap::new(), log: HashMap::new(), } } @@ -48,26 +50,13 @@ impl MessageLog { *self.round_participation.entry(msg.round).or_insert_with(|| 0) += sender_weight; } *self.participation.entry((msg.round, step)).or_insert_with(|| 0) += sender_weight; + *self.message_instances.entry((msg.round, msg.data.clone())).or_insert_with(|| 0) += + sender_weight; msgs.insert(step, signed); Ok(true) } - // For a given round, return the weight agreeing with the data - fn message_instances(&self, round: RoundNumber, data: &DataFor) -> u64 { - let mut weight = 0; - let Some(log) = self.log.get(&round) else { return 0 }; - for (participant, msgs) in log { - if let Some(msg) = msgs.get(&data.step()) { - let validator_weight = self.weights.weight(*participant); - if data == &msg.msg.data { - weight += validator_weight; - } - } - } - weight - } - // Get the participation in a given round pub(crate) fn round_participation(&self, round: RoundNumber) -> u64 { *self.round_participation.get(&round).unwrap_or(&0) @@ -80,6 +69,6 @@ impl MessageLog { // Check if consensus has been reached on a specific piece of data pub(crate) fn has_consensus(&self, round: RoundNumber, data: &DataFor) -> bool { - self.message_instances(round, data) >= self.weights.threshold() + *self.message_instances.get(&(round, data.clone())).unwrap_or(&0) >= self.weights.threshold() } }