diff --git a/crates/hotshot/src/tasks/mod.rs b/crates/hotshot/src/tasks/mod.rs index 466136d887..fe9a75dbfd 100644 --- a/crates/hotshot/src/tasks/mod.rs +++ b/crates/hotshot/src/tasks/mod.rs @@ -39,8 +39,7 @@ 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, @@ -48,7 +47,7 @@ use hotshot_types::{ }, }; 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 @@ -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, V: Versions> EventTransformerState - for BadProposalViewDos -{ - async fn recv_handler(&mut self, event: &HotShotEvent) -> Vec> { - vec![event.clone()] - } - - async fn send_handler( - &mut self, - event: &HotShotEvent, - _public_key: &TYPES::SignatureKey, - _private_key: &::PrivateKey, - _upgrade_lock: &UpgradeLock, - ) -> Vec> { - 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, V: Versions> EventTransformerState - for DoubleProposeVote -{ - async fn recv_handler(&mut self, event: &HotShotEvent) -> Vec> { - vec![event.clone()] - } - - async fn send_handler( - &mut self, - event: &HotShotEvent, - _public_key: &TYPES::SignatureKey, - _private_key: &::PrivateKey, - _upgrade_lock: &UpgradeLock, - ) -> Vec> { - 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> { - /// Store events from previous views - pub validated_proposals: Vec>, - /// 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, - /// How far back to look for a QC - pub view_look_back: usize, - /// Phantom - pub _phantom: std::marker::PhantomData, -} - -/// 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> DishonestLeader { - /// 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, - proposal: &Proposal>, - sender: &TYPES::SignatureKey, - ) -> HotShotEvent { - 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 + std::fmt::Debug, V: Versions> - EventTransformerState for DishonestLeader -{ - async fn recv_handler(&mut self, event: &HotShotEvent) -> Vec> { - vec![event.clone()] - } - - async fn send_handler( - &mut self, - event: &HotShotEvent, - _public_key: &TYPES::SignatureKey, - _private_key: &::PrivateKey, - _upgrade_lock: &UpgradeLock, - ) -> Vec> { - 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, - /// When leader how many times we will send DacSend and increment view number - pub total_views_add_to_cert: u64, -} - -#[async_trait] -impl + std::fmt::Debug, V: Versions> - EventTransformerState for DishonestDa -{ - async fn recv_handler(&mut self, event: &HotShotEvent) -> Vec> { - vec![event.clone()] - } - - async fn send_handler( - &mut self, - event: &HotShotEvent, - _public_key: &TYPES::SignatureKey, - _private_key: &::PrivateKey, - _upgrade_lock: &UpgradeLock, - ) -> Vec> { - 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, V: Versions>( handle: &mut SystemContextHandle,