mithril_common/messages/
aggregator_features.rs

1use std::collections::BTreeSet;
2
3use serde::{Deserialize, Serialize};
4
5use crate::entities::SignedEntityTypeDiscriminants;
6
7/// Message advertised by an Aggregator to inform about its features
8#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
9pub struct AggregatorFeaturesMessage {
10    /// Version of the OpenAPI specification
11    pub open_api_version: String,
12
13    /// URL of the documentation
14    pub documentation_url: String,
15
16    /// Capabilities of the Aggregator
17    pub capabilities: AggregatorCapabilities,
18}
19
20/// Capabilities of an Aggregator
21#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
22pub struct AggregatorCapabilities {
23    /// Signed entity types that are signed by the aggregator
24    pub signed_entity_types: BTreeSet<SignedEntityTypeDiscriminants>,
25
26    /// Cardano transactions prover capabilities
27    #[serde(skip_serializing_if = "Option::is_none")]
28    pub cardano_transactions_prover: Option<CardanoTransactionsProverCapabilities>,
29}
30
31/// Cardano transactions prover capabilities
32#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
33pub struct CardanoTransactionsProverCapabilities {
34    /// Maximum number of hashes allowed for a single request
35    pub max_hashes_allowed_by_request: usize,
36}
37
38#[cfg(test)]
39mod tests {
40    use crate::entities::CardanoTransactionsSigningConfig;
41
42    use super::*;
43
44    #[derive(Debug, Serialize, Deserialize, PartialEq)]
45    struct AggregatorFeaturesMessageUntilV0_1_45 {
46        pub open_api_version: String,
47        pub documentation_url: String,
48        pub capabilities: AggregatorCapabilitiesUntilV0_1_45,
49    }
50
51    #[derive(Debug, Serialize, Deserialize, PartialEq)]
52    struct AggregatorCapabilitiesUntilV0_1_45 {
53        pub signed_entity_types: BTreeSet<SignedEntityTypeDiscriminants>,
54        #[serde(skip_serializing_if = "Option::is_none")]
55        pub cardano_transactions_prover: Option<CardanoTransactionsProverCapabilities>,
56        #[serde(skip_serializing_if = "Option::is_none")]
57        pub cardano_transactions_signing_config: Option<CardanoTransactionsSigningConfig>,
58    }
59
60    fn golden_message_until_open_api_0_1_45() -> AggregatorFeaturesMessageUntilV0_1_45 {
61        AggregatorFeaturesMessageUntilV0_1_45 {
62            open_api_version: "0.0.1".to_string(),
63            documentation_url: "https://example.com".to_string(),
64            capabilities: AggregatorCapabilitiesUntilV0_1_45 {
65                signed_entity_types: BTreeSet::from([
66                    SignedEntityTypeDiscriminants::CardanoTransactions,
67                ]),
68                cardano_transactions_prover: Some(CardanoTransactionsProverCapabilities {
69                    max_hashes_allowed_by_request: 100,
70                }),
71                cardano_transactions_signing_config: None,
72            },
73        }
74    }
75
76    fn golden_message_current() -> AggregatorFeaturesMessage {
77        AggregatorFeaturesMessage {
78            open_api_version: "0.0.1".to_string(),
79            documentation_url: "https://example.com".to_string(),
80            capabilities: AggregatorCapabilities {
81                signed_entity_types: BTreeSet::from([
82                    SignedEntityTypeDiscriminants::CardanoTransactions,
83                ]),
84                cardano_transactions_prover: Some(CardanoTransactionsProverCapabilities {
85                    max_hashes_allowed_by_request: 100,
86                }),
87            },
88        }
89    }
90
91    const CURRENT_JSON: &str = r#"{
92        "open_api_version": "0.0.1",
93        "documentation_url": "https://example.com",
94        "capabilities": {
95            "signed_entity_types": ["CardanoTransactions"],
96            "cardano_transactions_prover": {
97                "max_hashes_allowed_by_request": 100
98            }
99        }
100    }"#;
101
102    #[test]
103    fn test_current_json_deserialized_into_message_supported_until_open_api_0_1_45() {
104        let json = CURRENT_JSON;
105        let message: AggregatorFeaturesMessageUntilV0_1_45 = serde_json::from_str(json).unwrap();
106
107        assert_eq!(golden_message_until_open_api_0_1_45(), message);
108    }
109
110    #[test]
111    fn test_current_json_deserialized_into_current_message() {
112        let json = CURRENT_JSON;
113        let message: AggregatorFeaturesMessage = serde_json::from_str(json).unwrap();
114
115        assert_eq!(golden_message_current(), message);
116    }
117}