primitives.rs 12.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Copyright 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 which are strictly necessary from a parachain-execution point
//! of view.

use sp_std::vec::Vec;

Shawn Tabrizi's avatar
Shawn Tabrizi committed
22
23
use frame_support::weights::Weight;
use parity_scale_codec::{CompactAs, Decode, Encode};
24
use sp_core::{RuntimeDebug, TypeId};
25
use sp_runtime::traits::Hash as _;
26
27

#[cfg(feature = "std")]
Shawn Tabrizi's avatar
Shawn Tabrizi committed
28
use serde::{Deserialize, Serialize};
29
30
31
32

#[cfg(feature = "std")]
use sp_core::bytes;

33
34
35
#[cfg(feature = "std")]
use parity_util_mem::MallocSizeOf;

Sergey Pepyakin's avatar
Sergey Pepyakin committed
36
use polkadot_core_primitives::{Hash, OutboundHrmpMessage};
37

38
39
/// Block number type used by the relay chain.
pub use polkadot_core_primitives::BlockNumber as RelayChainBlockNumber;
40
41

/// Parachain head data included in the chain.
Shawn Tabrizi's avatar
Shawn Tabrizi committed
42
43
44
#[derive(
	PartialEq, Eq, Clone, PartialOrd, Ord, Encode, Decode, RuntimeDebug, derive_more::From,
)]
45
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Default, Hash, MallocSizeOf))]
Shawn Tabrizi's avatar
Shawn Tabrizi committed
46
pub struct HeadData(#[cfg_attr(feature = "std", serde(with = "bytes"))] pub Vec<u8>);
47

48
49
50
51
52
53
54
55
#[cfg(feature = "std")]
impl HeadData {
	/// Returns the hash of this head data.
	pub fn hash(&self) -> Hash {
		sp_runtime::traits::BlakeTwo256::hash(&self.0)
	}
}

56
/// Parachain validation code.
57
#[derive(Default, PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, derive_more::From)]
58
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Hash, MallocSizeOf))]
59
pub struct ValidationCode(#[cfg_attr(feature = "std", serde(with = "bytes"))] pub Vec<u8>);
60

61
62
impl ValidationCode {
	/// Get the blake2-256 hash of the validation code bytes.
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
	pub fn hash(&self) -> ValidationCodeHash {
		ValidationCodeHash(sp_runtime::traits::BlakeTwo256::hash(&self.0[..]))
	}
}

/// Unit type wrapper around [`Hash`] that represents a validation code hash.
///
/// This type is produced by [`ValidationCode::hash`].
///
/// This type makes it easy to enforce that a hash is a validation code hash on the type level.
#[derive(Clone, Copy, Encode, Decode, Default, Hash, Eq, PartialEq, PartialOrd, Ord)]
#[cfg_attr(feature = "std", derive(MallocSizeOf))]
pub struct ValidationCodeHash(Hash);

impl sp_std::fmt::Display for ValidationCodeHash {
	fn fmt(&self, f: &mut sp_std::fmt::Formatter<'_>) -> sp_std::fmt::Result {
		self.0.fmt(f)
	}
}

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

impl AsRef<[u8]> for ValidationCodeHash {
	fn as_ref(&self) -> &[u8] {
		self.0.as_ref()
	}
}

impl From<Hash> for ValidationCodeHash {
	fn from(hash: Hash) -> ValidationCodeHash {
		ValidationCodeHash(hash)
98
99
100
	}
}

101
102
103
104
105
106
107
108
impl From<[u8; 32]> for ValidationCodeHash {
	fn from(hash: [u8; 32]) -> ValidationCodeHash {
		ValidationCodeHash(hash.into())
	}
}

impl sp_std::fmt::LowerHex for ValidationCodeHash {
	fn fmt(&self, f: &mut sp_std::fmt::Formatter<'_>) -> sp_std::fmt::Result {
109
		sp_std::fmt::LowerHex::fmt(&self.0, f)
110
111
	}
}
112

113
114
115
/// Parachain block data.
///
/// Contains everything required to validate para-block, may contain block and witness data.
116
#[derive(PartialEq, Eq, Clone, Encode, Decode, derive_more::From)]
117
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug, MallocSizeOf))]
118
pub struct BlockData(#[cfg_attr(feature = "std", serde(with = "bytes"))] pub Vec<u8>);
119
120
121

/// Unique identifier of a parachain.
#[derive(
Shawn Tabrizi's avatar
Shawn Tabrizi committed
122
123
124
125
126
127
128
129
130
131
132
133
	Clone,
	CompactAs,
	Copy,
	Decode,
	Default,
	Encode,
	Eq,
	Hash,
	Ord,
	PartialEq,
	PartialOrd,
	RuntimeDebug,
134
)]
Shawn Tabrizi's avatar
Shawn Tabrizi committed
135
136
137
#[cfg_attr(
	feature = "std",
	derive(serde::Serialize, serde::Deserialize, derive_more::Display, MallocSizeOf)
138
)]
139
140
141
142
143
144
145
pub struct Id(u32);

impl TypeId for Id {
	const TYPE_ID: [u8; 4] = *b"para";
}

impl From<Id> for u32 {
Shawn Tabrizi's avatar
Shawn Tabrizi committed
146
147
148
	fn from(x: Id) -> Self {
		x.0
	}
149
150
151
}

impl From<u32> for Id {
Shawn Tabrizi's avatar
Shawn Tabrizi committed
152
153
154
	fn from(x: u32) -> Self {
		Id(x)
	}
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
impl From<usize> for Id {
	fn from(x: usize) -> Self {
		use sp_std::convert::TryInto;
		// can't panic, so need to truncate
		let x = x.try_into().unwrap_or(u32::MAX);
		Id(x)
	}
}

// When we added a second From impl for Id, type inference could no longer
// determine which impl should apply for things like `5.into()`. It therefore
// raised a bunch of errors in our test code, scattered throughout the
// various modules' tests, that there is no impl of `From<i32>` (`i32` being
// the default numeric type).
//
// We can't use `cfg(test)` here, because that configuration directive does not
// propagate between crates, which would fail to fix tests in crates other than
// this one.
//
// Instead, let's take advantage of the observation that what really matters for a
// ParaId within a test context is that it is unique and constant. I believe that
// there is no case where someone does `(-1).into()` anyway, but if they do, it
// never matters whether the actual contained ID is `-1` or `4294967295`. Nobody
// does arithmetic on a `ParaId`; doing so would be a bug.
impl From<i32> for Id {
	fn from(x: i32) -> Self {
		Id(x as u32)
	}
}

187
const USER_INDEX_START: u32 = 1000;
188
const PUBLIC_INDEX_START: u32 = 2000;
189
190
191
192

/// The ID of the first user (non-system) parachain.
pub const LOWEST_USER_ID: Id = Id(USER_INDEX_START);

193
194
195
/// The ID of the first publicly registerable parachain.
pub const LOWEST_PUBLIC_ID: Id = Id(PUBLIC_INDEX_START);

196
197
198
199
200
201
202
impl Id {
	/// Create an `Id`.
	pub const fn new(id: u32) -> Self {
		Self(id)
	}
}

Alexander Popiak's avatar
Alexander Popiak committed
203
/// Determine if a parachain is a system parachain or not.
Shawn Tabrizi's avatar
Shawn Tabrizi committed
204
pub trait IsSystem {
Alexander Popiak's avatar
Alexander Popiak committed
205
	/// Returns `true` if a parachain is a system parachain, `false` otherwise.
Shawn Tabrizi's avatar
Shawn Tabrizi committed
206
207
208
209
210
211
212
213
214
	fn is_system(&self) -> bool;
}

impl IsSystem for Id {
	fn is_system(&self) -> bool {
		self.0 < USER_INDEX_START
	}
}

215
216
217
218
219
220
221
222
impl sp_std::ops::Add<u32> for Id {
	type Output = Self;

	fn add(self, other: u32) -> Self {
		Self(self.0 + other)
	}
}

223
224
225
226
227
228
229
230
impl sp_std::ops::Sub<u32> for Id {
	type Output = Self;

	fn sub(self, other: u32) -> Self {
		Self(self.0 - other)
	}
}

Shawn Tabrizi's avatar
Shawn Tabrizi committed
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
#[derive(Clone, Copy, Default, Encode, Decode, Eq, PartialEq, Ord, PartialOrd, RuntimeDebug)]
pub struct Sibling(pub Id);

impl From<Id> for Sibling {
	fn from(i: Id) -> Self {
		Self(i)
	}
}

impl From<Sibling> for Id {
	fn from(i: Sibling) -> Self {
		i.0
	}
}

impl AsRef<Id> for Sibling {
	fn as_ref(&self) -> &Id {
		&self.0
	}
}

impl TypeId for Sibling {
	const TYPE_ID: [u8; 4] = *b"sibl";
}

impl From<Sibling> for u32 {
Shawn Tabrizi's avatar
Shawn Tabrizi committed
257
258
259
	fn from(x: Sibling) -> Self {
		x.0.into()
	}
Shawn Tabrizi's avatar
Shawn Tabrizi committed
260
261
262
}

impl From<u32> for Sibling {
Shawn Tabrizi's avatar
Shawn Tabrizi committed
263
264
265
	fn from(x: u32) -> Self {
		Sibling(x.into())
	}
Shawn Tabrizi's avatar
Shawn Tabrizi committed
266
267
268
269
270
271
272
273
}

impl IsSystem for Sibling {
	fn is_system(&self) -> bool {
		IsSystem::is_system(&self.0)
	}
}

274
/// This type can be converted into and possibly from an [`AccountId`] (which itself is generic).
275
276
277
278
pub trait AccountIdConversion<AccountId>: Sized {
	/// Convert into an account ID. This is infallible.
	fn into_account(&self) -> AccountId;

279
	/// Try to convert an account ID into this type. Might not succeed.
280
281
282
283
284
285
	fn try_from_account(a: &AccountId) -> Option<Self>;
}

// TODO: Remove all of this, move sp-runtime::AccountIdConversion to own crate and and use that.
// #360
struct TrailingZeroInput<'a>(&'a [u8]);
286
287
impl<'a> parity_scale_codec::Input for TrailingZeroInput<'a> {
	fn remaining_len(&mut self) -> Result<Option<usize>, parity_scale_codec::Error> {
288
289
290
		Ok(None)
	}

291
	fn read(&mut self, into: &mut [u8]) -> Result<(), parity_scale_codec::Error> {
292
293
294
295
296
297
298
299
300
301
302
		let len = into.len().min(self.0.len());
		into[..len].copy_from_slice(&self.0[..len]);
		for i in &mut into[len..] {
			*i = 0;
		}
		self.0 = &self.0[len..];
		Ok(())
	}
}

/// Format is b"para" ++ encode(parachain ID) ++ 00.... where 00... is indefinite trailing
303
/// zeroes to fill [`AccountId`].
304
305
impl<T: Encode + Decode + Default> AccountIdConversion<T> for Id {
	fn into_account(&self) -> T {
Shawn Tabrizi's avatar
Shawn Tabrizi committed
306
307
308
		(b"para", self)
			.using_encoded(|b| T::decode(&mut TrailingZeroInput(b)))
			.unwrap_or_default()
309
310
	}

311
	fn try_from_account(x: &T) -> Option<Self> {
312
		x.using_encoded(|d| {
Shawn Tabrizi's avatar
Shawn Tabrizi committed
313
314
315
			if &d[0..4] != b"para" {
				return None
			}
316
317
318
319
320
321
322
323
324
325
326
			let mut cursor = &d[4..];
			let result = Decode::decode(&mut cursor).ok()?;
			if cursor.iter().all(|x| *x == 0) {
				Some(result)
			} else {
				None
			}
		})
	}
}

Sergey Pepyakin's avatar
Sergey Pepyakin committed
327
328
329
330
331
332
/// A type that uniquely identifies an HRMP channel. An HRMP channel is established between two paras.
/// In text, we use the notation `(A, B)` to specify a channel between A and B. The channels are
/// unidirectional, meaning that `(A, B)` and `(B, A)` refer to different channels. The convention is
/// that we use the first item tuple for the sender and the second for the recipient. Only one channel
/// is allowed between two participants in one direction, i.e. there cannot be 2 different channels
/// identified by `(A, B)`.
333
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Encode, Decode, RuntimeDebug)]
Sergey Pepyakin's avatar
Sergey Pepyakin committed
334
335
336
337
338
339
340
341
#[cfg_attr(feature = "std", derive(Hash))]
pub struct HrmpChannelId {
	/// The para that acts as the sender in this channel.
	pub sender: Id,
	/// The para that acts as the recipient in this channel.
	pub recipient: Id,
}

342
/// A message from a parachain to its Relay Chain.
343
pub type UpwardMessage = Vec<u8>;
344

Shaun W's avatar
Shaun W committed
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
/// Something that should be called when a downward message is received.
pub trait DmpMessageHandler {
	/// Handle some incoming DMP messages (note these are individual XCM messages).
	///
	/// Also, process messages up to some `max_weight`.
	fn handle_dmp_messages(
		iter: impl Iterator<Item = (RelayChainBlockNumber, Vec<u8>)>,
		max_weight: Weight,
	) -> Weight;
}
impl DmpMessageHandler for () {
	fn handle_dmp_messages(
		iter: impl Iterator<Item = (RelayChainBlockNumber, Vec<u8>)>,
		_max_weight: Weight,
	) -> Weight {
		iter.for_each(drop);
		0
	}
}

/// The aggregate XCMP message format.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode)]
pub enum XcmpMessageFormat {
	/// Encoded `VersionedXcm` messages, all concatenated.
	ConcatenatedVersionedXcm,
	/// Encoded `Vec<u8>` messages, all concatenated.
	ConcatenatedEncodedBlob,
	/// One or more channel control signals; these should be interpreted immediately upon receipt
	/// from the relay-chain.
	Signals,
}

/// Something that should be called for each batch of messages received over XCMP.
pub trait XcmpMessageHandler {
	/// Handle some incoming XCMP messages (note these are the big one-per-block aggregate
	/// messages).
	///
	/// Also, process messages up to some `max_weight`.
	fn handle_xcmp_messages<'a, I: Iterator<Item = (Id, RelayChainBlockNumber, &'a [u8])>>(
		iter: I,
		max_weight: Weight,
	) -> Weight;
}
impl XcmpMessageHandler for () {
	fn handle_xcmp_messages<'a, I: Iterator<Item = (Id, RelayChainBlockNumber, &'a [u8])>>(
		iter: I,
		_max_weight: Weight,
	) -> Weight {
		for _ in iter {}
		0
	}
}

398
399
/// Validation parameters for evaluating the parachain validity function.
// TODO: balance downloads (https://github.com/paritytech/polkadot/issues/220)
400
#[derive(PartialEq, Eq, Decode, Clone)]
401
402
403
404
#[cfg_attr(feature = "std", derive(Debug, Encode))]
pub struct ValidationParams {
	/// Previous head-data.
	pub parent_head: HeadData,
405
406
	/// The collation body.
	pub block_data: BlockData,
407
	/// The current relay-chain block number.
408
	pub relay_parent_number: RelayChainBlockNumber,
409
	/// The relay-chain block's storage root.
410
	pub relay_parent_storage_root: Hash,
411
412
413
}

/// The result of parachain validation.
Sergey Pepyakin's avatar
Sergey Pepyakin committed
414
// TODO: balance uploads (https://github.com/paritytech/polkadot/issues/220)
415
#[derive(PartialEq, Eq, Clone, Encode)]
416
417
418
419
420
421
#[cfg_attr(feature = "std", derive(Debug, Decode))]
pub struct ValidationResult {
	/// New head data that should be included in the relay chain state.
	pub head_data: HeadData,
	/// An update to the validation code that should be scheduled in the relay chain.
	pub new_validation_code: Option<ValidationCode>,
422
423
	/// Upward messages send by the Parachain.
	pub upward_messages: Vec<UpwardMessage>,
Sergey Pepyakin's avatar
Sergey Pepyakin committed
424
425
	/// Outbound horizontal messages sent by the parachain.
	pub horizontal_messages: Vec<OutboundHrmpMessage<Id>>,
426
427
428
429
	/// Number of downward messages that were processed by the Parachain.
	///
	/// It is expected that the Parachain processes them from first to last.
	pub processed_downward_messages: u32,
Sergey Pepyakin's avatar
Sergey Pepyakin committed
430
431
	/// The mark which specifies the block number up to which all inbound HRMP messages are processed.
	pub hrmp_watermark: RelayChainBlockNumber,
432
}