tempo_commonware_node/peer_manager/mod.rs
1//! Tracks active peers and consists of an [`Actor`] and a [`Mailbox`].
2//!
3//! This actor acts as a layer on top of the commonware p2p network actor. It
4//! reads chain state to determine who this node should peer with, and registers
5//! these peers with the P2P actor.
6//!
7//! The actor is configured via [`Config`] passed to the [`init`] function.
8//!
9//! Other parts of the system interact with the actor through its [`Mailbox`],
10//! which implements [`AddressableManager`], [`commonware_p2p::Provider`], and
11//! [`commonware_consensus::Reporter`] to receive
12//! [`commonware_consensus::marshal::Update`] from the marshal actor.
13//!
14//! # How peers are determined
15//!
16//! The set of peers is the union of two subsets:
17//!
18//! 1. Those entries in the Validator Config contract that have a field
19//! `active == true`.
20//! 2. The dealers and players as per the last DKG outcome.
21//!
22//! Because DKG ceremonies can fail, it happens that the DKG outcome contains
23//! validators that contain `active == false` in the contract. Therefore, the
24//! actor reads all entries in the contract to look up the egress and ingress
25//! addresses of the validators (active and inactive), before constructing an
26//! overall peer set `{dealers, players, active validators}` together with
27//! addresses.
28
29use commonware_consensus::types::{FixedEpocher, Height};
30use commonware_cryptography::ed25519::PublicKey;
31use commonware_p2p::AddressableManager;
32use commonware_runtime::{Clock, Metrics, Spawner};
33use futures::channel::mpsc;
34use tempo_node::TempoFullNode;
35
36mod actor;
37mod ingress;
38
39pub(crate) use actor::Actor;
40pub(crate) use ingress::Mailbox;
41
42use crate::executor;
43
44/// Configuration of the peer manager actor.
45pub(crate) struct Config<TOracle> {
46 /// The mailbox to the P2P network to register the peer sets.
47 pub(crate) oracle: TOracle,
48 /// A handle to the full execution node to read block headers and look up
49 /// the Validator Config contract
50 pub(crate) execution_node: TempoFullNode,
51 /// The mailbox to the executor actor. Used to check if the executor has
52 /// already finalized a block at a given height.
53 pub(crate) executor: executor::Mailbox,
54 /// The epoch strategy used by the node.
55 pub(crate) epoch_strategy: FixedEpocher,
56 /// The last finalized height according to the consensus layer (marshal).
57 /// Used during start to determine the correct boundary block, since
58 /// the execution layer may be behind.
59 pub(crate) last_finalized_height: Height,
60}
61
62/// Initializes a peer manager actor from a `config` with runtime `context`.
63pub(crate) fn init<TContext, TPeerManager>(
64 context: TContext,
65 config: Config<TPeerManager>,
66) -> (Actor<TContext, TPeerManager>, Mailbox)
67where
68 TContext: Clock + Metrics + Spawner,
69 TPeerManager: AddressableManager<PublicKey = PublicKey>,
70{
71 let (tx, rx) = mpsc::unbounded();
72 let actor = Actor::new(context, config, rx);
73 let mailbox = Mailbox::new(tx);
74 (actor, mailbox)
75}