catbuffer_rust/
voting_key_link_transaction_body_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::link_action_dto::*;
25use super::voting_key_dto::*;
26
27/// Binary layout for a voting key link transaction.
28#[derive(Debug, Clone)]
29pub struct VotingKeyLinkTransactionBodyBuilder {
30    /// Linked public key.
31    pub linked_public_key: VotingKeyDto,
32    /// Start finalization epoch.
33    pub start_epoch: FinalizationEpochDto,
34    /// End finalization epoch.
35    pub end_epoch: FinalizationEpochDto,
36    /// Link action.
37    pub link_action: LinkActionDto,
38}
39
40impl VotingKeyLinkTransactionBodyBuilder {
41    /// Creates an instance of VotingKeyLinkTransactionBodyBuilder from binary payload.
42    /// payload: Byte payload to use to serialize the object.
43    /// # Returns
44    /// A VotingKeyLinkTransactionBodyBuilder.
45    pub fn from_binary(payload: &[u8]) -> Self {
46        let mut _bytes = payload.to_vec();
47        let linked_public_key = VotingKeyDto::from_binary(&_bytes); // kind:CUSTOM1
48        _bytes = _bytes[linked_public_key.get_size()..].to_vec();
49        let start_epoch = FinalizationEpochDto::from_binary(&_bytes); // kind:CUSTOM1
50        _bytes = _bytes[start_epoch.get_size()..].to_vec();
51        let end_epoch = FinalizationEpochDto::from_binary(&_bytes); // kind:CUSTOM1
52        _bytes = _bytes[end_epoch.get_size()..].to_vec();
53        let link_action = LinkActionDto::from_binary(&_bytes); // kind:CUSTOM2
54        _bytes = (&_bytes[link_action.get_size()..]).to_vec();
55        // create object and call.
56        VotingKeyLinkTransactionBodyBuilder { linked_public_key, start_epoch, end_epoch, link_action } // TransactionBody
57    }
58
59    /// Gets the size of the type.
60    ///
61    /// Returns:
62    /// A size in bytes.
63    pub fn get_size(&self) -> usize {
64        let mut size = 0;
65        size += self.linked_public_key.get_size(); // linked_public_key_size;
66        size += self.start_epoch.get_size(); // start_epoch_size;
67        size += self.end_epoch.get_size(); // end_epoch_size;
68        size += self.link_action.get_size(); // link_action_size;
69        size
70    }
71
72    /// Serializes self to bytes.
73    ///
74    /// # Returns
75    /// A Serialized bytes.
76    pub fn serializer(&self) -> Vec<u8> {
77        let mut buf: Vec<u8> = vec![];
78        buf.append(&mut self.linked_public_key.serializer()); // kind:CUSTOM
79        buf.append(&mut self.start_epoch.serializer()); // kind:CUSTOM
80        buf.append(&mut self.end_epoch.serializer()); // kind:CUSTOM
81        buf.append(&mut self.link_action.serializer()); // kind:CUSTOM
82        buf
83    }
84}
85