Repository: paritytech/substrate
File: client/network/test/src/sync.rs (Rust)

Code before:
	let mut net = JustificationTestNet::new(runtime.handle().clone(), 3);
	let hashes = net.peer(0).push_blocks(20, false);
	runtime.block_on(net.wait_until_sync());

	let hashof10 = hashes[9];
	let hashof15 = hashes[14];
	let hashof20 = hashes[19];

	// there's currently no justification for block #10
	assert_eq!(net.peer(0).client().justifications(hashof10).unwrap(), None);
	assert_eq!(net.peer(1).client().justifications(hashof10).unwrap(), None);

	// we finalize block #10, #15 and #20 for peer 0 with a justification
	let just = (*b"FRNK", Vec::new());
	net.peer(0).client().finalize_block(hashof10, Some(just.clone()), true).unwrap();
	net.peer(0).client().finalize_block(hashof15, Some(just.clone()), true).unwrap();
	net.peer(0).client().finalize_block(hashof20, Some(just.clone()), true).unwrap();

	let hashof10 = net.peer(1).client().header(&BlockId::Number(10)).unwrap().unwrap().hash();
	let hashof15 = net.peer(1).client().header(&BlockId::Number(15)).unwrap().unwrap().hash();
	let hashof20 = net.peer(1).client().header(&BlockId::Number(20)).unwrap().unwrap().hash();

	// peer 1 should get the justifications from the network
	net.peer(1).request_justification(&hashof10, 10);
	net.peer(1).request_justification(&hashof15, 15);
	net.peer(1).request_justification(&hashof20, 20);

	runtime.block_on(futures::future::poll_fn::<(), _>(|cx| {
		net.poll(cx);

		for height in (10..21).step_by(5) {
			if net.peer(0).client().justifications(hashes[height - 1]).unwrap() !=
				Some(Justifications::from((*b"FRNK", Vec::new())))
			{
				return Poll::Pending
			}
			if net.peer(1).client().justifications(hashes[height - 1]).unwrap() !=
				Some(Justifications::from((*b"FRNK", Vec::new())))
			{
				return Poll::Pending
			}
		}

		Poll::Ready(())
	}));
}

#[test]
fn sync_justifications_across_forks() {
	sp_tracing::try_init_simple();
	let runtime = Runtime::new().unwrap();

Review comment (refactor):
This was just reverted to original form (as it was before `BlockId`-refactor:
https://github.com/paritytech/substrate/blob/97ad64bd507eacf1dc8c597cf8538c98facb3220/client/network/test/src/sync.rs#L280-L291

Code after:
	let hashof15 = hashes[14];
	let hashof20 = hashes[19];

	// there's currently no justification for block #10
	assert_eq!(net.peer(0).client().justifications(hashof10).unwrap(), None);
	assert_eq!(net.peer(1).client().justifications(hashof10).unwrap(), None);

	// we finalize block #10, #15 and #20 for peer 0 with a justification
	let just = (*b"FRNK", Vec::new());
	net.peer(0).client().finalize_block(hashof10, Some(just.clone()), true).unwrap();
	net.peer(0).client().finalize_block(hashof15, Some(just.clone()), true).unwrap();
	net.peer(0).client().finalize_block(hashof20, Some(just.clone()), true).unwrap();

	let hashof10 = net.peer(1).client().header(&BlockId::Number(10)).unwrap().unwrap().hash();
	let hashof15 = net.peer(1).client().header(&BlockId::Number(15)).unwrap().unwrap().hash();
	let hashof20 = net.peer(1).client().header(&BlockId::Number(20)).unwrap().unwrap().hash();

	// peer 1 should get the justifications from the network
	net.peer(1).request_justification(&hashof10, 10);
	net.peer(1).request_justification(&hashof15, 15);
	net.peer(1).request_justification(&hashof20, 20);

	futures::future::poll_fn::<(), _>(|cx| {
		net.poll(cx);

		for height in (10..21).step_by(5) {
			if net.peer(0).client().justifications(hashes[height - 1]).unwrap() !=
				Some(Justifications::from((*b"FRNK", Vec::new())))
			{
				return Poll::Pending
			}
			if net.peer(1).client().justifications(hashes[height - 1]).unwrap() !=
				Some(Justifications::from((*b"FRNK", Vec::new())))
			{
				return Poll::Pending
			}
		}

		Poll::Ready(())
	})
	.await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn sync_justifications_across_forks() {
	sp_tracing::try_init_simple();
	let mut net = JustificationTestNet::new(3);
	// we push 5 blocks
	net.peer(0).push_blocks(5, false);
	// and then two forks 5 and 6 blocks long
	let f1_best = net.peer(0).push_blocks_at(BlockId::Number(5), 5, false).pop().unwrap();