tests.rs 6.85 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Copyright 2018 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot 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.

// Polkadot 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 Polkadot.  If not, see <http://www.gnu.org/licenses/>.

//! Tests for polkadot and consensus network.

use super::{PolkadotProtocol, Status, CurrentConsensus, Knowledge, Message, FullStatus};

use parking_lot::Mutex;
use polkadot_consensus::GenericStatement;
use polkadot_primitives::{Block, Hash, SessionKey};
use polkadot_primitives::parachain::{CandidateReceipt, HeadData, BlockData};
25
use substrate_primitives::H512;
Arkadiy Paronyan's avatar
Arkadiy Paronyan committed
26
use codec::Encode;
27
use substrate_network::{PeerId, PeerInfo, ClientHandle, Context, Roles, message::Message as SubstrateMessage, specialization::Specialization, generic_message::Message as GenericMessage};
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

use std::sync::Arc;
use futures::Future;

#[derive(Default)]
struct TestContext {
	disabled: Vec<PeerId>,
	disconnected: Vec<PeerId>,
	messages: Vec<(PeerId, SubstrateMessage<Block>)>,
}

impl Context<Block> for TestContext {
	fn client(&self) -> &ClientHandle<Block> {
		unimplemented!()
	}

44
	fn disable_peer(&mut self, peer: PeerId, _reason: &str) {
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
		self.disabled.push(peer);
	}

	fn disconnect_peer(&mut self, peer: PeerId) {
		self.disconnected.push(peer);
	}

	fn peer_info(&self, _peer: PeerId) -> Option<PeerInfo<Block>> {
		unimplemented!()
	}

	fn send_message(&mut self, peer_id: PeerId, data: SubstrateMessage<Block>) {
		self.messages.push((peer_id, data))
	}
}

impl TestContext {
	fn has_message(&self, to: PeerId, message: Message) -> bool {
		use substrate_network::generic_message::Message as GenericMessage;

65
		let encoded = message.encode();
66
67
68
69
70
71
72
		self.messages.iter().any(|&(ref peer, ref msg)| match msg {
			GenericMessage::ChainSpecific(ref data) => peer == &to && data == &encoded,
			_ => false,
		})
	}
}

73
fn make_status(status: &Status, roles: Roles) -> FullStatus {
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
	FullStatus {
		version: 1,
		roles,
		best_number: 0,
		best_hash: Default::default(),
		genesis_hash: Default::default(),
		chain_status: status.encode(),
	}
}

fn make_consensus(parent_hash: Hash, local_key: SessionKey) -> (CurrentConsensus, Arc<Mutex<Knowledge>>) {
	let knowledge = Arc::new(Mutex::new(Knowledge::new()));
	let c = CurrentConsensus {
		knowledge: knowledge.clone(),
		parent_hash,
		local_session_key: local_key,
	};

	(c, knowledge)
}

fn on_message(protocol: &mut PolkadotProtocol, ctx: &mut TestContext, from: PeerId, message: Message) {
96
	let encoded = message.encode();
97
98
99
100
101
	protocol.on_message(ctx, from, GenericMessage::ChainSpecific(encoded));
}

#[test]
fn sends_session_key() {
102
	let mut protocol = PolkadotProtocol::new(None);
103
104
105
106
107
108

	let peer_a = 1;
	let peer_b = 2;
	let parent_hash = [0; 32].into();
	let local_key = [1; 32].into();

109
110
	let validator_status = Status { collating_for: None };
	let collator_status = Status { collating_for: Some(([2; 32].into(), 5.into())) };
111
112
113

	{
		let mut ctx = TestContext::default();
114
		protocol.on_connect(&mut ctx, peer_a, make_status(&validator_status, Roles::AUTHORITY));
115
116
117
118
119
120
121
		assert!(ctx.messages.is_empty());
	}

	{
		let mut ctx = TestContext::default();
		let (consensus, _knowledge) = make_consensus(parent_hash, local_key);
		protocol.new_consensus(&mut ctx, consensus);
122
		assert!(ctx.has_message(peer_a, Message::SessionKey(local_key)));
123
124
125
126
	}

	{
		let mut ctx = TestContext::default();
127
		protocol.on_connect(&mut ctx, peer_b, make_status(&collator_status, Roles::NONE));
128
		assert!(ctx.has_message(peer_b, Message::SessionKey(local_key)));
129
130
131
132
133
	}
}

#[test]
fn fetches_from_those_with_knowledge() {
134
	let mut protocol = PolkadotProtocol::new(None);
135
136
137
138
139
140
141
142
143
144
145
146

	let peer_a = 1;
	let peer_b = 2;
	let parent_hash = [0; 32].into();
	let local_key = [1; 32].into();

	let block_data = BlockData(vec![1, 2, 3, 4]);
	let block_data_hash = block_data.hash();
	let candidate_receipt = CandidateReceipt {
		parachain_index: 5.into(),
		collator: [255; 32].into(),
		head_data: HeadData(vec![9, 9, 9]),
147
		signature: H512::from([1; 64]).into(),
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
		balance_uploads: Vec::new(),
		egress_queue_roots: Vec::new(),
		fees: 1_000_000,
		block_data_hash,
	};

	let candidate_hash = candidate_receipt.hash();
	let a_key = [3; 32].into();
	let b_key = [4; 32].into();

	let status = Status { collating_for: None };

	let (consensus, knowledge) = make_consensus(parent_hash, local_key);
	protocol.new_consensus(&mut TestContext::default(), consensus);

	knowledge.lock().note_statement(a_key, &GenericStatement::Valid(candidate_hash));
	let recv = protocol.fetch_block_data(&mut TestContext::default(), &candidate_receipt, parent_hash);

	// connect peer A
	{
		let mut ctx = TestContext::default();
169
		protocol.on_connect(&mut ctx, peer_a, make_status(&status, Roles::AUTHORITY));
170
		assert!(ctx.has_message(peer_a, Message::SessionKey(local_key)));
171
172
173
174
175
	}

	// peer A gives session key and gets asked for data.
	{
		let mut ctx = TestContext::default();
176
177
		on_message(&mut protocol, &mut ctx, peer_a, Message::SessionKey(a_key));
		assert!(protocol.validators.contains_key(&a_key));
178
179
180
181
182
183
184
185
		assert!(ctx.has_message(peer_a, Message::RequestBlockData(1, candidate_hash)));
	}

	knowledge.lock().note_statement(b_key, &GenericStatement::Valid(candidate_hash));

	// peer B connects and sends session key. request already assigned to A
	{
		let mut ctx = TestContext::default();
186
		protocol.on_connect(&mut ctx, peer_b, make_status(&status, Roles::AUTHORITY));
187
		on_message(&mut protocol, &mut ctx, peer_b, Message::SessionKey(b_key));
188
189
190
191
192
193
194
195
		assert!(!ctx.has_message(peer_b, Message::RequestBlockData(2, candidate_hash)));

	}

	// peer A disconnects, triggering reassignment
	{
		let mut ctx = TestContext::default();
		protocol.on_disconnect(&mut ctx, peer_a);
196
		assert!(!protocol.validators.contains_key(&a_key));
197
198
199
200
201
202
203
204
205
206
207
		assert!(ctx.has_message(peer_b, Message::RequestBlockData(2, candidate_hash)));
	}

	// peer B comes back with block data.
	{
		let mut ctx = TestContext::default();
		on_message(&mut protocol, &mut ctx, peer_b, Message::BlockData(2, Some(block_data.clone())));
		drop(protocol);
		assert_eq!(recv.wait().unwrap(), block_data);
	}
}
208
209
210

#[test]
fn remove_bad_collator() {
211
	let mut protocol = PolkadotProtocol::new(None);
212
213
214
215
216
217
218
219

	let peer_id = 1;
	let account_id = [2; 32].into();

	let status = Status { collating_for: Some((account_id, 5.into())) };

	{
		let mut ctx = TestContext::default();
220
		protocol.on_connect(&mut ctx, peer_id, make_status(&status, Roles::NONE));
221
222
223
224
225
226
227
228
	}

	{
		let mut ctx = TestContext::default();
		protocol.disconnect_bad_collator(&mut ctx, account_id);
		assert!(ctx.disabled.contains(&peer_id));
	}
}