mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-11 13:39:25 +00:00
Set participation upon participation instead of constantly recalculating
This commit is contained in:
@@ -1003,6 +1003,7 @@ impl<N: Network + 'static> TendermintMachine<N> {
|
|||||||
|
|
||||||
// If it's been more than 60s, rebroadcast our own messages
|
// If it's been more than 60s, rebroadcast our own messages
|
||||||
() = rebroadcast_future => {
|
() = rebroadcast_future => {
|
||||||
|
log::trace!("rebroadcast future hit within tendermint machine");
|
||||||
let key = message_tape_key(self.genesis);
|
let key = message_tape_key(self.genesis);
|
||||||
let messages = self.db.get(key).unwrap_or(vec![]);
|
let messages = self.db.get(key).unwrap_or(vec![]);
|
||||||
let mut messages = messages.as_slice();
|
let mut messages = messages.as_slice();
|
||||||
|
|||||||
@@ -7,12 +7,19 @@ use crate::{ext::*, RoundNumber, Step, DataFor, SignedMessageFor, Evidence};
|
|||||||
type RoundLog<N> = HashMap<<N as Network>::ValidatorId, HashMap<Step, SignedMessageFor<N>>>;
|
type RoundLog<N> = HashMap<<N as Network>::ValidatorId, HashMap<Step, SignedMessageFor<N>>>;
|
||||||
pub(crate) struct MessageLog<N: Network> {
|
pub(crate) struct MessageLog<N: Network> {
|
||||||
weights: Arc<N::Weights>,
|
weights: Arc<N::Weights>,
|
||||||
|
round_participation: HashMap<RoundNumber, u64>,
|
||||||
|
participation: HashMap<(RoundNumber, Step), u64>,
|
||||||
pub(crate) log: HashMap<RoundNumber, RoundLog<N>>,
|
pub(crate) log: HashMap<RoundNumber, RoundLog<N>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<N: Network> MessageLog<N> {
|
impl<N: Network> MessageLog<N> {
|
||||||
pub(crate) fn new(weights: Arc<N::Weights>) -> MessageLog<N> {
|
pub(crate) fn new(weights: Arc<N::Weights>) -> MessageLog<N> {
|
||||||
MessageLog { weights, log: HashMap::new() }
|
MessageLog {
|
||||||
|
weights,
|
||||||
|
round_participation: HashMap::new(),
|
||||||
|
participation: HashMap::new(),
|
||||||
|
log: HashMap::new(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true if it's a new message
|
// Returns true if it's a new message
|
||||||
@@ -35,54 +42,44 @@ impl<N: Network> MessageLog<N> {
|
|||||||
return Ok(false);
|
return Ok(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Since we have a new message, update the participation
|
||||||
|
let sender_weight = self.weights.weight(msg.sender);
|
||||||
|
if msgs.is_empty() {
|
||||||
|
*self.round_participation.entry(msg.round).or_insert_with(|| 0) += sender_weight;
|
||||||
|
}
|
||||||
|
*self.participation.entry((msg.round, step)).or_insert_with(|| 0) += sender_weight;
|
||||||
|
|
||||||
msgs.insert(step, signed);
|
msgs.insert(step, signed);
|
||||||
Ok(true)
|
Ok(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// For a given round, return the participating weight for this step, and the weight agreeing with
|
// For a given round, return the weight agreeing with the data
|
||||||
// the data.
|
fn message_instances(&self, round: RoundNumber, data: &DataFor<N>) -> u64 {
|
||||||
pub(crate) fn message_instances(&self, round: RoundNumber, data: &DataFor<N>) -> (u64, u64) {
|
|
||||||
let mut participating = 0;
|
|
||||||
let mut weight = 0;
|
let mut weight = 0;
|
||||||
let Some(log) = self.log.get(&round) else { return (0, 0) };
|
let Some(log) = self.log.get(&round) else { return 0 };
|
||||||
for (participant, msgs) in log {
|
for (participant, msgs) in log {
|
||||||
if let Some(msg) = msgs.get(&data.step()) {
|
if let Some(msg) = msgs.get(&data.step()) {
|
||||||
let validator_weight = self.weights.weight(*participant);
|
let validator_weight = self.weights.weight(*participant);
|
||||||
participating += validator_weight;
|
|
||||||
if data == &msg.msg.data {
|
if data == &msg.msg.data {
|
||||||
weight += validator_weight;
|
weight += validator_weight;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(participating, weight)
|
weight
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the participation in a given round
|
// Get the participation in a given round
|
||||||
pub(crate) fn round_participation(&self, round: RoundNumber) -> u64 {
|
pub(crate) fn round_participation(&self, round: RoundNumber) -> u64 {
|
||||||
let mut weight = 0;
|
*self.round_participation.get(&round).unwrap_or(&0)
|
||||||
if let Some(round) = self.log.get(&round) {
|
|
||||||
for participant in round.keys() {
|
|
||||||
weight += self.weights.weight(*participant);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
weight
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if a supermajority of nodes have participated on a specific step
|
// Check if a supermajority of nodes have participated on a specific step
|
||||||
pub(crate) fn has_participation(&self, round: RoundNumber, step: Step) -> bool {
|
pub(crate) fn has_participation(&self, round: RoundNumber, step: Step) -> bool {
|
||||||
let mut participating = 0;
|
*self.participation.get(&(round, step)).unwrap_or(&0) >= self.weights.threshold()
|
||||||
let Some(log) = self.log.get(&round) else { return false };
|
|
||||||
for (participant, msgs) in log {
|
|
||||||
if msgs.get(&step).is_some() {
|
|
||||||
participating += self.weights.weight(*participant);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
participating >= self.weights.threshold()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if consensus has been reached on a specific piece of data
|
// Check if consensus has been reached on a specific piece of data
|
||||||
pub(crate) fn has_consensus(&self, round: RoundNumber, data: &DataFor<N>) -> bool {
|
pub(crate) fn has_consensus(&self, round: RoundNumber, data: &DataFor<N>) -> bool {
|
||||||
let (_, weight) = self.message_instances(round, data);
|
self.message_instances(round, data) >= self.weights.threshold()
|
||||||
weight >= self.weights.threshold()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user