//! Byzantine participant that sends conflicting notarize/finalize messages. use crate::{ threshold_simplex::types::{Finalize, Notarize, Proposal, View, Voter}, ThresholdSupervisor, Viewable, }; use commonware_codec::{DecodeExt, Encode}; use commonware_cryptography::{ bls12381::primitives::{group, variant::Variant}, Digest, Hasher, }; use commonware_p2p::{Receiver, Recipients, Sender}; use commonware_runtime::{Clock, Handle, Spawner}; use rand::{CryptoRng, Rng}; use std::marker::PhantomData; use tracing::debug; pub struct Config> { pub supervisor: S, pub namespace: Vec, } pub struct Conflicter< E: Clock + Rng + CryptoRng + Spawner, V: Variant, H: Hasher, S: ThresholdSupervisor, > { context: E, supervisor: S, namespace: Vec, _hasher: PhantomData, _variant: PhantomData, } impl< E: Clock + Rng + CryptoRng + Spawner, V: Variant, H: Hasher, S: ThresholdSupervisor, > Conflicter { pub fn new(context: E, cfg: Config) -> Self { Self { context, supervisor: cfg.supervisor, namespace: cfg.namespace, _hasher: PhantomData, _variant: PhantomData, } } pub fn start(mut self, pending_network: (impl Sender, impl Receiver)) -> Handle<()> { self.context.spawn_ref()(self.run(pending_network)) } async fn run(mut self, pending_network: (impl Sender, impl Receiver)) { let (mut sender, mut receiver) = pending_network; while let Ok((s, msg)) = receiver.recv().await { // Parse message let msg = match Voter::::decode(msg) { Ok(msg) => msg, Err(err) => { debug!(?err, sender = ?s, "failed to decode message"); continue; } }; // Process message match msg { Voter::Notarize(notarize) => { // Notarize random digest let view = notarize.view(); let share = self.supervisor.share(view).unwrap(); let payload = H::Digest::random(&mut self.context); let proposal = Proposal::new(view, notarize.proposal.parent, payload); let n = Notarize::::sign(&self.namespace, share, proposal); let msg = Voter::Notarize(n).encode().into(); sender.send(Recipients::All, msg, true).await.unwrap(); // Notarize received digest let n = Notarize::::sign(&self.namespace, share, notarize.proposal); let msg = Voter::Notarize(n).encode().into(); sender.send(Recipients::All, msg, true).await.unwrap(); } Voter::Finalize(finalize) => { // Finalize random digest let view = finalize.view(); let share = self.supervisor.share(view).unwrap(); let payload = H::Digest::random(&mut self.context); let proposal = Proposal::new(view, finalize.proposal.parent, payload); let f = Finalize::::sign(&self.namespace, share, proposal); let msg = Voter::Finalize(f).encode().into(); sender.send(Recipients::All, msg, true).await.unwrap(); // Finalize provided digest let f = Finalize::::sign(&self.namespace, share, finalize.proposal); let msg = Voter::Finalize(f).encode().into(); sender.send(Recipients::All, msg, true).await.unwrap(); } _ => continue, } } } }