lib.rs 10 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
// Copyright 2017-2020 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/>.

//! Primitive types used on the node-side.
//!
//! Unlike the `polkadot-primitives` crate, these primitives are only used on the node-side,
//! not shared between the node and the runtime. This crate builds on top of the primitives defined
//! there.

23
24
#![deny(missing_docs)]

25
use futures::Future;
26
use parity_scale_codec::{Decode, Encode};
asynchronous rob's avatar
asynchronous rob committed
27
28
29
use polkadot_primitives::v1::{
	Hash, CommittedCandidateReceipt, CandidateReceipt, CompactStatement,
	EncodeAs, Signed, SigningContext, ValidatorIndex, ValidatorId,
30
	UpwardMessage, ValidationCode, PersistedValidationData,
31
	HeadData, PoV, CollatorPair, Id as ParaId, OutboundHrmpMessage, CandidateCommitments, CandidateHash,
32
};
33
34
35
36
37
use polkadot_statement_table::{
	generic::{
		ValidityDoubleVote as TableValidityDoubleVote,
		MultipleCandidates as TableMultipleCandidates,
	},
asynchronous rob's avatar
asynchronous rob committed
38
	v1::Misbehavior as TableMisbehavior,
39
};
40
use std::pin::Pin;
41

42
43
pub use sp_core::traits::SpawnNamed;

asynchronous rob's avatar
asynchronous rob committed
44
45
pub mod approval;

46
/// A statement, where the candidate receipt is included in the `Seconded` variant.
asynchronous rob's avatar
asynchronous rob committed
47
48
49
50
51
///
/// This is the committed candidate receipt instead of the bare candidate receipt. As such,
/// it gives access to the commitments to validators who have not executed the candidate. This
/// is necessary to allow a block-producing validator to include candidates from outside of the para
/// it is assigned to.
52
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
53
54
55
pub enum Statement {
	/// A statement that a validator seconds a candidate.
	#[codec(index = "1")]
asynchronous rob's avatar
asynchronous rob committed
56
	Seconded(CommittedCandidateReceipt),
57
58
	/// A statement that a validator has deemed a candidate valid.
	#[codec(index = "2")]
59
	Valid(CandidateHash),
60
61
	/// A statement that a validator has deemed a candidate invalid.
	#[codec(index = "3")]
62
	Invalid(CandidateHash),
63
64
}

65
impl Statement {
66
67
68
69
70
71
72
73
74
75
76
	/// Get the candidate hash referenced by this statement.
	///
	/// If this is a `Statement::Seconded`, this does hash the candidate receipt, which may be expensive
	/// for large candidates.
	pub fn candidate_hash(&self) -> CandidateHash {
		match *self {
			Statement::Valid(ref h) | Statement::Invalid(ref h) => *h,
			Statement::Seconded(ref c) => c.hash(),
		}
	}

asynchronous rob's avatar
asynchronous rob committed
77
78
	/// Transform this statement into its compact version, which references only the hash
	/// of the candidate.
79
80
81
82
83
84
85
86
87
	pub fn to_compact(&self) -> CompactStatement {
		match *self {
			Statement::Seconded(ref c) => CompactStatement::Candidate(c.hash()),
			Statement::Valid(hash) => CompactStatement::Valid(hash),
			Statement::Invalid(hash) => CompactStatement::Invalid(hash),
		}
	}
}

88
89
impl EncodeAs<CompactStatement> for Statement {
	fn encode_as(&self) -> Vec<u8> {
90
		self.to_compact().encode()
91
92
93
94
95
96
	}
}

/// A statement, the corresponding signature, and the index of the sender.
///
/// Signing context and validator set should be apparent from context.
97
98
99
100
///
/// This statement is "full" in the sense that the `Seconded` variant includes the candidate receipt.
/// Only the compact `SignedStatement` is suitable for submission to the chain.
pub type SignedFullStatement = Signed<Statement, CompactStatement>;
101
102

/// A misbehaviour report.
103
#[derive(Debug, Clone)]
104
105
106
107
108
109
110
111
112
pub enum MisbehaviorReport {
	/// These validator nodes disagree on this candidate's validity, please figure it out
	///
	/// Most likely, the list of statments all agree except for the final one. That's not
	/// guaranteed, though; if somehow we become aware of lots of
	/// statements disagreeing about the validity of a candidate before taking action,
	/// this message should be dispatched with all of them, in arbitrary order.
	///
	/// This variant is also used when our own validity checks disagree with others'.
asynchronous rob's avatar
asynchronous rob committed
113
	CandidateValidityDisagreement(CandidateReceipt, Vec<SignedFullStatement>),
114
	/// I've noticed a peer contradicting itself about a particular candidate
asynchronous rob's avatar
asynchronous rob committed
115
	SelfContradiction(CandidateReceipt, SignedFullStatement, SignedFullStatement),
116
	/// This peer has seconded more than one parachain candidate for this relay parent head
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
	DoubleVote(SignedFullStatement, SignedFullStatement),
}

/// A utility struct used to convert `TableMisbehavior` to `MisbehaviorReport`s.
pub struct FromTableMisbehavior {
	/// Index of the validator.
	pub id: ValidatorIndex,
	/// The misbehavior reported by the table.
	pub report: TableMisbehavior,
	/// Signing context.
	pub signing_context: SigningContext,
	/// Misbehaving validator's public key.
	pub key: ValidatorId,
}

132
133
134
135
136
/// Candidate invalidity details
#[derive(Debug)]
pub enum InvalidCandidate {
	/// Failed to execute.`validate_block`. This includes function panicking.
	ExecutionError(String),
137
138
	/// Validation outputs check doesn't pass.
	InvalidOutputs,
139
140
141
142
143
144
145
146
147
148
149
150
151
152
	/// Execution timeout.
	Timeout,
	/// Validation input is over the limit.
	ParamsTooLarge(u64),
	/// Code size is over the limit.
	CodeTooLarge(u64),
	/// Validation function returned invalid data.
	BadReturn,
	/// Invalid relay chain parent.
	BadParent,
	/// POV hash does not match.
	HashMismatch,
	/// Bad collator signature.
	BadSignature,
153
154
	/// Para head hash does not match.
	ParaHeadHashMismatch,
155
156
}

157
158
159
/// Result of the validation of the candidate.
#[derive(Debug)]
pub enum ValidationResult {
160
161
	/// Candidate is valid. The validation process yields these outputs and the persisted validation
	/// data used to form inputs.
162
	Valid(CandidateCommitments, PersistedValidationData),
163
	/// Candidate is invalid.
164
	Invalid(InvalidCandidate),
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
}

impl std::convert::TryFrom<FromTableMisbehavior> for MisbehaviorReport {
	type Error = ();

	fn try_from(f: FromTableMisbehavior) -> Result<Self, Self::Error> {
		match f.report {
			TableMisbehavior::ValidityDoubleVote(
				TableValidityDoubleVote::IssuedAndValidity((c, s1), (d, s2))
			) => {
				let receipt = c.clone();
				let signed_1 = SignedFullStatement::new(
					Statement::Seconded(c),
					f.id,
					s1,
					&f.signing_context,
					&f.key,
				).ok_or(())?;
				let signed_2 = SignedFullStatement::new(
					Statement::Valid(d),
					f.id,
					s2,
					&f.signing_context,
					&f.key,
				).ok_or(())?;

asynchronous rob's avatar
asynchronous rob committed
191
				Ok(MisbehaviorReport::SelfContradiction(receipt.to_plain(), signed_1, signed_2))
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
			}
			TableMisbehavior::ValidityDoubleVote(
				TableValidityDoubleVote::IssuedAndInvalidity((c, s1), (d, s2))
			) => {
				let receipt = c.clone();
				let signed_1 = SignedFullStatement::new(
					Statement::Seconded(c),
					f.id,
					s1,
					&f.signing_context,
					&f.key,
				).ok_or(())?;
				let signed_2 = SignedFullStatement::new(
					Statement::Invalid(d),
					f.id,
					s2,
					&f.signing_context,
					&f.key,
				).ok_or(())?;

asynchronous rob's avatar
asynchronous rob committed
212
				Ok(MisbehaviorReport::SelfContradiction(receipt.to_plain(), signed_1, signed_2))
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
			}
			TableMisbehavior::ValidityDoubleVote(
				TableValidityDoubleVote::ValidityAndInvalidity(c, s1, s2)
			) => {
				let signed_1 = SignedFullStatement::new(
					Statement::Valid(c.hash()),
					f.id,
					s1,
					&f.signing_context,
					&f.key,
				).ok_or(())?;
				let signed_2 = SignedFullStatement::new(
					Statement::Invalid(c.hash()),
					f.id,
					s2,
					&f.signing_context,
					&f.key,
				).ok_or(())?;

asynchronous rob's avatar
asynchronous rob committed
232
				Ok(MisbehaviorReport::SelfContradiction(c.to_plain(), signed_1, signed_2))
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
			}
			TableMisbehavior::MultipleCandidates(
				TableMultipleCandidates {
					first,
					second,
				}
			) => {
				let signed_1 = SignedFullStatement::new(
					Statement::Seconded(first.0),
					f.id,
					first.1,
					&f.signing_context,
					&f.key,
				).ok_or(())?;

				let signed_2 = SignedFullStatement::new(
					Statement::Seconded(second.0),
					f.id,
					second.1,
					&f.signing_context,
					&f.key,
				).ok_or(())?;

				Ok(MisbehaviorReport::DoubleVote(signed_1, signed_2))
			}
			_ => Err(()),
		}
	}
261
}
262
263
264
265
266
267
268
269

/// The output of a collator.
///
/// This differs from `CandidateCommitments` in two ways:
///
/// - does not contain the erasure root; that's computed at the Polkadot level, not at Cumulus
/// - contains a proof of validity.
#[derive(Clone, Encode, Decode)]
Sergey Pepyakin's avatar
Sergey Pepyakin committed
270
pub struct Collation<BlockNumber = polkadot_primitives::v1::BlockNumber> {
271
272
	/// Messages destined to be interpreted by the Relay chain itself.
	pub upward_messages: Vec<UpwardMessage>,
Sergey Pepyakin's avatar
Sergey Pepyakin committed
273
274
	/// The horizontal messages sent by the parachain.
	pub horizontal_messages: Vec<OutboundHrmpMessage<ParaId>>,
275
276
277
278
	/// New validation code.
	pub new_validation_code: Option<ValidationCode>,
	/// The head-data produced as a result of execution.
	pub head_data: HeadData,
279
	/// Proof to verify the state transition of the parachain.
280
	pub proof_of_validity: PoV,
281
282
	/// The number of messages processed from the DMQ.
	pub processed_downward_messages: u32,
Sergey Pepyakin's avatar
Sergey Pepyakin committed
283
284
	/// The mark which specifies the block number up to which all inbound HRMP messages are processed.
	pub hrmp_watermark: BlockNumber,
285
286
}

Sergey Pepyakin's avatar
Sergey Pepyakin committed
287
288
289
290
291
292
/// Collation function.
///
/// Will be called with the hash of the relay chain block the parachain
/// block should be build on and the [`ValidationData`] that provides
/// information about the state of the parachain on the relay chain.
pub type CollatorFn = Box<
293
	dyn Fn(Hash, &PersistedValidationData) -> Pin<Box<dyn Future<Output = Option<Collation>> + Send>>
Sergey Pepyakin's avatar
Sergey Pepyakin committed
294
295
296
297
		+ Send
		+ Sync,
>;

298
299
300
301
/// Configuration for the collation generator
pub struct CollationGenerationConfig {
	/// Collator's authentication key, so it can sign things.
	pub key: CollatorPair,
Sergey Pepyakin's avatar
Sergey Pepyakin committed
302
303
	/// Collation function. See [`CollatorFn`] for more details.
	pub collator: CollatorFn,
304
305
306
307
308
309
310
311
312
	/// The parachain that this collator collates for
	pub para_id: ParaId,
}

impl std::fmt::Debug for CollationGenerationConfig {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		write!(f, "CollationGenerationConfig {{ ... }}")
	}
}