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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use crate::predicates as preds;
use crate::{
errors::ErrorExt,
light_client::Options,
operations::{
CommitValidator, Hasher, ProdCommitValidator, ProdHasher, ProdVotingPowerCalculator,
VotingPowerCalculator,
},
types::{LightBlock, Time},
};
use preds::{errors::VerificationError, ProdPredicates, VerificationPredicates};
#[derive(Debug)]
pub enum Verdict {
Success,
NotEnoughTrust(VerificationError),
Invalid(VerificationError),
}
impl From<Result<(), VerificationError>> for Verdict {
fn from(result: Result<(), VerificationError>) -> Self {
match result {
Ok(()) => Self::Success,
Err(e) if e.not_enough_trust() => Self::NotEnoughTrust(e),
Err(e) => Self::Invalid(e),
}
}
}
pub trait Verifier: Send + Sync {
fn verify(
&self,
untrusted: &LightBlock,
trusted: &LightBlock,
options: &Options,
now: Time,
) -> Verdict;
}
pub struct ProdVerifier {
predicates: Box<dyn VerificationPredicates>,
voting_power_calculator: Box<dyn VotingPowerCalculator>,
commit_validator: Box<dyn CommitValidator>,
hasher: Box<dyn Hasher>,
}
impl ProdVerifier {
pub fn new(
predicates: impl VerificationPredicates + 'static,
voting_power_calculator: impl VotingPowerCalculator + 'static,
commit_validator: impl CommitValidator + 'static,
hasher: impl Hasher + 'static,
) -> Self {
Self {
predicates: Box::new(predicates),
voting_power_calculator: Box::new(voting_power_calculator),
commit_validator: Box::new(commit_validator),
hasher: Box::new(hasher),
}
}
}
impl Default for ProdVerifier {
fn default() -> Self {
Self::new(
ProdPredicates::default(),
ProdVotingPowerCalculator::default(),
ProdCommitValidator::default(),
ProdHasher::default(),
)
}
}
impl Verifier for ProdVerifier {
fn verify(
&self,
untrusted: &LightBlock,
trusted: &LightBlock,
options: &Options,
now: Time,
) -> Verdict {
preds::verify(
&*self.predicates,
&*self.voting_power_calculator,
&*self.commit_validator,
&*self.hasher,
&trusted,
&untrusted,
options,
now,
)
.into()
}
}