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
use std::{
pin::Pin,
sync::Arc,
task::{Context, Poll},
};
use futures::{FutureExt, Stream, StreamExt};
use nimiq_blockchain_interface::AbstractBlockchain;
use nimiq_blockchain_proxy::BlockchainProxy;
use nimiq_light_blockchain::LightBlockchain;
use nimiq_network_interface::network::{CloseReason, Network, NetworkEvent};
use nimiq_primitives::policy::Policy;
use crate::{
messages::BlockError,
sync::{
pico::PicoMacroSync,
sync_interface::{MacroSync, MacroSyncReturn},
},
};
impl<TNetwork: Network> PicoMacroSync<TNetwork> {
// This function is the one that starts the PicoMacroSync process,
// by adding peers into the MacroSync component.
// It also removes peers from the internal data structures, when they leave
fn poll_network_events(
&mut self,
cx: &mut Context<'_>,
) -> Poll<Option<MacroSyncReturn<TNetwork::PeerId>>> {
while let Poll::Ready(Some(result)) = self.network_event_rx.poll_next_unpin(cx) {
match result {
Ok(NetworkEvent::PeerLeft(peer_id)) => {
// Remove the peer from internal data structures.
self.remove_peer_requests(peer_id);
}
Ok(NetworkEvent::PeerJoined(peer_id, _)) => {
// Query if that peer provides the necessary services for syncing
if self.network.peer_provides_required_services(peer_id) {
// Start the macro sync process
self.add_peer(peer_id);
} else {
// We can't sync with this peer as it doesn't provide the services that we need.
// Emit the peer as incompatible.
self.syncing_peers.remove(&peer_id);
return Poll::Ready(Some(MacroSyncReturn::Incompatible(peer_id)));
}
}
Ok(_) => {}
Err(_) => return Poll::Ready(None),
}
}
Poll::Pending
}
fn poll_heads(
&mut self,
cx: &mut Context<'_>,
) -> Poll<Option<MacroSyncReturn<TNetwork::PeerId>>> {
while let Poll::Ready(Some(Some((peer_head, peer_id)))) =
self.head_stream.poll_next_unpin(cx)
{
// The peer might have disconnected during the request.
if !self.network.has_peer(peer_id) {
continue;
}
match self.blockchain {
#[cfg(feature = "full")]
BlockchainProxy::Full(_) => {
panic!("Pico macro sync is not supported in the full blockchain")
}
BlockchainProxy::Light(ref light_blockchain) => {
let blockchain = light_blockchain.upgradable_read();
let macro_head = blockchain.macro_head.hash();
let election_head = blockchain.election_head.hash();
if macro_head == peer_head.macro_hash
&& election_head == peer_head.election_hash
{
// We are synced with this peer so we emit it
return Poll::Ready(Some(MacroSyncReturn::Good(peer_id)));
}
}
}
// Request macro headers from this peer
self.request_macro_headers(peer_head, peer_id);
}
Poll::Pending
}
fn poll_macro_blocks(
&mut self,
cx: &mut Context<'_>,
) -> Poll<Option<MacroSyncReturn<TNetwork::PeerId>>> {
while let Poll::Ready(Some(result)) = self.block_headers.poll_next_unpin(cx) {
match result {
(Ok(Ok(block)), peer_id) => {
if !block.is_macro() {
// We received a non macro block during the macro sync process.
log::warn!(%peer_id,
"Banning peer due to a non expected response",
);
self.disconnect_peer(peer_id, CloseReason::MaliciousPeer);
return Poll::Ready(None);
}
if let Some(peer_requests) = self.peer_requests.get_mut(&peer_id) {
if !peer_requests.update_request(block) {
// We received a block we were not expecting from this peer
log::warn!(%peer_id,
"Banning peer due to a non expected response",
);
self.disconnect_peer(peer_id, CloseReason::MaliciousPeer);
return Poll::Ready(None);
}
if peer_requests.is_ready() {
log::trace!(%peer_id, "All pending requests are ready");
while let Some((_, block)) = peer_requests.pop_request() {
let block = block.expect("At this point the queue should be ready");
let push_result = match self.blockchain {
#[cfg(feature = "full")]
BlockchainProxy::Full(_) => {
panic!("Pico macro sync is not supported in the full blockchain")
}
BlockchainProxy::Light(ref light_blockchain) => {
let blockchain = light_blockchain.upgradable_read();
let election_head = blockchain.election_head_hash();
let macro_head = blockchain.macro_head_hash();
if Policy::is_election_block_at(block.block_number()) {
if election_head == block.hash() {
// Known block so we just ignore and continue
continue;
}
if Policy::genesis_block_number()
== blockchain.block_number()
{
log::info!("PicoSync: Pushing latest election");
// If we are currently at the genesis block we push the election block we obtained
LightBlockchain::push_pico_election(
blockchain,
block.clone(),
)
} else {
// We don't accept other election blocks if we are not in the genesis
self.syncing_peers.remove(&peer_id);
log::warn!(
block = block.block_number(),
head = blockchain.block_number(),
%peer_id,
"Conflicting election from peer");
return Poll::Ready(Some(
MacroSyncReturn::Conflicting(peer_id),
));
}
} else {
if macro_head == block.hash() {
// Known block so we just ignore and continue
continue;
}
// We only accept macro blocks from the next epoch
if blockchain.epoch_number() + 1
== Policy::epoch_at(block.block_number())
{
log::info!("PicoSync: Pushing latest macro");
LightBlockchain::push_macro(
blockchain,
block.clone(),
)
} else {
self.syncing_peers.remove(&peer_id);
log::warn!(
block = block.block_number(),
head = blockchain.block_number(),
current_epoch=blockchain.epoch_number() + 1,
block_epoch=Policy::epoch_at(block.block_number()),
%peer_id,
"Conflicting macro block from peer");
return Poll::Ready(Some(
MacroSyncReturn::Conflicting(peer_id),
));
}
}
}
};
match push_result {
Ok(push_result) => {
log::debug!(
block_number = block.block_number(),
?push_result,
"Pushed a macro block",
);
}
Err(error) => {
log::warn!(
block_number = block.block_number(),
?error,
%peer_id,
"Failed to push macro block",
);
// Since we cannot differentiate between a malicious peer and someone who's behind and
// we don't know if the initial state (election block) that we obtained was good
// then we need to fallback to light macro sync.
self.syncing_peers.remove(&peer_id);
return Poll::Ready(Some(MacroSyncReturn::Conflicting(
peer_id,
)));
}
}
}
// At this point we applied all the pending requests from this peer
self.peer_requests.remove(&peer_id);
// Re-request the head after applying these blocks in order to know if we are up to date with this peer
// or if there is more to sync
let future =
Self::request_head(Arc::clone(&self.network), peer_id).boxed();
self.head_stream.push(future);
}
} else {
// If we don't have any pending requests from this peer, we proceed requesting the head
let future = Self::request_head(Arc::clone(&self.network), peer_id).boxed();
self.head_stream.push(future);
}
}
// A peer that replies with a block other than the requested one is either
// buggy or malicious; either way we ban it
(Ok(Err(BlockError::ResponseHashMismatch)), peer_id) => {
log::warn!(%peer_id, "Banning peer that replied with a block other than the requested one");
self.disconnect_peer(peer_id, CloseReason::MaliciousPeer);
}
(Ok(Err(error)), peer_id) => {
trace!(%error, %peer_id, "Received a response for a failed request on the remote side");
// If a block request fails, we disconnect from this peer
self.disconnect_peer(peer_id, CloseReason::Error);
}
(Err(error), peer_id) => {
trace!(?error, %peer_id, "Failed block request");
// If a block request fails, we disconnect from this peer
self.disconnect_peer(peer_id, CloseReason::Error);
}
}
}
Poll::Pending
}
}
impl<TNetwork: Network> Stream for PicoMacroSync<TNetwork> {
type Item = MacroSyncReturn<TNetwork::PeerId>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
if let Poll::Ready(o) = self.poll_network_events(cx) {
return Poll::Ready(o);
}
if let Poll::Ready(o) = self.poll_heads(cx) {
return Poll::Ready(o);
}
if let Poll::Ready(o) = self.poll_macro_blocks(cx) {
return Poll::Ready(o);
}
Poll::Pending
}
}