Skip to main content

hyli_model/
block.rs

1use crate::*;
2use borsh::{BorshDeserialize, BorshSerialize};
3use derive_more::derive::Display;
4use serde::{Deserialize, Serialize};
5use std::cmp::Ordering;
6
7#[derive(Debug, Serialize, Deserialize, Clone, BorshSerialize, BorshDeserialize, Display)]
8#[display("")]
9pub struct SignedBlock {
10    pub data_proposals: Vec<(LaneId, Vec<DataProposal>)>,
11    pub consensus_proposal: ConsensusProposal,
12    // NB: this can be different for different validators
13    pub certificate: AggregateSignature,
14}
15
16impl SignedBlock {
17    pub fn parent_hash(&self) -> &ConsensusProposalHash {
18        &self.consensus_proposal.parent_hash
19    }
20
21    pub fn height(&self) -> BlockHeight {
22        BlockHeight(self.consensus_proposal.slot)
23    }
24
25    pub fn has_txs(&self) -> bool {
26        for (_, _, txs) in self.iter_txs() {
27            if !txs.is_empty() {
28                return true;
29            }
30        }
31
32        false
33    }
34
35    pub fn count_txs(&self) -> usize {
36        self.iter_txs().map(|(_, _, txs)| txs.len()).sum()
37    }
38
39    pub fn iter_txs(&self) -> impl Iterator<Item = (LaneId, DataProposalHash, &Vec<Transaction>)> {
40        self.data_proposals
41            .iter()
42            .flat_map(|(lane_id, dps)| std::iter::zip(std::iter::repeat(lane_id.clone()), dps))
43            .map(|(lane_id, dp)| {
44                (
45                    lane_id.clone(),
46                    dp.parent_data_proposal_hash.as_tx_parent_hash(),
47                    &dp.txs,
48                )
49            })
50    }
51
52    pub fn iter_txs_with_id(&self) -> impl Iterator<Item = (LaneId, TxId, &Transaction)> {
53        self.iter_txs().flat_map(move |(lane_id, dp_hash, txs)| {
54            txs.iter()
55                .map(move |tx| (lane_id.clone(), TxId(dp_hash.clone(), tx.hashed()), tx))
56        })
57    }
58}
59
60impl Hashed<ConsensusProposalHash> for SignedBlock {
61    fn hashed(&self) -> ConsensusProposalHash {
62        self.consensus_proposal.hashed()
63    }
64}
65
66impl Ord for SignedBlock {
67    fn cmp(&self, other: &Self) -> Ordering {
68        self.height().0.cmp(&other.height().0)
69    }
70}
71
72impl PartialOrd for SignedBlock {
73    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
74        Some(self.cmp(other))
75    }
76}
77
78impl PartialEq for SignedBlock {
79    fn eq(&self, other: &Self) -> bool {
80        self.hashed() == other.hashed()
81    }
82}
83
84impl Eq for SignedBlock {}
85
86impl std::default::Default for SignedBlock {
87    fn default() -> Self {
88        SignedBlock {
89            consensus_proposal: ConsensusProposal::default(),
90            data_proposals: vec![],
91            certificate: AggregateSignature {
92                signature: crate::Signature("signature".into()),
93                validators: vec![],
94            },
95        }
96    }
97}