-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy patheffect.rs
252 lines (204 loc) · 7.18 KB
/
effect.rs
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
use derive_where::derive_where;
use malachitebft_core_types::*;
use crate::input::RequestId;
use crate::types::SignedConsensusMsg;
use crate::ConsensusMsg;
/// Provides a way to construct the appropriate [`Resume`] value to
/// resume execution after handling an [`Effect`].
///
/// Eeach `Effect` embeds a value that implements [`Resumable`]
/// which is used to construct the appropriate [`Resume`] value.
///
/// ## Example
///
/// ```rust,ignore
/// fn effect_handler(effect: Effect<Ctx>) -> Result<Resume<Ctx>, Error> {
/// match effect {
/// Effect::ResetTimeouts(r) => {
/// reset_timeouts();
/// Ok(r.resume_with(()))
/// }
/// Effect::GetValidatorSet(height, r) => {)
/// let validator_set = get_validator_set(height);
/// Ok(r.resume_with(validator_set))
/// }
/// // ...
/// }
/// ```
pub trait Resumable<Ctx: Context> {
/// The value type that will be used to resume execution
type Value;
/// Creates the appropriate [`Resume`] value to resume execution with.
fn resume_with(self, value: Self::Value) -> Resume<Ctx>;
}
/// An effect which may be yielded by a consensus process.
///
/// Effects are handled by the caller using [`process!`][process]
/// and the consensus process is then resumed with an appropriate [`Resume`] value, as per
/// the documentation for each effect.
///
/// [process]: crate::process
#[must_use]
#[derive_where(Debug)]
pub enum Effect<Ctx>
where
Ctx: Context,
{
/// Reset all timeouts
/// Resume with: [`Resume::Continue`]
ResetTimeouts(resume::Continue),
/// Cancel all timeouts
/// Resume with: [`Resume::Continue`]
CancelAllTimeouts(resume::Continue),
/// Cancel a given timeout
/// Resume with: [`Resume::Continue`]
CancelTimeout(Timeout, resume::Continue),
/// Schedule a timeout
/// Resume with: [`Resume::Continue`]
ScheduleTimeout(Timeout, resume::Continue),
/// Consensus is starting a new round with the given proposer
/// Resume with: [`Resume::Continue`]
StartRound(Ctx::Height, Round, Ctx::Address, resume::Continue),
/// Broadcast a message
/// Resume with: [`Resume::Continue`]
Broadcast(SignedConsensusMsg<Ctx>, resume::Continue),
/// Get a value to propose at the given height and round, within the given timeout
/// Resume with: [`Resume::Continue`]
GetValue(Ctx::Height, Round, Timeout, resume::Continue),
/// Restream the value identified by the given information.
/// Resume with: [`Resume::Continue`]
RestreamValue(
/// Height of the value
Ctx::Height,
/// Round of the value
Round,
/// Valid round of the value
Round,
/// Address of the proposer for that value
Ctx::Address,
/// Value ID of the value to restream
ValueId<Ctx>,
/// For resumption
resume::Continue,
),
/// Get the validator set at the given height
/// Resume with: [`Resume::ValidatorSet`]
GetValidatorSet(Ctx::Height, resume::ValidatorSet),
/// Consensus has decided on a value
/// Resume with: [`Resume::Continue`]
Decide(CommitCertificate<Ctx>, resume::Continue),
/// Consensus has been stuck in Prevote or Precommit step, ask for vote sets from peers
/// Resume with: [`Resume::Continue`]
GetVoteSet(Ctx::Height, Round, resume::Continue),
/// A peer has required our vote set, send the response
/// Resume with: [`Resume::Continue`]`
SendVoteSetResponse(
RequestId,
Ctx::Height,
Round,
VoteSet<Ctx>,
resume::Continue,
),
/// Persist a consensus message in the Write-Ahead Log for crash recovery
/// Resume with: [`Resume::Continue`]`
PersistMessage(SignedConsensusMsg<Ctx>, resume::Continue),
/// Persist a timeout in the Write-Ahead Log for crash recovery
/// Resume with: [`Resume::Continue`]`
PersistTimeout(Timeout, resume::Continue),
/// Sign a vote with this node's private key
/// Resume with: [`Resume::SignedVote`]
SignVote(Ctx::Vote, resume::SignedVote),
/// Sign a proposal with this node's private key
/// Resume with: [`Resume::SignedProposal`]
SignProposal(Ctx::Proposal, resume::SignedProposal),
/// Verify a signature
/// Resume with: [`Resume::SignatureValidity`]
VerifySignature(
SignedMessage<Ctx, ConsensusMsg<Ctx>>,
PublicKey<Ctx>,
resume::SignatureValidity,
),
/// Verify a commit certificate
VerifyCertificate(
CommitCertificate<Ctx>,
Ctx::ValidatorSet,
ThresholdParams,
resume::CertificateValidity,
),
}
/// A value with which the consensus process can be resumed after yielding an [`Effect`].
#[must_use]
#[allow(clippy::manual_non_exhaustive)]
#[derive_where(Debug)]
pub enum Resume<Ctx>
where
Ctx: Context,
{
/// Internal effect to start the coroutine.
#[doc(hidden)]
Start,
/// Resume execution
Continue,
/// Resume execution with `Some(Ctx::ValidatorSet)` if a validator set
/// was successfully fetched, or `None` otherwise.
ValidatorSet(Option<Ctx::ValidatorSet>),
/// Resume execution with the validity of the signature
SignatureValidity(bool),
/// Resume execution with the signed vote
SignedVote(SignedMessage<Ctx, Ctx::Vote>),
/// Resume execution with the signed proposal
SignedProposal(SignedMessage<Ctx, Ctx::Proposal>),
/// Resume execution with the result of the verification of the [`CommitCertificate`]
CertificateValidity(Result<(), CertificateError<Ctx>>),
}
pub mod resume {
use super::*;
#[derive(Debug, Default)]
pub struct Continue;
impl<Ctx: Context> Resumable<Ctx> for Continue {
type Value = ();
fn resume_with(self, _: ()) -> Resume<Ctx> {
Resume::Continue
}
}
#[derive(Debug, Default)]
pub struct ValidatorSet;
impl<Ctx: Context> Resumable<Ctx> for ValidatorSet {
type Value = Option<Ctx::ValidatorSet>;
fn resume_with(self, value: Self::Value) -> Resume<Ctx> {
Resume::ValidatorSet(value)
}
}
#[derive(Debug, Default)]
pub struct SignatureValidity;
impl<Ctx: Context> Resumable<Ctx> for SignatureValidity {
type Value = bool;
fn resume_with(self, value: Self::Value) -> Resume<Ctx> {
Resume::SignatureValidity(value)
}
}
#[derive(Debug, Default)]
pub struct SignedVote;
impl<Ctx: Context> Resumable<Ctx> for SignedVote {
type Value = SignedMessage<Ctx, Ctx::Vote>;
fn resume_with(self, value: Self::Value) -> Resume<Ctx> {
Resume::SignedVote(value)
}
}
#[derive(Debug, Default)]
pub struct SignedProposal;
impl<Ctx: Context> Resumable<Ctx> for SignedProposal {
type Value = SignedMessage<Ctx, Ctx::Proposal>;
fn resume_with(self, a: Self::Value) -> Resume<Ctx> {
Resume::SignedProposal(a)
}
}
#[derive(Debug, Default)]
pub struct CertificateValidity;
impl<Ctx: Context> Resumable<Ctx> for CertificateValidity {
type Value = Result<(), CertificateError<Ctx>>;
fn resume_with(self, value: Self::Value) -> Resume<Ctx> {
Resume::CertificateValidity(value)
}
}
}