@@ -5,7 +5,7 @@ use crate::{
5
5
Address , BigNum , ChainOf , ValidatorId ,
6
6
} ;
7
7
use once_cell:: sync:: Lazy ;
8
- use serde:: { Deserialize , Deserializer , Serialize } ;
8
+ use serde:: { Deserialize , Serialize } ;
9
9
use std:: { collections:: HashMap , num:: NonZeroU8 , time:: Duration } ;
10
10
use thiserror:: Error ;
11
11
@@ -47,6 +47,10 @@ impl Default for Environment {
47
47
}
48
48
}
49
49
50
+ /// Examples:
51
+ /// ```
52
+ #[ doc = include_str ! ( "../../primitives/examples/get_cfg_response.rs" ) ]
53
+ /// ```
50
54
#[ derive( Serialize , Deserialize , Debug , Clone ) ]
51
55
pub struct Config {
52
56
/// The maximum number of [`Channel`](crate::Channel)s that the worker
@@ -73,7 +77,7 @@ pub struct Config {
73
77
/// finish before running a new tick in the Validator Worker.
74
78
///
75
79
/// In milliseconds
76
- #[ serde( deserialize_with = "milliseconds_to_std_duration " ) ]
80
+ #[ serde( with = "std_duration_millis " ) ]
77
81
pub wait_time : Duration ,
78
82
/// The maximum allowed limit of [`ValidatorMessage`](crate::sentry::ValidatorMessage)s per page
79
83
/// returned by Sentry's GET `/v5/channel/0xXXX.../validator-messages` route.
@@ -104,12 +108,12 @@ pub struct Config {
104
108
/// - GET `/v5/analytics/for-admin`
105
109
///
106
110
/// In milliseconds
107
- #[ serde( deserialize_with = "milliseconds_to_std_duration " ) ]
111
+ #[ serde( with = "std_duration_millis " ) ]
108
112
pub analytics_maxtime : Duration ,
109
113
/// The amount of time that should have passed before sending a new heartbeat.
110
114
///
111
115
/// In milliseconds
112
- #[ serde( deserialize_with = "milliseconds_to_std_duration " ) ]
116
+ #[ serde( with = "std_duration_millis " ) ]
113
117
pub heartbeat_time : Duration ,
114
118
/// The pro miles below which the [`ApproveState`](crate::validator::ApproveState)
115
119
/// becomes **unhealthy** in the [`Channel`](crate::Channel)'s Follower.
@@ -128,7 +132,7 @@ pub struct Config {
128
132
/// to a validator.
129
133
///
130
134
/// In milliseconds
131
- #[ serde( deserialize_with = "milliseconds_to_std_duration " ) ]
135
+ #[ serde( with = "std_duration_millis " ) ]
132
136
pub propagation_timeout : Duration ,
133
137
/// The Client timeout for `SentryApi`.
134
138
///
@@ -138,20 +142,20 @@ pub struct Config {
138
142
/// [`Config.propagation_timeout`](Config::propagation_timeout).
139
143
///
140
144
/// In milliseconds
141
- #[ serde( deserialize_with = "milliseconds_to_std_duration " ) ]
145
+ #[ serde( with = "std_duration_millis " ) ]
142
146
pub fetch_timeout : Duration ,
143
147
/// The Client timeout for `SentryApi` when collecting all channels
144
148
/// and Validators using the `/campaign/list` route.
145
149
///
146
150
/// In milliseconds
147
- #[ serde( deserialize_with = "milliseconds_to_std_duration " ) ]
151
+ #[ serde( with = "std_duration_millis " ) ]
148
152
pub all_campaigns_timeout : Duration ,
149
153
/// The timeout for a single tick of a [`Channel`](crate::Channel) in
150
154
/// the Validator Worker.
151
155
/// This timeout is applied to both the leader and follower ticks.
152
156
///
153
157
/// In milliseconds
154
- #[ serde( deserialize_with = "milliseconds_to_std_duration " ) ]
158
+ #[ serde( with = "std_duration_millis " ) ]
155
159
pub channel_tick_timeout : Duration ,
156
160
/// The default IP rate limit that will be imposed if
157
161
/// [`Campaign.event_submission`](crate::Campaign::event_submission) is [`None`].
@@ -210,7 +214,7 @@ impl Config {
210
214
#[ derive( Serialize , Deserialize , Debug , Clone ) ]
211
215
pub struct PlatformConfig {
212
216
pub url : ApiUrl ,
213
- #[ serde( deserialize_with = "milliseconds_to_std_duration " ) ]
217
+ #[ serde( with = "std_duration_millis " ) ]
214
218
pub keep_alive_interval : Duration ,
215
219
}
216
220
@@ -248,21 +252,35 @@ pub struct Limits {
248
252
pub units_for_slot : limits:: UnitsForSlot ,
249
253
}
250
254
251
- fn milliseconds_to_std_duration < ' de , D > ( deserializer : D ) -> Result < Duration , D :: Error >
252
- where
253
- D : Deserializer < ' de > ,
254
- {
255
- use serde:: de:: Error ;
256
- use toml:: Value ;
255
+ /// Module for [`Config`] (de)serialization of [`std::time::Duration`] from
256
+ /// and to milliseconds.
257
+ pub mod std_duration_millis {
258
+ use serde:: { Deserialize , Deserializer , Serializer } ;
259
+ use std:: time:: Duration ;
260
+
261
+ pub fn deserialize < ' de , D > ( deserializer : D ) -> Result < Duration , D :: Error >
262
+ where
263
+ D : Deserializer < ' de > ,
264
+ {
265
+ use serde:: de:: Error ;
266
+ use toml:: Value ;
257
267
258
- let toml_value: Value = Value :: deserialize ( deserializer) ?;
268
+ let toml_value: Value = Value :: deserialize ( deserializer) ?;
259
269
260
- let milliseconds = match toml_value {
261
- Value :: Integer ( mills) => u64:: try_from ( mills) . map_err ( Error :: custom) ,
262
- _ => Err ( Error :: custom ( "Only integers allowed for this value" ) ) ,
263
- } ?;
270
+ let milliseconds = match toml_value {
271
+ Value :: Integer ( mills) => u64:: try_from ( mills) . map_err ( Error :: custom) ,
272
+ _ => Err ( Error :: custom ( "Only integers allowed for this value" ) ) ,
273
+ } ?;
264
274
265
- Ok ( Duration :: from_millis ( milliseconds) )
275
+ Ok ( Duration :: from_millis ( milliseconds) )
276
+ }
277
+
278
+ pub fn serialize < S > ( duration : & Duration , serializer : S ) -> Result < S :: Ok , S :: Error >
279
+ where
280
+ S : Serializer ,
281
+ {
282
+ serializer. serialize_str ( & duration. as_millis ( ) . to_string ( ) )
283
+ }
266
284
}
267
285
268
286
pub mod limits {
0 commit comments