use super::ActorArtifact; use crate::dkg::{ReshareBlock, state_sync::Plan}; use commonware_actor::mailbox::{Policy, Sender}; use commonware_consensus::{ marshal::core::{Mailbox as MarshalMailbox, Variant}, simplex::scheme::Scheme, }; use commonware_utils::channel::oneshot; use std::collections::VecDeque; /// Messages sent to the DKG probe actor. pub(crate) enum Message where S: Scheme, V: Variant, V::ApplicationBlock: ReshareBlock, { /// Subscribe to the probe artifact (see [`Mailbox::subscribe`]). Subscribe { /// Channel used to resolve the subscriber. response: oneshot::Sender>, }, /// Attach marshal and the plan to serve peers' requests (see [`Mailbox::attach`]). Attach { /// Marshal mailbox that serves latest-finalization requests. marshal: MarshalMailbox, /// Plan whose checkpoints serve boundary requests. plan: Plan, }, } impl Policy for Message where S: Scheme, V: Variant, V::ApplicationBlock: ReshareBlock, { type Overflow = VecDeque; fn handle(overflow: &mut Self::Overflow, message: Self) { overflow.push_back(message); } } /// Mailbox for a running DKG probe actor. #[derive(Clone)] pub struct Mailbox where S: Scheme, V: Variant, V::ApplicationBlock: ReshareBlock, { sender: Sender>, } impl Mailbox where S: Scheme, V: Variant, V::ApplicationBlock: ReshareBlock, { pub(crate) const fn new(sender: Sender>) -> Self { Self { sender } } /// Subscribes to the probe artifact. /// /// The first live subscriber starts discovery. Dropping the returned /// receiver cancels the subscription. If discovery has already resolved, the /// receiver gets the cached artifact immediately. If the actor has stopped, /// or is serving without an artifact, the receiver closes without a value. pub fn subscribe(&self) -> oneshot::Receiver> { let (response, receiver) = oneshot::channel(); let _ = self.sender.enqueue(Message::Subscribe { response }); receiver } /// Attaches marshal and the [`Plan`] so the actor can serve peers' requests. /// /// The actor answers latest-finalization requests from marshal and boundary requests from the /// plan's checkpoints, so pass a clone of the plan given to the DKG actors. /// /// If subscribers are pending, the actor starts serving once they are /// resolved or dropped. A source node can attach without ever subscribing, /// so it serves without sending discovery requests. /// /// # Panics /// /// Panics if the plan was created with [`Plan::disabled`]. pub fn attach(&self, marshal: MarshalMailbox, plan: Plan) { assert!( plan.is_enabled(), "probe requires an enabled state sync plan" ); let _ = self.sender.enqueue(Message::Attach { marshal, plan }); } } #[cfg(test)] mod tests { use super::*; use crate::dkg::tests::mocks; use commonware_cryptography::Signer as _; use commonware_runtime::{Runner as _, Supervisor as _, deterministic}; use commonware_utils::{NZU64, NZUsize}; #[test] #[should_panic(expected = "probe requires an enabled state sync plan")] fn attach_rejects_disabled_plan() { deterministic::Runner::default().start(|mut context| async move { let fixture = mocks::scheme_fixture(&mut context); let marshal = mocks::closed_marshal_mailbox( context.child("marshal"), &mocks::TestSigner::from_seed(0), fixture.schemes[0].clone(), "disabled-plan", NZU64!(2), ) .await; let (sender, _receiver) = commonware_actor::mailbox::new(context.child("probe"), NZUsize!(1)); Mailbox::new(sender).attach(marshal, Plan::disabled()); }); } }