bee_inx/node/
config.rs

1// Copyright 2022 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use bee_block as bee;
5use inx::proto;
6
7use crate::maybe_missing;
8
9/// The [`BaseToken`] type.
10#[derive(Clone, Debug, PartialEq, Eq)]
11pub struct BaseToken {
12    pub name: String,
13    pub ticker_symbol: String,
14    pub unit: String,
15    pub subunit: String,
16    pub decimals: u32,
17    pub use_metric_prefix: bool,
18}
19
20impl From<proto::BaseToken> for BaseToken {
21    fn from(value: proto::BaseToken) -> Self {
22        Self {
23            name: value.name,
24            ticker_symbol: value.ticker_symbol,
25            unit: value.unit,
26            subunit: value.subunit,
27            decimals: value.decimals,
28            use_metric_prefix: value.use_metric_prefix,
29        }
30    }
31}
32
33impl From<BaseToken> for proto::BaseToken {
34    fn from(value: BaseToken) -> Self {
35        Self {
36            name: value.name,
37            ticker_symbol: value.ticker_symbol,
38            unit: value.unit,
39            subunit: value.subunit,
40            decimals: value.decimals,
41            use_metric_prefix: value.use_metric_prefix,
42        }
43    }
44}
45
46#[derive(Clone, Debug, PartialEq, Eq)]
47pub struct MilestoneKeyRange {
48    pub public_key: Box<[u8]>,
49    pub start_index: bee::payload::milestone::MilestoneIndex,
50    pub end_index: bee::payload::milestone::MilestoneIndex,
51}
52
53impl From<proto::MilestoneKeyRange> for MilestoneKeyRange {
54    fn from(value: proto::MilestoneKeyRange) -> Self {
55        Self {
56            public_key: value.public_key.into_boxed_slice(),
57            start_index: value.start_index.into(),
58            end_index: value.end_index.into(),
59        }
60    }
61}
62
63impl From<MilestoneKeyRange> for proto::MilestoneKeyRange {
64    fn from(value: MilestoneKeyRange) -> Self {
65        Self {
66            public_key: value.public_key.into_vec(),
67            start_index: value.start_index.0,
68            end_index: value.end_index.0,
69        }
70    }
71}
72
73/// The [`NodeConfiguration`] type.
74#[derive(Clone, Debug, PartialEq, Eq)]
75pub struct NodeConfiguration {
76    pub milestone_public_key_count: u32,
77    pub milestone_key_ranges: Box<[MilestoneKeyRange]>,
78    pub base_token: BaseToken,
79    pub supported_protocol_versions: Box<[u8]>,
80}
81
82impl TryFrom<proto::NodeConfiguration> for NodeConfiguration {
83    type Error = bee::InxError;
84
85    fn try_from(value: proto::NodeConfiguration) -> Result<Self, Self::Error> {
86        Ok(NodeConfiguration {
87            milestone_public_key_count: value.milestone_public_key_count,
88            milestone_key_ranges: value.milestone_key_ranges.into_iter().map(Into::into).collect(),
89            base_token: maybe_missing!(value.base_token).into(),
90            supported_protocol_versions: value.supported_protocol_versions.into_iter().map(|v| v as u8).collect(),
91        })
92    }
93}
94
95impl From<NodeConfiguration> for proto::NodeConfiguration {
96    fn from(value: NodeConfiguration) -> Self {
97        Self {
98            milestone_public_key_count: value.milestone_public_key_count,
99            milestone_key_ranges: value
100                .milestone_key_ranges
101                .into_vec()
102                .into_iter()
103                .map(Into::into)
104                .collect(),
105            base_token: Some(value.base_token.into()),
106            supported_protocol_versions: value
107                .supported_protocol_versions
108                .into_vec()
109                .into_iter()
110                .map(|v| v as _)
111                .collect(),
112        }
113    }
114}