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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
use std::collections::VecDeque;

use massa_consensus_exports::{
    block_status::{BlockStatus, DiscardReason},
    error::ConsensusError,
};
use massa_logging::massa_trace;
use massa_models::{
    block_id::{BlockId, BlockIdSerializer},
    clique::Clique,
    prehash::PreHashSet,
    slot::Slot,
};
use massa_serialization::Serializer;

use super::ConsensusState;

impl ConsensusState {
    pub fn insert_parents_descendants(
        &mut self,
        add_block_id: BlockId,
        add_block_slot: Slot,
        parents_hash: Vec<BlockId>,
    ) {
        // add as child to parents
        for parent_h in parents_hash.iter() {
            if let Some(BlockStatus::Active {
                a_block: a_parent, ..
            }) = self.blocks_state.get_mut(parent_h)
            {
                a_parent.children[add_block_slot.thread as usize]
                    .insert(add_block_id, add_block_slot.period);
            }
        }

        // add same-thread creator parent
        let same_thread_parent_creator =
            parents_hash
                .get(add_block_slot.thread as usize)
                .and_then(|parent_id| {
                    if let Some(BlockStatus::Active { a_block, .. }) =
                        self.blocks_state.get(parent_id)
                    {
                        Some(a_block.creator_address)
                    } else {
                        None
                    }
                });
        if let Some(BlockStatus::Active { a_block, .. }) = self.blocks_state.get_mut(&add_block_id)
        {
            a_block.same_thread_parent_creator = same_thread_parent_creator;
        } else {
            panic!("block status should be active");
        };

        // add as descendant to ancestors if not final
        let mut ancestors: VecDeque<BlockId> = parents_hash.iter().copied().collect();
        let mut visited = PreHashSet::<BlockId>::default();
        while let Some(ancestor_h) = ancestors.pop_back() {
            if !visited.insert(ancestor_h) {
                continue;
            }
            if let Some(BlockStatus::Active { a_block: ab, .. }) =
                self.blocks_state.get_mut(&ancestor_h)
            {
                // No need to add descendants if ancestor is final,
                // because only non-final active blocks are scanned for descendents for finality detection.
                if ab.is_final {
                    continue;
                }
                ab.descendants.insert(add_block_id);
                for (ancestor_parent_h, _) in ab.parents.iter() {
                    ancestors.push_front(*ancestor_parent_h);
                }
            }
        }
    }

    pub fn compute_fitness_find_blockclique(
        &mut self,
        add_block_id: &BlockId,
    ) -> Result<usize, ConsensusError> {
        let block_id_serializer = BlockIdSerializer::new();
        let mut blockclique_i = 0usize;
        let mut max_clique_fitness = (0u64, num::BigInt::default());
        for (clique_i, clique) in self.max_cliques.iter_mut().enumerate() {
            clique.fitness = 0;
            clique.is_blockclique = false;
            let mut sum_hash = num::BigInt::default();
            for block_h in clique.block_ids.iter() {
                let fitness = match self.blocks_state.get(block_h) {
                    Some(BlockStatus::Active { a_block, .. }) => a_block.fitness,
                    _ => return Err(ConsensusError::ContainerInconsistency(format!("inconsistency inside block statuses computing fitness while adding {} - missing {}", add_block_id, block_h))),
                };
                clique.fitness = clique
                    .fitness
                    .checked_add(fitness)
                    .ok_or(ConsensusError::FitnessOverflow)?;
                let mut bytes = Vec::new();
                block_id_serializer
                    .serialize(block_h, &mut bytes)
                    .map_err(|err| ConsensusError::SerializationError(err.to_string()))?;
                sum_hash -= num::BigInt::from_bytes_be(num::bigint::Sign::Plus, &bytes);
            }
            let cur_fit = (clique.fitness, sum_hash);
            if cur_fit > max_clique_fitness {
                blockclique_i = clique_i;
                max_clique_fitness = cur_fit;
            }
        }
        self.max_cliques[blockclique_i].is_blockclique = true;
        Ok(blockclique_i)
    }

    pub fn list_stale_blocks(&self, fitness_threshold: u64) -> PreHashSet<BlockId> {
        // iterate from largest to smallest to minimize reallocations
        let mut indices: Vec<usize> = (0..self.max_cliques.len()).collect();
        indices.sort_unstable_by_key(|&i| std::cmp::Reverse(self.max_cliques[i].block_ids.len()));
        let mut high_set = PreHashSet::<BlockId>::default();
        let mut low_set = PreHashSet::<BlockId>::default();
        for clique_i in indices.into_iter() {
            if self.max_cliques[clique_i].fitness >= fitness_threshold {
                high_set.extend(&self.max_cliques[clique_i].block_ids);
            } else {
                low_set.extend(&self.max_cliques[clique_i].block_ids);
            }
        }
        &low_set - &high_set
    }

    pub fn remove_block(&mut self, add_block_id: &BlockId, block_id: &BlockId) {
        let sequence_number = self.blocks_state.sequence_counter();
        self.blocks_state.transition_map(block_id, |block_status, block_statuses| {
        if let Some(BlockStatus::Active {
            a_block: active_block,
            ..
        }) = block_status
        {
            if active_block.is_final {
               panic!("inconsistency inside block statuses removing stale blocks adding {} - block {} was already final", add_block_id, block_id);
            }

            // remove from gi_head
            if let Some(other_incomps) = self.gi_head.remove(block_id) {
                for other_incomp in other_incomps.into_iter() {
                    if let Some(other_incomp_lst) = self.gi_head.get_mut(&other_incomp) {
                        other_incomp_lst.remove(block_id);
                    }
                }
            }

            // remove from cliques
            let stale_block_fitness = active_block.fitness;
            self.max_cliques.iter_mut().for_each(|c| {
                if c.block_ids.remove(block_id) {
                    c.fitness -= stale_block_fitness;
                }
            });
            self.max_cliques.retain(|c| !c.block_ids.is_empty()); // remove empty cliques
            if self.max_cliques.is_empty() {
                // make sure at least one clique remains
                self.max_cliques = vec![Clique {
                    block_ids: PreHashSet::<BlockId>::default(),
                    fitness: 0,
                    is_blockclique: true,
                }];
            }

            // remove from parent's children
            for (parent_h, _parent_period) in active_block.parents.iter() {
                if let Some(BlockStatus::Active {
                    a_block: parent_active_block,
                    ..
                }) = block_statuses.get_mut(parent_h)
                {
                    parent_active_block.children[active_block.slot.thread as usize]
                        .remove(block_id);
                }
            }

            massa_trace!("consensus.block_graph.add_block_to_graph.stale", {
                "hash": block_id
            });

            // mark as stale
            self.new_stale_blocks
                .insert(*block_id, (active_block.creator_address, active_block.slot));
            Some(
                BlockStatus::Discarded {
                    slot: active_block.slot,
                    creator: active_block.creator_address,
                    parents: active_block.parents.iter().map(|(h, _)| *h).collect(),
                    reason: DiscardReason::Stale,
                    sequence_number,
                }
            )
        } else {
            panic!("inconsistency inside block statuses removing stale blocks adding {} - block {} is missing", add_block_id, block_id);
        }
    });
    }

    pub fn list_final_blocks(&self) -> Result<PreHashSet<BlockId>, ConsensusError> {
        // short-circuiting intersection of cliques from smallest to largest
        let mut indices: Vec<usize> = (0..self.max_cliques.len()).collect();
        indices.sort_unstable_by_key(|&i| self.max_cliques[i].block_ids.len());
        let mut indices_iter = indices.iter();
        let mut final_candidates = self.max_cliques
            [*indices_iter.next().expect("expected at least one clique")]
        .block_ids
        .clone();
        for i in indices_iter {
            final_candidates.retain(|v| self.max_cliques[*i].block_ids.contains(v));
            if final_candidates.is_empty() {
                break;
            }
        }

        // restrict search to cliques with high enough fitness, sort cliques by fitness (highest to lowest)
        massa_trace!(
            "consensus.block_graph.add_block_to_graph.list_final_blocks.restrict",
            {}
        );
        indices.retain(|&i| self.max_cliques[i].fitness > self.config.delta_f0);
        indices.sort_unstable_by_key(|&i| std::cmp::Reverse(self.max_cliques[i].fitness));

        let mut final_blocks = PreHashSet::<BlockId>::default();
        for clique_i in indices.into_iter() {
            massa_trace!(
                "consensus.block_graph.add_block_to_graph.list_final_blocks.loop",
                { "clique_i": clique_i }
            );
            // check in cliques from highest to lowest fitness
            if final_candidates.is_empty() {
                // no more final candidates
                break;
            }
            let clique = &self.max_cliques[clique_i];

            // compute the total fitness of all the descendants of the candidate within the clique
            let loc_candidates = final_candidates.clone();
            for candidate_h in loc_candidates.into_iter() {
                let descendants = match self.blocks_state.get(&candidate_h) {
                    Some(BlockStatus::Active { a_block, .. }) => &a_block.descendants,
                    _ => {
                        return Err(ConsensusError::MissingBlock(format!(
                            "missing block when computing total fitness of descendants: {}",
                            candidate_h
                        )))
                    }
                };
                let desc_fit: u64 = descendants
                    .intersection(&clique.block_ids)
                    .map(|h| {
                        if let Some(BlockStatus::Active { a_block: ab, .. }) =
                            self.blocks_state.get(h)
                        {
                            return ab.fitness;
                        }
                        0
                    })
                    .sum();
                if desc_fit > self.config.delta_f0 {
                    // candidate is final
                    final_candidates.remove(&candidate_h);
                    final_blocks.insert(candidate_h);
                }
            }
        }
        Ok(final_blocks)
    }

    /// get the clique of higher fitness
    pub fn get_blockclique(&self) -> PreHashSet<BlockId> {
        self.max_cliques
            .iter()
            .find(|c| c.is_blockclique)
            .expect("blockclique missing")
            .block_ids
            .clone()
    }

    pub fn mark_final_blocks(
        &mut self,
        add_block_id: &BlockId,
        final_blocks: PreHashSet<BlockId>,
    ) -> Result<(), ConsensusError> {
        for block_id in final_blocks.into_iter() {
            // remove from gi_head
            if let Some(other_incomps) = self.gi_head.remove(&block_id) {
                for other_incomp in other_incomps.into_iter() {
                    if let Some(other_incomp_lst) = self.gi_head.get_mut(&other_incomp) {
                        other_incomp_lst.remove(&block_id);
                    }
                }
            }

            // mark as final and update latest_final_blocks_periods
            if let Some(BlockStatus::Active {
                a_block: final_block,
                ..
            }) = self.blocks_state.get_mut(&block_id)
            {
                massa_trace!("consensus.block_graph.add_block_to_graph.final", {
                    "hash": block_id
                });
                final_block.is_final = true;
                // remove from cliques
                let final_block_fitness = final_block.fitness;
                self.max_cliques.iter_mut().for_each(|c| {
                    if c.block_ids.remove(&block_id) {
                        c.fitness -= final_block_fitness;
                    }
                });
                self.max_cliques.retain(|c| !c.block_ids.is_empty()); // remove empty cliques
                if self.max_cliques.is_empty() {
                    // make sure at least one clique remains
                    self.max_cliques = vec![Clique {
                        block_ids: PreHashSet::<BlockId>::default(),
                        fitness: 0,
                        is_blockclique: true,
                    }];
                }
                // update latest final blocks
                if final_block.slot.period
                    > self.latest_final_blocks_periods[final_block.slot.thread as usize].1
                {
                    self.latest_final_blocks_periods[final_block.slot.thread as usize] =
                        (block_id, final_block.slot.period);
                }
                // update new final blocks list
                self.new_final_blocks.insert(block_id);
            } else {
                return Err(ConsensusError::ContainerInconsistency(format!("inconsistency inside block statuses updating final blocks adding {} - block {} is missing", add_block_id, block_id)));
            }
        }
        Ok(())
    }
}