mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-10 05:09:22 +00:00
* add slash tx * ignore unsigned tx replays * verify that provided evidence is valid * fix clippy + fmt * move application tx handling to another module * partially handle the tendermint txs * fix pr comments * support unsigned app txs * add slash target to the votes * enforce provided, unsigned, signed tx ordering within a block * bug fixes * add unit test for tendermint txs * bug fixes * update tests for tendermint txs * add tx ordering test * tidy up tx ordering test * cargo +nightly fmt * Misc fixes from rebasing * Finish resolving clippy * Remove sha3 from tendermint-machine * Resolve a DoS in SlashEvidence's read Also moves Evidence from Vec<Message> to (Message, Option<Message>). That should meet all requirements while being a bit safer. * Make lazy_static a dev-depend for tributary * Various small tweaks One use of sort was inefficient, sorting unsigned || signed when unsigned was already properly sorted. Given how the unsigned TXs were given a nonce of 0, an unstable sort may swap places with an unsigned TX and a signed TX with a nonce of 0 (leading to a faulty block). The extra protection added here sorts signed, then concats. * Fix Tributary tests I broke, start review on tendermint/tx.rs * Finish reviewing everything outside tests and empty_signature * Remove empty_signature empty_signature led to corrupted local state histories. Unfortunately, the API is only sane with a signature. We now use the actual signature, which risks creating a signature over a malicious message if we have ever have an invariant producing malicious messages. Prior, we only signed the message after the local machine confirmed it was okay per the local view of consensus. This is tolerated/preferred over a corrupt state history since production of such messages is already an invariant. TODOs are added to make handling of this theoretical invariant further robust. * Remove async_sequential for tokio::test There was no competition for resources forcing them to be run sequentially. * Modify block order test to be statistically significant without multiple runs * Clean tests --------- Co-authored-by: Luke Parker <lukeparker5132@gmail.com>
111 lines
3.8 KiB
Rust
111 lines
3.8 KiB
Rust
use std::{sync::Arc, collections::HashMap};
|
|
|
|
use log::debug;
|
|
|
|
use crate::{ext::*, RoundNumber, Step, Data, DataFor, TendermintError, SignedMessageFor};
|
|
|
|
type RoundLog<N> = HashMap<<N as Network>::ValidatorId, HashMap<Step, SignedMessageFor<N>>>;
|
|
pub(crate) struct MessageLog<N: Network> {
|
|
weights: Arc<N::Weights>,
|
|
precommitted: HashMap<N::ValidatorId, SignedMessageFor<N>>,
|
|
pub(crate) log: HashMap<RoundNumber, RoundLog<N>>,
|
|
}
|
|
|
|
impl<N: Network> MessageLog<N> {
|
|
pub(crate) fn new(weights: Arc<N::Weights>) -> MessageLog<N> {
|
|
MessageLog { weights, precommitted: HashMap::new(), log: HashMap::new() }
|
|
}
|
|
|
|
// Returns true if it's a new message
|
|
pub(crate) fn log(&mut self, signed: SignedMessageFor<N>) -> Result<bool, TendermintError<N>> {
|
|
let msg = &signed.msg;
|
|
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 != msg.data {
|
|
debug!(
|
|
target: "tendermint",
|
|
"Validator sent multiple messages for the same block + round + step"
|
|
);
|
|
Err(TendermintError::Malicious(msg.sender, Some(existing.clone())))?;
|
|
}
|
|
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 let Data::Precommit(Some((prev_hash, _))) = prev.msg.data {
|
|
if hash != prev_hash {
|
|
debug!(target: "tendermint", "Validator precommitted to multiple blocks");
|
|
Err(TendermintError::Malicious(msg.sender, Some(prev.clone())))?;
|
|
}
|
|
} else {
|
|
panic!("message in precommitted wasn't Precommit");
|
|
}
|
|
}
|
|
self.precommitted.insert(msg.sender, signed.clone());
|
|
}
|
|
|
|
msgs.insert(step, signed);
|
|
Ok(true)
|
|
}
|
|
|
|
// For a given round, return the participating weight for this step, and the weight agreeing with
|
|
// the data.
|
|
pub(crate) fn message_instances(&self, round: RoundNumber, data: DataFor<N>) -> (u64, u64) {
|
|
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.weights.weight(*participant);
|
|
participating += validator_weight;
|
|
if data == msg.msg.data {
|
|
weight += validator_weight;
|
|
}
|
|
}
|
|
}
|
|
(participating, weight)
|
|
}
|
|
|
|
// Get the participation in a given round
|
|
pub(crate) fn round_participation(&self, round: RoundNumber) -> u64 {
|
|
let mut weight = 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
|
|
pub(crate) fn has_participation(&self, round: RoundNumber, 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()
|
|
}
|
|
|
|
// Check if consensus has been reached on a specific piece of data
|
|
pub(crate) fn has_consensus(&self, round: RoundNumber, data: DataFor<N>) -> bool {
|
|
let (_, weight) = self.message_instances(round, data);
|
|
weight >= self.weights.threshold()
|
|
}
|
|
|
|
pub(crate) fn get(
|
|
&self,
|
|
round: RoundNumber,
|
|
sender: N::ValidatorId,
|
|
step: Step,
|
|
) -> Option<&SignedMessageFor<N>> {
|
|
self.log.get(&round).and_then(|round| round.get(&sender).and_then(|msgs| msgs.get(&step)))
|
|
}
|
|
}
|