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
/*
* // Copyright (c) 2016-2019, Jaguar0625, gimre, BloodyRookie, Tech Bureau, Corp.
* // Copyright (c) 2020-present, Jaguar0625, gimre, BloodyRookie.
* // All rights reserved.
* //
* // This file is part of Catapult.
* //
* // Catapult is free software: you can redistribute it and/or modify
* // it under the terms of the GNU Lesser General Public License as published by
* // the Free Software Foundation, either version 3 of the License, or
* // (at your option) any later version.
* //
* // Catapult is distributed in the hope that it will be useful,
* // but WITHOUT ANY WARRANTY; without even the implied warranty of
* // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* // GNU Lesser General Public License for more details.
* //
* // You should have received a copy of the GNU Lesser General Public License
* // along with Catapult. If not, see <http://www.gnu.org/licenses/>.
*/
use super::address_dto::*;
use super::block_fee_multiplier_dto::*;
use super::difficulty_dto::*;
use super::entity_type_dto::*;
use super::generator_utils::*;
use super::hash256_dto::*;
use super::height_dto::*;
use super::key_dto::*;
use super::network_type_dto::*;
use super::signature_dto::*;
use super::timestamp_dto::*;
use super::vrf_proof_builder::*;
/// Binary layout for a block header.
#[derive(Debug, Clone)]
pub struct BlockHeaderBuilder {
/// Entity signature.
signature: SignatureDto,
/// Entity signer's public key.
signer_public_key: KeyDto,
/// Entity version.
version: u8,
/// Entity network.
network: NetworkTypeDto,
/// Entity type.
_type: EntityTypeDto,
/// Block height.
height: HeightDto,
/// Number of milliseconds elapsed since creation of nemesis block.
timestamp: TimestampDto,
/// Block difficulty.
difficulty: DifficultyDto,
/// Generation hash proof.
generation_hash_proof: VrfProofBuilder,
/// Previous block hash.
previous_block_hash: Hash256Dto,
/// Hash of the transactions in this block.
transactions_hash: Hash256Dto,
/// Hash of the receipts generated by this block.
receipts_hash: Hash256Dto,
/// Hash of the global chain state at this block.
state_hash: Hash256Dto,
/// Beneficiary address designated by harvester.
beneficiary_address: AddressDto,
/// Fee multiplier applied to block transactions.
fee_multiplier: BlockFeeMultiplierDto,
}
impl BlockHeaderBuilder {
/// Creates an instance of BlockHeaderBuilder from binary payload.
/// payload: Byte payload to use to serialize the object.
/// # Returns
/// A BlockHeaderBuilder.
pub fn from_binary(_bytes: &[u8]) -> Self {
let signature = SignatureDto::from_binary(&_bytes); // kind:CUSTOM1
let mut _bytes = _bytes[signature.get_size()..].to_vec();
let signer_public_key = KeyDto::from_binary(&_bytes); // kind:CUSTOM1
let mut _bytes = _bytes[signer_public_key.get_size()..].to_vec();
let buf = fixed_bytes::<1>(&_bytes);
let version = u8::from_le_bytes(buf); // kind:SIMPLE
let _bytes = (&_bytes[1..]).to_vec();
let network = NetworkTypeDto::from_binary(&_bytes); // kind:CUSTOM2
let mut _bytes = _bytes[network.get_size()..].to_vec();
let _type = EntityTypeDto::from_binary(&_bytes); // kind:CUSTOM2
let _bytes = (&_bytes[_type.get_size()..]).to_vec();
let height = HeightDto::from_binary(&_bytes); // kind:CUSTOM1
let mut _bytes = _bytes[height.get_size()..].to_vec();
let timestamp = TimestampDto::from_binary(&_bytes); // kind:CUSTOM1
let mut _bytes = _bytes[timestamp.get_size()..].to_vec();
let difficulty = DifficultyDto::from_binary(&_bytes); // kind:CUSTOM1
let mut _bytes = _bytes[difficulty.get_size()..].to_vec();
let generation_hash_proof = VrfProofBuilder::from_binary(&_bytes); // kind:CUSTOM1
let mut _bytes = _bytes[generation_hash_proof.get_size()..].to_vec();
let previous_block_hash = Hash256Dto::from_binary(&_bytes); // kind:CUSTOM1
let mut _bytes = _bytes[previous_block_hash.get_size()..].to_vec();
let transactions_hash = Hash256Dto::from_binary(&_bytes); // kind:CUSTOM1
let mut _bytes = _bytes[transactions_hash.get_size()..].to_vec();
let receipts_hash = Hash256Dto::from_binary(&_bytes); // kind:CUSTOM1
let mut _bytes = _bytes[receipts_hash.get_size()..].to_vec();
let state_hash = Hash256Dto::from_binary(&_bytes); // kind:CUSTOM1
let mut _bytes = _bytes[state_hash.get_size()..].to_vec();
let beneficiary_address = AddressDto::from_binary(&_bytes); // kind:CUSTOM1
let mut _bytes = _bytes[beneficiary_address.get_size()..].to_vec();
let fee_multiplier = BlockFeeMultiplierDto::from_binary(&_bytes); // kind:CUSTOM1
let mut _bytes = _bytes[fee_multiplier.get_size()..].to_vec();
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 }
}
/// Gets entity signature.
///
/// # Returns
/// A Entity signature.
pub fn get_signature(&self) -> SignatureDto {
self.signature.clone()
}
/// Gets entity signer's public key.
///
/// # Returns
/// A Entity signer's public key.
pub fn get_signer_public_key(&self) -> KeyDto {
self.signer_public_key.clone()
}
/// Gets entity version.
///
/// # Returns
/// A Entity version.
pub fn get_version(&self) -> u8 {
self.version.clone()
}
/// Gets entity network.
///
/// # Returns
/// A Entity network.
pub fn get_network(&self) -> NetworkTypeDto {
self.network.clone()
}
/// Gets entity type.
///
/// # Returns
/// A Entity type.
pub fn get_type(&self) -> EntityTypeDto {
self._type
}
/// Gets block height.
///
/// # Returns
/// A Block height.
pub fn get_height(&self) -> HeightDto {
self.height.clone()
}
/// Gets number of milliseconds elapsed since creation of nemesis block.
///
/// # Returns
/// A Number of milliseconds elapsed since creation of nemesis block.
pub fn get_timestamp(&self) -> TimestampDto {
self.timestamp.clone()
}
/// Gets block difficulty.
///
/// # Returns
/// A Block difficulty.
pub fn get_difficulty(&self) -> DifficultyDto {
self.difficulty.clone()
}
/// Gets generation hash proof.
///
/// # Returns
/// A Generation hash proof.
pub fn get_generation_hash_proof(&self) -> VrfProofBuilder {
self.generation_hash_proof.clone()
}
/// Gets previous block hash.
///
/// # Returns
/// A Previous block hash.
pub fn get_previous_block_hash(&self) -> Hash256Dto {
self.previous_block_hash.clone()
}
/// Gets hash of the transactions in this block.
///
/// # Returns
/// A Hash of the transactions in this block.
pub fn get_transactions_hash(&self) -> Hash256Dto {
self.transactions_hash.clone()
}
/// Gets hash of the receipts generated by this block.
///
/// # Returns
/// A Hash of the receipts generated by this block.
pub fn get_receipts_hash(&self) -> Hash256Dto {
self.receipts_hash.clone()
}
/// Gets hash of the global chain state at this block.
///
/// # Returns
/// A Hash of the global chain state at this block.
pub fn get_state_hash(&self) -> Hash256Dto {
self.state_hash.clone()
}
/// Gets beneficiary address designated by harvester.
///
/// # Returns
/// A Beneficiary address designated by harvester.
pub fn get_beneficiary_address(&self) -> AddressDto {
self.beneficiary_address.clone()
}
/// Gets fee multiplier applied to block transactions.
///
/// # Returns
/// A Fee multiplier applied to block transactions.
pub fn get_fee_multiplier(&self) -> BlockFeeMultiplierDto {
self.fee_multiplier.clone()
}
/// Gets the size of the type.
///
/// Returns:
/// A size in bytes.
pub fn get_size(&self) -> usize {
let mut size = 0;
size += 4; // size;
size += 4; // verifiable_entity_header__reserved1;
size += self.signature.get_size(); // signature;
size += self.signer_public_key.get_size(); // signer_public_key;
size += 4; // entity_body__reserved1;
size += 1; // version;
size += self.network.get_size(); // network;
size += self._type.get_size(); // type;
size += self.height.get_size(); // height;
size += self.timestamp.get_size(); // timestamp;
size += self.difficulty.get_size(); // difficulty;
size += self.generation_hash_proof.get_size(); // generation_hash_proof;
size += self.previous_block_hash.get_size(); // previous_block_hash;
size += self.transactions_hash.get_size(); // transactions_hash;
size += self.receipts_hash.get_size(); // receipts_hash;
size += self.state_hash.get_size(); // state_hash;
size += self.beneficiary_address.get_size(); // beneficiary_address;
size += self.fee_multiplier.get_size(); // fee_multiplier;
size
}
/// Serializes self to bytes.
///
/// # Returns
/// A Serialized bytes.
pub fn serializer(&self) -> Vec<u8> {
let mut buf: Vec<u8> = vec![];
buf.append(&mut self.get_size().to_le_bytes().to_vec()); // kind:SIMPLE
buf.append(&mut 4u16.to_le_bytes().to_vec());
buf.append(&mut self.signature.serializer()); // kind:CUSTOM
buf.append(&mut self.signer_public_key.serializer()); // kind:CUSTOM
buf.append(&mut 4u16.to_le_bytes().to_vec());
buf.append(&mut self.get_version().to_le_bytes().to_vec()); // kind:SIMPLE
buf.append(&mut self.network.serializer()); // kind:CUSTOM
buf.append(&mut self._type.serializer()); // kind:CUSTOM
buf.append(&mut self.height.serializer()); // kind:CUSTOM
buf.append(&mut self.timestamp.serializer()); // kind:CUSTOM
buf.append(&mut self.difficulty.serializer()); // kind:CUSTOM
buf.append(&mut self.generation_hash_proof.serializer()); // kind:CUSTOM
buf.append(&mut self.previous_block_hash.serializer()); // kind:CUSTOM
buf.append(&mut self.transactions_hash.serializer()); // kind:CUSTOM
buf.append(&mut self.receipts_hash.serializer()); // kind:CUSTOM
buf.append(&mut self.state_hash.serializer()); // kind:CUSTOM
buf.append(&mut self.beneficiary_address.serializer()); // kind:CUSTOM
buf.append(&mut self.fee_multiplier.serializer()); // kind:CUSTOM
buf
}
}