exocore-chain 0.1.23

Storage of Exocore (Distributed applications framework)
Documentation
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
use std::{
    cmp::Ordering,
    collections::{HashMap, HashSet},
    str::FromStr,
};

use exocore_core::{
    cell::{Cell, CellNodeRole, CellNodes, Node, NodeId},
    sec::signature::Signature,
    time::{Clock, ConsistentTimestamp},
};
use exocore_protos::generated::data_chain_capnp::chain_operation;
use itertools::Itertools;

use crate::{
    block::{Block, BlockOffset},
    chain,
    engine::EngineError,
    operation::{GroupId, OperationId, OperationType},
    pending, CommitManagerConfig,
};

/// Structure that contains information on the pending store and blocks in it.
/// It is used by the commit manager to know if it needs to propose, sign,
/// commit blocks
pub struct PendingBlocks {
    pub blocks: HashMap<GroupId, PendingBlock>,
    pub blocks_status: HashMap<GroupId, BlockStatus>,
    pub operations_blocks: HashMap<OperationId, HashSet<GroupId>>,
    pub entries_operations_count: usize,
}

impl PendingBlocks {
    pub fn new<PS: pending::PendingStore, CS: chain::ChainStore>(
        config: &CommitManagerConfig,
        clock: &Clock,
        cell: &Cell,
        pending_store: &PS,
        chain_store: &CS,
    ) -> Result<PendingBlocks, EngineError> {
        let local_node = cell.local_node();
        let now = clock.consistent_time(local_node.node());
        let last_stored_block = chain_store
            .get_last_block()?
            .ok_or(EngineError::UninitializedChain)?;

        debug!(
            "{}: Checking for pending blocks. last_block_offset={} next_offset={}",
            cell,
            last_stored_block.offset(),
            last_stored_block.next_offset(),
        );

        // first pass to fetch all groups proposal
        let mut groups_id = Vec::new();
        let mut entries_operations_count = 0;
        for pending_op in pending_store.operations_iter(..)? {
            match pending_op.operation_type {
                OperationType::BlockPropose => {
                    groups_id.push(pending_op.operation_id);
                }
                OperationType::Entry => {
                    entries_operations_count += 1;
                }
                _ => {}
            }
        }

        // then we get all operations for each block proposal
        let mut blocks = HashMap::<OperationId, PendingBlock>::new();
        for group_id in groups_id.iter_mut() {
            let group_operations = if let Some(group_operations) =
                pending_store.get_group_operations(*group_id)?
            {
                group_operations
            } else {
                warn!(
                    "Didn't have any operations for block proposal with group_id={}, which shouldn't be possible",
                    group_id
                );
                continue;
            };

            let mut operations = Vec::new();
            let mut proposal: Option<PendingBlockProposal> = None;
            let mut signatures = Vec::new();
            let mut refusals = Vec::new();

            for operation in group_operations.operations {
                let operation_reader = operation.frame.get_reader()?;

                match operation_reader.get_operation().which()? {
                    chain_operation::operation::Which::BlockPropose(reader) => {
                        let block_frame = crate::block::read_header_frame(reader?.get_block()?)?;
                        let block_header_reader = block_frame.get_reader()?;
                        for operation_header in block_header_reader.get_operations_header()? {
                            operations.push(operation_header.get_operation_id());
                        }

                        let node_id_str = operation_reader.get_node_id()?;
                        let node_id = NodeId::from_str(node_id_str)
                            .map_err(|_| anyhow!("Couldn't convert to NodeID: {}", node_id_str))?;
                        let node = cell.nodes().get(&node_id).map(|cn| cn.node().clone());

                        proposal = Some(PendingBlockProposal {
                            node,
                            offset: block_header_reader.get_offset(),
                            operation,
                        })
                    }
                    chain_operation::operation::Which::BlockSign(_reader) => {
                        signatures.push(PendingBlockSignature::from_operation(operation_reader)?);
                    }
                    chain_operation::operation::Which::BlockRefuse(_reader) => {
                        refusals.push(PendingBlockRefusal::from_operation(operation_reader)?);
                    }
                    chain_operation::operation::Which::Entry(_) => {
                        warn!("Found a non-block related operation in block group, which shouldn't be possible (group_id={})", group_id);
                    }
                };
            }

            let proposal = proposal.expect("no proposal operation for group of the proposal");

            let nodes = cell.nodes();
            let has_my_refusal = refusals.iter().any(|sig| sig.node_id == *local_node.id());
            let has_my_signature = signatures.iter().any(|sig| sig.node_id == *local_node.id());
            let has_sigs_quorum = nodes.has_quorum(signatures.len(), Some(CellNodeRole::Chain));
            let has_refusal_quorum = nodes.has_quorum(refusals.len(), Some(CellNodeRole::Chain));
            let has_expired = proposal.has_expired(config, now);

            let status = match chain_store.get_block(proposal.offset) {
                Err(err) if err.is_fatal() => {
                    return Err(err.into());
                }
                Ok(block) => {
                    if block.get_proposed_operation_id()? == *group_id {
                        // we found the block and it has the same operation id, so it's valid past
                        // block
                        BlockStatus::PastCommitted
                    } else if has_sigs_quorum {
                        // we found a different block at the offset, and it had quorum. it means we
                        // diverged
                        BlockStatus::PastDiverged
                    } else {
                        // another proposal for the same block offset was made, but not accepted
                        BlockStatus::PastRefused
                    }
                }
                _ => {
                    let expected_next_offset = last_stored_block.next_offset();
                    if has_refusal_quorum || has_my_refusal {
                        BlockStatus::NextRefused
                    } else if has_expired {
                        BlockStatus::NextExpired
                    } else if proposal.offset < expected_next_offset {
                        // means it was a proposed block for a diverged chain
                        BlockStatus::PastRefused
                    } else if proposal.offset >= expected_next_offset {
                        BlockStatus::NextPotential
                    } else {
                        BlockStatus::NextRefused
                    }
                }
            };

            let pending_block = PendingBlock {
                group_id: *group_id,
                status,

                proposal,
                refusals,
                signatures,

                has_my_refusal,
                has_my_signature,

                operations,
            };

            debug!("{}: Found new pending block: {:?}", cell, pending_block);
            blocks.insert(*group_id, pending_block);
        }

        let operations_blocks = Self::map_operations_blocks(&blocks);
        let blocks_status = Self::map_blocks_status(&blocks);

        Ok(PendingBlocks {
            blocks,
            blocks_status,
            operations_blocks,
            entries_operations_count,
        })
    }

    pub fn get_block(&self, block_op_id: &OperationId) -> &PendingBlock {
        self.blocks
            .get(block_op_id)
            .expect("Couldn't find block in map")
    }

    pub fn get_block_mut(&mut self, block_op_id: &OperationId) -> &mut PendingBlock {
        self.blocks
            .get_mut(block_op_id)
            .expect("Couldn't find block in map")
    }

    pub fn map_operations_blocks(
        pending_blocks: &HashMap<OperationId, PendingBlock>,
    ) -> HashMap<OperationId, HashSet<OperationId>> {
        let mut operations_blocks: HashMap<OperationId, HashSet<OperationId>> = HashMap::new();
        for block in pending_blocks.values() {
            for operation_id in &block.operations {
                let operation = operations_blocks
                    .entry(*operation_id)
                    .or_insert_with(HashSet::new);
                operation.insert(block.group_id);
            }
        }
        operations_blocks
    }

    pub fn map_blocks_status(
        pending_blocks: &HashMap<OperationId, PendingBlock>,
    ) -> HashMap<OperationId, BlockStatus> {
        let mut blocks_status = HashMap::new();
        for (block_group_id, block) in pending_blocks {
            blocks_status.insert(*block_group_id, block.status);
        }
        blocks_status
    }

    pub fn potential_next_blocks(&self) -> Vec<&PendingBlock> {
        // we sort potential next blocks by which block has better potential to become a
        // block
        self.blocks
            .values()
            .filter(|block| block.status == BlockStatus::NextPotential)
            .sorted_by(|a, b| PendingBlock::compare_potential_next_block(a, b).reverse())
            .collect()
    }
}

/// Information about a block in the pending store.
///
/// This block could be a past block (committed to chain or refused), which will
/// eventually be cleaned up, or could be a next potential or refused block.
pub struct PendingBlock {
    pub group_id: OperationId,
    pub status: BlockStatus,

    pub proposal: PendingBlockProposal,
    pub refusals: Vec<PendingBlockRefusal>,
    pub signatures: Vec<PendingBlockSignature>,
    pub has_my_refusal: bool,
    pub has_my_signature: bool,

    pub operations: Vec<OperationId>,
}

impl PendingBlock {
    pub fn add_my_signature(&mut self, signature: PendingBlockSignature) {
        self.signatures.push(signature);
        self.has_my_signature = true;
    }

    pub fn add_my_refusal(&mut self, refusal: PendingBlockRefusal) {
        self.refusals.push(refusal);
        self.has_my_refusal = true;
    }

    pub fn validate_signature(&self, cell: &Cell, signature: &PendingBlockSignature) -> bool {
        let nodes = cell.nodes();
        let node = if let Some(cell_node) = nodes.get(&signature.node_id) {
            cell_node.node()
        } else {
            return false;
        };

        let block = if let Ok(block) = self.proposal.get_block() {
            block
        } else {
            return false;
        };

        let signature_data = block.inner().inner().multihash_bytes();
        signature.signature.validate(node, signature_data)
    }

    pub fn compare_potential_next_block(a: &PendingBlock, b: &PendingBlock) -> Ordering {
        if a.has_my_signature {
            return Ordering::Greater;
        } else if b.has_my_signature {
            return Ordering::Less;
        }

        match a.signatures.len().cmp(&b.signatures.len()) {
            o @ Ordering::Greater => return o,
            o @ Ordering::Less => return o,
            Ordering::Equal => {}
        }

        // fallback to operation id, which is time ordered
        if a.group_id < b.group_id {
            Ordering::Greater
        } else {
            Ordering::Less
        }
    }
}

impl std::fmt::Debug for PendingBlock {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        let mut d = f.debug_struct("PendingBlock");

        d.field("offset", &self.proposal.offset)
            .field("group_id", &self.group_id)
            .field("status", &self.status)
            .field("nb_signatures", &self.signatures.len())
            .field("has_my_signature", &self.has_my_signature)
            .field("has_my_refusal", &self.has_my_refusal);

        if let Some(node) = &self.proposal.node {
            d.field("node", &node.to_string());
        }

        d.finish()
    }
}

#[derive(PartialEq, Debug, Clone, Copy)]
pub enum BlockStatus {
    PastRefused,
    PastCommitted,
    PastDiverged,
    NextExpired,
    NextPotential,
    NextRefused,
}

/// Block proposal wrapper
pub struct PendingBlockProposal {
    pub node: Option<Node>,
    pub offset: BlockOffset,
    pub operation: pending::StoredOperation,
}

impl PendingBlockProposal {
    pub fn get_block(&self) -> Result<crate::block::BlockHeaderFrame<&[u8]>, EngineError> {
        let operation_reader = self.operation.frame.get_reader()?;
        let inner_operation = operation_reader.get_operation();
        match inner_operation.which()? {
            chain_operation::operation::Which::BlockPropose(block_prop) => {
                Ok(crate::block::read_header_frame(block_prop?.get_block()?)?)
            }
            _ => Err(anyhow!(
                "Expected block sign pending op to create block signature, but got something else"
            )
            .into()),
        }
    }

    pub fn has_expired(&self, config: &CommitManagerConfig, now: ConsistentTimestamp) -> bool {
        let op_time = ConsistentTimestamp::from(self.operation.operation_id);
        (now - op_time).map_or(false, |elapsed| elapsed >= config.block_proposal_timeout)
    }
}

/// Block refusal wrapper
pub struct PendingBlockRefusal {
    pub node_id: NodeId,
}

impl PendingBlockRefusal {
    pub fn from_operation(
        operation_reader: chain_operation::Reader,
    ) -> Result<PendingBlockRefusal, EngineError> {
        let inner_operation = operation_reader.get_operation();
        match inner_operation.which()? {
            chain_operation::operation::Which::BlockRefuse(_sig) => {
                let node_id_str = operation_reader.get_node_id()?;
                let node_id = NodeId::from_str(node_id_str)
                    .map_err(|_| anyhow!("Couldn't convert to NodeID: {}", node_id_str))?;
                Ok(PendingBlockRefusal { node_id })
            }
            _ => Err(anyhow!(
                "Expected block refuse pending op to create block refusal, but got something else"
            )
            .into()),
        }
    }
}

/// Block signature wrapper
pub struct PendingBlockSignature {
    pub node_id: NodeId,
    pub signature: Signature,
}

impl PendingBlockSignature {
    pub fn from_operation(
        operation_reader: chain_operation::Reader,
    ) -> Result<PendingBlockSignature, EngineError> {
        let inner_operation = operation_reader.get_operation();
        match inner_operation.which()? {
            chain_operation::operation::Which::BlockSign(sig) => {
                let op_signature_reader = sig?;
                let signature_reader = op_signature_reader.get_signature()?;

                let node_id_str = operation_reader.get_node_id()?;
                let node_id = NodeId::from_str(node_id_str)
                    .map_err(|_| anyhow!("Couldn't convert to NodeID: {}", node_id_str))?;
                let signature = Signature::from_bytes(signature_reader.get_node_signature()?);

                Ok(PendingBlockSignature { node_id, signature })
            }
            _ => Err(anyhow!(
                "Expected block sign pending op to create block signature, but got something else"
            )
            .into()),
        }
    }
}