blueprint_client_core/
lib.rs

1pub mod error;
2use error::Error;
3
4use blueprint_std::hash::Hash;
5
6pub type OperatorSet<K, V> = std::collections::BTreeMap<K, V>;
7
8#[auto_impl::auto_impl(&, Arc)]
9pub trait BlueprintServicesClient: Send + Sync + 'static {
10    /// The ID of for operators at the blueprint/application layer. Typically a cryptograpgic key in the form of a point on
11    /// some elliptic curve, e.g., an ECDSA public key (point). However, this is not required.
12    type PublicApplicationIdentity: Eq + PartialEq + Hash + Ord + PartialOrd + Send + Sync + 'static;
13    /// The ID of the operator's account, not necessarily associated with the `PublicApplicationIdentity`,
14    /// but may be cryptographically related thereto. E.g., AccountId32
15    type PublicAccountIdentity: Send + Sync + 'static;
16    /// A generalized ID that distinguishes the current blueprint from others
17    type Id: Send + Sync + 'static;
18    type Error: core::error::Error + From<Error> + Send + Sync + 'static;
19
20    /// Returns the set of operators for the current job
21    async fn get_operators(
22        &self,
23    ) -> Result<
24        OperatorSet<Self::PublicAccountIdentity, Self::PublicApplicationIdentity>,
25        Self::Error,
26    >;
27    /// Returns the ID of the operator
28    async fn operator_id(&self) -> Result<Self::PublicApplicationIdentity, Self::Error>;
29    /// Returns the unique ID for this blueprint
30    async fn blueprint_id(&self) -> Result<Self::Id, Self::Error>;
31
32    /// Returns an operator set with the index of the current operator within that set
33    async fn get_operators_and_operator_id(
34        &self,
35    ) -> Result<(OperatorSet<usize, Self::PublicApplicationIdentity>, usize), Self::Error> {
36        let operators = self
37            .get_operators()
38            .await
39            .map_err(|e| Error::GetOperatorsAndOperatorId(e.to_string()))?;
40        let my_id = self
41            .operator_id()
42            .await
43            .map_err(|e| Error::GetOperatorsAndOperatorId(e.to_string()))?;
44        let mut ret = OperatorSet::new();
45        let mut ret_id = None;
46        for (id, op) in operators.into_values().enumerate() {
47            if my_id == op {
48                ret_id = Some(id);
49            }
50
51            ret.insert(id, op);
52        }
53
54        let ret_id = ret_id.ok_or_else(|| {
55            Error::GetOperatorsAndOperatorId("Operator index not found".to_string())
56        })?;
57        Ok((ret, ret_id))
58    }
59
60    /// Returns the index of the current operator in the operator set
61    async fn get_operator_index(&self) -> Result<usize, Self::Error> {
62        let (_, index) = self
63            .get_operators_and_operator_id()
64            .await
65            .map_err(|err| Error::GetOperatorIndex(err.to_string()))?;
66        Ok(index)
67    }
68}