iota_json_rpc_types/
iota_protocol.rs1use 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
15const 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 u64_attribute = |name: &str| {
84 response.attributes.get(name).and_then(|value| match value {
85 Some(IotaProtocolConfigValue::U64(value)) => Some(*value),
86 _ => None,
87 })
88 };
89 Self {
90 allow_view_function: response
91 .feature_flags
92 .get(PACKAGE_METADATA_WITH_DYNAMIC_MODULE_METADATA)
93 .copied()
94 .unwrap_or(false),
95 max_move_package_size: u64_attribute("max_move_package_size"),
96 max_move_system_package_size: u64_attribute("max_move_system_package_size"),
97 }
98 }
99}
100
101impl From<ProtocolConfig> for ProtocolConfigResponse {
102 fn from(config: ProtocolConfig) -> Self {
103 ProtocolConfigResponse {
104 protocol_version: config.version,
105 attributes: config
106 .attr_map()
107 .into_iter()
108 .map(|(k, v)| (k, v.map(IotaProtocolConfigValue::from)))
109 .collect(),
110 min_supported_protocol_version: ProtocolVersion::MIN,
111 max_supported_protocol_version: ProtocolVersion::MAX,
112 feature_flags: config.feature_map(),
113 }
114 }
115}