tendermint/block/
size.rs

1//! Block size parameters
2
3use serde::{Deserialize, Serialize};
4
5use crate::serializers;
6
7/// Block size parameters
8#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
9pub struct Size {
10    /// Maximum number of bytes in a block
11    #[serde(with = "serializers::from_str")]
12    pub max_bytes: u64,
13
14    /// Maximum amount of gas which can be spent on a block
15    #[serde(with = "serializers::from_str")]
16    pub max_gas: i64,
17
18    /// This parameter has no value anymore in Tendermint-core
19    #[serde(with = "serializers::from_str", default = "Size::default_time_iota_ms")]
20    pub time_iota_ms: i64,
21}
22
23impl Size {
24    /// The default value for the `time_iota_ms` parameter.
25    pub const fn default_time_iota_ms() -> i64 {
26        1000
27    }
28}
29
30mod v0_34 {
31    use super::Size;
32    use crate::error::Error;
33    use tendermint_proto::v0_34::{
34        abci::BlockParams as RawAbciSize, types::BlockParams as RawSize,
35    };
36    use tendermint_proto::Protobuf;
37
38    impl Protobuf<RawSize> for Size {}
39
40    impl TryFrom<RawSize> for Size {
41        type Error = Error;
42
43        fn try_from(value: RawSize) -> Result<Self, Self::Error> {
44            Ok(Self {
45                max_bytes: value
46                    .max_bytes
47                    .try_into()
48                    .map_err(Error::integer_overflow)?,
49                max_gas: value.max_gas,
50                time_iota_ms: value.time_iota_ms,
51            })
52        }
53    }
54
55    impl From<Size> for RawSize {
56        fn from(value: Size) -> Self {
57            // Todo: make the struct more robust so this can become infallible.
58            RawSize {
59                max_bytes: value.max_bytes as i64,
60                max_gas: value.max_gas,
61                time_iota_ms: value.time_iota_ms,
62            }
63        }
64    }
65
66    impl Protobuf<RawAbciSize> for Size {}
67
68    impl TryFrom<RawAbciSize> for Size {
69        type Error = Error;
70
71        fn try_from(value: RawAbciSize) -> Result<Self, Self::Error> {
72            Ok(Self {
73                max_bytes: value
74                    .max_bytes
75                    .try_into()
76                    .map_err(Error::integer_overflow)?,
77                max_gas: value.max_gas,
78                time_iota_ms: Self::default_time_iota_ms(),
79            })
80        }
81    }
82
83    impl From<Size> for RawAbciSize {
84        fn from(value: Size) -> Self {
85            // Todo: make the struct more robust so this can become infallible.
86            RawAbciSize {
87                max_bytes: value.max_bytes as i64,
88                max_gas: value.max_gas,
89            }
90        }
91    }
92}
93
94mod v0_37 {
95    use super::Size;
96    use crate::error::Error;
97    use tendermint_proto::v0_37::types::BlockParams as RawSize;
98    use tendermint_proto::Protobuf;
99
100    impl Protobuf<RawSize> for Size {}
101
102    impl TryFrom<RawSize> for Size {
103        type Error = Error;
104
105        fn try_from(value: RawSize) -> Result<Self, Self::Error> {
106            Ok(Self {
107                max_bytes: value
108                    .max_bytes
109                    .try_into()
110                    .map_err(Error::integer_overflow)?,
111                max_gas: value.max_gas,
112                time_iota_ms: Size::default_time_iota_ms(),
113            })
114        }
115    }
116
117    impl From<Size> for RawSize {
118        fn from(value: Size) -> Self {
119            // Todo: make the struct more robust so this can become infallible.
120            RawSize {
121                max_bytes: value.max_bytes as i64,
122                max_gas: value.max_gas,
123            }
124        }
125    }
126}
127
128mod v0_38 {
129    use super::Size;
130    use crate::error::Error;
131    use tendermint_proto::v0_38::types::BlockParams as RawSize;
132    use tendermint_proto::Protobuf;
133
134    impl Protobuf<RawSize> for Size {}
135
136    impl TryFrom<RawSize> for Size {
137        type Error = Error;
138
139        fn try_from(value: RawSize) -> Result<Self, Self::Error> {
140            Ok(Self {
141                max_bytes: value
142                    .max_bytes
143                    .try_into()
144                    .map_err(Error::integer_overflow)?,
145                max_gas: value.max_gas,
146                time_iota_ms: Size::default_time_iota_ms(),
147            })
148        }
149    }
150
151    impl From<Size> for RawSize {
152        fn from(value: Size) -> Self {
153            // Todo: make the struct more robust so this can become infallible.
154            RawSize {
155                max_bytes: value.max_bytes as i64,
156                max_gas: value.max_gas,
157            }
158        }
159    }
160}