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
use block_sync::BlockRequest;
use bytes::Bytes;
use ethereum_types::H256;
use network::PeerId;
use rlp::RlpStream;
use std::time::Instant;
use sync_io::SyncIo;
use types::BlockNumber;
use super::{
request_id::generate_request_id,
sync_packet::{SyncPacket::*, *},
};
use super::{BlockSet, ChainSync, PeerAsking};
pub struct SyncRequester;
impl SyncRequester {
pub fn request_blocks(
sync: &mut ChainSync,
io: &mut dyn SyncIo,
peer_id: PeerId,
request: BlockRequest,
block_set: BlockSet,
) {
match request {
BlockRequest::Headers { start, count, skip } => {
SyncRequester::request_headers_by_hash(
sync, io, peer_id, &start, count, skip, false, block_set,
);
}
BlockRequest::Bodies { hashes } => {
SyncRequester::request_bodies(sync, io, peer_id, hashes, block_set);
}
BlockRequest::Receipts { hashes } => {
SyncRequester::request_receipts(sync, io, peer_id, hashes, block_set);
}
}
}
fn request_bodies(
sync: &mut ChainSync,
io: &mut dyn SyncIo,
peer_id: PeerId,
hashes: Vec<H256>,
set: BlockSet,
) {
let mut rlp = RlpStream::new_list(hashes.len());
trace!(target: "sync", "{} <- GetBlockBodies: {} entries starting from {:?}, set = {:?}", peer_id, hashes.len(), hashes.first(), set);
for h in &hashes {
rlp.append(&h.clone());
}
SyncRequester::send_request(
sync,
io,
peer_id,
PeerAsking::BlockBodies,
GetBlockBodiesPacket,
rlp.out(),
);
let peer = sync.peers.get_mut(&peer_id).expect("peer_id may originate either from on_packet, where it is already validated or from enumerating self.peers. qed");
peer.asking_blocks = hashes;
peer.block_set = Some(set);
}
pub fn request_fork_header(
sync: &mut ChainSync,
io: &mut dyn SyncIo,
peer_id: PeerId,
n: BlockNumber,
) {
trace!(target: "sync", "{} <- GetForkHeader: at {}", peer_id, n);
let mut rlp = RlpStream::new_list(4);
rlp.append(&n);
rlp.append(&1u32);
rlp.append(&0u32);
rlp.append(&0u32);
SyncRequester::send_request(
sync,
io,
peer_id,
PeerAsking::ForkHeader,
GetBlockHeadersPacket,
rlp.out(),
);
}
pub fn request_pooled_transactions(
sync: &mut ChainSync,
io: &mut dyn SyncIo,
peer_id: PeerId,
hashes: &[H256],
) {
trace!(target: "sync", "{} <- GetPooledTransactions: {:?}", peer_id, hashes);
let mut rlp = RlpStream::new_list(hashes.len());
for h in hashes {
rlp.append(h);
}
SyncRequester::send_request(
sync,
io,
peer_id,
PeerAsking::PooledTransactions,
GetPooledTransactionsPacket,
rlp.out(),
)
}
pub fn request_snapshot_data(sync: &mut ChainSync, io: &mut dyn SyncIo, peer_id: PeerId) {
if let Some(hash) = sync.snapshot.needed_chunk() {
if let Some(ref mut peer) = sync.peers.get_mut(&peer_id) {
peer.asking_snapshot_data = Some(hash.clone());
}
SyncRequester::request_snapshot_chunk(sync, io, peer_id, &hash);
}
}
pub fn request_snapshot_manifest(sync: &mut ChainSync, io: &mut dyn SyncIo, peer_id: PeerId) {
trace!(target: "sync", "{} <- GetSnapshotManifest", peer_id);
let rlp = RlpStream::new_list(0);
SyncRequester::send_request(
sync,
io,
peer_id,
PeerAsking::SnapshotManifest,
GetSnapshotManifestPacket,
rlp.out(),
);
}
fn request_headers_by_hash(
sync: &mut ChainSync,
io: &mut dyn SyncIo,
peer_id: PeerId,
h: &H256,
count: u64,
skip: u64,
reverse: bool,
set: BlockSet,
) {
trace!(target: "sync", "{} <- GetBlockHeaders: {} entries starting from {}, set = {:?}", peer_id, count, h, set);
let mut rlp = RlpStream::new_list(4);
rlp.append(h);
rlp.append(&count);
rlp.append(&skip);
rlp.append(&if reverse { 1u32 } else { 0u32 });
SyncRequester::send_request(
sync,
io,
peer_id,
PeerAsking::BlockHeaders,
GetBlockHeadersPacket,
rlp.out(),
);
let peer = sync.peers.get_mut(&peer_id).expect("peer_id may originate either from on_packet, where it is already validated or from enumerating self.peers. qed");
peer.asking_hash = Some(h.clone());
peer.block_set = Some(set);
}
fn request_receipts(
sync: &mut ChainSync,
io: &mut dyn SyncIo,
peer_id: PeerId,
hashes: Vec<H256>,
set: BlockSet,
) {
let mut rlp = RlpStream::new_list(hashes.len());
trace!(target: "sync", "{} <- GetBlockReceipts: {} entries starting from {:?}, set = {:?}", peer_id, hashes.len(), hashes.first(), set);
for h in &hashes {
rlp.append(&h.clone());
}
SyncRequester::send_request(
sync,
io,
peer_id,
PeerAsking::BlockReceipts,
GetReceiptsPacket,
rlp.out(),
);
let peer = sync.peers.get_mut(&peer_id).expect("peer_id may originate either from on_packet, where it is already validated or from enumerating self.peers. qed");
peer.asking_blocks = hashes;
peer.block_set = Some(set);
}
fn request_snapshot_chunk(
sync: &mut ChainSync,
io: &mut dyn SyncIo,
peer_id: PeerId,
chunk: &H256,
) {
trace!(target: "sync", "{} <- GetSnapshotData {:?}", peer_id, chunk);
let mut rlp = RlpStream::new_list(1);
rlp.append(chunk);
SyncRequester::send_request(
sync,
io,
peer_id,
PeerAsking::SnapshotData,
GetSnapshotDataPacket,
rlp.out(),
);
}
fn send_request(
sync: &mut ChainSync,
io: &mut dyn SyncIo,
peer_id: PeerId,
asking: PeerAsking,
packet_id: SyncPacket,
packet: Bytes,
) {
if let Some(ref mut peer) = sync.peers.get_mut(&peer_id) {
if peer.asking != PeerAsking::Nothing {
warn!(target:"sync", "Asking {:?} while requesting {:?}", peer.asking, asking);
}
peer.asking = asking;
peer.ask_time = Instant::now();
let (packet, _) = generate_request_id(packet, peer, packet_id);
let result = io.send(peer_id, packet_id, packet);
if let Err(e) = result {
debug!(target:"sync", "Error sending request: {:?}", e);
io.disconnect_peer(peer_id);
}
}
}
}