Skip to content

Commit

Permalink
sim-rs: send all of a node's votes in the same message
Browse files Browse the repository at this point in the history
  • Loading branch information
SupernaviX committed Nov 22, 2024
1 parent 3651b6c commit 82ae013
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 41 deletions.
4 changes: 2 additions & 2 deletions sim-rs/sim-cli/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,10 @@ impl EventMonitor {
*eb_stake.entry(eb).or_default() += stake as f64;
}
Event::NoVote { .. } => {}
Event::VoteSent { .. } => {
Event::VotesSent { .. } => {
vote_messages.sent += 1;
}
Event::VoteReceived { .. } => {
Event::VotesReceived { .. } => {
vote_messages.received += 1;
}
}
Expand Down
32 changes: 20 additions & 12 deletions sim-rs/sim-core/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,15 @@ pub enum Event {
eb: EndorserBlockId,
reason: NoVoteReason,
},
VoteSent {
#[serde(flatten)]
vote: Vote,
VotesSent {
slot: u64,
producer: NodeId,
sender: NodeId,
recipient: NodeId,
},
VoteReceived {
#[serde(flatten)]
vote: Vote,
VotesReceived {
slot: u64,
producer: NodeId,
sender: NodeId,
recipient: NodeId,
},
Expand Down Expand Up @@ -240,17 +240,25 @@ impl EventTracker {
});
}

pub fn track_vote_sent(&self, vote: &Vote, sender: NodeId, recipient: NodeId) {
self.send(Event::VoteSent {
vote: vote.clone(),
pub fn track_votes_sent(&self, votes: &[Vote], sender: NodeId, recipient: NodeId) {
let Some(Vote { slot, producer, .. }) = votes.first() else {
return;
};
self.send(Event::VotesSent {
slot: *slot,
producer: *producer,
sender,
recipient,
});
}

pub fn track_vote_received(&self, vote: &Vote, sender: NodeId, recipient: NodeId) {
self.send(Event::VoteReceived {
vote: vote.clone(),
pub fn track_votes_received(&self, votes: &[Vote], sender: NodeId, recipient: NodeId) {
let Some(Vote { slot, producer, .. }) = votes.first() else {
return;
};
self.send(Event::VotesReceived {
slot: *slot,
producer: *producer,
sender,
recipient,
});
Expand Down
4 changes: 2 additions & 2 deletions sim-rs/sim-core/src/sim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ enum SimulationMessage {
RequestEB(EndorserBlockId),
EB(Arc<EndorserBlock>),
// Get out the vote
Vote(Vote),
Votes(Arc<Vec<Vote>>),
}

impl HasBytesSize for SimulationMessage {
Expand All @@ -250,7 +250,7 @@ impl HasBytesSize for SimulationMessage {
Self::RequestEB(_) => 8,
Self::EB(_) => 32,

Self::Vote(_) => 8,
Self::Votes(v) => 8 * v.len() as u64,
}
}
}
59 changes: 34 additions & 25 deletions sim-rs/sim-core/src/sim/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ impl Node {
}

// Voting
SimulationMessage::Vote(vote) => {
self.receive_vote(from, vote)?;
SimulationMessage::Votes(votes) => {
self.receive_votes(from, votes)?;
}
}
}
Expand Down Expand Up @@ -300,17 +300,26 @@ impl Node {
let Some(ebs) = self.leios.ebs_by_slot.remove(&eb_slot) else {
return Ok(());
};
let mut votes = vec![];
for eb_id in ebs {
let eb = self.leios.ebs.get(&eb_id).unwrap();
match self.should_vote_for(slot, eb) {
Ok(()) => {
self.vote_for(slot, eb_id)?;
let vote = Vote {
slot,
producer: self.id,
eb: eb_id,
};
votes.push(vote);
}
Err(reason) => {
self.tracker.track_no_vote(slot, self.id, eb_id, reason);
}
}
}
if !votes.is_empty() {
self.send_votes(votes)?;
}
return Ok(());
}
}
Expand Down Expand Up @@ -594,19 +603,21 @@ impl Node {
Ok(())
}

fn receive_vote(&mut self, from: NodeId, vote: Vote) -> Result<()> {
self.tracker.track_vote_received(&vote, from, self.id);
let votes = self.leios.votes_by_eb.entry(vote.eb).or_default();
if !votes.insert(vote.clone()) {
return Ok(());
fn receive_votes(&mut self, from: NodeId, votes: Arc<Vec<Vote>>) -> Result<()> {
self.tracker.track_votes_received(&votes, from, self.id);
for vote in votes.iter() {
let eb_votes = self.leios.votes_by_eb.entry(vote.eb).or_default();
if !eb_votes.insert(vote.clone()) {
return Ok(());
}
}
// We haven't seen this vote before, so propagate it to our neighbors
// We haven't seen these votes before, so propagate them to our neighbors
for peer in &self.peers {
if *peer == from {
continue;
}
self.tracker.track_vote_sent(&vote, self.id, *peer);
self.send_to(*peer, SimulationMessage::Vote(vote.clone()))?;
self.tracker.track_votes_sent(&votes, self.id, *peer);
self.send_to(*peer, SimulationMessage::Votes(votes.clone()))?;
}
Ok(())
}
Expand Down Expand Up @@ -725,21 +736,19 @@ impl Node {
Ok(())
}

fn vote_for(&mut self, slot: u64, eb_id: EndorserBlockId) -> Result<()> {
let vote = Vote {
producer: self.id,
slot,
eb: eb_id,
};
self.tracker.track_vote(&vote);
self.leios
.votes_by_eb
.entry(eb_id)
.or_default()
.insert(vote.clone());
fn send_votes(&mut self, votes: Vec<Vote>) -> Result<()> {
for vote in &votes {
self.tracker.track_vote(vote);
self.leios
.votes_by_eb
.entry(vote.eb)
.or_default()
.insert(vote.clone());
}
let votes = Arc::new(votes);
for peer in &self.peers {
self.tracker.track_vote_sent(&vote, self.id, *peer);
self.send_to(*peer, SimulationMessage::Vote(vote.clone()))?;
self.tracker.track_votes_sent(&votes, self.id, *peer);
self.send_to(*peer, SimulationMessage::Votes(votes.clone()))?;
}
Ok(())
}
Expand Down

0 comments on commit 82ae013

Please sign in to comment.