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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//! Defines the [`Communicator`] trait and related types.

use std::convert::TryFrom;
use std::convert::TryInto;
use std::future::Future;
use std::sync::Arc;

use crate::invocation;
use crate::invocation::Invocation;
use crate::log_entry::LogEntry;
use crate::AcceptError;
use crate::CommitError;
use crate::Conflict;
use crate::CoordNum;
use crate::NodeInfo;
use crate::PrepareError;
use crate::Promise;
use crate::RoundNum;

/// Shorthand to extract `Abstain` type out of `C`.
pub type AbstainOf<C> = <C as Communicator>::Abstain;
/// Shorthand to extract `CoordNum` type out of `C`.
pub type CoordNumOf<C> = <C as Communicator>::CoordNum;
/// Shorthand to extract `Error` type out of `C`.
pub type ErrorOf<C> = <C as Communicator>::Error;
/// Shorthand to extract `LogEntry` type out of `C`.
pub type LogEntryOf<C> = <C as Communicator>::LogEntry;
/// Shorthand to extract log entry `Id` type out of `C`.
pub type LogEntryIdOf<C> = <LogEntryOf<C> as LogEntry>::Id;
/// Shorthand to extract `Nay` type out of `C`.
pub type NayOf<C> = <C as Communicator>::Nay;
/// Shorthand to extract `Node` type (`impl NodeInfo`) out of `C`.
pub type NodeOf<C> = <C as Communicator>::Node;
/// Shorthand to extract node (`impl NodeInfo`) `Id` type out of `C`.
pub type NodeIdOf<C> = <NodeOf<C> as NodeInfo>::Id;
/// Shorthand to extract `RoundNum` type out of `C`.
pub type RoundNumOf<C> = <C as Communicator>::RoundNum;
/// Shorthand to extract `Yea` type out of `C`.
pub type YeaOf<C> = <C as Communicator>::Yea;

/// Invokes `Acceptance` type constructor so as to be compatible with `C`.
pub type AcceptanceFor<C> = Acceptance<CoordNumOf<C>, LogEntryOf<C>, YeaOf<C>, NayOf<C>>;
/// Invokes `Conflict` type constructor so as to be compatible with `C`.
pub type ConflictFor<C> = Conflict<CoordNumOf<C>, LogEntryOf<C>>;
/// Invokes `Promise` type constructor so as to be compatible with `C`.
pub type PromiseFor<C> = Promise<RoundNumOf<C>, CoordNumOf<C>, LogEntryOf<C>>;
/// Invokes `Vote` type constructor so as to be compatible with `C`.
pub type VoteFor<C> = Vote<RoundNumOf<C>, CoordNumOf<C>, LogEntryOf<C>, AbstainOf<C>>;

/// Defines how [`Node`][crate::Node]s call others'
/// [`RequestHandler`][crate::RequestHandler]s.
///
/// The [simplest possible
/// implementation][crate::prototyping::DirectCommunicator] directly calls
/// `RequestHandler` methods, requiring that all nodes live in the same process.
/// This is useful for prototyping and testing. Most other use cases require a
/// different, custom implementation.
///
/// # Soundness
///
/// Implementations must be secure, i.e. prevent forgery and replay attacks.
/// This implicitly means that a restarted node will not see messages intended
/// for its previous run, i.e. delayed messages sent over a connectionless
/// protocol. Failure to shield a node from such messages may cause it to come
/// out of passive participation mode early and lead to inconsistency.
pub trait Communicator: Sized + 'static {
    /// NodeInfo
    type Node: NodeInfo;

    /// The round number type.
    type RoundNum: RoundNum;
    /// The coordination number type.
    type CoordNum: CoordNum;

    /// The log entry type.
    type LogEntry: LogEntry;

    /// The communication error type.
    type Error: std::fmt::Debug + Send + Sync + 'static;

    /// Type of future returned from `send_prepare`.
    type SendPrepare: Future<Output = Result<VoteFor<Self>, Self::Error>>;
    /// Information sent along with abstentions.
    type Abstain: std::fmt::Debug + Send + Sync + 'static;

    /// Type of future returned from `send_proposal`.
    type SendProposal: Future<Output = Result<AcceptanceFor<Self>, Self::Error>>;
    /// Information sent along with yea votes.
    type Yea: std::fmt::Debug + Send + Sync + 'static;
    /// Information sent along with nay votes.
    type Nay: std::fmt::Debug + Send + Sync + 'static;

    /// Type of future returned from `send_commit`.
    type SendCommit: Future<Output = Result<Committed, Self::Error>>;
    /// Type of future returned from `send_commit_by_id`.
    type SendCommitById: Future<Output = Result<Committed, Self::Error>>;

    /// Send a prepare message to all `receivers`.
    ///
    /// Implementations should attempt to call each receivers'
    /// [`RequestHandler::handle_prepare`][crate::RequestHandler::
    /// handle_prepare]. The return value must contain exactly one entry per
    /// receiver with a future of `handle_prepare`'s result.
    fn send_prepare<'a>(
        &mut self,
        receivers: &'a [Self::Node],
        round_num: Self::RoundNum,
        coord_num: Self::CoordNum,
    ) -> Vec<(&'a Self::Node, Self::SendPrepare)>;

    /// Send a proposal message to all `receivers`.
    ///
    /// Implementations should attempt to call each receivers'
    /// [`RequestHandler::handle_proposal`][crate::RequestHandler::
    /// handle_proposal]. The return value must contain exactly one entry per
    /// receiver with a future of `handle_proposal`'s result.
    fn send_proposal<'a>(
        &mut self,
        receivers: &'a [Self::Node],
        round_num: Self::RoundNum,
        coord_num: Self::CoordNum,
        log_entry: Arc<Self::LogEntry>,
    ) -> Vec<(&'a Self::Node, Self::SendProposal)>;

    /// Send a commit message to all `receivers`.
    ///
    /// Implementations should attempt to call each receivers'
    /// [`RequestHandler::handle_commit`][crate::RequestHandler::
    /// handle_commit]. The return value must contain exactly one entry per
    /// receiver with a future of `handle_commit`'s result.
    fn send_commit<'a>(
        &mut self,
        receivers: &'a [Self::Node],
        round_num: Self::RoundNum,
        coord_num: Self::CoordNum,
        log_entry: Arc<Self::LogEntry>,
    ) -> Vec<(&'a Self::Node, Self::SendCommit)>;

    /// Send a commit-by-id message to all `receivers`.
    ///
    /// Implementations should attempt to call each receivers'
    /// [`RequestHandler::handle_commit_by_id`][crate::RequestHandler::
    /// handle_commit_by_id]. The return value must contain exactly one entry
    /// per receiver with a future of `handle_commit_by_id`'s result.
    fn send_commit_by_id<'a>(
        &mut self,
        receivers: &'a [Self::Node],
        round_num: Self::RoundNum,
        coord_num: Self::CoordNum,
        log_entry_id: <Self::LogEntry as LogEntry>::Id,
    ) -> Vec<(&'a Self::Node, Self::SendCommitById)>;
}

/// A vote cast in a leader election.
#[derive(Debug)]
pub enum Vote<R, C, E, A> {
    /// The node voted for the candidate.
    Given(Promise<R, C, E>),
    /// The node couldn't vote for the candidate.
    Conflicted(Conflict<C, E>),
    /// The node abstained, refusing to vote at all.
    Abstained(A),
}

impl<I: Invocation> TryFrom<Result<invocation::PromiseFor<I>, PrepareError<I>>>
    for Vote<
        invocation::RoundNumOf<I>,
        invocation::CoordNumOf<I>,
        invocation::LogEntryOf<I>,
        invocation::AbstainOf<I>,
    >
{
    type Error = PrepareError<I>;

    fn try_from(
        result: Result<invocation::PromiseFor<I>, PrepareError<I>>,
    ) -> Result<Self, Self::Error> {
        result
            .map(Vote::Given)
            .or_else(|err| err.try_into().map(Vote::Conflicted))
    }
}

impl<I: Invocation> TryFrom<PrepareError<I>>
    for Conflict<invocation::CoordNumOf<I>, invocation::LogEntryOf<I>>
{
    type Error = PrepareError<I>;

    fn try_from(error: PrepareError<I>) -> Result<Self, Self::Error> {
        match error {
            PrepareError::Supplanted(coord_num) => Ok(Conflict::Supplanted { coord_num }),
            PrepareError::Converged(coord_num, log_entry) => Ok(Conflict::Converged {
                coord_num,
                log_entry,
            }),
            _ => Err(error),
        }
    }
}

impl<R, C, E, A> From<Result<Promise<R, C, E>, Conflict<C, E>>> for Vote<R, C, E, A> {
    fn from(result: Result<Promise<R, C, E>, Conflict<C, E>>) -> Self {
        match result {
            Ok(promise) => Vote::Given(promise),
            Err(rejection) => Vote::Conflicted(rejection),
        }
    }
}

/// A vote cast on a proposal.
#[derive(Debug)]
pub enum Acceptance<C, E, Y, X> {
    /// The node voted for the proposal.
    Given(Y),
    /// The node couldn't vote for the proposal.
    Conflicted(Conflict<C, E>),
    /// The node voted against the proposal.
    Refused(X),
}

impl<I: Invocation> TryFrom<Result<invocation::YeaOf<I>, AcceptError<I>>>
    for Acceptance<
        invocation::CoordNumOf<I>,
        invocation::LogEntryOf<I>,
        invocation::YeaOf<I>,
        invocation::NayOf<I>,
    >
{
    type Error = AcceptError<I>;

    fn try_from(result: Result<invocation::YeaOf<I>, AcceptError<I>>) -> Result<Self, Self::Error> {
        result
            .map(Acceptance::Given)
            .or_else(|err| err.try_into().map(Acceptance::Conflicted))
    }
}

impl<I: Invocation> TryFrom<AcceptError<I>>
    for Conflict<invocation::CoordNumOf<I>, invocation::LogEntryOf<I>>
{
    type Error = AcceptError<I>;

    fn try_from(error: AcceptError<I>) -> Result<Self, Self::Error> {
        match error {
            AcceptError::Supplanted(coord_num) => Ok(Conflict::Supplanted { coord_num }),
            AcceptError::Converged(coord_num, log_entry) => Ok(Conflict::Converged {
                coord_num,
                log_entry,
            }),
            _ => Err(error),
        }
    }
}

impl<C, E, Y, X> From<Result<Y, Conflict<C, E>>> for Acceptance<C, E, Y, X> {
    fn from(result: Result<Y, Conflict<C, E>>) -> Self {
        result
            .map(Acceptance::Given)
            .unwrap_or_else(Acceptance::Conflicted)
    }
}

/// Node successfully committed the log entry.
pub struct Committed;

impl From<()> for Committed {
    fn from(_: ()) -> Self {
        Self
    }
}

impl<I: Invocation> TryFrom<Result<(), CommitError<I>>> for Committed {
    type Error = CommitError<I>;

    fn try_from(result: Result<(), CommitError<I>>) -> Result<Self, Self::Error> {
        result.map(|_| Committed)
    }
}