iota_types/block/
protocol.rs1use alloc::string::String;
5use core::borrow::Borrow;
6
7use packable::{prefix::StringPrefix, Packable};
8
9use crate::block::{helper::network_name_to_id, output::RentStructure, Error, PROTOCOL_VERSION};
10
11#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Packable)]
13#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
14#[packable(unpack_error = Error)]
15pub struct ProtocolParameters {
16 #[cfg_attr(feature = "serde", serde(alias = "protocolVersion"))]
18 protocol_version: u8,
19 #[packable(unpack_error_with = |err| Error::InvalidNetworkName(err.into_item_err()))]
21 #[cfg_attr(feature = "serde", serde(alias = "networkName"))]
22 network_name: StringPrefix<u8>,
23 #[packable(unpack_error_with = |err| Error::InvalidBech32Hrp(err.into_item_err()))]
25 #[cfg_attr(feature = "serde", serde(alias = "bech32Hrp"))]
26 bech32_hrp: StringPrefix<u8>,
27 #[cfg_attr(feature = "serde", serde(alias = "minPowScore"))]
29 min_pow_score: u32,
30 #[cfg_attr(feature = "serde", serde(alias = "belowMaxDepth"))]
32 below_max_depth: u8,
33 #[cfg_attr(feature = "serde", serde(alias = "rentStructure"))]
35 rent_structure: RentStructure,
36 #[cfg_attr(feature = "serde", serde(alias = "tokenSupply"))]
38 token_supply: u64,
39}
40
41impl Borrow<()> for ProtocolParameters {
43 fn borrow(&self) -> &() {
44 &()
45 }
46}
47
48impl Default for ProtocolParameters {
49 fn default() -> Self {
50 Self::new(
52 PROTOCOL_VERSION,
53 String::from("shimmer"),
54 String::from("smr"),
55 1500,
56 15,
57 RentStructure::default(),
58 1_813_620_509_061_365,
59 )
60 .unwrap()
61 }
62}
63
64impl ProtocolParameters {
65 pub fn new(
67 protocol_version: u8,
68 network_name: String,
69 bech32_hrp: String,
70 min_pow_score: u32,
71 below_max_depth: u8,
72 rent_structure: RentStructure,
73 token_supply: u64,
74 ) -> Result<Self, Error> {
75 Ok(Self {
76 protocol_version,
77 network_name: <StringPrefix<u8>>::try_from(network_name).map_err(Error::InvalidStringPrefix)?,
78 bech32_hrp: <StringPrefix<u8>>::try_from(bech32_hrp).map_err(Error::InvalidStringPrefix)?,
79 min_pow_score,
80 below_max_depth,
81 rent_structure,
82 token_supply,
83 })
84 }
85
86 pub fn protocol_version(&self) -> u8 {
88 self.protocol_version
89 }
90
91 pub fn network_name(&self) -> &str {
93 &self.network_name
94 }
95
96 pub fn network_id(&self) -> u64 {
98 network_name_to_id(&self.network_name)
99 }
100
101 pub fn bech32_hrp(&self) -> &str {
103 &self.bech32_hrp
104 }
105
106 pub fn min_pow_score(&self) -> u32 {
108 self.min_pow_score
109 }
110
111 pub fn below_max_depth(&self) -> u8 {
113 self.below_max_depth
114 }
115
116 pub fn rent_structure(&self) -> &RentStructure {
118 &self.rent_structure
119 }
120
121 pub fn token_supply(&self) -> u64 {
123 self.token_supply
124 }
125}
126
127#[cfg(any(feature = "test", feature = "rand"))]
129pub fn protocol_parameters() -> ProtocolParameters {
130 ProtocolParameters::new(
131 2,
132 String::from("testnet"),
133 String::from("rms"),
134 1500,
135 15,
136 crate::block::output::RentStructure::new(500, 10, 1),
137 1_813_620_509_061_365,
138 )
139 .unwrap()
140}
141
142#[cfg(feature = "dto")]
143#[allow(missing_docs)]
144pub mod dto {
145
146 use super::*;
147 use crate::block::{error::dto::DtoError, output::dto::RentStructureDto};
148
149 #[derive(Clone, Debug, Eq, PartialEq)]
150 #[cfg_attr(
151 feature = "serde",
152 derive(serde::Serialize, serde::Deserialize),
153 serde(rename_all = "camelCase")
154 )]
155 pub struct ProtocolParametersDto {
156 #[cfg_attr(feature = "serde", serde(rename = "version"))]
157 pub protocol_version: u8,
158 pub network_name: String,
159 pub bech32_hrp: String,
160 pub min_pow_score: u32,
161 pub below_max_depth: u8,
162 pub rent_structure: RentStructureDto,
163 pub token_supply: String,
164 }
165
166 impl TryFrom<ProtocolParametersDto> for ProtocolParameters {
167 type Error = DtoError;
168
169 fn try_from(value: ProtocolParametersDto) -> Result<Self, Self::Error> {
170 Ok(Self::new(
171 value.protocol_version,
172 value.network_name,
173 value.bech32_hrp,
174 value.min_pow_score,
175 value.below_max_depth,
176 value.rent_structure.into(),
177 value
178 .token_supply
179 .parse()
180 .map_err(|_| DtoError::InvalidField("token_supply"))?,
181 )?)
182 }
183 }
184}