1use crate::helpers::PlaceholderJsonSchemaImpl;
5use crate::models::DisplayRole;
6use crate::pagination::PaginatedResponse;
7use cosmwasm_std::Decimal;
8use nym_contracts_common::{IdentityKey, NaiveFloat};
9use nym_crypto::asymmetric::ed25519;
10use nym_crypto::asymmetric::ed25519::serde_helpers::bs58_ed25519_pubkey;
11use nym_mixnet_contract_common::reward_params::Performance;
12use nym_mixnet_contract_common::NodeId;
13use schemars::JsonSchema;
14use serde::{Deserialize, Serialize};
15use std::time::Duration;
16use time::{Date, OffsetDateTime};
17use utoipa::ToSchema;
18
19pub use config_score::*;
20
21pub type StakeSaturation = Decimal;
22
23#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, ToSchema)]
24#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
25#[cfg_attr(
26 feature = "generate-ts",
27 ts(
28 export,
29 export_to = "ts-packages/types/src/types/rust/StakeSaturationResponse.ts"
30 )
31)]
32pub struct StakeSaturationResponse {
33 #[cfg_attr(feature = "generate-ts", ts(type = "string"))]
34 #[schema(value_type = String)]
35 pub saturation: StakeSaturation,
36
37 #[cfg_attr(feature = "generate-ts", ts(type = "string"))]
38 #[schema(value_type = String)]
39 pub uncapped_saturation: StakeSaturation,
40 pub as_at: i64,
41}
42
43pub mod config_score {
44 use nym_contracts_common::NaiveFloat;
45 use serde::{Deserialize, Serialize};
46 use std::cmp::Ordering;
47 use utoipa::ToSchema;
48
49 #[derive(Clone, Debug, Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
50 pub struct ConfigScoreDataResponse {
51 pub parameters: ConfigScoreParams,
52 pub version_history: Vec<HistoricalNymNodeVersionEntry>,
53 }
54
55 #[derive(Clone, Debug, Serialize, Deserialize, schemars::JsonSchema, ToSchema, PartialEq)]
56 pub struct HistoricalNymNodeVersionEntry {
57 pub id: u32,
59
60 pub version_information: HistoricalNymNodeVersion,
62 }
63
64 impl PartialOrd for HistoricalNymNodeVersionEntry {
65 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
66 self.id.partial_cmp(&other.id)
68 }
69 }
70
71 impl From<nym_mixnet_contract_common::HistoricalNymNodeVersionEntry>
72 for HistoricalNymNodeVersionEntry
73 {
74 fn from(value: nym_mixnet_contract_common::HistoricalNymNodeVersionEntry) -> Self {
75 HistoricalNymNodeVersionEntry {
76 id: value.id,
77 version_information: value.version_information.into(),
78 }
79 }
80 }
81
82 #[derive(Clone, Debug, Serialize, Deserialize, schemars::JsonSchema, ToSchema, PartialEq)]
83 pub struct HistoricalNymNodeVersion {
84 pub semver: String,
87
88 pub introduced_at_height: u64,
90 }
93
94 impl From<nym_mixnet_contract_common::HistoricalNymNodeVersion> for HistoricalNymNodeVersion {
95 fn from(value: nym_mixnet_contract_common::HistoricalNymNodeVersion) -> Self {
96 HistoricalNymNodeVersion {
97 semver: value.semver,
98 introduced_at_height: value.introduced_at_height,
99 }
100 }
101 }
102
103 #[derive(Clone, Debug, Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
104 pub struct ConfigScoreParams {
105 pub version_weights: OutdatedVersionWeights,
107
108 pub version_score_formula_params: VersionScoreFormulaParams,
110 }
111
112 #[derive(Clone, Debug, Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
114 pub struct OutdatedVersionWeights {
115 pub major: u32,
116 pub minor: u32,
117 pub patch: u32,
118 pub prerelease: u32,
119 }
120
121 #[derive(Clone, Debug, Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
124 pub struct VersionScoreFormulaParams {
125 pub penalty: f64,
126 pub penalty_scaling: f64,
127 }
128
129 impl From<nym_mixnet_contract_common::ConfigScoreParams> for ConfigScoreParams {
130 fn from(value: nym_mixnet_contract_common::ConfigScoreParams) -> Self {
131 ConfigScoreParams {
132 version_weights: value.version_weights.into(),
133 version_score_formula_params: value.version_score_formula_params.into(),
134 }
135 }
136 }
137
138 impl From<nym_mixnet_contract_common::OutdatedVersionWeights> for OutdatedVersionWeights {
139 fn from(value: nym_mixnet_contract_common::OutdatedVersionWeights) -> Self {
140 OutdatedVersionWeights {
141 major: value.major,
142 minor: value.minor,
143 patch: value.patch,
144 prerelease: value.prerelease,
145 }
146 }
147 }
148
149 impl From<nym_mixnet_contract_common::VersionScoreFormulaParams> for VersionScoreFormulaParams {
150 fn from(value: nym_mixnet_contract_common::VersionScoreFormulaParams) -> Self {
151 VersionScoreFormulaParams {
152 penalty: value.penalty.naive_to_f64(),
153 penalty_scaling: value.penalty_scaling.naive_to_f64(),
154 }
155 }
156 }
157}
158
159#[derive(Clone, Debug, Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
160pub struct NodeRefreshBody {
161 #[serde(with = "bs58_ed25519_pubkey")]
162 #[schemars(with = "String")]
163 #[schema(value_type = String)]
164 pub node_identity: ed25519::PublicKey,
165
166 pub request_timestamp: i64,
168
169 #[schemars(with = "PlaceholderJsonSchemaImpl")]
170 #[schema(value_type = String)]
171 pub signature: ed25519::Signature,
172}
173
174impl NodeRefreshBody {
175 pub fn plaintext(node_identity: ed25519::PublicKey, request_timestamp: i64) -> Vec<u8> {
176 node_identity
177 .to_bytes()
178 .into_iter()
179 .chain(request_timestamp.to_be_bytes())
180 .chain(b"describe-cache-refresh-request".iter().copied())
181 .collect()
182 }
183
184 pub fn new(private_key: &ed25519::PrivateKey) -> Self {
185 let node_identity = private_key.public_key();
186 let request_timestamp = OffsetDateTime::now_utc().unix_timestamp();
187 let signature = private_key.sign(Self::plaintext(node_identity, request_timestamp));
188 NodeRefreshBody {
189 node_identity,
190 request_timestamp,
191 signature,
192 }
193 }
194
195 pub fn verify_signature(&self) -> bool {
196 self.node_identity
197 .verify(
198 Self::plaintext(self.node_identity, self.request_timestamp),
199 &self.signature,
200 )
201 .is_ok()
202 }
203
204 pub fn is_stale(&self) -> bool {
205 let Ok(encoded) = OffsetDateTime::from_unix_timestamp(self.request_timestamp) else {
206 return true;
207 };
208 let now = OffsetDateTime::now_utc();
209
210 if encoded > now {
211 return true;
212 }
213
214 if (encoded + Duration::from_secs(30)) < now {
215 return true;
216 }
217
218 false
219 }
220}
221
222#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, ToSchema)]
223pub struct UptimeResponse {
224 #[schema(value_type = u32)]
225 pub mix_id: NodeId,
226 pub avg_uptime: u8,
228 pub node_performance: NodePerformance,
229}
230
231#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, ToSchema)]
232pub struct GatewayUptimeResponse {
233 pub identity: String,
234 pub avg_uptime: u8,
236 pub node_performance: NodePerformance,
237}
238
239type Uptime = u8;
240
241#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
242pub struct MixnodeStatusReportResponse {
243 pub mix_id: NodeId,
244 pub identity: IdentityKey,
245 pub owner: String,
246 #[schema(value_type = u8)]
247 pub most_recent: Uptime,
248 #[schema(value_type = u8)]
249 pub last_hour: Uptime,
250 #[schema(value_type = u8)]
251 pub last_day: Uptime,
252}
253
254#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
255pub struct GatewayStatusReportResponse {
256 pub identity: String,
257 pub owner: String,
258 #[schema(value_type = u8)]
259 pub most_recent: Uptime,
260 #[schema(value_type = u8)]
261 pub last_hour: Uptime,
262 #[schema(value_type = u8)]
263 pub last_day: Uptime,
264}
265
266#[derive(Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
267#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
268#[cfg_attr(
269 feature = "generate-ts",
270 ts(
271 export,
272 export_to = "ts-packages/types/src/types/rust/PerformanceHistoryResponse.ts"
273 )
274)]
275pub struct PerformanceHistoryResponse {
276 #[schema(value_type = u32)]
277 pub node_id: NodeId,
278 pub history: PaginatedResponse<HistoricalPerformanceResponse>,
279}
280
281#[derive(Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
282#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
283#[cfg_attr(
284 feature = "generate-ts",
285 ts(
286 export,
287 export_to = "ts-packages/types/src/types/rust/UptimeHistoryResponse.ts"
288 )
289)]
290pub struct UptimeHistoryResponse {
291 #[schema(value_type = u32)]
292 pub node_id: NodeId,
293 pub history: PaginatedResponse<HistoricalUptimeResponse>,
294}
295
296#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
297#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
298#[cfg_attr(
299 feature = "generate-ts",
300 ts(
301 export,
302 export_to = "ts-packages/types/src/types/rust/HistoricalUptimeResponse.ts"
303 )
304)]
305pub struct HistoricalUptimeResponse {
306 #[schema(value_type = String, example = "1970-01-01")]
307 #[schemars(with = "String")]
308 #[cfg_attr(feature = "generate-ts", ts(type = "string"))]
309 pub date: Date,
310
311 pub uptime: Uptime,
312}
313
314#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
315#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
316#[cfg_attr(
317 feature = "generate-ts",
318 ts(
319 export,
320 export_to = "ts-packages/types/src/types/rust/HistoricalPerformanceResponse.ts"
321 )
322)]
323pub struct HistoricalPerformanceResponse {
324 #[schema(value_type = String, example = "1970-01-01")]
325 #[schemars(with = "String")]
326 #[cfg_attr(feature = "generate-ts", ts(type = "string"))]
327 pub date: Date,
328
329 pub performance: f64,
330}
331
332#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
333pub struct OldHistoricalUptimeResponse {
334 pub date: String,
335 #[schema(value_type = u8)]
336 pub uptime: Uptime,
337}
338
339#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
340pub struct MixnodeUptimeHistoryResponse {
341 pub mix_id: NodeId,
342 pub identity: String,
343 pub history: Vec<OldHistoricalUptimeResponse>,
344}
345
346#[derive(Clone, Serialize, Deserialize, schemars::JsonSchema, ToSchema)]
347pub struct GatewayUptimeHistoryResponse {
348 pub identity: String,
349 pub history: Vec<OldHistoricalUptimeResponse>,
350}
351
352#[derive(
353 Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema, ToSchema, Default,
354)]
355#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
356#[cfg_attr(
357 feature = "generate-ts",
358 ts(
359 export,
360 export_to = "ts-packages/types/src/types/rust/MixnodeStatus.ts"
361 )
362)]
363#[serde(rename_all = "snake_case")]
364pub enum MixnodeStatus {
365 Active, Standby, Inactive, #[default]
369 NotFound, }
371impl MixnodeStatus {
372 pub fn is_active(&self) -> bool {
373 *self == MixnodeStatus::Active
374 }
375}
376
377#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, ToSchema)]
378#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
379#[cfg_attr(
380 feature = "generate-ts",
381 ts(
382 export,
383 export_to = "ts-packages/types/src/types/rust/MixnodeStatusResponse.ts"
384 )
385)]
386pub struct MixnodeStatusResponse {
387 pub status: MixnodeStatus,
388}
389
390#[derive(Clone, Debug, Default, Serialize, Deserialize, JsonSchema, ToSchema)]
391pub struct NodePerformance {
392 #[schema(value_type = String)]
393 pub most_recent: Performance,
394 #[schema(value_type = String)]
395 pub last_hour: Performance,
396 #[schema(value_type = String)]
397 pub last_24h: Performance,
398}
399
400#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, JsonSchema, ToSchema)]
404#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
405#[cfg_attr(
406 feature = "generate-ts",
407 ts(
408 export,
409 export_to = "ts-packages/types/src/types/rust/NodeAnnotationV1.ts"
410 )
411)]
412pub struct NodeAnnotationV1 {
413 #[cfg_attr(feature = "generate-ts", ts(type = "string"))]
414 #[schema(value_type = String)]
416 pub last_24h_performance: Performance,
417 pub current_role: Option<DisplayRole>,
418
419 pub detailed_performance: DetailedNodePerformanceV1,
420}
421
422#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, JsonSchema, ToSchema)]
423#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
424#[cfg_attr(
425 feature = "generate-ts",
426 ts(
427 export,
428 export_to = "ts-packages/types/src/types/rust/NodeAnnotationV2.ts"
429 )
430)]
431pub struct NodeAnnotationV2 {
432 pub current_role: Option<DisplayRole>,
433
434 pub detailed_performance: DetailedNodePerformanceV2,
435}
436
437impl From<NodeAnnotationV2> for NodeAnnotationV1 {
438 fn from(value: NodeAnnotationV2) -> Self {
439 let scaled_performance =
441 value.detailed_performance.performance_score.clamp(0.0, 1.0) * 100.;
442 #[allow(clippy::unwrap_used)]
443 let legacy_performance =
444 Performance::from_percentage_value(scaled_performance as u64).unwrap();
445
446 NodeAnnotationV1 {
447 last_24h_performance: legacy_performance,
448 current_role: value.current_role,
449 detailed_performance: DetailedNodePerformanceV1 {
450 performance_score: value.detailed_performance.performance_score,
451 routing_score: value.detailed_performance.routing_score,
452 config_score: value.detailed_performance.config_score,
453 },
454 }
455 }
456}
457
458#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, JsonSchema, ToSchema)]
459#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
460#[cfg_attr(
461 feature = "generate-ts",
462 ts(
463 export,
464 export_to = "ts-packages/types/src/types/rust/DetailedNodePerformanceV1.ts"
465 )
466)]
467#[non_exhaustive]
468pub struct DetailedNodePerformanceV1 {
469 pub performance_score: f64,
471
472 pub routing_score: RoutingScore,
473 pub config_score: ConfigScore,
474}
475
476impl DetailedNodePerformanceV1 {
477 pub fn new(
478 performance_score: f64,
479 routing_score: RoutingScore,
480 config_score: ConfigScore,
481 ) -> DetailedNodePerformanceV1 {
482 Self {
483 performance_score,
484 routing_score,
485 config_score,
486 }
487 }
488
489 pub fn to_rewarding_performance(&self) -> Performance {
490 Performance::naive_try_from_f64(self.performance_score).unwrap_or_default()
491 }
492}
493
494#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, JsonSchema, ToSchema)]
495#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
496#[cfg_attr(
497 feature = "generate-ts",
498 ts(
499 export,
500 export_to = "ts-packages/types/src/types/rust/DetailedNodePerformanceV2.ts"
501 )
502)]
503#[non_exhaustive]
504pub struct DetailedNodePerformanceV2 {
505 pub performance_score: f64,
509
510 pub routing_score: RoutingScore,
511 pub config_score: ConfigScore,
512 pub stress_testing_score: StressTestingScore,
513}
514
515impl DetailedNodePerformanceV2 {
516 pub fn new(
517 performance_score: f64,
518 routing_score: RoutingScore,
519 config_score: ConfigScore,
520 stress_testing_score: StressTestingScore,
521 ) -> DetailedNodePerformanceV2 {
522 Self {
523 performance_score,
524 routing_score,
525 config_score,
526 stress_testing_score,
527 }
528 }
529
530 pub fn to_rewarding_performance(&self) -> Performance {
531 Performance::naive_try_from_f64(self.performance_score).unwrap_or_default()
532 }
533}
534
535#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, JsonSchema, ToSchema)]
536#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
537#[cfg_attr(
538 feature = "generate-ts",
539 ts(export, export_to = "ts-packages/types/src/types/rust/RoutingScore.ts")
540)]
541#[non_exhaustive]
542pub struct RoutingScore {
543 pub score: f64,
545}
546
547impl RoutingScore {
548 pub fn new(score: f64) -> RoutingScore {
549 Self { score }
550 }
551
552 pub const fn zero() -> RoutingScore {
553 RoutingScore { score: 0.0 }
554 }
555
556 pub fn legacy_performance(&self) -> Performance {
557 Performance::naive_try_from_f64(self.score).unwrap_or_default()
558 }
559}
560
561#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, JsonSchema, ToSchema)]
562#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
563#[cfg_attr(
564 feature = "generate-ts",
565 ts(
566 export,
567 export_to = "ts-packages/types/src/types/rust/StressTestingScore.ts"
568 )
569)]
570pub struct StressTestingScore {
571 pub score: f64,
572 pub was_reachable: bool,
576}
577
578impl StressTestingScore {
579 pub fn unreachable() -> Self {
580 StressTestingScore {
581 score: 0.0,
582 was_reachable: false,
583 }
584 }
585}
586
587#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, JsonSchema, ToSchema)]
588#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
589#[cfg_attr(
590 feature = "generate-ts",
591 ts(export, export_to = "ts-packages/types/src/types/rust/ConfigScore.ts")
592)]
593#[non_exhaustive]
594pub struct ConfigScore {
595 pub score: f64,
597
598 pub versions_behind: Option<u32>,
599 pub self_described_api_available: bool,
600 pub accepted_terms_and_conditions: bool,
601 pub runs_nym_node_binary: bool,
602}
603
604impl ConfigScore {
605 pub fn new(
606 score: f64,
607 versions_behind: u32,
608 accepted_terms_and_conditions: bool,
609 runs_nym_node_binary: bool,
610 ) -> ConfigScore {
611 Self {
612 score,
613 versions_behind: Some(versions_behind),
614 self_described_api_available: true,
615 accepted_terms_and_conditions,
616 runs_nym_node_binary,
617 }
618 }
619
620 pub fn bad_semver() -> ConfigScore {
621 ConfigScore {
622 score: 0.0,
623 versions_behind: None,
624 self_described_api_available: true,
625 accepted_terms_and_conditions: false,
626 runs_nym_node_binary: false,
627 }
628 }
629
630 pub fn unavailable() -> ConfigScore {
631 ConfigScore {
632 score: 0.0,
633 versions_behind: None,
634 self_described_api_available: false,
635 accepted_terms_and_conditions: false,
636 runs_nym_node_binary: false,
637 }
638 }
639}
640
641#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, ToSchema)]
642#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
643#[cfg_attr(
644 feature = "generate-ts",
645 ts(
646 export,
647 export_to = "ts-packages/types/src/types/rust/AnnotationResponseV1.ts"
648 )
649)]
650pub struct AnnotationResponseV1 {
651 #[schema(value_type = u32)]
652 pub node_id: NodeId,
653 pub annotation: Option<NodeAnnotationV1>,
654}
655
656#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, ToSchema)]
657#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
658#[cfg_attr(
659 feature = "generate-ts",
660 ts(
661 export,
662 export_to = "ts-packages/types/src/types/rust/AnnotationResponseV2.ts"
663 )
664)]
665pub struct AnnotationResponseV2 {
666 #[schema(value_type = u32)]
667 pub node_id: NodeId,
668 pub annotation: Option<NodeAnnotationV2>,
669}
670
671#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, ToSchema)]
672#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
673#[cfg_attr(
674 feature = "generate-ts",
675 ts(
676 export,
677 export_to = "ts-packages/types/src/types/rust/NodePerformanceResponse.ts"
678 )
679)]
680pub struct NodePerformanceResponse {
681 #[schema(value_type = u32)]
682 pub node_id: NodeId,
683 pub performance: Option<f64>,
684}
685
686#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, ToSchema)]
687#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
688#[cfg_attr(
689 feature = "generate-ts",
690 ts(
691 export,
692 export_to = "ts-packages/types/src/types/rust/NodeDatePerformanceResponse.ts"
693 )
694)]
695pub struct NodeDatePerformanceResponse {
696 #[schema(value_type = u32)]
697 pub node_id: NodeId,
698 #[schema(value_type = String, example = "1970-01-01")]
699 #[schemars(with = "String")]
700 #[cfg_attr(feature = "generate-ts", ts(type = "string"))]
701 pub date: Date,
702 pub performance: Option<f64>,
703}