mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 20:29:23 +00:00
* Clear upons upon round, not block * Cache the proposal for a round * Rebase onto develop, which reverted this PR, and re-apply this PR * Set participation upon participation instead of constantly recalculating * Cache message instances * Add missing txn commit Identified by @akildemir. * Correct clippy lint identified upon rebase * Fix tendermint chain sync (#581) * fix p2p Reqres protocol * stabilize tributary chain sync * fix pr comments --------- Co-authored-by: akildemir <34187742+akildemir@users.noreply.github.com>
This commit is contained in:
@@ -3,7 +3,6 @@ use std::{
|
||||
collections::{HashSet, HashMap},
|
||||
};
|
||||
|
||||
use parity_scale_codec::Encode;
|
||||
use serai_db::{Get, DbTxn, Db};
|
||||
|
||||
use crate::{
|
||||
@@ -20,7 +19,7 @@ pub(crate) struct BlockData<N: Network> {
|
||||
|
||||
pub(crate) number: BlockNumber,
|
||||
pub(crate) validator_id: Option<N::ValidatorId>,
|
||||
pub(crate) proposal: Option<N::Block>,
|
||||
pub(crate) our_proposal: Option<N::Block>,
|
||||
|
||||
pub(crate) log: MessageLog<N>,
|
||||
pub(crate) slashes: HashSet<N::ValidatorId>,
|
||||
@@ -43,7 +42,7 @@ impl<N: Network> BlockData<N> {
|
||||
weights: Arc<N::Weights>,
|
||||
number: BlockNumber,
|
||||
validator_id: Option<N::ValidatorId>,
|
||||
proposal: Option<N::Block>,
|
||||
our_proposal: Option<N::Block>,
|
||||
) -> BlockData<N> {
|
||||
BlockData {
|
||||
db,
|
||||
@@ -51,7 +50,7 @@ impl<N: Network> BlockData<N> {
|
||||
|
||||
number,
|
||||
validator_id,
|
||||
proposal,
|
||||
our_proposal,
|
||||
|
||||
log: MessageLog::new(weights),
|
||||
slashes: HashSet::new(),
|
||||
@@ -108,17 +107,17 @@ impl<N: Network> BlockData<N> {
|
||||
self.populate_end_time(round);
|
||||
}
|
||||
|
||||
// 11-13
|
||||
// L11-13
|
||||
self.round = Some(RoundData::<N>::new(
|
||||
round,
|
||||
time.unwrap_or_else(|| self.end_time[&RoundNumber(round.0 - 1)]),
|
||||
));
|
||||
self.end_time.insert(round, self.round().end_time());
|
||||
|
||||
// 14-21
|
||||
// L14-21
|
||||
if Some(proposer) == self.validator_id {
|
||||
let (round, block) = self.valid.clone().unzip();
|
||||
block.or_else(|| self.proposal.clone()).map(|block| Data::Proposal(round, block))
|
||||
block.or_else(|| self.our_proposal.clone()).map(|block| Data::Proposal(round, block))
|
||||
} else {
|
||||
self.round_mut().set_timeout(Step::Propose);
|
||||
None
|
||||
@@ -198,8 +197,8 @@ impl<N: Network> BlockData<N> {
|
||||
assert!(!new_round);
|
||||
None?;
|
||||
}
|
||||
// Put this message to the DB
|
||||
txn.put(&msg_key, res.encode());
|
||||
// Put that we're sending this message to the DB
|
||||
txn.put(&msg_key, []);
|
||||
|
||||
txn.commit();
|
||||
}
|
||||
|
||||
@@ -288,7 +288,7 @@ pub trait Network: Sized + Send + Sync {
|
||||
async fn slash(&mut self, validator: Self::ValidatorId, slash_event: SlashEvent);
|
||||
|
||||
/// Validate a block.
|
||||
async fn validate(&mut self, block: &Self::Block) -> Result<(), BlockError>;
|
||||
async fn validate(&self, block: &Self::Block) -> Result<(), BlockError>;
|
||||
|
||||
/// Add a block, returning the proposal for the next one.
|
||||
///
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -2,21 +2,30 @@ use std::{sync::Arc, collections::HashMap};
|
||||
|
||||
use parity_scale_codec::Encode;
|
||||
|
||||
use crate::{ext::*, RoundNumber, Step, DataFor, TendermintError, SignedMessageFor, Evidence};
|
||||
use crate::{ext::*, RoundNumber, Step, DataFor, SignedMessageFor, Evidence};
|
||||
|
||||
type RoundLog<N> = HashMap<<N as Network>::ValidatorId, HashMap<Step, SignedMessageFor<N>>>;
|
||||
pub(crate) struct MessageLog<N: Network> {
|
||||
weights: Arc<N::Weights>,
|
||||
round_participation: HashMap<RoundNumber, u64>,
|
||||
participation: HashMap<(RoundNumber, Step), u64>,
|
||||
message_instances: HashMap<(RoundNumber, DataFor<N>), u64>,
|
||||
pub(crate) log: HashMap<RoundNumber, RoundLog<N>>,
|
||||
}
|
||||
|
||||
impl<N: Network> 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(),
|
||||
message_instances: 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>> {
|
||||
pub(crate) fn log(&mut self, signed: SignedMessageFor<N>) -> Result<bool, Evidence> {
|
||||
let msg = &signed.msg;
|
||||
// Clarity, and safety around default != new edge cases
|
||||
let round = self.log.entry(msg.round).or_insert_with(HashMap::new);
|
||||
@@ -30,69 +39,36 @@ impl<N: Network> MessageLog<N> {
|
||||
target: "tendermint",
|
||||
"Validator sent multiple messages for the same block + round + step"
|
||||
);
|
||||
Err(TendermintError::Malicious(
|
||||
msg.sender,
|
||||
Some(Evidence::ConflictingMessages(existing.encode(), signed.encode())),
|
||||
))?;
|
||||
Err(Evidence::ConflictingMessages(existing.encode(), signed.encode()))?;
|
||||
}
|
||||
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;
|
||||
*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 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
|
||||
*self.round_participation.get(&round).unwrap_or(&0)
|
||||
}
|
||||
|
||||
// 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()
|
||||
*self.participation.get(&(round, step)).unwrap_or(&0) >= 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)))
|
||||
*self.message_instances.get(&(round, data.clone())).unwrap_or(&0) >= self.weights.threshold()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,6 +57,7 @@ impl<N: Network> RoundData<N> {
|
||||
|
||||
// Poll all set timeouts, returning the Step whose timeout has just expired
|
||||
pub(crate) async fn timeout_future(&self) -> Step {
|
||||
/*
|
||||
let now = Instant::now();
|
||||
log::trace!(
|
||||
target: "tendermint",
|
||||
@@ -64,6 +65,7 @@ impl<N: Network> RoundData<N> {
|
||||
self.step,
|
||||
self.timeouts.iter().map(|(k, v)| (k, v.duration_since(now))).collect::<HashMap<_, _>>()
|
||||
);
|
||||
*/
|
||||
|
||||
let timeout_future = |step| {
|
||||
let timeout = self.timeouts.get(&step).copied();
|
||||
|
||||
@@ -145,7 +145,7 @@ impl Network for TestNetwork {
|
||||
println!("Slash for {id} due to {event:?}");
|
||||
}
|
||||
|
||||
async fn validate(&mut self, block: &TestBlock) -> Result<(), BlockError> {
|
||||
async fn validate(&self, block: &TestBlock) -> Result<(), BlockError> {
|
||||
block.valid
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user