mod.rs 9.11 KB
Newer Older
Shawn Tabrizi's avatar
Shawn Tabrizi committed
1
// Copyright 2017-2020 Parity Technologies (UK) Ltd.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 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/>.

//! WASM re-execution of a parachain candidate.
//! In the context of relay-chain candidate evaluation, there are some additional
//! steps to ensure that the provided input parameters are correct.
//! Assuming the parameters are correct, this module provides a wrapper around
//! a WASM VM for re-execution of a parachain candidate.

23
use std::any::{TypeId, Any};
24
use crate::primitives::{ValidationParams, ValidationResult, UpwardMessage};
25
26
use codec::{Decode, Encode};
use sp_core::storage::{ChildStorageKey, ChildInfo};
27
28
use sp_core::traits::CallInWasm;
use sp_wasm_interface::HostFunctions as _;
29
30

#[cfg(not(target_os = "unknown"))]
31
pub use validation_host::{run_worker, ValidationPool, EXECUTION_TIMEOUT_SEC};
32
33

mod validation_host;
34
35
36
37

// maximum memory in bytes
const MAX_RUNTIME_MEM: usize = 1024 * 1024 * 1024; // 1 GiB
const MAX_CODE_MEM: usize = 16 * 1024 * 1024; // 16 MiB
38

39
40
41
42
43
sp_externalities::decl_extension! {
	/// The extension that is registered at the `Externalities` when validating a parachain state
	/// transition.
	pub(crate) struct ParachainExt(Box<dyn Externalities>);
}
44

45
46
47
48
impl ParachainExt {
	pub fn new<T: Externalities + 'static>(ext: T) -> Self {
		Self(Box::new(ext))
	}
49
}
50

51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/// A stub validation-pool defined when compiling for WASM.
#[cfg(target_os = "unknown")]
#[derive(Clone)]
pub struct ValidationPool {
	_inner: (), // private field means not publicly-instantiable
}

#[cfg(target_os = "unknown")]
impl ValidationPool {
	/// Create a new `ValidationPool`.
	pub fn new() -> Self {
		ValidationPool { _inner: () }
	}
}

66
/// WASM code execution mode.
67
68
///
/// > Note: When compiling for WASM, the `Remote` variants are not available.
69
pub enum ExecutionMode<'a> {
70
71
72
	/// Execute in-process. The execution can not be interrupted or aborted.
	Local,
	/// Remote execution in a spawned process.
73
	Remote(&'a ValidationPool),
74
	/// Remote execution in a spawned test runner.
75
	RemoteTest(&'a ValidationPool),
76
77
}

78
79
80
/// Error type for the wasm executor
#[derive(Debug, derive_more::Display, derive_more::From)]
pub enum Error {
81
82
83
	/// Wasm executor error.
	#[display(fmt = "WASM executor error: {:?}", _0)]
	WasmExecutor(sc_executor::error::Error),
84
85
	/// Call data is too large.
	#[display(fmt = "Validation parameters are {} bytes, max allowed is {}", _0, MAX_RUNTIME_MEM)]
86
	#[from(ignore)]
87
	ParamsTooLarge(usize),
88
89
90
	/// Code size it too large.
	#[display(fmt = "WASM code is {} bytes, max allowed is {}", _0, MAX_CODE_MEM)]
	CodeTooLarge(usize),
91
92
93
	/// Bad return data or type.
	#[display(fmt = "Validation function returned invalid data.")]
	BadReturn,
94
95
96
97
98
	#[display(fmt = "Validation function timeout.")]
	Timeout,
	#[display(fmt = "IO error: {}", _0)]
	Io(std::io::Error),
	#[display(fmt = "System error: {}", _0)]
99
	System(Box<dyn std::error::Error + Send>),
100
101
	#[display(fmt = "WASM worker error: {}", _0)]
	External(String),
102
103
104
	#[display(fmt = "Shared memory error: {}", _0)]
	#[cfg(not(target_os = "unknown"))]
	SharedMem(shared_memory::SharedMemError),
105
106
107
108
109
}

impl std::error::Error for Error {
	fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
		match self {
110
			Error::WasmExecutor(ref err) => Some(err),
111
112
			Error::Io(ref err) => Some(err),
			Error::System(ref err) => Some(&**err),
113
114
			#[cfg(not(target_os = "unknown"))]
			Error::SharedMem(ref err) => Some(err),
115
			_ => None,
116
117
118
119
		}
	}
}

120
/// Externalities for parachain validation.
121
pub trait Externalities: Send {
122
	/// Called when a message is to be posted to the parachain's relay chain.
123
	fn post_upward_message(&mut self, message: UpwardMessage) -> Result<(), String>;
124
125
}

126
127
128
129
130
131
132
/// Validate a candidate under the given validation code.
///
/// This will fail if the validation code is not a proper parachain validation module.
pub fn validate_candidate<E: Externalities + 'static>(
	validation_code: &[u8],
	params: ValidationParams,
	ext: E,
133
	options: ExecutionMode<'_>,
134
135
136
137
138
139
) -> Result<ValidationResult, Error> {
	match options {
		ExecutionMode::Local => {
			validate_candidate_internal(validation_code, &params.encode(), ext)
		},
		#[cfg(not(target_os = "unknown"))]
140
141
		ExecutionMode::Remote(pool) => {
			pool.validate_candidate(validation_code, params, ext, false)
142
143
		},
		#[cfg(not(target_os = "unknown"))]
144
145
		ExecutionMode::RemoteTest(pool) => {
			pool.validate_candidate(validation_code, params, ext, true)
146
147
		},
		#[cfg(target_os = "unknown")]
148
		ExecutionMode::Remote(pool) =>
149
150
151
			Err(Error::System(Box::<dyn std::error::Error + Send + Sync>::from(
				"Remote validator not available".to_string()
			) as Box<_>)),
152
		#[cfg(target_os = "unknown")]
153
		ExecutionMode::RemoteTest(pool) =>
154
155
156
			Err(Error::System(Box::<dyn std::error::Error + Send + Sync>::from(
				"Remote validator not available".to_string()
			) as Box<_>)),
157
158
159
	}
}

160
/// The host functions provided by the wasm executor to the parachain wasm blob.
161
162
163
164
type HostFunctions = (
	sp_io::SubstrateHostFunctions,
	crate::wasm_api::parachain::HostFunctions,
);
165
166
167
168
169
170
171
172
173
174

/// Validate a candidate under the given validation code.
///
/// This will fail if the validation code is not a proper parachain validation module.
pub fn validate_candidate_internal<E: Externalities + 'static>(
	validation_code: &[u8],
	encoded_call_data: &[u8],
	externalities: E,
) -> Result<ValidationResult, Error> {
	let mut ext = ValidationExternalities(ParachainExt::new(externalities));
175

176
	let executor = sc_executor::WasmExecutor::new(
177
178
		sc_executor::WasmExecutionMethod::Interpreted,
		// TODO: Make sure we don't use more than 1GB: https://github.com/paritytech/polkadot/issues/699
179
180
		Some(1024),
		HostFunctions::host_functions(),
Gavin Wood's avatar
Gavin Wood committed
181
		false,
Gavin Wood's avatar
Gavin Wood committed
182
		8
183
184
185
	);
	let res = executor.call_in_wasm(
		validation_code,
Gavin Wood's avatar
Gavin Wood committed
186
		None,
187
188
189
		"validate_block",
		encoded_call_data,
		&mut ext,
190
191
192
	)?;

	ValidationResult::decode(&mut &res[..]).map_err(|_| Error::BadReturn.into())
193
194
}

195
196
197
/// The validation externalities that will panic on any storage related access. They just provide
/// access to the parachain extension.
struct ValidationExternalities(ParachainExt);
198

199
200
201
impl sp_externalities::Externalities for ValidationExternalities {
	fn storage(&self, _: &[u8]) -> Option<Vec<u8>> {
		panic!("storage: unsupported feature for parachain validation")
202
203
	}

204
205
	fn storage_hash(&self, _: &[u8]) -> Option<Vec<u8>> {
		panic!("storage_hash: unsupported feature for parachain validation")
206
207
	}

208
209
210
	fn child_storage_hash(&self, _: ChildStorageKey, _: ChildInfo, _: &[u8]) -> Option<Vec<u8>> {
		panic!("child_storage_hash: unsupported feature for parachain validation")
	}
211

212
213
214
	fn child_storage(&self, _: ChildStorageKey, _: ChildInfo, _: &[u8]) -> Option<Vec<u8>> {
		panic!("child_storage: unsupported feature for parachain validation")
	}
215

216
217
218
	fn kill_child_storage(&mut self, _: ChildStorageKey, _: ChildInfo) {
		panic!("kill_child_storage: unsupported feature for parachain validation")
	}
219

220
221
222
	fn clear_prefix(&mut self, _: &[u8]) {
		panic!("clear_prefix: unsupported feature for parachain validation")
	}
223

224
225
	fn clear_child_prefix(&mut self, _: ChildStorageKey, _: ChildInfo, _: &[u8]) {
		panic!("clear_child_prefix: unsupported feature for parachain validation")
226
227
	}

228
229
	fn place_storage(&mut self, _: Vec<u8>, _: Option<Vec<u8>>) {
		panic!("place_storage: unsupported feature for parachain validation")
230
231
	}

232
233
	fn place_child_storage(&mut self, _: ChildStorageKey, _: ChildInfo, _: Vec<u8>, _: Option<Vec<u8>>) {
		panic!("place_child_storage: unsupported feature for parachain validation")
234
	}
235

236
237
238
	fn chain_id(&self) -> u64 {
		panic!("chain_id: unsupported feature for parachain validation")
	}
239

240
241
242
	fn storage_root(&mut self) -> Vec<u8> {
		panic!("storage_root: unsupported feature for parachain validation")
	}
243

244
245
246
	fn child_storage_root(&mut self, _: ChildStorageKey) -> Vec<u8> {
		panic!("child_storage_root: unsupported feature for parachain validation")
	}
247

248
249
250
	fn storage_changes_root(&mut self, _: &[u8]) -> Result<Option<Vec<u8>>, ()> {
		panic!("storage_changes_root: unsupported feature for parachain validation")
	}
251

252
253
254
255
256
257
258
	fn next_child_storage_key(&self, _: ChildStorageKey, _: ChildInfo, _: &[u8]) -> Option<Vec<u8>> {
		panic!("next_child_storage_key: unsupported feature for parachain validation")
	}

	fn next_storage_key(&self, _: &[u8]) -> Option<Vec<u8>> {
		panic!("next_storage_key: unsupported feature for parachain validation")
	}
Gavin Wood's avatar
Gavin Wood committed
259
260
261
262
263
264
265
266

	fn wipe(&mut self) {
		panic!("wipe: unsupported feature for parachain validation")
	}

	fn commit(&mut self) {
		panic!("commit: unsupported feature for parachain validation")
	}
267
268
269
270
271
272
273
274
}

impl sp_externalities::ExtensionStore for ValidationExternalities {
	fn extension_by_type_id(&mut self, type_id: TypeId) -> Option<&mut dyn Any> {
		if type_id == TypeId::of::<ParachainExt>() {
			Some(&mut self.0)
		} else {
			None
275
276
277
		}
	}
}