massa_protocol_exports/
controller_trait.rs

1// Copyright (c) 2022 MASSA LABS <info@massa.net>
2
3use std::collections::HashMap;
4use std::net::SocketAddr;
5
6use crate::error::ProtocolError;
7use crate::BootstrapPeers;
8
9use crate::PeerId;
10use massa_models::prehash::{PreHashMap, PreHashSet};
11use massa_models::stats::NetworkStats;
12use massa_models::{block_header::SecuredHeader, block_id::BlockId};
13use massa_storage::Storage;
14use peernet::peer::PeerConnectionType;
15
16#[cfg_attr(feature = "test-exports", mockall_wrap::wrap, mockall::automock)]
17pub trait ProtocolController: Send + Sync {
18    /// Perform all operations needed to stop the ProtocolController
19    /// without dropping it completely yet.
20    fn stop(&mut self);
21
22    /// Sends the order to propagate the header of a block
23    ///
24    /// # Arguments
25    /// * `block_id`: ID of the block
26    /// * `storage`: Storage instance containing references to the block and all its dependencies
27    fn integrated_block(&self, block_id: BlockId, storage: Storage) -> Result<(), ProtocolError>;
28
29    /// Notify to protocol an attack attempt.
30    ///
31    /// # Arguments
32    /// * `block_id`: ID of the block
33    fn notify_block_attack(&self, block_id: BlockId) -> Result<(), ProtocolError>;
34
35    /// Update the block wish list
36    ///
37    /// # Arguments
38    /// * `new`: new blocks to add to the wish list
39    /// * `remove`: blocks to remove from the wish list
40    fn send_wishlist_delta(
41        &self,
42        new: PreHashMap<BlockId, Option<SecuredHeader>>,
43        remove: PreHashSet<BlockId>,
44    ) -> Result<(), ProtocolError>;
45
46    /// Propagate a batch of operation (from pool).
47    /// note: Full `OperationId` is replaced by a `OperationPrefixId` later by the worker.
48    ///
49    /// # Arguments:
50    /// * `operations`: operations to propagate
51    fn propagate_operations(&self, operations: Storage) -> Result<(), ProtocolError>;
52
53    /// Propagate a batch of endorsement (from pool).
54    ///
55    /// The endorsements are propagated as-is: callers must have fully validated them beforehand
56    /// (valid signature and creator matching the PoS draw of their `(slot, index)` pair), as is
57    /// done for endorsements coming from peers in `note_endorsements_from_peer`.
58    ///
59    /// # Arguments:
60    /// * `endorsements`: endorsements to propagate
61    fn propagate_endorsements(&self, endorsements: Storage) -> Result<(), ProtocolError>;
62
63    /// Get the stats from the protocol
64    /// Returns a tuple containing the stats and the list of peers
65    #[allow(clippy::type_complexity)]
66    fn get_stats(
67        &self,
68    ) -> Result<
69        (
70            NetworkStats,
71            HashMap<PeerId, (SocketAddr, PeerConnectionType)>,
72        ),
73        ProtocolError,
74    >;
75
76    /// Get a list of peers to be sent to someone that bootstrap to us
77    fn get_bootstrap_peers(&self) -> Result<BootstrapPeers, ProtocolError>;
78
79    /// Ban a list of Peer Id
80    fn ban_peers(&self, peer_ids: Vec<PeerId>) -> Result<(), ProtocolError>;
81
82    /// Unban a list of Peer Id
83    fn unban_peers(&self, peer_ids: Vec<PeerId>) -> Result<(), ProtocolError>;
84
85    /// Returns a boxed clone of self.
86    /// Useful to allow cloning `Box<dyn ProtocolController>`.
87    fn clone_box(&self) -> Box<dyn ProtocolController>;
88}
89
90/// Allow cloning `Box<dyn ProtocolController>`
91/// Uses `ProtocolController::clone_box` internally
92impl Clone for Box<dyn ProtocolController> {
93    fn clone(&self) -> Box<dyn ProtocolController> {
94        self.clone_box()
95    }
96}
97
98/// Protocol manager used to stop the protocol
99pub trait ProtocolManager {
100    /// Stop the protocol
101    /// Note that we do not take self by value to consume it
102    /// because it is not allowed to move out of Box<dyn ProtocolManager>
103    /// This will improve if the `unsized_fn_params` feature stabilizes enough to be safely usable.
104    fn stop(&mut self);
105}