ccp_shared/
proof.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
17mod idx;
18pub mod raw;
19
20use serde::Deserialize;
21use serde::Serialize;
22
23pub use self::idx::ProofIdx;
24use crate::types;
25
26/// Uniquely identifies a proof.
27#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
28#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
29pub struct CCProofId {
30    pub global_nonce: types::GlobalNonce,
31    pub difficulty: types::Difficulty,
32    // unique in one epoch
33    pub idx: ProofIdx,
34}
35
36/// Contains all necessary information to submit proof to verify it.
37#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
38#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
39pub struct CCProof {
40    pub id: CCProofId,
41    pub local_nonce: types::LocalNonce,
42    pub cu_id: types::CUID,
43    pub result_hash: types::ResultHash,
44}
45
46impl CCProofId {
47    pub fn new(
48        global_nonce: types::GlobalNonce,
49        difficulty: types::Difficulty,
50        idx: ProofIdx,
51    ) -> Self {
52        Self {
53            global_nonce,
54            difficulty,
55            idx,
56        }
57    }
58
59    /// Returns true, if proofs was generated after the supplied one.
60    pub fn after(&self, other: &Self) -> bool {
61        self.after_raw(other.idx)
62    }
63
64    /// Returns true, if proofs was generated after the supplied proof index.
65    pub fn after_raw(&self, proof_idx: ProofIdx) -> bool {
66        self.idx > proof_idx
67    }
68}
69
70impl CCProof {
71    pub fn new(
72        id: CCProofId,
73        local_nonce: types::LocalNonce,
74        cu_id: types::CUID,
75        result_hash: types::ResultHash,
76    ) -> Self {
77        Self {
78            id,
79            local_nonce,
80            cu_id,
81            result_hash,
82        }
83    }
84
85    /// Returns true, if proofs was generated after the supplied one.
86    pub fn after(&self, other: &Self) -> bool {
87        self.id.idx > other.id.idx
88    }
89}
90
91#[derive(Debug, Clone, Serialize, Deserialize)]
92pub struct BatchRequest {
93    pub last_seen_proof_idx: ProofIdx,
94    pub proof_batch_size: usize,
95}
96
97#[derive(Debug, Clone, Serialize, Deserialize)]
98pub struct BatchResponse {
99    pub cu_id: types::CUID,
100    pub proof_batches: Vec<CCProof>,
101}
102
103impl BatchResponse {
104    pub fn new(cu_id: types::CUID, proof_batches: Vec<CCProof>) -> Self {
105        Self {
106            cu_id,
107            proof_batches,
108        }
109    }
110}