iota_json_rpc_types/
iota_protocol.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use std::collections::BTreeMap;
6
7use iota_protocol_config::{ProtocolConfig, ProtocolConfigValue, ProtocolVersion};
8use iota_types::iota_serde::{AsProtocolVersion, BigInt, Readable};
9use schemars::JsonSchema;
10use serde::{Deserialize, Serialize};
11use serde_with::{DisplayFromStr, serde_as};
12
13#[serde_as]
14#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema, PartialEq)]
15#[serde(rename_all = "camelCase", rename = "ProtocolConfigValue")]
16pub enum IotaProtocolConfigValue {
17    U16(
18        #[schemars(with = "BigInt<u16>")]
19        #[serde_as(as = "BigInt<u16>")]
20        u16,
21    ),
22    U32(
23        #[schemars(with = "BigInt<u32>")]
24        #[serde_as(as = "BigInt<u32>")]
25        u32,
26    ),
27    U64(
28        #[schemars(with = "BigInt<u64>")]
29        #[serde_as(as = "BigInt<u64>")]
30        u64,
31    ),
32    F64(
33        #[schemars(with = "String")]
34        #[serde_as(as = "DisplayFromStr")]
35        f64,
36    ),
37    Bool(
38        #[schemars(with = "String")]
39        #[serde_as(as = "DisplayFromStr")]
40        bool,
41    ),
42}
43
44impl From<ProtocolConfigValue> for IotaProtocolConfigValue {
45    fn from(value: ProtocolConfigValue) -> Self {
46        match value {
47            ProtocolConfigValue::u16(y) => IotaProtocolConfigValue::U16(y),
48            ProtocolConfigValue::u32(y) => IotaProtocolConfigValue::U32(y),
49            ProtocolConfigValue::u64(x) => IotaProtocolConfigValue::U64(x),
50            ProtocolConfigValue::bool(z) => IotaProtocolConfigValue::Bool(z),
51        }
52    }
53}
54
55#[serde_as]
56#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema, PartialEq)]
57#[serde(rename_all = "camelCase", rename = "ProtocolConfig")]
58pub struct ProtocolConfigResponse {
59    #[schemars(with = "AsProtocolVersion")]
60    #[serde_as(as = "Readable<AsProtocolVersion, _>")]
61    pub min_supported_protocol_version: ProtocolVersion,
62    #[schemars(with = "AsProtocolVersion")]
63    #[serde_as(as = "Readable<AsProtocolVersion, _>")]
64    pub max_supported_protocol_version: ProtocolVersion,
65    #[schemars(with = "AsProtocolVersion")]
66    #[serde_as(as = "Readable<AsProtocolVersion, _>")]
67    pub protocol_version: ProtocolVersion,
68    pub feature_flags: BTreeMap<String, bool>,
69    pub attributes: BTreeMap<String, Option<IotaProtocolConfigValue>>,
70}
71
72impl From<ProtocolConfig> for ProtocolConfigResponse {
73    fn from(config: ProtocolConfig) -> Self {
74        ProtocolConfigResponse {
75            protocol_version: config.version,
76            attributes: config
77                .attr_map()
78                .into_iter()
79                .map(|(k, v)| (k, v.map(IotaProtocolConfigValue::from)))
80                .collect(),
81            min_supported_protocol_version: ProtocolVersion::MIN,
82            max_supported_protocol_version: ProtocolVersion::MAX,
83            feature_flags: config.feature_map(),
84        }
85    }
86}