|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use mithril_common::{StdResult, messages::AggregatorCapabilities}; |
| 4 | + |
| 5 | +use crate::{AggregatorDiscoverer, AggregatorEndpoint, MithrilNetwork}; |
| 6 | + |
| 7 | +/// An aggregator discoverer for specific capabilities. |
| 8 | +pub struct CapableAggregatorDiscoverer { |
| 9 | + capabilities: AggregatorCapabilities, |
| 10 | + inner_discoverer: Arc<dyn AggregatorDiscoverer>, |
| 11 | +} |
| 12 | + |
| 13 | +impl CapableAggregatorDiscoverer { |
| 14 | + /// Creates a new `CapableAggregatorDiscoverer` instance with the provided capabilities. |
| 15 | + pub fn new( |
| 16 | + capabilities: AggregatorCapabilities, |
| 17 | + inner_discoverer: Arc<dyn AggregatorDiscoverer>, |
| 18 | + ) -> Self { |
| 19 | + Self { |
| 20 | + capabilities, |
| 21 | + inner_discoverer, |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + /// Check if the available capabilities match the required capabilities. |
| 26 | + /// |
| 27 | + /// Returns true if: |
| 28 | + /// - The aggregate signature types are the same. |
| 29 | + /// - All required signed entity types are included in the available signed entity types. |
| 30 | + fn capabilities_match( |
| 31 | + required: &AggregatorCapabilities, |
| 32 | + available: &AggregatorCapabilities, |
| 33 | + ) -> bool { |
| 34 | + if available.aggregate_signature_type != required.aggregate_signature_type { |
| 35 | + return false; |
| 36 | + } |
| 37 | + |
| 38 | + let available_signed_entity_types = &available.signed_entity_types; |
| 39 | + let required_signed_entity_types = &required.signed_entity_types; |
| 40 | + |
| 41 | + required_signed_entity_types |
| 42 | + .iter() |
| 43 | + .all(|req| available_signed_entity_types.contains(req)) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/// An iterator over aggregator endpoints filtered by capabilities. |
| 48 | +struct CapableAggregatorDiscovererIterator { |
| 49 | + capabilities: AggregatorCapabilities, |
| 50 | + inner_iterator: Box<dyn Iterator<Item = AggregatorEndpoint>>, |
| 51 | +} |
| 52 | + |
| 53 | +impl Iterator for CapableAggregatorDiscovererIterator { |
| 54 | + type Item = AggregatorEndpoint; |
| 55 | + |
| 56 | + fn next(&mut self) -> Option<Self::Item> { |
| 57 | + for aggregator_endpoint in &mut self.inner_iterator { |
| 58 | + let aggregator_capabilities = tokio::runtime::Handle::current() |
| 59 | + .block_on(async { aggregator_endpoint.retrieve_capabilities().await }); |
| 60 | + if CapableAggregatorDiscoverer::capabilities_match( |
| 61 | + &self.capabilities, |
| 62 | + &aggregator_capabilities.ok()?, |
| 63 | + ) { |
| 64 | + return Some(aggregator_endpoint); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + None |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +#[async_trait::async_trait] |
| 73 | +impl AggregatorDiscoverer for CapableAggregatorDiscoverer { |
| 74 | + async fn get_available_aggregators( |
| 75 | + &self, |
| 76 | + network: MithrilNetwork, |
| 77 | + ) -> StdResult<Box<dyn Iterator<Item = AggregatorEndpoint>>> { |
| 78 | + let aggregator_endpoints = self.inner_discoverer.get_available_aggregators(network).await?; |
| 79 | + |
| 80 | + Ok(Box::new(CapableAggregatorDiscovererIterator { |
| 81 | + capabilities: self.capabilities.clone(), |
| 82 | + inner_iterator: aggregator_endpoints, |
| 83 | + })) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +#[cfg(test)] |
| 88 | +mod tests { |
| 89 | + use std::collections::BTreeSet; |
| 90 | + |
| 91 | + use mithril_common::{ |
| 92 | + AggregateSignatureType, |
| 93 | + entities::SignedEntityTypeDiscriminants::{ |
| 94 | + CardanoDatabase, CardanoStakeDistribution, CardanoTransactions, |
| 95 | + }, |
| 96 | + }; |
| 97 | + |
| 98 | + use super::*; |
| 99 | + |
| 100 | + #[test] |
| 101 | + fn capabilities_match_success() { |
| 102 | + let required = AggregatorCapabilities { |
| 103 | + aggregate_signature_type: AggregateSignatureType::Concatenation, |
| 104 | + signed_entity_types: BTreeSet::from([CardanoTransactions, CardanoStakeDistribution]), |
| 105 | + cardano_transactions_prover: None, |
| 106 | + }; |
| 107 | + |
| 108 | + let available = AggregatorCapabilities { |
| 109 | + aggregate_signature_type: AggregateSignatureType::Concatenation, |
| 110 | + signed_entity_types: BTreeSet::from([ |
| 111 | + CardanoTransactions, |
| 112 | + CardanoStakeDistribution, |
| 113 | + CardanoDatabase, |
| 114 | + ]), |
| 115 | + cardano_transactions_prover: None, |
| 116 | + }; |
| 117 | + |
| 118 | + assert!(CapableAggregatorDiscoverer::capabilities_match( |
| 119 | + &required, &available |
| 120 | + )); |
| 121 | + } |
| 122 | + |
| 123 | + #[test] |
| 124 | + fn capabilities_match_failure() { |
| 125 | + let required = AggregatorCapabilities { |
| 126 | + aggregate_signature_type: AggregateSignatureType::Concatenation, |
| 127 | + signed_entity_types: BTreeSet::from([CardanoTransactions, CardanoStakeDistribution]), |
| 128 | + cardano_transactions_prover: None, |
| 129 | + }; |
| 130 | + |
| 131 | + let available = AggregatorCapabilities { |
| 132 | + aggregate_signature_type: AggregateSignatureType::Concatenation, |
| 133 | + signed_entity_types: BTreeSet::from([CardanoTransactions]), |
| 134 | + cardano_transactions_prover: None, |
| 135 | + }; |
| 136 | + |
| 137 | + assert!(!CapableAggregatorDiscoverer::capabilities_match( |
| 138 | + &required, &available |
| 139 | + )); |
| 140 | + } |
| 141 | + |
| 142 | + #[tokio::test] |
| 143 | + async fn get_available_aggregators_success() { |
| 144 | + todo!() |
| 145 | + } |
| 146 | + |
| 147 | + #[tokio::test] |
| 148 | + async fn get_available_aggregators_success_when_one_aggregator_capabilities_does_not_match() { |
| 149 | + todo!() |
| 150 | + } |
| 151 | + |
| 152 | + #[tokio::test] |
| 153 | + async fn get_available_aggregators_success_when_one_aggregator_retruns_an_error() { |
| 154 | + todo!() |
| 155 | + } |
| 156 | +} |
0 commit comments