//! Byzantine participant that sends nullify and finalize messages for the same view. use crate::simplex::{ scheme, types::{Finalize, Nullify, Vote}, }; use commonware_codec::{DecodeExt, Encode}; use commonware_cryptography::{certificate::Scheme, Hasher}; use commonware_p2p::{Receiver, Recipients, Sender}; use commonware_runtime::{spawn_cell, ContextCell, Handle, Spawner}; use std::marker::PhantomData; use tracing::debug; pub struct Config { pub scheme: S, pub namespace: Vec, } pub struct Nuller { context: ContextCell, scheme: S, namespace: Vec, _hasher: PhantomData, } impl Nuller where E: Spawner, S: scheme::Scheme, H: Hasher, { pub fn new(context: E, cfg: Config) -> Self { Self { context: ContextCell::new(context), scheme: cfg.scheme, namespace: cfg.namespace, _hasher: PhantomData, } } pub fn start(mut self, vote_network: (impl Sender, impl Receiver)) -> Handle<()> { spawn_cell!(self.context, self.run(vote_network).await) } async fn run(self, vote_network: (impl Sender, impl Receiver)) { let (mut sender, mut receiver) = vote_network; while let Ok((s, msg)) = receiver.recv().await { // Parse message let msg = match Vote::::decode(msg) { Ok(msg) => msg, Err(err) => { debug!(?err, sender = ?s, "failed to decode message"); continue; } }; // Process message match msg { Vote::Notarize(notarize) => { // Nullify let n = Nullify::sign(&self.scheme, &self.namespace, notarize.round()).unwrap(); let msg = Vote::::Nullify(n).encode().into(); sender.send(Recipients::All, msg, true).await.unwrap(); // Finalize digest let proposal = notarize.proposal; let f = Finalize::::sign(&self.scheme, &self.namespace, proposal).unwrap(); let msg = Vote::Finalize(f).encode().into(); sender.send(Recipients::All, msg, true).await.unwrap(); } _ => continue, } } } }