catbuffer_rust/
block_header_builder.rs

1/*
2 * // Copyright (c) 2016-2019, Jaguar0625, gimre, BloodyRookie, Tech Bureau, Corp.
3 * // Copyright (c) 2020-present, Jaguar0625, gimre, BloodyRookie.
4 * // All rights reserved.
5 * //
6 * // This file is part of Catapult.
7 * //
8 * // Catapult is free software: you can redistribute it and/or modify
9 * // it under the terms of the GNU Lesser General Public License as published by
10 * // the Free Software Foundation, either version 3 of the License, or
11 * // (at your option) any later version.
12 * //
13 * // Catapult is distributed in the hope that it will be useful,
14 * // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * // GNU Lesser General Public License for more details.
17 * //
18 * // You should have received a copy of the GNU Lesser General Public License
19 * // along with Catapult. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22use super::address_dto::*;
23use super::block_fee_multiplier_dto::*;
24use super::difficulty_dto::*;
25use super::entity_type_dto::*;
26use super::generator_utils::*;
27use super::hash256_dto::*;
28use super::height_dto::*;
29use super::key_dto::*;
30use super::network_type_dto::*;
31use super::signature_dto::*;
32use super::timestamp_dto::*;
33use super::vrf_proof_builder::*;
34
35/// Binary layout for a block header.
36#[derive(Debug, Clone)]
37pub struct BlockHeaderBuilder {
38    /// Entity signature.
39    signature: SignatureDto,
40    /// Entity signer's public key.
41    signer_public_key: KeyDto,
42    /// Entity version.
43    version: u8,
44    /// Entity network.
45    network: NetworkTypeDto,
46    /// Entity type.
47    _type: EntityTypeDto,
48    /// Block height.
49    height: HeightDto,
50    /// Number of milliseconds elapsed since creation of nemesis block.
51    timestamp: TimestampDto,
52    /// Block difficulty.
53    difficulty: DifficultyDto,
54    /// Generation hash proof.
55    generation_hash_proof: VrfProofBuilder,
56    /// Previous block hash.
57    previous_block_hash: Hash256Dto,
58    /// Hash of the transactions in this block.
59    transactions_hash: Hash256Dto,
60    /// Hash of the receipts generated by this block.
61    receipts_hash: Hash256Dto,
62    /// Hash of the global chain state at this block.
63    state_hash: Hash256Dto,
64    /// Beneficiary address designated by harvester.
65    beneficiary_address: AddressDto,
66    /// Fee multiplier applied to block transactions.
67    fee_multiplier: BlockFeeMultiplierDto,
68}
69
70
71impl BlockHeaderBuilder {
72    /// Creates an instance of BlockHeaderBuilder from binary payload.
73    /// payload: Byte payload to use to serialize the object.
74    /// # Returns
75    /// A BlockHeaderBuilder.
76    pub fn from_binary(_bytes: &[u8]) -> Self {
77        let signature = SignatureDto::from_binary(&_bytes); // kind:CUSTOM1
78        let mut _bytes = _bytes[signature.get_size()..].to_vec();
79        let signer_public_key = KeyDto::from_binary(&_bytes); // kind:CUSTOM1
80        let mut _bytes = _bytes[signer_public_key.get_size()..].to_vec();
81        let buf = fixed_bytes::<1>(&_bytes);
82        let version = u8::from_le_bytes(buf); // kind:SIMPLE
83        let _bytes = (&_bytes[1..]).to_vec();
84        let network = NetworkTypeDto::from_binary(&_bytes); // kind:CUSTOM2
85        let mut _bytes = _bytes[network.get_size()..].to_vec();
86        let _type = EntityTypeDto::from_binary(&_bytes); // kind:CUSTOM2
87        let _bytes = (&_bytes[_type.get_size()..]).to_vec();
88        let height = HeightDto::from_binary(&_bytes); // kind:CUSTOM1
89        let mut _bytes = _bytes[height.get_size()..].to_vec();
90        let timestamp = TimestampDto::from_binary(&_bytes); // kind:CUSTOM1
91        let mut _bytes = _bytes[timestamp.get_size()..].to_vec();
92        let difficulty = DifficultyDto::from_binary(&_bytes); // kind:CUSTOM1
93        let mut _bytes = _bytes[difficulty.get_size()..].to_vec();
94        let generation_hash_proof = VrfProofBuilder::from_binary(&_bytes); // kind:CUSTOM1
95        let mut _bytes = _bytes[generation_hash_proof.get_size()..].to_vec();
96        let previous_block_hash = Hash256Dto::from_binary(&_bytes); // kind:CUSTOM1
97        let mut _bytes = _bytes[previous_block_hash.get_size()..].to_vec();
98        let transactions_hash = Hash256Dto::from_binary(&_bytes); // kind:CUSTOM1
99        let mut _bytes = _bytes[transactions_hash.get_size()..].to_vec();
100        let receipts_hash = Hash256Dto::from_binary(&_bytes); // kind:CUSTOM1
101        let mut _bytes = _bytes[receipts_hash.get_size()..].to_vec();
102        let state_hash = Hash256Dto::from_binary(&_bytes); // kind:CUSTOM1
103        let mut _bytes = _bytes[state_hash.get_size()..].to_vec();
104        let beneficiary_address = AddressDto::from_binary(&_bytes); // kind:CUSTOM1
105        let mut _bytes = _bytes[beneficiary_address.get_size()..].to_vec();
106        let fee_multiplier = BlockFeeMultiplierDto::from_binary(&_bytes); // kind:CUSTOM1
107        let mut _bytes = _bytes[fee_multiplier.get_size()..].to_vec();
108        BlockHeaderBuilder { signature, signer_public_key, version, network, _type, height, timestamp, difficulty, generation_hash_proof, previous_block_hash, transactions_hash, receipts_hash, state_hash, beneficiary_address, fee_multiplier }
109    }
110
111    /// Gets entity signature.
112    ///
113    /// # Returns
114    /// A Entity signature.
115    pub fn get_signature(&self) -> SignatureDto {
116        self.signature.clone()
117    }
118
119    /// Gets entity signer's public key.
120    ///
121    /// # Returns
122    /// A Entity signer's public key.
123    pub fn get_signer_public_key(&self) -> KeyDto {
124        self.signer_public_key.clone()
125    }
126
127    /// Gets entity version.
128    ///
129    /// # Returns
130    /// A Entity version.
131    pub fn get_version(&self) -> u8 {
132        self.version.clone()
133    }
134
135    /// Gets entity network.
136    ///
137    /// # Returns
138    /// A Entity network.
139    pub fn get_network(&self) -> NetworkTypeDto {
140        self.network.clone()
141    }
142
143    /// Gets entity type.
144    ///
145    /// # Returns
146    /// A Entity type.
147    pub fn get_type(&self) -> EntityTypeDto {
148        self._type
149    }
150
151    /// Gets block height.
152    ///
153    /// # Returns
154    /// A Block height.
155    pub fn get_height(&self) -> HeightDto {
156        self.height.clone()
157    }
158
159    /// Gets number of milliseconds elapsed since creation of nemesis block.
160    ///
161    /// # Returns
162    /// A Number of milliseconds elapsed since creation of nemesis block.
163    pub fn get_timestamp(&self) -> TimestampDto {
164        self.timestamp.clone()
165    }
166
167    /// Gets block difficulty.
168    ///
169    /// # Returns
170    /// A Block difficulty.
171    pub fn get_difficulty(&self) -> DifficultyDto {
172        self.difficulty.clone()
173    }
174
175    /// Gets generation hash proof.
176    ///
177    /// # Returns
178    /// A Generation hash proof.
179    pub fn get_generation_hash_proof(&self) -> VrfProofBuilder {
180        self.generation_hash_proof.clone()
181    }
182
183    /// Gets previous block hash.
184    ///
185    /// # Returns
186    /// A Previous block hash.
187    pub fn get_previous_block_hash(&self) -> Hash256Dto {
188        self.previous_block_hash.clone()
189    }
190
191    /// Gets hash of the transactions in this block.
192    ///
193    /// # Returns
194    /// A Hash of the transactions in this block.
195    pub fn get_transactions_hash(&self) -> Hash256Dto {
196        self.transactions_hash.clone()
197    }
198
199    /// Gets hash of the receipts generated by this block.
200    ///
201    /// # Returns
202    /// A Hash of the receipts generated by this block.
203    pub fn get_receipts_hash(&self) -> Hash256Dto {
204        self.receipts_hash.clone()
205    }
206
207    /// Gets hash of the global chain state at this block.
208    ///
209    /// # Returns
210    /// A Hash of the global chain state at this block.
211    pub fn get_state_hash(&self) -> Hash256Dto {
212        self.state_hash.clone()
213    }
214
215    /// Gets beneficiary address designated by harvester.
216    ///
217    /// # Returns
218    /// A Beneficiary address designated by harvester.
219    pub fn get_beneficiary_address(&self) -> AddressDto {
220        self.beneficiary_address.clone()
221    }
222
223    /// Gets fee multiplier applied to block transactions.
224    ///
225    /// # Returns
226    /// A Fee multiplier applied to block transactions.
227    pub fn get_fee_multiplier(&self) -> BlockFeeMultiplierDto {
228        self.fee_multiplier.clone()
229    }
230
231    /// Gets the size of the type.
232    ///
233    /// Returns:
234    /// A size in bytes.
235    pub fn get_size(&self) -> usize {
236        let mut size = 0;
237        size += 4; // size;
238        size += 4; // verifiable_entity_header__reserved1;
239        size += self.signature.get_size(); // signature;
240        size += self.signer_public_key.get_size(); // signer_public_key;
241        size += 4; // entity_body__reserved1;
242        size += 1; // version;
243        size += self.network.get_size(); // network;
244        size += self._type.get_size(); // type;
245        size += self.height.get_size(); // height;
246        size += self.timestamp.get_size(); // timestamp;
247        size += self.difficulty.get_size(); // difficulty;
248        size += self.generation_hash_proof.get_size(); // generation_hash_proof;
249        size += self.previous_block_hash.get_size(); // previous_block_hash;
250        size += self.transactions_hash.get_size(); // transactions_hash;
251        size += self.receipts_hash.get_size(); // receipts_hash;
252        size += self.state_hash.get_size(); // state_hash;
253        size += self.beneficiary_address.get_size(); // beneficiary_address;
254        size += self.fee_multiplier.get_size(); // fee_multiplier;
255        size
256    }
257
258    /// Serializes self to bytes.
259    ///
260    /// # Returns
261    /// A Serialized bytes.
262    pub fn serializer(&self) -> Vec<u8> {
263        let mut buf: Vec<u8> = vec![];
264        buf.append(&mut self.get_size().to_le_bytes().to_vec()); // kind:SIMPLE
265        buf.append(&mut 4u16.to_le_bytes().to_vec());
266        buf.append(&mut self.signature.serializer()); // kind:CUSTOM
267        buf.append(&mut self.signer_public_key.serializer()); // kind:CUSTOM
268        buf.append(&mut 4u16.to_le_bytes().to_vec());
269        buf.append(&mut self.get_version().to_le_bytes().to_vec()); // kind:SIMPLE
270        buf.append(&mut self.network.serializer()); // kind:CUSTOM
271        buf.append(&mut self._type.serializer()); // kind:CUSTOM
272        buf.append(&mut self.height.serializer()); // kind:CUSTOM
273        buf.append(&mut self.timestamp.serializer()); // kind:CUSTOM
274        buf.append(&mut self.difficulty.serializer()); // kind:CUSTOM
275        buf.append(&mut self.generation_hash_proof.serializer()); // kind:CUSTOM
276        buf.append(&mut self.previous_block_hash.serializer()); // kind:CUSTOM
277        buf.append(&mut self.transactions_hash.serializer()); // kind:CUSTOM
278        buf.append(&mut self.receipts_hash.serializer()); // kind:CUSTOM
279        buf.append(&mut self.state_hash.serializer()); // kind:CUSTOM
280        buf.append(&mut self.beneficiary_address.serializer()); // kind:CUSTOM
281        buf.append(&mut self.fee_multiplier.serializer()); // kind:CUSTOM
282        buf
283    }
284}
285