1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright (c) 2022 MASSA LABS <info@massa.net>

//! Pool controller implementation

use massa_models::{
    block_id::BlockId, denunciation::Denunciation, denunciation::DenunciationPrecursor,
    endorsement::EndorsementId, operation::OperationId, slot::Slot,
};
use massa_pool_exports::{PoolConfig, PoolController, PoolManager};
use massa_storage::Storage;
use parking_lot::RwLock;
use std::sync::mpsc::TrySendError;
use std::sync::{mpsc::SyncSender, Arc};
use tracing::{info, warn};

use crate::{
    denunciation_pool::DenunciationPool, endorsement_pool::EndorsementPool,
    operation_pool::OperationPool,
};

/// A generic command to send commands to a pool
#[allow(clippy::large_enum_variant)]
pub enum Command {
    /// Add items to the pool
    AddItems(Storage),
    /// Add denunciation precursor to the pool
    AddDenunciationPrecursor(DenunciationPrecursor),
    /// Notify of new final consensus periods
    NotifyFinalCsPeriods(Vec<u64>),
    /// Stop the worker
    Stop,
}

/// Pool controller
#[derive(Clone)]
pub struct PoolControllerImpl {
    /// Config
    pub(crate) _config: PoolConfig,
    /// Shared reference to the operation pool
    pub(crate) operation_pool: Arc<RwLock<OperationPool>>,
    /// Shared reference to the endorsement pool
    pub(crate) endorsement_pool: Arc<RwLock<EndorsementPool>>,
    /// Shared reference to the denunciation pool
    pub(crate) denunciation_pool: Arc<RwLock<DenunciationPool>>,
    /// Operation write worker command sender
    pub(crate) operations_input_sender: SyncSender<Command>,
    /// Endorsement write worker command sender
    pub(crate) endorsements_input_sender: SyncSender<Command>,
    /// Denunciation write worker command sender
    pub(crate) denunciations_input_sender: SyncSender<Command>,
    /// Last final periods from Consensus
    pub last_cs_final_periods: Vec<u64>,
}

impl PoolController for PoolControllerImpl {
    /// Asynchronously add operations to pool. Simply print a warning on failure.
    fn add_operations(&mut self, ops: Storage) {
        match self
            .operations_input_sender
            .try_send(Command::AddItems(ops))
        {
            Err(TrySendError::Disconnected(_)) => {
                warn!("Could not add operations to pool: worker is unreachable.");
            }
            Err(TrySendError::Full(_)) => {
                warn!("Could not add operations to pool: worker channel is full.");
            }
            Ok(_) => {}
        }
    }

    /// Asynchronously add endorsements to pool. Simply print a warning on failure.
    fn add_endorsements(&mut self, endorsements: Storage) {
        // Send endorsements to the denunciation pool - so we got unfiltered endorsements
        // from protocol & endorsement factory
        match self
            .denunciations_input_sender
            .try_send(Command::AddItems(endorsements.clone()))
        {
            Err(TrySendError::Disconnected(_)) => {
                warn!("Could not add endorsements to pool: worker is unreachable.");
            }
            Err(TrySendError::Full(_)) => {
                warn!("Could not add endorsements to pool: worker channel is full.");
            }
            Ok(_) => {}
        }

        // Now send endorsements to endorsement pool - storage is cleaned up
        match self
            .endorsements_input_sender
            .try_send(Command::AddItems(endorsements))
        {
            Err(TrySendError::Disconnected(_)) => {
                warn!("Could not add endorsements to pool: worker is unreachable.");
            }
            Err(TrySendError::Full(_)) => {
                warn!("Could not add endorsements to pool: worker channel is full.");
            }
            Ok(_) => {}
        }
    }

    /// Add denunciation precursor to pool
    fn add_denunciation_precursor(&self, denunciation_precursor: DenunciationPrecursor) {
        match self
            .denunciations_input_sender
            .try_send(Command::AddDenunciationPrecursor(denunciation_precursor))
        {
            Err(TrySendError::Disconnected(_)) => {
                warn!("Could not add denunciation precursor to pool: worker is unreachable.");
            }
            Err(TrySendError::Full(_)) => {
                warn!("Could not add denunciation precursor to pool: worker channel is full.");
            }
            Ok(_) => {}
        }
    }

    /// Asynchronously notify of new final consensus periods. Simply print a warning on failure.
    fn notify_final_cs_periods(&mut self, final_cs_periods: &[u64]) {
        self.last_cs_final_periods = final_cs_periods.to_vec();

        match self
            .operations_input_sender
            .try_send(Command::NotifyFinalCsPeriods(final_cs_periods.to_vec()))
        {
            Err(TrySendError::Disconnected(_)) => {
                warn!("Could not notify operation pool of new final slots: worker is unreachable.");
            }
            Err(TrySendError::Full(_)) => {
                warn!(
                    "Could not notify operation pool of new final slots: worker channel is full."
                );
            }
            Ok(_) => {}
        }

        match self
            .endorsements_input_sender
            .try_send(Command::NotifyFinalCsPeriods(final_cs_periods.to_vec()))
        {
            Err(TrySendError::Disconnected(_)) => {
                warn!(
                    "Could not notify endorsement pool of new final slots: worker is unreachable."
                );
            }
            Err(TrySendError::Full(_)) => {
                warn!(
                    "Could not notify endorsement pool of new final slots: worker channel is full."
                );
            }
            Ok(_) => {}
        }

        match self
            .denunciations_input_sender
            .try_send(Command::NotifyFinalCsPeriods(final_cs_periods.to_vec()))
        {
            Err(TrySendError::Disconnected(_)) => {
                warn!(
                    "Could not notify endorsement pool of new final slots: worker is unreachable."
                );
            }
            Err(TrySendError::Full(_)) => {
                warn!(
                    "Could not notify endorsement pool of new final slots: worker channel is full."
                );
            }
            Ok(_) => {}
        }
    }

    /// get operations for block creation
    fn get_block_operations(&self, slot: &Slot) -> (Vec<OperationId>, Storage) {
        self.operation_pool.read().get_block_operations(slot)
    }

    /// get endorsements for a block
    fn get_block_endorsements(
        &self,
        target_block: &BlockId,
        target_slot: &Slot,
    ) -> (Vec<Option<EndorsementId>>, Storage) {
        self.endorsement_pool
            .read()
            .get_block_endorsements(target_slot, target_block)
    }

    /// get denunciationsq for a block
    fn get_block_denunciations(&self, target_slot: &Slot) -> Vec<Denunciation> {
        self.denunciation_pool
            .read()
            .get_block_denunciations(target_slot)
    }

    /// Get the number of endorsements in the pool
    fn get_endorsement_count(&self) -> usize {
        self.endorsement_pool.read().len()
    }

    /// Get the number of operations in the pool
    fn get_operation_count(&self) -> usize {
        self.operation_pool.read().len()
    }

    /// Check if the pool contains a list of endorsements. Returns one boolean per item.
    fn contains_endorsements(&self, endorsements: &[EndorsementId]) -> Vec<bool> {
        let lck = self.endorsement_pool.read();
        endorsements.iter().map(|id| lck.contains(id)).collect()
    }

    /// Check if the pool contains a list of operations. Returns one boolean per item.
    fn contains_operations(&self, operations: &[OperationId]) -> Vec<bool> {
        let lck = self.operation_pool.read();
        operations.iter().map(|id| lck.contains(id)).collect()
    }

    /// Get the number of denunciations in the pool
    fn get_denunciation_count(&self) -> usize {
        self.denunciation_pool.read().len()
    }

    /// Returns a boxed clone of self.
    /// Allows cloning `Box<dyn PoolController>`,
    fn clone_box(&self) -> Box<dyn PoolController> {
        Box::new(self.clone())
    }

    /// Get final consensus periods
    fn get_final_cs_periods(&self) -> Vec<u64> {
        self.last_cs_final_periods.clone()
    }
}

/// Implementation of the pool manager.
///
/// Contains the operations and endorsements thread handles.
pub struct PoolManagerImpl {
    /// Handle used to join the operation thread
    pub(crate) operations_thread_handle: Option<std::thread::JoinHandle<()>>,
    /// Handle used to join the endorsement thread
    pub(crate) endorsements_thread_handle: Option<std::thread::JoinHandle<()>>,
    /// Handle used to join the denunciation thread
    pub(crate) denunciations_thread_handle: Option<std::thread::JoinHandle<()>>,
    /// Operations input data mpsc (used to stop the pool thread)
    pub(crate) operations_input_sender: SyncSender<Command>,
    /// Endorsements input data mpsc (used to stop the pool thread)
    pub(crate) endorsements_input_sender: SyncSender<Command>,
    /// Denunciations input data mpsc (used to stop the pool thread)
    pub(crate) denunciations_input_sender: SyncSender<Command>,
}

impl PoolManager for PoolManagerImpl {
    /// Stops the worker
    fn stop(&mut self) {
        info!("stopping pool workers...");
        let _ = self.operations_input_sender.send(Command::Stop);
        let _ = self.endorsements_input_sender.send(Command::Stop);
        let _ = self.denunciations_input_sender.send(Command::Stop);
        if let Some(join_handle) = self.operations_thread_handle.take() {
            join_handle
                .join()
                .expect("operations pool thread panicked on try to join");
        }
        if let Some(join_handle) = self.endorsements_thread_handle.take() {
            join_handle
                .join()
                .expect("endorsements pool thread panicked on try to join");
        }
        if let Some(join_handle) = self.denunciations_thread_handle.take() {
            join_handle
                .join()
                .expect("denunciations pool thread panicked on try to join");
        }
        info!("pool workers stopped");
    }
}