Skip to main content

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::move_package::ProtocolBuildConfig;
9use schemars::JsonSchema;
10use serde::{Deserialize, Serialize};
11use serde_with::{DisplayFromStr, serde_as};
12
13use crate::iota_primitives::ProtocolVersion as ProtocolVersionSchema;
14
15/// Feature-flag key mirroring
16/// [`ProtocolConfig::package_metadata_with_dynamic_module_metadata`]; the flag
17/// is exposed in [`ProtocolConfigResponse::feature_flags`] under its field
18/// name.
19const PACKAGE_METADATA_WITH_DYNAMIC_MODULE_METADATA: &str =
20    "package_metadata_with_dynamic_module_metadata";
21
22#[serde_as]
23#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema, PartialEq)]
24#[serde(rename_all = "camelCase", rename = "ProtocolConfigValue")]
25pub enum IotaProtocolConfigValue {
26    U16(
27        #[schemars(with = "String")]
28        #[serde_as(as = "DisplayFromStr")]
29        u16,
30    ),
31    U32(
32        #[schemars(with = "String")]
33        #[serde_as(as = "DisplayFromStr")]
34        u32,
35    ),
36    U64(
37        #[schemars(with = "String")]
38        #[serde_as(as = "DisplayFromStr")]
39        u64,
40    ),
41    F64(
42        #[schemars(with = "String")]
43        #[serde_as(as = "DisplayFromStr")]
44        f64,
45    ),
46    Bool(
47        #[schemars(with = "String")]
48        #[serde_as(as = "DisplayFromStr")]
49        bool,
50    ),
51}
52
53impl From<ProtocolConfigValue> for IotaProtocolConfigValue {
54    fn from(value: ProtocolConfigValue) -> Self {
55        match value {
56            ProtocolConfigValue::u16(y) => IotaProtocolConfigValue::U16(y),
57            ProtocolConfigValue::u32(y) => IotaProtocolConfigValue::U32(y),
58            ProtocolConfigValue::u64(x) => IotaProtocolConfigValue::U64(x),
59            ProtocolConfigValue::bool(z) => IotaProtocolConfigValue::Bool(z),
60        }
61    }
62}
63
64#[serde_as]
65#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema, PartialEq)]
66#[serde(rename_all = "camelCase", rename = "ProtocolConfig")]
67pub struct ProtocolConfigResponse {
68    #[schemars(with = "ProtocolVersionSchema")]
69    #[serde_as(as = "ProtocolVersionSchema")]
70    pub min_supported_protocol_version: ProtocolVersion,
71    #[schemars(with = "ProtocolVersionSchema")]
72    #[serde_as(as = "ProtocolVersionSchema")]
73    pub max_supported_protocol_version: ProtocolVersion,
74    #[schemars(with = "ProtocolVersionSchema")]
75    #[serde_as(as = "ProtocolVersionSchema")]
76    pub protocol_version: ProtocolVersion,
77    pub feature_flags: BTreeMap<String, bool>,
78    pub attributes: BTreeMap<String, Option<IotaProtocolConfigValue>>,
79}
80
81impl From<&ProtocolConfigResponse> for ProtocolBuildConfig {
82    fn from(response: &ProtocolConfigResponse) -> Self {
83        let max_move_package_size =
84            response
85                .attributes
86                .get("max_move_package_size")
87                .and_then(|value| match value {
88                    Some(IotaProtocolConfigValue::U64(max)) => Some(*max),
89                    _ => None,
90                });
91        Self {
92            allow_view_function: response
93                .feature_flags
94                .get(PACKAGE_METADATA_WITH_DYNAMIC_MODULE_METADATA)
95                .copied()
96                .unwrap_or(false),
97            max_move_package_size,
98        }
99    }
100}
101
102impl From<ProtocolConfig> for ProtocolConfigResponse {
103    fn from(config: ProtocolConfig) -> Self {
104        ProtocolConfigResponse {
105            protocol_version: config.version,
106            attributes: config
107                .attr_map()
108                .into_iter()
109                .map(|(k, v)| (k, v.map(IotaProtocolConfigValue::from)))
110                .collect(),
111            min_supported_protocol_version: ProtocolVersion::MIN,
112            max_supported_protocol_version: ProtocolVersion::MAX,
113            feature_flags: config.feature_map(),
114        }
115    }
116}