vapcore-sync 1.12.0

Tetsy Vapory (VapCore) Blockchain Synchronization
Documentation
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
// This file is part of Tetsy Vapory.

// Tetsy Vapory is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Tetsy Vapory is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Tetsy Vapory.  If not, see <http://www.gnu.org/licenses/>.

use std::cmp;
use std::time::{Duration, Instant};

use crate::sync_io::SyncIo;

use bytes::Bytes;
use enum_primitive::FromPrimitive;
use vapory_types::H256;
use log::{debug, trace, warn};
use network::{self, PeerId};
use parking_lot::RwLock;
use tetsy_rlp::{Rlp, RlpStream};
use common_types::{ids::BlockId, BlockNumber};

use super::sync_packet::{PacketInfo, SyncPacket};
use super::sync_packet::SyncPacket::{
	StatusPacket,
	TransactionsPacket,
	GetBlockHeadersPacket,
	BlockHeadersPacket,
	GetBlockBodiesPacket,
	BlockBodiesPacket,
	GetNodeDataPacket,
	NodeDataPacket,
	GetReceiptsPacket,
	ReceiptsPacket,
	GetSnapshotManifestPacket,
	SnapshotManifestPacket,
	GetSnapshotDataPacket,
	SnapshotDataPacket,
	ConsensusDataPacket,
	GetPrivateStatePacket,
	PrivateStatePacket,
};

use super::{
	ChainSync,
	SyncHandler,
	RlpResponseResult,
	PacketDecodeError,
	MAX_BODIES_TO_SEND,
	MAX_HEADERS_TO_SEND,
	MAX_NODE_DATA_TO_SEND,
	MAX_NODE_DATA_TOTAL_DURATION,
	MAX_NODE_DATA_SINGLE_DURATION,
	MAX_RECEIPTS_HEADERS_TO_SEND,
};

/// The Chain Sync Supplier: answers requests from peers with available data
pub struct SyncSupplier;

impl SyncSupplier {
	/// Dispatch incoming requests and responses
	// Take a u8 and not a SyncPacketId because this is the entry point
	// to chain sync from the outside world.
	pub fn dispatch_packet(sync: &RwLock<ChainSync>, io: &mut dyn SyncIo, peer: PeerId, packet_id: u8, data: &[u8]) {
		let rlp = Rlp::new(data);

		if let Some(id) = SyncPacket::from_u8(packet_id) {
			let result = match id {
				GetBlockBodiesPacket => SyncSupplier::return_rlp(
					io, &rlp, peer,
					SyncSupplier::return_block_bodies,
					|e| format!("Error sending block bodies: {:?}", e)),

				GetBlockHeadersPacket => SyncSupplier::return_rlp(
					io, &rlp, peer,
					SyncSupplier::return_block_headers,
					|e| format!("Error sending block headers: {:?}", e)),

				GetReceiptsPacket => SyncSupplier::return_rlp(
					io, &rlp, peer,
					SyncSupplier::return_receipts,
					|e| format!("Error sending receipts: {:?}", e)),

				GetNodeDataPacket => SyncSupplier::return_rlp(
					io, &rlp, peer,
					SyncSupplier::return_node_data,
					|e| format!("Error sending nodes: {:?}", e)),

				GetSnapshotManifestPacket => SyncSupplier::return_rlp(
					io, &rlp, peer,
					SyncSupplier::return_snapshot_manifest,
					|e| format!("Error sending snapshot manifest: {:?}", e)),

				GetSnapshotDataPacket => SyncSupplier::return_rlp(
					io, &rlp, peer,
					SyncSupplier::return_snapshot_data,
					|e| format!("Error sending snapshot data: {:?}", e)),

				GetPrivateStatePacket => SyncSupplier::return_rlp(
					io, &rlp, peer,
					SyncSupplier::return_private_state,
					|e| format!("Error sending private state data: {:?}", e)),

				StatusPacket => {
					sync.write().on_packet(io, peer, packet_id, data);
					Ok(())
				},
				// Packets that require the peer to be confirmed
				_ => {
					if !sync.read().peers.contains_key(&peer) {
						debug!(target:"sync", "Unexpected packet {} from unregistered peer: {}:{}", packet_id, peer, io.peer_version(peer));
						return;
					}
					trace!(target: "sync", "{} -> Dispatching packet: {}", peer, packet_id);

					match id {
						ConsensusDataPacket => {
							SyncHandler::on_consensus_packet(io, peer, &rlp)
						},
						TransactionsPacket => {
							let res = {
								let sync_ro = sync.read();
								SyncHandler::on_peer_transactions(&*sync_ro, io, peer, &rlp)
							};
							if res.is_err() {
								// peer sent invalid data, disconnect.
								io.disable_peer(peer);
								sync.write().deactivate_peer(io, peer);
							}
						},
						_ => {
							sync.write().on_packet(io, peer, packet_id, data);
						}
					}

					Ok(())
				}
			};

			result.unwrap_or_else(|e| {
				debug!(target:"sync", "{} -> Malformed packet {} : {}", peer, packet_id, e);
			})
		}
	}

	/// Respond to GetBlockHeaders request
	fn return_block_headers(io: &dyn SyncIo, r: &Rlp, peer_id: PeerId) -> RlpResponseResult {
		let payload_soft_limit = io.payload_soft_limit();
		// Packet layout:
		// [ block: { P , B_32 }, maxHeaders: P, skip: P, reverse: P in { 0 , 1 } ]
		let max_headers: usize = r.val_at(1)?;
		let skip: usize = r.val_at(2)?;
		let reverse: bool = r.val_at(3)?;
		let last = io.chain().chain_info().best_block_number;
		let number = if r.at(0)?.size() == 32 {
			// id is a hash
			let hash: H256 = r.val_at(0)?;
			trace!(target: "sync", "{} -> GetBlockHeaders (hash: {}, max: {}, skip: {}, reverse:{})", peer_id, hash, max_headers, skip, reverse);
			match io.chain().block_header(BlockId::Hash(hash)) {
				Some(hdr) => {
					let number = hdr.number().into();
					debug_assert_eq!(hdr.hash(), hash);

					if max_headers == 1 || io.chain().block_hash(BlockId::Number(number)) != Some(hash) {
						// Non canonical header or single header requested
						// TODO: handle single-step reverse hashchains of non-canon hashes
						trace!(target:"sync", "Returning single header: {:?}", hash);
						let mut rlp = RlpStream::new_list(1);
						rlp.append_raw(&hdr.into_inner(), 1);
						return Ok(Some((BlockHeadersPacket.id(), rlp)));
					}
					number
				}
				None => return Ok(Some((BlockHeadersPacket.id(), RlpStream::new_list(0)))) //no such header, return nothing
			}
		} else {
			let number = r.val_at::<BlockNumber>(0)?;
			trace!(target: "sync", "{} -> GetBlockHeaders (number: {}, max: {}, skip: {}, reverse:{})", peer_id, number, max_headers, skip, reverse);
			number
		};

		let mut number = if reverse {
			cmp::min(last, number)
		} else {
			cmp::max(0, number)
		};
		let max_count = cmp::min(MAX_HEADERS_TO_SEND, max_headers);
		let mut count = 0;
		let mut data = Bytes::new();
		let inc = skip.saturating_add(1) as BlockNumber;
		let overlay = io.chain_overlay().read();

		// We are checking the `overlay` as well since it's where the ForkBlock
		// header is cached : so peers can confirm we are on the right fork,
		// even if we are not synced until the fork block
		while (number <= last || overlay.contains_key(&number)) && count < max_count {
			if let Some(hdr) = overlay.get(&number) {
				trace!(target: "sync", "{}: Returning cached fork header", peer_id);
				data.extend_from_slice(hdr);
				count += 1;
			} else if let Some(hdr) = io.chain().block_header(BlockId::Number(number)) {
				data.append(&mut hdr.into_inner());
				count += 1;
				// Check that the packet won't be oversized
				if data.len() > payload_soft_limit {
					break;
				}
			} else {
				// No required block.
				break;
			}
			if reverse {
				if number <= inc || number == 0 {
					break;
				}
				number = number.saturating_sub(inc);
			} else {
				number = number.saturating_add(inc);
			}
		}
		let mut rlp = RlpStream::new_list(count as usize);
		rlp.append_raw(&data, count as usize);
		trace!(target: "sync", "{} -> GetBlockHeaders: returned {} entries", peer_id, count);
		Ok(Some((BlockHeadersPacket.id(), rlp)))
	}

	/// Respond to GetBlockBodies request
	fn return_block_bodies(io: &dyn SyncIo, r: &Rlp, peer_id: PeerId) -> RlpResponseResult {
		let payload_soft_limit = io.payload_soft_limit();
		let mut count = r.item_count().unwrap_or(0);
		if count == 0 {
			debug!(target: "sync", "Empty GetBlockBodies request, ignoring.");
			return Ok(None);
		}
		count = cmp::min(count, MAX_BODIES_TO_SEND);
		let mut added = 0usize;
		let mut data = Bytes::new();
		for i in 0..count {
			if let Some(body) = io.chain().block_body(BlockId::Hash(r.val_at::<H256>(i)?)) {
				data.append(&mut body.into_inner());
				added += 1;
				// Check that the packet won't be oversized
				if data.len() > payload_soft_limit {
					break;
				}
			}
		}
		let mut rlp = RlpStream::new_list(added);
		rlp.append_raw(&data, added);
		trace!(target: "sync", "{} -> GetBlockBodies: returned {} entries", peer_id, added);
		Ok(Some((BlockBodiesPacket.id(), rlp)))
	}

	/// Respond to GetNodeData request
	fn return_node_data(io: &dyn SyncIo, r: &Rlp, peer_id: PeerId) -> RlpResponseResult {
		let payload_soft_limit = io.payload_soft_limit(); // 4Mb
		let mut count = r.item_count().unwrap_or(0);
		trace!(target: "sync", "{} -> GetNodeData: {} entries requested", peer_id, count);
		if count == 0 {
			debug!(target: "sync", "Empty GetNodeData request, ignoring.");
			return Ok(None);
		}
		count = cmp::min(count, MAX_NODE_DATA_TO_SEND);
		let mut added = 0usize;
		let mut data = Vec::new();
		let mut total_bytes = 0;
		let mut total_elpsd = Duration::from_secs(0);
		for i in 0..count {
			let hash = &r.val_at(i)?;
			let elpsd = Instant::now();
			let state = io.chain().state_data(hash);

			total_elpsd += elpsd.elapsed();
			if elpsd.elapsed() > MAX_NODE_DATA_SINGLE_DURATION || total_elpsd > MAX_NODE_DATA_TOTAL_DURATION {
				warn!(target: "sync", "{} -> GetNodeData:   item {}/{} – slow state fetch for hash {:?}; took {:?}",
					peer_id, i, count, hash, elpsd);
				break;
			}
			if let Some(node) = state {
				total_bytes += node.len();
				if total_bytes > payload_soft_limit {
					break;
				}
				data.push(node);
				added += 1;
			}
		}
		trace!(target: "sync", "{} -> GetNodeData: returning {}/{} entries ({} bytes total in {:?})",
			peer_id, added, count, total_bytes, total_elpsd);
		let mut rlp = RlpStream::new_list(added);
		for d in data {
			rlp.append(&d);
		}
		Ok(Some((NodeDataPacket.id(), rlp)))
	}

	fn return_receipts(io: &dyn SyncIo, rlp: &Rlp, peer_id: PeerId) -> RlpResponseResult {
		let payload_soft_limit = io.payload_soft_limit();
		let mut count = rlp.item_count().unwrap_or(0);
		trace!(target: "sync", "{} -> GetReceipts: {} entries", peer_id, count);
		if count == 0 {
			debug!(target: "sync", "Empty GetReceipts request, ignoring.");
			return Ok(None);
		}
		count = cmp::min(count, MAX_RECEIPTS_HEADERS_TO_SEND);
		let mut added_headers = 0usize;
		let mut data = Bytes::new();
		let mut total_bytes = 0;
		for i in 0..count {
			if let Some(receipts) = io.chain().block_receipts(&rlp.val_at::<H256>(i)?) {
				let mut receipts_bytes = ::tetsy_rlp::encode(&receipts);
				total_bytes += receipts_bytes.len();
				if total_bytes > payload_soft_limit { break; }
				data.append(&mut receipts_bytes);
				added_headers += 1;
			}
		}
		let mut rlp_result = RlpStream::new_list(added_headers);
		rlp_result.append_raw(&data, added_headers);
		Ok(Some((ReceiptsPacket.id(), rlp_result)))
	}

	/// Respond to GetSnapshotManifest request
	fn return_snapshot_manifest(io: &dyn SyncIo, r: &Rlp, peer_id: PeerId) -> RlpResponseResult {
		let count = r.item_count().unwrap_or(0);
		trace!(target: "warp", "{} -> GetSnapshotManifest", peer_id);
		if count != 0 {
			debug!(target: "warp", "Invalid GetSnapshotManifest request, ignoring.");
			return Ok(None);
		}
		let rlp = match io.snapshot_service().manifest() {
			Some(manifest) => {
				trace!(target: "warp", "{} <- SnapshotManifest", peer_id);
				let mut rlp = RlpStream::new_list(1);
				rlp.append_raw(&manifest.into_rlp(), 1);
				rlp
			},
			None => {
				trace!(target: "warp", "{}: No snapshot manifest to return", peer_id);
				RlpStream::new_list(0)
			}
		};
		Ok(Some((SnapshotManifestPacket.id(), rlp)))
	}

	/// Respond to GetSnapshotData request
	fn return_snapshot_data(io: &dyn SyncIo, r: &Rlp, peer_id: PeerId) -> RlpResponseResult {
		let hash: H256 = r.val_at(0)?;
		trace!(target: "warp", "{} -> GetSnapshotData {:?}", peer_id, hash);
		let rlp = match io.snapshot_service().chunk(hash) {
			Some(data) => {
				let mut rlp = RlpStream::new_list(1);
				trace!(target: "warp", "{} <- SnapshotData", peer_id);
				rlp.append(&data);
				rlp
			},
			None => {
				trace!(target: "warp", "{}: No snapshot data to return", peer_id);
				RlpStream::new_list(0)
			}
		};
		Ok(Some((SnapshotDataPacket.id(), rlp)))
	}

	/// Respond to GetPrivateStatePacket
	fn return_private_state(io: &dyn SyncIo, r: &Rlp, peer_id: PeerId) -> RlpResponseResult {
		let hash: H256 = r.val_at(0)?;
		trace!(target: "privatetx", "{} -> GetPrivateStatePacket {:?}", peer_id, hash);
		io.private_state().map_or(Ok(None), |db| {
			let state = db.state(&hash);
			match state {
				Err(err) => {
					trace!(target: "privatetx", "Cannot retrieve state from db {:?}", err);
					Ok(None)
				}
				Ok(bytes) => {
					let mut rlp = RlpStream::new_list(1);
					rlp.append(&bytes);
					Ok(Some((PrivateStatePacket.id(), rlp)))
				}
			}
		})
	}

	fn return_rlp<FRlp, FError>(io: &mut dyn SyncIo, rlp: &Rlp, peer: PeerId, rlp_func: FRlp, error_func: FError) -> Result<(), PacketDecodeError>
		where FRlp : Fn(&dyn SyncIo, &Rlp, PeerId) -> RlpResponseResult,
			FError : FnOnce(network::Error) -> String
	{
		let response = rlp_func(io, rlp, peer);
		match response {
			Err(e) => Err(e),
			Ok(Some((packet_id, rlp_stream))) => {
				io.respond(packet_id, rlp_stream.out()).unwrap_or_else(
					|e| debug!(target: "sync", "{:?}", error_func(e)));
				Ok(())
			}
			_ => Ok(())
		}
	}
}

#[cfg(test)]
mod test {
	use std::{collections::VecDeque, str::FromStr};

	use crate::{
		blocks::SyncHeader,
		chain::RlpResponseResult,
		tests::{helpers::TestIo, snapshot::TestSnapshotService}
	};

	use super::{
		SyncPacket::{GetReceiptsPacket, GetNodeDataPacket},
		BlockNumber, BlockId, SyncSupplier, PacketInfo
	};

	use super::super::tests::dummy_sync_with_peer;

	use bytes::Bytes;
	use client_traits::BlockChainClient;
	use vapcore::test_helpers::{EachBlockWith, TestBlockChainClient};
	use vapory_types::H256;
	use parking_lot::RwLock;
	use tetsy_rlp::{Rlp, RlpStream};

	#[test]
	fn return_block_headers() {
		fn make_hash_req(h: &H256, count: usize, skip: usize, reverse: bool) -> Bytes {
			let mut rlp = RlpStream::new_list(4);
			rlp.append(h);
			rlp.append(&count);
			rlp.append(&skip);
			rlp.append(&if reverse {1u32} else {0u32});
			rlp.out()
		}

		fn make_num_req(n: usize, count: usize, skip: usize, reverse: bool) -> Bytes {
			let mut rlp = RlpStream::new_list(4);
			rlp.append(&n);
			rlp.append(&count);
			rlp.append(&skip);
			rlp.append(&if reverse {1u32} else {0u32});
			rlp.out()
		}

		fn to_header_vec(rlp: RlpResponseResult) -> Vec<SyncHeader> {
			Rlp::new(&rlp.unwrap().unwrap().1.out()).iter().map(|r| SyncHeader::from_rlp(r.as_raw().to_vec()).unwrap()).collect()
		}

		let mut client = TestBlockChainClient::new();
		client.add_blocks(100, EachBlockWith::Nothing);
		let blocks: Vec<_> = (0 .. 100)
			.map(|i| (&client as &dyn BlockChainClient).block(BlockId::Number(i as BlockNumber)).map(|b| b.into_inner()).unwrap()).collect();
		let headers: Vec<_> = blocks.iter().map(|b| SyncHeader::from_rlp(Rlp::new(b).at(0).unwrap().as_raw().to_vec()).unwrap()).collect();
		let hashes: Vec<_> = headers.iter().map(|h| h.header.hash()).collect();

		let queue = RwLock::new(VecDeque::new());
		let ss = TestSnapshotService::new();
		let io = TestIo::new(&mut client, &ss, &queue, None, None);

		let unknown: H256 = H256::zero();
		let result = SyncSupplier::return_block_headers(&io, &Rlp::new(&make_hash_req(&unknown, 1, 0, false)), 0);
		assert!(to_header_vec(result).is_empty());
		let result = SyncSupplier::return_block_headers(&io, &Rlp::new(&make_hash_req(&unknown, 1, 0, true)), 0);
		assert!(to_header_vec(result).is_empty());

		let result = SyncSupplier::return_block_headers(&io, &Rlp::new(&make_hash_req(&hashes[2], 1, 0, true)), 0);
		assert_eq!(to_header_vec(result), vec![headers[2].clone()]);

		let result = SyncSupplier::return_block_headers(&io, &Rlp::new(&make_hash_req(&hashes[2], 1, 0, false)), 0);
		assert_eq!(to_header_vec(result), vec![headers[2].clone()]);

		let result = SyncSupplier::return_block_headers(&io, &Rlp::new(&make_hash_req(&hashes[50], 3, 5, false)), 0);
		assert_eq!(to_header_vec(result), vec![headers[50].clone(), headers[56].clone(), headers[62].clone()]);

		let result = SyncSupplier::return_block_headers(&io, &Rlp::new(&make_hash_req(&hashes[50], 3, 5, true)), 0);
		assert_eq!(to_header_vec(result), vec![headers[50].clone(), headers[44].clone(), headers[38].clone()]);

		let result = SyncSupplier::return_block_headers(&io, &Rlp::new(&make_num_req(2, 1, 0, true)), 0);
		assert_eq!(to_header_vec(result), vec![headers[2].clone()]);

		let result = SyncSupplier::return_block_headers(&io, &Rlp::new(&make_num_req(2, 1, 0, false)), 0);
		assert_eq!(to_header_vec(result), vec![headers[2].clone()]);

		let result = SyncSupplier::return_block_headers(&io, &Rlp::new(&make_num_req(50, 3, 5, false)), 0);
		assert_eq!(to_header_vec(result), vec![headers[50].clone(), headers[56].clone(), headers[62].clone()]);

		let result = SyncSupplier::return_block_headers(&io, &Rlp::new(&make_num_req(50, 3, 5, true)), 0);
		assert_eq!(to_header_vec(result), vec![headers[50].clone(), headers[44].clone(), headers[38].clone()]);
	}

	#[test]
	fn respect_packet_limit() {
		let small_num_blocks = 10;
		let large_num_blocks = 50;
		let tx_per_block = 100;

		let mut client = TestBlockChainClient::new();
		client.add_blocks(large_num_blocks, EachBlockWith::Transactions(tx_per_block));

		let mut small_rlp_request = RlpStream::new_list(small_num_blocks);
		let mut large_rlp_request = RlpStream::new_list(large_num_blocks);

		for i in 0..small_num_blocks {
			let hash: H256 = client.block_hash(BlockId::Number(i as u64)).unwrap();
			small_rlp_request.append(&hash);
			large_rlp_request.append(&hash);
		}

		for i in small_num_blocks..large_num_blocks {
			let hash: H256 = client.block_hash(BlockId::Number(i as u64)).unwrap();
			large_rlp_request.append(&hash);
		}

		let queue = RwLock::new(VecDeque::new());
		let ss = TestSnapshotService::new();
		let io = TestIo::new(&mut client, &ss, &queue, None, None);

		let small_result = SyncSupplier::return_block_bodies(&io, &Rlp::new(&small_rlp_request.out()), 0);
		let small_result = small_result.unwrap().unwrap().1;
		assert_eq!(Rlp::new(&small_result.out()).item_count().unwrap(), small_num_blocks);

		let large_result = SyncSupplier::return_block_bodies(&io, &Rlp::new(&large_rlp_request.out()), 0);
		let large_result = large_result.unwrap().unwrap().1;
		assert!(Rlp::new(&large_result.out()).item_count().unwrap() < large_num_blocks);
	}

	#[test]
	fn return_nodes() {
		let mut client = TestBlockChainClient::new();
		let queue = RwLock::new(VecDeque::new());
		let sync = dummy_sync_with_peer(H256::zero(), &client);
		let ss = TestSnapshotService::new();
		let mut io = TestIo::new(&mut client, &ss, &queue, None, None);

		let mut node_list = RlpStream::new_list(3);
		node_list.append(&H256::from_str("0000000000000000000000000000000000000000000000005555555555555555").unwrap());
		node_list.append(&H256::from_str("ffffffffffffffffffffffffffffffffffffffffffffaaaaaaaaaaaaaaaaaaaa").unwrap());
		node_list.append(&H256::from_str("aff0000000000000000000000000000000000000000000000000000000000000").unwrap());

		let node_request = node_list.out();
		// it returns rlp ONLY for hashes started with "f"
		let result = SyncSupplier::return_node_data(&io, &Rlp::new(&node_request.clone()), 0);

		assert!(result.is_ok());
		let rlp_result = result.unwrap();
		assert!(rlp_result.is_some());

		// the length of one rlp-encoded hash
		let rlp = rlp_result.unwrap().1.out();
		let rlp = Rlp::new(&rlp);
		assert_eq!(Ok(1), rlp.item_count());

		io.sender = Some(2usize);

		SyncSupplier::dispatch_packet(&RwLock::new(sync), &mut io, 0usize, GetNodeDataPacket.id(), &node_request);
		assert_eq!(1, io.packets.len());
	}

	#[test]
	fn return_receipts_empty() {
		let mut client = TestBlockChainClient::new();
		let queue = RwLock::new(VecDeque::new());
		let ss = TestSnapshotService::new();
		let io = TestIo::new(&mut client, &ss, &queue, None, None);

		let result = SyncSupplier::return_receipts(&io, &Rlp::new(&[0xc0]), 0);

		assert!(result.is_ok());
	}

	#[test]
	fn return_receipts() {
		let mut client = TestBlockChainClient::new();
		let queue = RwLock::new(VecDeque::new());
		let sync = dummy_sync_with_peer(H256::zero(), &client);
		let ss = TestSnapshotService::new();
		let mut io = TestIo::new(&mut client, &ss, &queue, None, None);

		let mut receipt_list = RlpStream::new_list(4);
		receipt_list.append(&H256::from_str("0000000000000000000000000000000000000000000000005555555555555555").unwrap());
		receipt_list.append(&H256::from_str("ff00000000000000000000000000000000000000000000000000000000000000").unwrap());
		receipt_list.append(&H256::from_str("fff0000000000000000000000000000000000000000000000000000000000000").unwrap());
		receipt_list.append(&H256::from_str("aff0000000000000000000000000000000000000000000000000000000000000").unwrap());

		let receipts_request = receipt_list.out();
		// it returns rlp ONLY for hashes started with "f"
		let result = SyncSupplier::return_receipts(&io, &Rlp::new(&receipts_request.clone()), 0);

		assert!(result.is_ok());
		let rlp_result = result.unwrap();
		assert!(rlp_result.is_some());

		// the length of two rlp-encoded receipts
		assert_eq!(603, rlp_result.unwrap().1.out().len());

		io.sender = Some(2usize);
		SyncSupplier::dispatch_packet(&RwLock::new(sync), &mut io, 0usize, GetReceiptsPacket.id(), &receipts_request);
		assert_eq!(1, io.packets.len());
	}
}