1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * Copyright 2024 Fluence DAO
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

mod idx;

use serde::Deserialize;
use serde::Serialize;

pub use self::idx::ProofIdx;
use crate::types;

/// Uniquely identifies a proof.
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct CCProofId {
    pub global_nonce: types::GlobalNonce,
    pub difficulty: types::Difficulty,
    // unique in one epoch
    pub idx: ProofIdx,
}

/// Contains all necessary information to submit proof to verify it.
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct CCProof {
    pub id: CCProofId,
    pub local_nonce: types::LocalNonce,
    pub cu_id: types::CUID,
    pub result_hash: types::ResultHash,
}

impl CCProofId {
    pub fn new(
        global_nonce: types::GlobalNonce,
        difficulty: types::Difficulty,
        idx: ProofIdx,
    ) -> Self {
        Self {
            global_nonce,
            difficulty,
            idx,
        }
    }

    /// Returns true, if proofs was generated after the supplied one.
    pub fn after(&self, other: &Self) -> bool {
        self.after_raw(other.idx)
    }

    /// Returns true, if proofs was generated after the supplied proof index.
    pub fn after_raw(&self, proof_idx: ProofIdx) -> bool {
        self.idx > proof_idx
    }
}

impl CCProof {
    pub fn new(
        id: CCProofId,
        local_nonce: types::LocalNonce,
        cu_id: types::CUID,
        result_hash: types::ResultHash,
    ) -> Self {
        Self {
            id,
            local_nonce,
            cu_id,
            result_hash,
        }
    }

    /// Returns true, if proofs was generated after the supplied one.
    pub fn after(&self, other: &Self) -> bool {
        self.id.idx > other.id.idx
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatchRequest {
    pub last_seen_proof_idx: ProofIdx,
    pub proof_batch_size: usize,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatchResponse {
    pub cu_id: types::CUID,
    pub proof_batches: Vec<CCProof>,
}

impl BatchResponse {
    pub fn new(cu_id: types::CUID, proof_batches: Vec<CCProof>) -> Self {
        Self {
            cu_id,
            proof_batches,
        }
    }
}