gadget_client_core/
lib.rs

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