openmina_core/snark/
snark_cmp.rs

1use std::borrow::Cow;
2
3use mina_p2p_messages::v2::NonZeroCurvePoint;
4
5use super::{Snark, SnarkInfo, SnarkJobCommitment, SnarkJobId};
6
7#[derive(Debug, Eq, PartialEq)]
8pub struct SnarkCmp<'a> {
9    pub job_id: Cow<'a, SnarkJobId>,
10    pub fee: u64,
11    pub prover: &'a NonZeroCurvePoint,
12}
13
14impl SnarkCmp<'_> {
15    pub fn tie_breaker_hash(&self) -> [u8; 32] {
16        super::tie_breaker_hash(&self.job_id, self.prover)
17    }
18}
19
20impl Ord for SnarkCmp<'_> {
21    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
22        self.job_id
23            .cmp(&other.job_id)
24            .then_with(|| self.fee.cmp(&other.fee).reverse())
25            .then_with(|| self.tie_breaker_hash().cmp(&other.tie_breaker_hash()))
26    }
27}
28
29impl PartialOrd for SnarkCmp<'_> {
30    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
31        Some(self.cmp(other))
32    }
33}
34
35impl<'a> From<&'a SnarkJobCommitment> for SnarkCmp<'a> {
36    fn from(value: &'a SnarkJobCommitment) -> Self {
37        Self {
38            job_id: Cow::Borrowed(&value.job_id),
39            fee: value.fee.0.as_u64(),
40            prover: &value.snarker,
41        }
42    }
43}
44
45impl<'a> From<&'a Snark> for SnarkCmp<'a> {
46    fn from(value: &'a Snark) -> Self {
47        Self {
48            job_id: Cow::Owned(value.job_id()),
49            fee: value.fee.0.as_u64(),
50            prover: &value.snarker,
51        }
52    }
53}
54
55impl<'a> From<&'a SnarkInfo> for SnarkCmp<'a> {
56    fn from(value: &'a SnarkInfo) -> Self {
57        Self {
58            job_id: Cow::Borrowed(&value.job_id),
59            fee: value.fee.0.as_u64(),
60            prover: &value.prover,
61        }
62    }
63}
64
65pub fn snark_cmp<'a, A, B>(a: A, b: B) -> std::cmp::Ordering
66where
67    A: Into<SnarkCmp<'a>>,
68    B: Into<SnarkCmp<'a>>,
69{
70    a.into().cmp(&b.into())
71}
72
73impl<T> PartialEq<T> for SnarkJobCommitment
74where
75    for<'a> &'a T: Into<SnarkCmp<'a>>,
76{
77    fn eq(&self, other: &T) -> bool {
78        Into::<SnarkCmp<'_>>::into(self) == Into::<SnarkCmp<'_>>::into(other)
79    }
80}
81
82impl<T> PartialOrd<T> for SnarkJobCommitment
83where
84    for<'a> &'a T: Into<SnarkCmp<'a>>,
85{
86    fn partial_cmp(&self, other: &T) -> Option<std::cmp::Ordering> {
87        Some(snark_cmp(self, other))
88    }
89}
90
91impl<T> PartialEq<T> for SnarkInfo
92where
93    for<'a> &'a T: Into<SnarkCmp<'a>>,
94{
95    fn eq(&self, other: &T) -> bool {
96        Into::<SnarkCmp<'_>>::into(self) == Into::<SnarkCmp<'_>>::into(other)
97    }
98}
99
100impl<T> PartialOrd<T> for SnarkInfo
101where
102    for<'a> &'a T: Into<SnarkCmp<'a>>,
103{
104    fn partial_cmp(&self, other: &T) -> Option<std::cmp::Ordering> {
105        Some(snark_cmp(self, other))
106    }
107}
108
109impl<T> PartialEq<T> for Snark
110where
111    for<'a> &'a T: Into<SnarkCmp<'a>>,
112{
113    fn eq(&self, other: &T) -> bool {
114        Into::<SnarkCmp<'_>>::into(self) == Into::<SnarkCmp<'_>>::into(other)
115    }
116}
117
118impl<T> PartialOrd<T> for Snark
119where
120    for<'a> &'a T: Into<SnarkCmp<'a>>,
121{
122    fn partial_cmp(&self, other: &T) -> Option<std::cmp::Ordering> {
123        Some(snark_cmp(self, other))
124    }
125}