dpp/block/extended_block_info/v0/
mod.rs

1use crate::block::block_info::BlockInfo;
2#[cfg(feature = "json-conversion")]
3use crate::serialization::json_safe_fields;
4
5use bincode::{Decode, Encode};
6use serde::{Deserialize, Serialize};
7
8/// Extended Block information
9#[cfg_attr(feature = "json-conversion", json_safe_fields)]
10#[derive(Clone, Debug, PartialEq, Eq, Encode, Decode, Serialize, Deserialize)]
11#[serde(rename_all = "camelCase")]
12pub struct ExtendedBlockInfoV0 {
13    /// Basic block info
14    pub basic_info: BlockInfo,
15    /// App hash
16    pub app_hash: [u8; 32],
17    /// Quorum Hash
18    pub quorum_hash: [u8; 32],
19    /// The block id hash
20    pub block_id_hash: [u8; 32],
21    /// The proposer pro_tx_hash
22    pub proposer_pro_tx_hash: [u8; 32],
23    /// Signature
24    #[serde(with = "signature_serializer")]
25    pub signature: [u8; 96],
26    /// Round
27    pub round: u32,
28}
29
30/// Trait for getting values from `ExtendedBlockInfoV0`
31pub trait ExtendedBlockInfoV0Getters {
32    /// Returns a reference to the basic block info.
33    fn basic_info(&self) -> &BlockInfo;
34
35    /// Returns a mutable reference to the basic block info.
36    fn basic_info_mut(&mut self) -> &mut BlockInfo;
37
38    /// Returns an owned copy of the basic block info.
39    fn basic_info_owned(self) -> BlockInfo;
40
41    /// Returns the app hash.
42    fn app_hash(&self) -> &[u8; 32];
43
44    /// Returns the quorum hash.
45    fn quorum_hash(&self) -> &[u8; 32];
46    /// Proposer pro tx hash.
47    fn proposer_pro_tx_hash(&self) -> &[u8; 32];
48    /// The block id hash
49    fn block_id_hash(&self) -> &[u8; 32];
50
51    /// Returns the signature.
52    fn signature(&self) -> &[u8; 96];
53
54    /// Returns the round.
55    fn round(&self) -> u32;
56}
57
58/// Trait for setting values in `ExtendedBlockInfoV0`
59pub trait ExtendedBlockInfoV0Setters {
60    /// Sets the basic block info.
61    fn set_basic_info(&mut self, info: BlockInfo);
62
63    /// Sets the app hash.
64    fn set_app_hash(&mut self, hash: [u8; 32]);
65
66    /// Sets the quorum hash.
67    fn set_quorum_hash(&mut self, hash: [u8; 32]);
68
69    /// Sets the signature.
70    fn set_signature(&mut self, signature: [u8; 96]);
71
72    /// Sets the round.
73    fn set_round(&mut self, round: u32);
74}
75
76impl ExtendedBlockInfoV0Getters for ExtendedBlockInfoV0 {
77    fn basic_info(&self) -> &BlockInfo {
78        &self.basic_info
79    }
80
81    fn basic_info_mut(&mut self) -> &mut BlockInfo {
82        &mut self.basic_info
83    }
84
85    fn basic_info_owned(self) -> BlockInfo {
86        self.basic_info
87    }
88
89    fn app_hash(&self) -> &[u8; 32] {
90        &self.app_hash
91    }
92
93    fn quorum_hash(&self) -> &[u8; 32] {
94        &self.quorum_hash
95    }
96
97    fn proposer_pro_tx_hash(&self) -> &[u8; 32] {
98        &self.proposer_pro_tx_hash
99    }
100
101    fn block_id_hash(&self) -> &[u8; 32] {
102        &self.block_id_hash
103    }
104
105    fn signature(&self) -> &[u8; 96] {
106        &self.signature
107    }
108
109    fn round(&self) -> u32 {
110        self.round
111    }
112}
113
114impl ExtendedBlockInfoV0Setters for ExtendedBlockInfoV0 {
115    fn set_basic_info(&mut self, info: BlockInfo) {
116        self.basic_info = info;
117    }
118
119    fn set_app_hash(&mut self, hash: [u8; 32]) {
120        self.app_hash = hash;
121    }
122
123    fn set_quorum_hash(&mut self, hash: [u8; 32]) {
124        self.quorum_hash = hash;
125    }
126
127    fn set_signature(&mut self, signature: [u8; 96]) {
128        self.signature = signature;
129    }
130
131    fn set_round(&mut self, round: u32) {
132        self.round = round;
133    }
134}
135
136mod signature_serializer {
137    use super::*;
138    use serde::de::Error;
139    use serde::{Deserializer, Serializer};
140
141    pub fn serialize<S>(signature: &[u8; 96], serializer: S) -> Result<S::Ok, S::Error>
142    where
143        S: Serializer,
144    {
145        serializer.serialize_bytes(signature)
146    }
147
148    pub fn deserialize<'de, D>(deserializer: D) -> Result<[u8; 96], D::Error>
149    where
150        D: Deserializer<'de>,
151    {
152        let buf: Vec<u8> = Deserialize::deserialize(deserializer)?;
153        if buf.len() != 96 {
154            return Err(Error::invalid_length(buf.len(), &"array of length 96"));
155        }
156        let mut arr = [0u8; 96];
157        arr.copy_from_slice(&buf);
158        Ok(arr)
159    }
160}