//! Byzantine participant that sends outdated notarize and finalize messages. use crate::{ simplex::types::{Finalize, Notarize, Proposal, View, Voter}, Supervisor, Viewable, }; use commonware_codec::{Decode, Encode}; use commonware_cryptography::{Hasher, Signer}; use commonware_p2p::{Receiver, Recipients, Sender}; use commonware_runtime::{Clock, Handle, Spawner}; use rand::{CryptoRng, Rng}; use std::collections::HashMap; use tracing::debug; pub struct Config> { pub crypto: C, pub supervisor: S, pub namespace: Vec, pub view_delta: u64, } pub struct Outdated< E: Clock + Rng + CryptoRng + Spawner, C: Signer, H: Hasher, S: Supervisor, > { context: E, crypto: C, supervisor: S, namespace: Vec, history: HashMap>, view_delta: u64, } impl< E: Clock + Rng + CryptoRng + Spawner, C: Signer, H: Hasher, S: Supervisor, > Outdated { pub fn new(context: E, cfg: Config) -> Self { Self { context, crypto: cfg.crypto, supervisor: cfg.supervisor, namespace: cfg.namespace, history: HashMap::new(), view_delta: cfg.view_delta, } } pub fn start(mut self, voter_network: (impl Sender, impl Receiver)) -> Handle<()> { self.context.spawn_ref()(self.run(voter_network)) } async fn run(mut self, voter_network: (impl Sender, impl Receiver)) { let (mut sender, mut receiver) = voter_network; while let Ok((s, msg)) = receiver.recv().await { // Parse message let msg = match Voter::::decode_cfg(msg, &usize::MAX) { Ok(msg) => msg, Err(err) => { debug!(?err, sender = ?s, "failed to decode message"); continue; } }; let view = msg.view(); // Process message match msg { Voter::Notarize(notarize) => { // Store proposal self.history.insert(view, notarize.proposal.clone()); // Notarize old digest let view = view.saturating_sub(self.view_delta); let public_key_index = self .supervisor .is_participant(view, &self.crypto.public_key()) .unwrap(); let Some(proposal) = self.history.get(&view) else { continue; }; debug!(?view, "notarizing old proposal"); let msg = Notarize::sign( &self.namespace, &mut self.crypto, public_key_index, proposal.clone(), ); let msg = Voter::Notarize(msg).encode().into(); sender.send(Recipients::All, msg, true).await.unwrap(); } Voter::Finalize(finalize) => { // Store proposal self.history.insert(view, finalize.proposal.clone()); // Finalize old digest let view = view.saturating_sub(self.view_delta); let public_key_index = self .supervisor .is_participant(view, &self.crypto.public_key()) .unwrap(); let Some(proposal) = self.history.get(&view) else { continue; }; debug!(?view, "finalizing old proposal"); let msg = Finalize::sign( &self.namespace, &mut self.crypto, public_key_index, proposal.clone(), ); let msg = Voter::Finalize(msg).encode().into(); sender.send(Recipients::All, msg, true).await.unwrap(); } _ => continue, } } } }