Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CLEANUP] remove unnecessary byzantine testing code #3644

Merged
merged 2 commits into from
Sep 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 2 additions & 204 deletions crates/hotshot/src/tasks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,15 @@ use hotshot_task_impls::{
use hotshot_types::message::UpgradeLock;
use hotshot_types::{
constants::EVENT_CHANNEL_SIZE,
data::QuorumProposal,
message::{Messages, Proposal},
message::Messages,
request_response::RequestReceiver,
traits::{
network::ConnectedNetwork,
node_implementation::{ConsensusTime, NodeImplementation, NodeType},
},
};
use std::fmt::Debug;
use std::{collections::HashSet, sync::Arc, time::Duration};
use std::{sync::Arc, time::Duration};
use vbs::version::StaticVersionType;

/// event for global event stream
Expand Down Expand Up @@ -538,207 +537,6 @@ where
}
}

#[derive(Debug)]
/// An `EventTransformerState` that multiplies `QuorumProposalSend` events, incrementing the view number of the proposal
pub struct BadProposalViewDos {
/// The number of times to duplicate a `QuorumProposalSend` event
pub multiplier: u64,
/// The view number increment each time it's duplicated
pub increment: u64,
}

#[async_trait]
impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> EventTransformerState<TYPES, I, V>
for BadProposalViewDos
{
async fn recv_handler(&mut self, event: &HotShotEvent<TYPES>) -> Vec<HotShotEvent<TYPES>> {
vec![event.clone()]
}

async fn send_handler(
&mut self,
event: &HotShotEvent<TYPES>,
_public_key: &TYPES::SignatureKey,
_private_key: &<TYPES::SignatureKey as SignatureKey>::PrivateKey,
_upgrade_lock: &UpgradeLock<TYPES, V>,
) -> Vec<HotShotEvent<TYPES>> {
match event {
HotShotEvent::QuorumProposalSend(proposal, signature) => {
let mut result = Vec::new();

for n in 0..self.multiplier {
let mut modified_proposal = proposal.clone();

modified_proposal.data.view_number += n * self.increment;

result.push(HotShotEvent::QuorumProposalSend(
modified_proposal,
signature.clone(),
));
}

result
}
_ => vec![event.clone()],
}
}
}

#[derive(Debug)]
/// An `EventHandlerState` that doubles the `QuorumVoteSend` and `QuorumProposalSend` events
pub struct DoubleProposeVote;

#[async_trait]
impl<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions> EventTransformerState<TYPES, I, V>
for DoubleProposeVote
{
async fn recv_handler(&mut self, event: &HotShotEvent<TYPES>) -> Vec<HotShotEvent<TYPES>> {
vec![event.clone()]
}

async fn send_handler(
&mut self,
event: &HotShotEvent<TYPES>,
_public_key: &TYPES::SignatureKey,
_private_key: &<TYPES::SignatureKey as SignatureKey>::PrivateKey,
_upgrade_lock: &UpgradeLock<TYPES, V>,
) -> Vec<HotShotEvent<TYPES>> {
match event {
HotShotEvent::QuorumProposalSend(_, _) | HotShotEvent::QuorumVoteSend(_) => {
vec![event.clone(), event.clone()]
}
_ => vec![event.clone()],
}
}
}

#[derive(Debug)]
/// An `EventHandlerState` that modifies justify_qc on `QuorumProposalSend` to that of a previous view to mock dishonest leader
pub struct DishonestLeader<TYPES: NodeType, I: NodeImplementation<TYPES>> {
/// Store events from previous views
pub validated_proposals: Vec<QuorumProposal<TYPES>>,
/// How many times current node has been elected leader and sent proposal
pub total_proposals_from_node: u64,
/// Which proposals to be dishonest at
pub dishonest_at_proposal_numbers: HashSet<u64>,
/// How far back to look for a QC
pub view_look_back: usize,
/// Phantom
pub _phantom: std::marker::PhantomData<I>,
}

/// Add method that will handle `QuorumProposalSend` events
/// If we have previous proposals stored and the total_proposals_from_node matches a value specified in dishonest_at_proposal_numbers
/// Then send out the event with the modified proposal that has an older QC
impl<TYPES: NodeType, I: NodeImplementation<TYPES>> DishonestLeader<TYPES, I> {
/// When a leader is sending a proposal this method will mock a dishonest leader
/// We accomplish this by looking back a number of specified views and using that cached proposals QC
fn handle_proposal_send_event(
&self,
event: &HotShotEvent<TYPES>,
proposal: &Proposal<TYPES, QuorumProposal<TYPES>>,
sender: &TYPES::SignatureKey,
) -> HotShotEvent<TYPES> {
let length = self.validated_proposals.len();
if !self
.dishonest_at_proposal_numbers
.contains(&self.total_proposals_from_node)
|| length == 0
{
return event.clone();
}

// Grab proposal from specified view look back
let proposal_from_look_back = if length - 1 < self.view_look_back {
// If look back is too far just take the first proposal
self.validated_proposals[0].clone()
} else {
let index = (self.validated_proposals.len() - 1) - self.view_look_back;
self.validated_proposals[index].clone()
};

// Create a dishonest proposal by using the old proposals qc
let mut dishonest_proposal = proposal.clone();
dishonest_proposal.data.justify_qc = proposal_from_look_back.justify_qc;

HotShotEvent::QuorumProposalSend(dishonest_proposal, sender.clone())
}
}

#[async_trait]
impl<TYPES: NodeType, I: NodeImplementation<TYPES> + std::fmt::Debug, V: Versions>
EventTransformerState<TYPES, I, V> for DishonestLeader<TYPES, I>
{
async fn recv_handler(&mut self, event: &HotShotEvent<TYPES>) -> Vec<HotShotEvent<TYPES>> {
vec![event.clone()]
}

async fn send_handler(
&mut self,
event: &HotShotEvent<TYPES>,
_public_key: &TYPES::SignatureKey,
_private_key: &<TYPES::SignatureKey as SignatureKey>::PrivateKey,
_upgrade_lock: &UpgradeLock<TYPES, V>,
) -> Vec<HotShotEvent<TYPES>> {
match event {
HotShotEvent::QuorumProposalSend(proposal, sender) => {
self.total_proposals_from_node += 1;
return vec![self.handle_proposal_send_event(event, proposal, sender)];
}
HotShotEvent::QuorumProposalValidated(proposal, _) => {
self.validated_proposals.push(proposal.clone());
}
_ => {}
}
vec![event.clone()]
}
}

#[derive(Debug)]
/// An `EventHandlerState` that modifies view number on the certificate of `DacSend` event to that of a future view
pub struct DishonestDa {
/// How many times current node has been elected leader and sent Da Cert
pub total_da_certs_sent_from_node: u64,
/// Which proposals to be dishonest at
pub dishonest_at_da_cert_sent_numbers: HashSet<u64>,
/// When leader how many times we will send DacSend and increment view number
pub total_views_add_to_cert: u64,
}

#[async_trait]
impl<TYPES: NodeType, I: NodeImplementation<TYPES> + std::fmt::Debug, V: Versions>
EventTransformerState<TYPES, I, V> for DishonestDa
{
async fn recv_handler(&mut self, event: &HotShotEvent<TYPES>) -> Vec<HotShotEvent<TYPES>> {
vec![event.clone()]
}

async fn send_handler(
&mut self,
event: &HotShotEvent<TYPES>,
_public_key: &TYPES::SignatureKey,
_private_key: &<TYPES::SignatureKey as SignatureKey>::PrivateKey,
_upgrade_lock: &UpgradeLock<TYPES, V>,
) -> Vec<HotShotEvent<TYPES>> {
if let HotShotEvent::DacSend(cert, sender) = event {
self.total_da_certs_sent_from_node += 1;
if self
.dishonest_at_da_cert_sent_numbers
.contains(&self.total_da_certs_sent_from_node)
{
let mut result = vec![HotShotEvent::DacSend(cert.clone(), sender.clone())];
for i in 1..=self.total_views_add_to_cert {
let mut bad_cert = cert.clone();
bad_cert.view_number = cert.view_number + i;
result.push(HotShotEvent::DacSend(bad_cert, sender.clone()));
}
return result;
}
}
vec![event.clone()]
}
}

/// adds tasks for sending/receiving messages to/from the network.
pub async fn add_network_tasks<TYPES: NodeType, I: NodeImplementation<TYPES>, V: Versions>(
handle: &mut SystemContextHandle<TYPES, I, V>,
Expand Down
Loading