Skip to main content

tycho_collator/validator/
mod.rs

1use std::sync::Arc;
2
3use anyhow::Result;
4use async_trait::async_trait;
5use tycho_crypto::ed25519::PublicKey;
6use tycho_network::{Network, OverlayService, PeerId, PeerResolver};
7use tycho_types::models::{BlockId, BlockIdShort, ShardIdent, ValidatorDescription};
8use tycho_util::FastHashMap;
9
10pub use self::impls::*;
11
12pub mod proto;
13pub mod rpc;
14
15mod impls {
16    pub use self::std_impl::{ValidatorStdImpl, ValidatorStdImplConfig};
17
18    mod std_impl;
19}
20
21// === Validator ===
22
23#[async_trait]
24pub trait Validator: Send + Sync + 'static {
25    /// Adds a new session for the specified shard.
26    fn add_session(&self, info: AddSession<'_>) -> Result<()>;
27
28    /// Collects signatures for the specified block.
29    async fn validate(
30        &self,
31        session_id: ValidationSessionId,
32        block_id: &BlockId,
33    ) -> Result<ValidationStatus>;
34
35    /// Cancels validation before the specified block.
36    ///
37    /// If `session_id` is provided, it will be used to directly cancel the specific session,
38    /// avoiding unnecessary lookups and potential ambiguity.
39    fn cancel_validation(
40        &self,
41        before: &BlockIdShort,
42        session_id: Option<ValidationSessionId>,
43    ) -> Result<()>;
44}
45
46// === Types ===
47
48pub struct ValidatorNetworkContext {
49    pub network: Network,
50    pub peer_resolver: PeerResolver,
51    pub overlays: OverlayService,
52    pub zerostate_id: BlockId,
53}
54
55/// (seqno, subset `short_hash`)
56pub type ValidationSessionId = (u32, u32);
57
58pub trait CompositeValidationSessionId {
59    fn seqno(&self) -> u32;
60}
61
62impl CompositeValidationSessionId for ValidationSessionId {
63    fn seqno(&self) -> u32 {
64        self.0
65    }
66}
67
68#[derive(Debug, Clone, Copy)]
69pub struct AddSession<'a> {
70    pub shard_ident: ShardIdent,
71    pub start_block_seqno: u32,
72    pub session_id: ValidationSessionId,
73    pub validators: &'a [ValidatorDescription],
74}
75
76#[derive(Debug, Clone)]
77pub enum ValidationStatus {
78    Skipped,
79    Complete(ValidationComplete),
80}
81
82#[derive(Debug, Clone)]
83pub struct ValidationComplete {
84    pub signatures: BlockSignatures,
85    pub total_weight: u64,
86}
87
88pub type BlockSignatures = FastHashMap<PeerId, Arc<[u8; 64]>>;
89
90#[derive(Debug, Clone)]
91pub struct BriefValidatorDescr {
92    pub peer_id: PeerId,
93    pub public_key: PublicKey,
94    pub weight: u64,
95}
96
97impl TryFrom<&ValidatorDescription> for BriefValidatorDescr {
98    type Error = anyhow::Error;
99
100    fn try_from(descr: &ValidatorDescription) -> Result<Self> {
101        let Some(public_key) = PublicKey::from_bytes(descr.public_key.0) else {
102            anyhow::bail!("invalid validator public key");
103        };
104
105        Ok(Self {
106            peer_id: PeerId(descr.public_key.0),
107            public_key,
108            weight: descr.weight,
109        })
110    }
111}