testing.rs 2.38 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
25
26
27
28
29
30
31
32
33
34
35
36
// Copyright 2021 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/>.

//! Various things for testing other crates.
//!
//! N.B. This is not guarded with some feature flag. Overexposing items here may affect the final
//!      artifact even for production builds.

pub mod worker_common {
	pub use crate::worker_common::{spawn_with_program_path, SpawnErr};
}

/// A function that emulates the stitches together behaviors of the preparation and the execution
/// worker in a single synchronous function.
pub fn validate_candidate(
	code: &[u8],
	params: &[u8],
) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
	use crate::executor_intf::{prevalidate, prepare, execute, TaskExecutor};

	let blob = prevalidate(code)?;
	let artifact = prepare(blob)?;
	let executor = TaskExecutor::new()?;
37
38
39
40
	let result = unsafe {
		// SAFETY: This is trivially safe since the artifact is obtained by calling `prepare`.
		execute(&artifact, params, executor)?
	};
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

	Ok(result)
}

/// Use this macro to declare a `fn main() {}` that will check the arguments and dispatch them to
/// the appropriate worker, making the executable that can be used for spawning workers.
#[macro_export]
macro_rules! decl_puppet_worker_main {
	() => {
		fn main() {
			let args = std::env::args().collect::<Vec<_>>();
			if args.len() < 2 {
				panic!("wrong number of arguments");
			}

			let subcommand = &args[1];
			match subcommand.as_ref() {
				"sleep" => {
					std::thread::sleep(std::time::Duration::from_secs(5));
				}
				"prepare-worker" => {
					let socket_path = &args[2];
					$crate::prepare_worker_entrypoint(socket_path);
				}
				"execute-worker" => {
					let socket_path = &args[2];
					$crate::execute_worker_entrypoint(socket_path);
				}
				other => panic!("unknown subcommand: {}", other),
			}
		}
	};
}