iota_stardust_types/block/
protocol.rs

1// Copyright (c) 2026 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4//! Protocol parameters.
5
6use alloc::string::String;
7use core::borrow::Borrow;
8
9use packable::{Packable, prefix::StringPrefix};
10
11use super::Error;
12
13/// The current protocol version.
14pub const PROTOCOL_VERSION: u8 = 2;
15
16/// Protocol parameters for unpacking from Hornet snapshots.
17#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Packable)]
18#[packable(unpack_error = Error)]
19#[cfg_attr(
20    feature = "serde",
21    derive(serde::Serialize, serde::Deserialize),
22    serde(rename_all = "camelCase")
23)]
24pub struct ProtocolParameters {
25    /// The version of the protocol running.
26    #[cfg_attr(feature = "serde", serde(rename = "version"))]
27    protocol_version: u8,
28    /// The human friendly name of the network.
29    #[packable(unpack_error_with = |err| Error::InvalidNetworkName(err.into_item_err()))]
30    network_name: StringPrefix<u8>,
31    /// The HRP prefix used for Bech32 addresses in the network.
32    #[packable(unpack_error_with = |err| Error::InvalidBech32Hrp(format!("{:?}", err.into_item_err())))]
33    bech32_hrp: StringPrefix<u8>,
34    /// The minimum pow score of the network.
35    min_pow_score: u32,
36    /// The below max depth parameter of the network.
37    below_max_depth: u8,
38    /// The byte cost for rent calculation.
39    rent_byte_cost: u32,
40    /// The byte factor for data fields in rent calculation.
41    rent_byte_factor_data: u8,
42    /// The byte factor for key fields in rent calculation.
43    rent_byte_factor_key: u8,
44    /// TokenSupply defines the current token supply on the network.
45    token_supply: u64,
46}
47
48// This implementation is required to make [`ProtocolParameters`] a [`Packable`]
49// visitor.
50impl Borrow<()> for ProtocolParameters {
51    fn borrow(&self) -> &() {
52        &()
53    }
54}
55
56impl Default for ProtocolParameters {
57    fn default() -> Self {
58        Self {
59            protocol_version: PROTOCOL_VERSION,
60            network_name: StringPrefix::try_from(String::from("shimmer"))
61                .expect("network name should be valid"),
62            bech32_hrp: StringPrefix::try_from(String::from("smr"))
63                .expect("bech32 hrp should be valid"),
64            min_pow_score: 1500,
65            below_max_depth: 15,
66            rent_byte_cost: 100,
67            rent_byte_factor_data: 1,
68            rent_byte_factor_key: 10,
69            token_supply: 1_813_620_509_061_365,
70        }
71    }
72}
73
74impl ProtocolParameters {
75    /// Returns the protocol version of the [`ProtocolParameters`].
76    pub fn protocol_version(&self) -> u8 {
77        self.protocol_version
78    }
79
80    /// Returns the network name of the [`ProtocolParameters`].
81    pub fn network_name(&self) -> &str {
82        &self.network_name
83    }
84
85    /// Returns the bech32 HRP of the [`ProtocolParameters`].
86    pub fn bech32_hrp(&self) -> &str {
87        &self.bech32_hrp
88    }
89
90    /// Returns the minimum PoW score of the [`ProtocolParameters`].
91    pub fn min_pow_score(&self) -> u32 {
92        self.min_pow_score
93    }
94
95    /// Returns the below max depth of the [`ProtocolParameters`].
96    pub fn below_max_depth(&self) -> u8 {
97        self.below_max_depth
98    }
99
100    /// Returns the rent byte cost of the [`ProtocolParameters`].
101    pub fn rent_byte_cost(&self) -> u32 {
102        self.rent_byte_cost
103    }
104
105    /// Returns the rent byte factor for data fields.
106    pub fn rent_byte_factor_data(&self) -> u8 {
107        self.rent_byte_factor_data
108    }
109
110    /// Returns the rent byte factor for key fields.
111    pub fn rent_byte_factor_key(&self) -> u8 {
112        self.rent_byte_factor_key
113    }
114
115    /// Returns the token supply of the [`ProtocolParameters`].
116    pub fn token_supply(&self) -> u64 {
117        self.token_supply
118    }
119}