ccp_shared/
nox_ccp_api.rs

1/*
2 * Copyright 2024 Fluence DAO
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use std::collections::HashMap;
18use std::future::IntoFuture;
19
20use super::types::*;
21use crate::proof::BatchRequest;
22use crate::proof::BatchResponse;
23use crate::proof::ProofIdx;
24
25pub trait NoxCCPApi: Send {
26    type Error: Send;
27    type CommitmentError;
28    type CommitmentReadiness: IntoFuture<Output = Result<(), Self::CommitmentError>>;
29
30    /// Aims to apply a (new) allocation of CC compute jobs (which are threads with
31    /// particular global_nonce_cu + difficulty) per physical_core_id.
32    ///
33    /// It checks if a job already exists, in this case it doesn't recreate it,
34    /// but probably pin to another physical core according to the supplied allocation map.
35    /// If there is no such job, it creates,
36    /// if jobs are outdated (global_nonce_cu + difficulty aren't the same as supplied),
37    /// then it restart them all with the provided parameters.
38    ///
39    /// All other cores which aren't defined in the supplied map is supposed to manage
40    /// "useful" job in workers and existing jobs for them will be released.
41    ///
42    /// This function is supposed to be called on any of the following situations:
43    ///  - new global nonce or difficulty (== new epoch)
44    ///  - CU allocation is changed (e.g. a core is assigned or released to CC)
45    ///    after event from the on-chain part
46    ///  - Nox (re)started
47    fn on_active_commitment(
48        &mut self,
49        epoch_parameters: EpochParameters,
50        cu_allocation: CUAllocation,
51    ) -> impl std::future::Future<Output = Result<Self::CommitmentReadiness, Self::Error>> + Send;
52
53    /// Stops all active jobs.
54    fn on_no_active_commitment(
55        &mut self,
56    ) -> impl std::future::Future<Output = Result<(), Self::Error>> + Send;
57
58    /// Returns proofs after the provided last known proof indexes for current epoch.
59    fn get_proofs_after(
60        &self,
61        last_known_proofs: HashMap<CUID, ProofIdx>,
62        limit: usize,
63    ) -> impl std::future::Future<Output = Result<Vec<BatchResponse>, Self::Error>> + Send;
64
65    /// Returns proof batch after the last provided proof idx for current epoch.
66    fn get_batch_proofs_after(
67        &self,
68        reqs: HashMap<CUID, BatchRequest>,
69        min_batch_count: usize,
70        max_batch_count: usize,
71    ) -> impl std::future::Future<Output = Result<Vec<BatchResponse>, Self::Error>> + Send;
72
73    /// Set utility
74    fn realloc_utility_cores(
75        &self,
76        utility_core_ids: Vec<LogicalCoreId>,
77    ) -> impl std::future::Future<Output = ()> + Send;
78}