|
| 1 | +use clap::Parser; |
| 2 | + |
| 3 | +use mithril_client::{ |
| 4 | + AggregatorDiscoveryType, ClientBuilder, MithrilNetwork, MithrilResult, |
| 5 | + RequiredAggregatorCapabilities, |
| 6 | + common::{AggregateSignatureType, SignedEntityTypeDiscriminants}, |
| 7 | +}; |
| 8 | + |
| 9 | +/// Clap command to select an aggregator from the available ones with automatic discovery. |
| 10 | +#[derive(Parser, Debug, Clone)] |
| 11 | +pub struct AggregatorSelectCommand { |
| 12 | + /// Path to the Cardano node database directory. |
| 13 | + #[clap(long)] |
| 14 | + network: MithrilNetwork, |
| 15 | + |
| 16 | + /// Maximum number of entries to retrieve |
| 17 | + #[clap(long, default_value_t = 1)] |
| 18 | + max_entries: usize, |
| 19 | + |
| 20 | + /// Signed entity types to consider for the discovery |
| 21 | + /// |
| 22 | + /// If not provided, all signed entity types are considered. |
| 23 | + #[clap(long, value_parser, num_args = 0.., value_delimiter = ',')] |
| 24 | + signed_entity_types: Vec<SignedEntityTypeDiscriminants>, |
| 25 | + |
| 26 | + /// Aggregate signature types to consider for the discovery |
| 27 | + /// |
| 28 | + /// If not provided, all aggregate signature types are considered. |
| 29 | + #[clap(long, value_parser, num_args = 0.., value_delimiter = ',')] |
| 30 | + aggregate_signature_types: Vec<AggregateSignatureType>, |
| 31 | +} |
| 32 | + |
| 33 | +impl AggregatorSelectCommand { |
| 34 | + /// Main command execution |
| 35 | + pub async fn execute(&self) -> MithrilResult<()> { |
| 36 | + let required_capabilities = self.build_required_capabilities(); |
| 37 | + let client_builder = |
| 38 | + ClientBuilder::new(AggregatorDiscoveryType::Automatic(self.network.clone())) |
| 39 | + .with_capabilities(required_capabilities) |
| 40 | + .with_default_aggregator_discoverer(); |
| 41 | + let aggregator_endpoints = client_builder |
| 42 | + .discover_aggregator(&self.network)? |
| 43 | + .take(self.max_entries); |
| 44 | + |
| 45 | + println!( |
| 46 | + "Discovering at most {} aggregator endpoints:", |
| 47 | + self.max_entries, |
| 48 | + ); |
| 49 | + let mut found_aggregators = 0; |
| 50 | + for endpoint in aggregator_endpoints { |
| 51 | + println!("- Found: {endpoint}"); |
| 52 | + found_aggregators += 1; |
| 53 | + } |
| 54 | + if found_aggregators == 0 { |
| 55 | + println!("- No aggregator endpoint found matching the requirements."); |
| 56 | + } |
| 57 | + |
| 58 | + Ok(()) |
| 59 | + } |
| 60 | + |
| 61 | + fn build_required_capabilities(&self) -> RequiredAggregatorCapabilities { |
| 62 | + if self.signed_entity_types.is_empty() && self.aggregate_signature_types.is_empty() { |
| 63 | + return RequiredAggregatorCapabilities::All; |
| 64 | + } |
| 65 | + |
| 66 | + let mut required_capabilities = vec![]; |
| 67 | + if !self.signed_entity_types.is_empty() { |
| 68 | + let mut required_capabilities_signed_entity_types = vec![]; |
| 69 | + for signed_entity_type in &self.signed_entity_types { |
| 70 | + required_capabilities_signed_entity_types.push( |
| 71 | + RequiredAggregatorCapabilities::SignedEntityType(*signed_entity_type), |
| 72 | + ); |
| 73 | + } |
| 74 | + required_capabilities.push(RequiredAggregatorCapabilities::Or( |
| 75 | + required_capabilities_signed_entity_types, |
| 76 | + )); |
| 77 | + } |
| 78 | + |
| 79 | + if !self.aggregate_signature_types.is_empty() { |
| 80 | + let mut required_capabilities_aggregate_signature_types = vec![]; |
| 81 | + for aggregate_signature_type in &self.aggregate_signature_types { |
| 82 | + required_capabilities_aggregate_signature_types.push( |
| 83 | + RequiredAggregatorCapabilities::AggregateSignatureType( |
| 84 | + *aggregate_signature_type, |
| 85 | + ), |
| 86 | + ); |
| 87 | + } |
| 88 | + required_capabilities.push(RequiredAggregatorCapabilities::Or( |
| 89 | + required_capabilities_aggregate_signature_types, |
| 90 | + )); |
| 91 | + } |
| 92 | + if required_capabilities.len() == 1 { |
| 93 | + return required_capabilities.into_iter().next().unwrap(); |
| 94 | + } else { |
| 95 | + return RequiredAggregatorCapabilities::And(required_capabilities); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +#[cfg(test)] |
| 101 | +mod tests { |
| 102 | + use super::*; |
| 103 | + use mithril_client::common::SignedEntityTypeDiscriminants; |
| 104 | + |
| 105 | + #[test] |
| 106 | + fn test_build_required_capabilities_all() { |
| 107 | + let command = AggregatorSelectCommand { |
| 108 | + network: MithrilNetwork::dummy(), |
| 109 | + max_entries: 1, |
| 110 | + signed_entity_types: vec![], |
| 111 | + aggregate_signature_types: vec![], |
| 112 | + }; |
| 113 | + |
| 114 | + let required_capabilities = command.build_required_capabilities(); |
| 115 | + assert_eq!(required_capabilities, RequiredAggregatorCapabilities::All); |
| 116 | + } |
| 117 | + |
| 118 | + #[test] |
| 119 | + fn test_build_required_capabilities_signed_entity_types() { |
| 120 | + let command = AggregatorSelectCommand { |
| 121 | + network: MithrilNetwork::dummy(), |
| 122 | + max_entries: 1, |
| 123 | + signed_entity_types: vec![ |
| 124 | + SignedEntityTypeDiscriminants::CardanoTransactions, |
| 125 | + SignedEntityTypeDiscriminants::CardanoStakeDistribution, |
| 126 | + ], |
| 127 | + aggregate_signature_types: vec![], |
| 128 | + }; |
| 129 | + |
| 130 | + let required_capabilities = command.build_required_capabilities(); |
| 131 | + |
| 132 | + assert_eq!( |
| 133 | + required_capabilities, |
| 134 | + RequiredAggregatorCapabilities::Or(vec![ |
| 135 | + RequiredAggregatorCapabilities::SignedEntityType( |
| 136 | + SignedEntityTypeDiscriminants::CardanoTransactions |
| 137 | + ), |
| 138 | + RequiredAggregatorCapabilities::SignedEntityType( |
| 139 | + SignedEntityTypeDiscriminants::CardanoStakeDistribution |
| 140 | + ), |
| 141 | + ]) |
| 142 | + ); |
| 143 | + } |
| 144 | + |
| 145 | + #[test] |
| 146 | + fn test_build_required_capabilities_aggregate_signature_types() { |
| 147 | + let command = AggregatorSelectCommand { |
| 148 | + network: MithrilNetwork::dummy(), |
| 149 | + max_entries: 1, |
| 150 | + signed_entity_types: vec![], |
| 151 | + aggregate_signature_types: vec![AggregateSignatureType::Concatenation], |
| 152 | + }; |
| 153 | + let required_capabilities = command.build_required_capabilities(); |
| 154 | + |
| 155 | + assert_eq!( |
| 156 | + required_capabilities, |
| 157 | + RequiredAggregatorCapabilities::Or(vec![ |
| 158 | + RequiredAggregatorCapabilities::AggregateSignatureType( |
| 159 | + AggregateSignatureType::Concatenation |
| 160 | + ), |
| 161 | + ]) |
| 162 | + ); |
| 163 | + } |
| 164 | + |
| 165 | + #[test] |
| 166 | + fn test_build_required_capabilities_both() { |
| 167 | + let command = AggregatorSelectCommand { |
| 168 | + network: MithrilNetwork::dummy(), |
| 169 | + max_entries: 1, |
| 170 | + signed_entity_types: vec![ |
| 171 | + SignedEntityTypeDiscriminants::CardanoTransactions, |
| 172 | + SignedEntityTypeDiscriminants::CardanoStakeDistribution, |
| 173 | + ], |
| 174 | + aggregate_signature_types: vec![AggregateSignatureType::Concatenation], |
| 175 | + }; |
| 176 | + let required_capabilities = command.build_required_capabilities(); |
| 177 | + |
| 178 | + assert_eq!( |
| 179 | + required_capabilities, |
| 180 | + RequiredAggregatorCapabilities::And(vec![ |
| 181 | + RequiredAggregatorCapabilities::Or(vec![ |
| 182 | + RequiredAggregatorCapabilities::SignedEntityType( |
| 183 | + SignedEntityTypeDiscriminants::CardanoTransactions |
| 184 | + ), |
| 185 | + RequiredAggregatorCapabilities::SignedEntityType( |
| 186 | + SignedEntityTypeDiscriminants::CardanoStakeDistribution |
| 187 | + ), |
| 188 | + ]), |
| 189 | + RequiredAggregatorCapabilities::Or(vec![ |
| 190 | + RequiredAggregatorCapabilities::AggregateSignatureType( |
| 191 | + AggregateSignatureType::Concatenation |
| 192 | + ), |
| 193 | + ]), |
| 194 | + ]) |
| 195 | + ); |
| 196 | + } |
| 197 | +} |
0 commit comments