catbuffer_rust/
importance_block_footer_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::amount_dto::*;
23use super::generator_utils::*;
24use super::hash256_dto::*;
25
26/// Binary layout for an importance block footer.
27#[derive(Debug, Clone)]
28pub struct ImportanceBlockFooterBuilder {
29    /// Number of voting eligible accounts.
30    voting_eligible_accounts_count: u32,
31    /// Number of harvesting eligible accounts.
32    harvesting_eligible_accounts_count: u64,
33    /// Total balance eligible for voting.
34    total_voting_balance: AmountDto,
35    /// Previous importance block hash.
36    previous_importance_block_hash: Hash256Dto,
37}
38
39
40impl ImportanceBlockFooterBuilder {
41    /// Creates an instance of ImportanceBlockFooterBuilder from binary payload.
42    /// payload: Byte payload to use to serialize the object.
43    /// # Returns
44    /// A ImportanceBlockFooterBuilder.
45    pub fn from_binary(_bytes: &[u8]) -> Self {
46        let buf = fixed_bytes::<4>(&_bytes);
47        let voting_eligible_accounts_count = u32::from_le_bytes(buf); // kind:SIMPLE
48        let _bytes = (&_bytes[4..]).to_vec();
49        let buf = fixed_bytes::<8>(&_bytes);
50        let harvesting_eligible_accounts_count = u64::from_le_bytes(buf); // kind:SIMPLE
51        let _bytes = (&_bytes[8..]).to_vec();
52        let total_voting_balance = AmountDto::from_binary(&_bytes); // kind:CUSTOM1
53        let mut _bytes = _bytes[total_voting_balance.get_size()..].to_vec();
54        let previous_importance_block_hash = Hash256Dto::from_binary(&_bytes); // kind:CUSTOM1
55        let mut _bytes = _bytes[previous_importance_block_hash.get_size()..].to_vec();
56        ImportanceBlockFooterBuilder { voting_eligible_accounts_count, harvesting_eligible_accounts_count, total_voting_balance, previous_importance_block_hash }
57    }
58
59    /// Gets number of voting eligible accounts.
60    ///
61    /// # Returns
62    /// A Number of voting eligible accounts.
63    pub fn get_voting_eligible_accounts_count(&self) -> u32 {
64        self.voting_eligible_accounts_count.clone()
65    }
66
67    /// Gets number of harvesting eligible accounts.
68    ///
69    /// # Returns
70    /// A Number of harvesting eligible accounts.
71    pub fn get_harvesting_eligible_accounts_count(&self) -> u64 {
72        self.harvesting_eligible_accounts_count.clone()
73    }
74
75    /// Gets total balance eligible for voting.
76    ///
77    /// # Returns
78    /// A Total balance eligible for voting.
79    pub fn get_total_voting_balance(&self) -> AmountDto {
80        self.total_voting_balance.clone()
81    }
82
83    /// Gets previous importance block hash.
84    ///
85    /// # Returns
86    /// A Previous importance block hash.
87    pub fn get_previous_importance_block_hash(&self) -> Hash256Dto {
88        self.previous_importance_block_hash.clone()
89    }
90
91    /// Gets the size of the type.
92    ///
93    /// Returns:
94    /// A size in bytes.
95    pub fn get_size(&self) -> usize {
96        let mut size = 0;
97        size += 4; // voting_eligible_accounts_count;
98        size += 8; // harvesting_eligible_accounts_count;
99        size += self.total_voting_balance.get_size(); // total_voting_balance;
100        size += self.previous_importance_block_hash.get_size(); // previous_importance_block_hash;
101        size
102    }
103
104    /// Serializes self to bytes.
105    ///
106    /// # Returns
107    /// A Serialized bytes.
108    pub fn serializer(&self) -> Vec<u8> {
109        let mut buf: Vec<u8> = vec![];
110        buf.append(&mut self.get_voting_eligible_accounts_count().to_le_bytes().to_vec()); // kind:SIMPLE
111        buf.append(&mut self.get_harvesting_eligible_accounts_count().to_le_bytes().to_vec()); // kind:SIMPLE
112        buf.append(&mut self.total_voting_balance.serializer()); // kind:CUSTOM
113        buf.append(&mut self.previous_importance_block_hash.serializer()); // kind:CUSTOM
114        buf
115    }
116}
117