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
extern crate parking_lot;
extern crate num_traits as num;
#[macro_use]
extern crate futures;
#[macro_use]
extern crate log;
#[cfg(test)]
extern crate tokio;
#[cfg(test)]
extern crate exit_future;
#[cfg(feature = "derive-codec")]
#[macro_use]
extern crate parity_codec_derive;
#[cfg(feature = "derive-codec")]
extern crate parity_codec;
pub mod bitfield;
pub mod round;
pub mod vote_graph;
pub mod voter;
mod bridge_state;
#[cfg(test)]
mod testing;
use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "derive-codec", derive(Encode, Decode))]
pub struct Prevote<H, N> {
pub target_hash: H,
pub target_number: N,
}
impl<H, N> Prevote<H, N> {
pub fn new(target_hash: H, target_number: N) -> Self {
Prevote { target_hash, target_number }
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "derive-codec", derive(Encode, Decode))]
pub struct Precommit<H, N> {
pub target_hash: H,
pub target_number: N,
}
impl<H, N> Precommit<H, N> {
pub fn new(target_hash: H, target_number: N) -> Self {
Precommit { target_hash, target_number }
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum Error {
NotDescendent,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::NotDescendent => write!(f, "Block not descendent of base"),
}
}
}
impl ::std::error::Error for Error {
fn description(&self) -> &str {
match *self {
Error::NotDescendent => "Block not descendent of base",
}
}
}
pub trait BlockNumberOps:
::std::fmt::Debug +
::std::cmp::Ord +
::std::ops::Add<Output=Self> +
::std::ops::Sub<Output=Self> +
::num::One +
::num::Zero +
::num::AsPrimitive<usize>
{}
impl<T> BlockNumberOps for T where
T: ::std::fmt::Debug,
T: ::std::cmp::Ord,
T: ::std::ops::Add<Output=Self>,
T: ::std::ops::Sub<Output=Self>,
T: ::num::One,
T: ::num::Zero,
T: ::num::AsPrimitive<usize>,
{}
pub trait Chain<H, N: Copy + BlockNumberOps> {
fn ancestry(&self, base: H, block: H) -> Result<Vec<H>, Error>;
fn best_chain_containing(&self, base: H) -> Option<(H, N)>;
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "derive-codec", derive(Encode, Decode))]
pub struct Equivocation<Id, V, S> {
pub round_number: u64,
pub identity: Id,
pub first: (V, S),
pub second: (V, S),
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "derive-codec", derive(Encode, Decode))]
pub enum Message<H, N> {
Prevote(Prevote<H, N>),
Precommit(Precommit<H, N>),
}
impl<H, N: Copy> Message<H, N> {
pub fn target(&self) -> (&H, N) {
match *self {
Message::Prevote(ref v) => (&v.target_hash, v.target_number),
Message::Precommit(ref v) => (&v.target_hash, v.target_number),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "derive-codec", derive(Encode, Decode))]
pub struct SignedMessage<H, N, S, Id> {
pub message: Message<H, N>,
pub signature: S,
pub id: Id,
}
impl<H, N: Copy, S, Id> SignedMessage<H, N, S, Id> {
pub fn target(&self) -> (&H, N) {
self.message.target()
}
}
#[cfg(test)]
mod tests {
#[cfg(feature = "derive-codec")]
#[test]
fn codec_was_derived() {
use parity_codec::{Encode, Decode};
let signed = ::SignedMessage {
message: ::Message::Prevote(::Prevote {
target_hash: b"Hello".to_vec(),
target_number: 5,
}),
signature: b"Signature".to_vec(),
id: 5000,
};
let encoded = signed.encode();
let signed2 = ::SignedMessage::decode(&mut &encoded[..]).unwrap();
assert_eq!(signed, signed2);
}
}