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
use alloc::string::String;
use core::borrow::Borrow;
use packable::{prefix::StringPrefix, Packable};
use crate::block::{helper::network_name_to_id, output::RentStructure, Error, PROTOCOL_VERSION};
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Packable)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[packable(unpack_error = Error)]
pub struct ProtocolParameters {
#[cfg_attr(feature = "serde", serde(alias = "protocolVersion"))]
protocol_version: u8,
#[packable(unpack_error_with = |err| Error::InvalidNetworkName(err.into_item_err()))]
#[cfg_attr(feature = "serde", serde(alias = "networkName"))]
network_name: StringPrefix<u8>,
#[packable(unpack_error_with = |err| Error::InvalidBech32Hrp(err.into_item_err()))]
#[cfg_attr(feature = "serde", serde(alias = "bech32Hrp"))]
bech32_hrp: StringPrefix<u8>,
#[cfg_attr(feature = "serde", serde(alias = "minPowScore"))]
min_pow_score: u32,
#[cfg_attr(feature = "serde", serde(alias = "belowMaxDepth"))]
below_max_depth: u8,
#[cfg_attr(feature = "serde", serde(alias = "rentStructure"))]
rent_structure: RentStructure,
#[cfg_attr(feature = "serde", serde(alias = "tokenSupply"))]
token_supply: u64,
}
impl Borrow<()> for ProtocolParameters {
fn borrow(&self) -> &() {
&()
}
}
impl Default for ProtocolParameters {
fn default() -> Self {
Self::new(
PROTOCOL_VERSION,
String::from("shimmer"),
String::from("smr"),
1500,
15,
RentStructure::default(),
1_813_620_509_061_365,
)
.unwrap()
}
}
impl ProtocolParameters {
pub fn new(
protocol_version: u8,
network_name: String,
bech32_hrp: String,
min_pow_score: u32,
below_max_depth: u8,
rent_structure: RentStructure,
token_supply: u64,
) -> Result<ProtocolParameters, Error> {
Ok(ProtocolParameters {
protocol_version,
network_name: <StringPrefix<u8>>::try_from(network_name).map_err(Error::InvalidStringPrefix)?,
bech32_hrp: <StringPrefix<u8>>::try_from(bech32_hrp).map_err(Error::InvalidStringPrefix)?,
min_pow_score,
below_max_depth,
rent_structure,
token_supply,
})
}
pub fn protocol_version(&self) -> u8 {
self.protocol_version
}
pub fn network_name(&self) -> &str {
&self.network_name
}
pub fn network_id(&self) -> u64 {
network_name_to_id(&self.network_name)
}
pub fn bech32_hrp(&self) -> &str {
&self.bech32_hrp
}
pub fn min_pow_score(&self) -> u32 {
self.min_pow_score
}
pub fn below_max_depth(&self) -> u8 {
self.below_max_depth
}
pub fn rent_structure(&self) -> &RentStructure {
&self.rent_structure
}
pub fn token_supply(&self) -> u64 {
self.token_supply
}
}
#[cfg(any(feature = "test", feature = "rand"))]
pub fn protocol_parameters() -> ProtocolParameters {
ProtocolParameters::new(
2,
String::from("testnet"),
String::from("rms"),
1500,
15,
crate::block::output::RentStructure::new(500, 10, 1),
1_813_620_509_061_365,
)
.unwrap()
}
#[cfg(feature = "dto")]
#[allow(missing_docs)]
pub mod dto {
use super::*;
use crate::block::{error::dto::DtoError, output::dto::RentStructureDto};
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "camelCase")
)]
pub struct ProtocolParametersDto {
#[cfg_attr(feature = "serde", serde(rename = "version"))]
pub protocol_version: u8,
pub network_name: String,
pub bech32_hrp: String,
pub min_pow_score: u32,
pub below_max_depth: u8,
pub rent_structure: RentStructureDto,
pub token_supply: String,
}
impl TryFrom<ProtocolParametersDto> for ProtocolParameters {
type Error = DtoError;
fn try_from(value: ProtocolParametersDto) -> Result<Self, Self::Error> {
Ok(ProtocolParameters::new(
value.protocol_version,
value.network_name,
value.bech32_hrp,
value.min_pow_score,
value.below_max_depth,
value.rent_structure.into(),
value
.token_supply
.parse()
.map_err(|_| DtoError::InvalidField("token_supply"))?,
)?)
}
}
}
#[cfg(feature = "inx")]
mod inx {
use packable::PackableExt;
use super::*;
use crate::block::InxError;
impl TryFrom<::inx::proto::RawProtocolParameters> for ProtocolParameters {
type Error = crate::block::error::inx::InxError;
fn try_from(value: ::inx::proto::RawProtocolParameters) -> Result<Self, Self::Error> {
Self::unpack_verified(value.params, &()).map_err(|e| InxError::InvalidRawBytes(format!("{e:?}")))
}
}
impl From<ProtocolParameters> for ::inx::proto::RawProtocolParameters {
fn from(value: ProtocolParameters) -> Self {
Self {
protocol_version: value.protocol_version() as u32,
params: value.pack_to_vec(),
}
}
}
}