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
20impl AggregatorFeaturesMessage {
21    /// Create a dummy AggregatorFeaturesMessage
22    pub fn dummy() -> Self {
23        AggregatorFeaturesMessage {
24            open_api_version: "0.0.1".to_string(),
25            documentation_url: "https://example.com".to_string(),
26            capabilities: AggregatorCapabilities {
27                signed_entity_types: BTreeSet::from([
28                    SignedEntityTypeDiscriminants::MithrilStakeDistribution,
29                ]),
30                cardano_transactions_prover: None,
31            },
32        }
33    }
34}
35
36/// Capabilities of an Aggregator
37#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
38pub struct AggregatorCapabilities {
39    /// Signed entity types that are signed by the aggregator
40    pub signed_entity_types: BTreeSet<SignedEntityTypeDiscriminants>,
41
42    /// Cardano transactions prover capabilities
43    #[serde(skip_serializing_if = "Option::is_none")]
44    pub cardano_transactions_prover: Option<CardanoTransactionsProverCapabilities>,
45}
46
47/// Cardano transactions prover capabilities
48#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
49pub struct CardanoTransactionsProverCapabilities {
50    /// Maximum number of hashes allowed for a single request
51    pub max_hashes_allowed_by_request: usize,
52}
53
54#[cfg(test)]
55mod tests {
56    use crate::entities::CardanoTransactionsSigningConfig;
57
58    use super::*;
59
60    #[derive(Debug, Serialize, Deserialize, PartialEq)]
61    struct AggregatorFeaturesMessageUntilV0_1_45 {
62        pub open_api_version: String,
63        pub documentation_url: String,
64        pub capabilities: AggregatorCapabilitiesUntilV0_1_45,
65    }
66
67    #[derive(Debug, Serialize, Deserialize, PartialEq)]
68    struct AggregatorCapabilitiesUntilV0_1_45 {
69        pub signed_entity_types: BTreeSet<SignedEntityTypeDiscriminants>,
70        #[serde(skip_serializing_if = "Option::is_none")]
71        pub cardano_transactions_prover: Option<CardanoTransactionsProverCapabilities>,
72        #[serde(skip_serializing_if = "Option::is_none")]
73        pub cardano_transactions_signing_config: Option<CardanoTransactionsSigningConfig>,
74    }
75
76    fn golden_message_until_open_api_0_1_45() -> AggregatorFeaturesMessageUntilV0_1_45 {
77        AggregatorFeaturesMessageUntilV0_1_45 {
78            open_api_version: "0.0.1".to_string(),
79            documentation_url: "https://example.com".to_string(),
80            capabilities: AggregatorCapabilitiesUntilV0_1_45 {
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                cardano_transactions_signing_config: None,
88            },
89        }
90    }
91
92    fn golden_message_current() -> AggregatorFeaturesMessage {
93        AggregatorFeaturesMessage {
94            open_api_version: "0.0.1".to_string(),
95            documentation_url: "https://example.com".to_string(),
96            capabilities: AggregatorCapabilities {
97                signed_entity_types: BTreeSet::from([
98                    SignedEntityTypeDiscriminants::CardanoTransactions,
99                ]),
100                cardano_transactions_prover: Some(CardanoTransactionsProverCapabilities {
101                    max_hashes_allowed_by_request: 100,
102                }),
103            },
104        }
105    }
106
107    const CURRENT_JSON: &str = r#"{
108        "open_api_version": "0.0.1",
109        "documentation_url": "https://example.com",
110        "capabilities": {
111            "signed_entity_types": ["CardanoTransactions"],
112            "cardano_transactions_prover": {
113                "max_hashes_allowed_by_request": 100
114            }
115        }
116    }"#;
117
118    #[test]
119    fn test_current_json_deserialized_into_message_supported_until_open_api_0_1_45() {
120        let json = CURRENT_JSON;
121        let message: AggregatorFeaturesMessageUntilV0_1_45 = serde_json::from_str(json).unwrap();
122
123        assert_eq!(golden_message_until_open_api_0_1_45(), message);
124    }
125
126    #[test]
127    fn test_current_json_deserialized_into_current_message() {
128        let json = CURRENT_JSON;
129        let message: AggregatorFeaturesMessage = serde_json::from_str(json).unwrap();
130
131        assert_eq!(golden_message_current(), message);
132    }
133}