catbuffer_rust/pinned_voting_key_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::finalization_epoch_dto::*;
23use super::generator_utils::*;
24use super::voting_key_dto::*;
25
26/// Pinned voting key.
27#[derive(Debug, Clone)]
28pub struct PinnedVotingKeyBuilder {
29 /// Voting key.
30 voting_key: VotingKeyDto,
31 /// Start finalization epoch.
32 start_epoch: FinalizationEpochDto,
33 /// End finalization epoch.
34 end_epoch: FinalizationEpochDto,
35}
36
37
38impl PinnedVotingKeyBuilder {
39 /// Creates an instance of PinnedVotingKeyBuilder from binary payload.
40 /// payload: Byte payload to use to serialize the object.
41 /// # Returns
42 /// A PinnedVotingKeyBuilder.
43 pub fn from_binary(_bytes: &[u8]) -> Self {
44 let voting_key = VotingKeyDto::from_binary(&_bytes); // kind:CUSTOM1
45 let mut _bytes = _bytes[voting_key.get_size()..].to_vec();
46 let start_epoch = FinalizationEpochDto::from_binary(&_bytes); // kind:CUSTOM1
47 let mut _bytes = _bytes[start_epoch.get_size()..].to_vec();
48 let end_epoch = FinalizationEpochDto::from_binary(&_bytes); // kind:CUSTOM1
49 let mut _bytes = _bytes[end_epoch.get_size()..].to_vec();
50 PinnedVotingKeyBuilder { voting_key, start_epoch, end_epoch }
51 }
52
53 /// Gets voting key.
54 ///
55 /// # Returns
56 /// A Voting key.
57 pub fn get_voting_key(&self) -> VotingKeyDto {
58 self.voting_key.clone()
59 }
60
61 /// Gets start finalization epoch.
62 ///
63 /// # Returns
64 /// A Start finalization epoch.
65 pub fn get_start_epoch(&self) -> FinalizationEpochDto {
66 self.start_epoch.clone()
67 }
68
69 /// Gets end finalization epoch.
70 ///
71 /// # Returns
72 /// A End finalization epoch.
73 pub fn get_end_epoch(&self) -> FinalizationEpochDto {
74 self.end_epoch.clone()
75 }
76
77 /// Gets the size of the type.
78 ///
79 /// Returns:
80 /// A size in bytes.
81 pub fn get_size(&self) -> usize {
82 let mut size = 0;
83 size += self.voting_key.get_size(); // voting_key;
84 size += self.start_epoch.get_size(); // start_epoch;
85 size += self.end_epoch.get_size(); // end_epoch;
86 size
87 }
88
89 /// Serializes self to bytes.
90 ///
91 /// # Returns
92 /// A Serialized bytes.
93 pub fn serializer(&self) -> Vec<u8> {
94 let mut buf: Vec<u8> = vec![];
95 buf.append(&mut self.voting_key.serializer()); // kind:CUSTOM
96 buf.append(&mut self.start_epoch.serializer()); // kind:CUSTOM
97 buf.append(&mut self.end_epoch.serializer()); // kind:CUSTOM
98 buf
99 }
100}
101