mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-30 17:31:03 +00:00
refactor out validation hosts to pool struct (#972)
* refactor out validation hosts to pool struct * make web-wasm compatible * typo * remove now-unused static hosts
This commit is contained in:
committed by
GitHub
parent
a2a4f4c755
commit
15a83079ba
@@ -28,7 +28,7 @@ use sp_core::traits::CallInWasm;
|
||||
use sp_wasm_interface::HostFunctions as _;
|
||||
|
||||
#[cfg(not(target_os = "unknown"))]
|
||||
pub use validation_host::{run_worker, EXECUTION_TIMEOUT_SEC};
|
||||
pub use validation_host::{run_worker, ValidationPool, EXECUTION_TIMEOUT_SEC};
|
||||
|
||||
mod validation_host;
|
||||
|
||||
@@ -48,16 +48,31 @@ impl ParachainExt {
|
||||
}
|
||||
}
|
||||
|
||||
/// 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: () }
|
||||
}
|
||||
}
|
||||
|
||||
/// WASM code execution mode.
|
||||
///
|
||||
/// > Note: When compiling for WASM, the `Remote` variants are not available.
|
||||
pub enum ExecutionMode {
|
||||
pub enum ExecutionMode<'a> {
|
||||
/// Execute in-process. The execution can not be interrupted or aborted.
|
||||
Local,
|
||||
/// Remote execution in a spawned process.
|
||||
Remote,
|
||||
Remote(&'a ValidationPool),
|
||||
/// Remote execution in a spawned test runner.
|
||||
RemoteTest,
|
||||
RemoteTest(&'a ValidationPool),
|
||||
}
|
||||
|
||||
/// Error type for the wasm executor
|
||||
@@ -115,27 +130,27 @@ pub fn validate_candidate<E: Externalities + 'static>(
|
||||
validation_code: &[u8],
|
||||
params: ValidationParams,
|
||||
ext: E,
|
||||
options: ExecutionMode,
|
||||
options: ExecutionMode<'_>,
|
||||
) -> Result<ValidationResult, Error> {
|
||||
match options {
|
||||
ExecutionMode::Local => {
|
||||
validate_candidate_internal(validation_code, ¶ms.encode(), ext)
|
||||
},
|
||||
#[cfg(not(target_os = "unknown"))]
|
||||
ExecutionMode::Remote => {
|
||||
validation_host::validate_candidate(validation_code, params, ext, false)
|
||||
ExecutionMode::Remote(pool) => {
|
||||
pool.validate_candidate(validation_code, params, ext, false)
|
||||
},
|
||||
#[cfg(not(target_os = "unknown"))]
|
||||
ExecutionMode::RemoteTest => {
|
||||
validation_host::validate_candidate(validation_code, params, ext, true)
|
||||
ExecutionMode::RemoteTest(pool) => {
|
||||
pool.validate_candidate(validation_code, params, ext, true)
|
||||
},
|
||||
#[cfg(target_os = "unknown")]
|
||||
ExecutionMode::Remote =>
|
||||
ExecutionMode::Remote(pool) =>
|
||||
Err(Error::System(Box::<dyn std::error::Error + Send + Sync>::from(
|
||||
"Remote validator not available".to_string()
|
||||
) as Box<_>)),
|
||||
#[cfg(target_os = "unknown")]
|
||||
ExecutionMode::RemoteTest =>
|
||||
ExecutionMode::RemoteTest(pool) =>
|
||||
Err(Error::System(Box::<dyn std::error::Error + Send + Sync>::from(
|
||||
"Remote validator not available".to_string()
|
||||
) as Box<_>)),
|
||||
|
||||
@@ -33,8 +33,6 @@ const WORKER_ARGS_TEST: &[&'static str] = &["--nocapture", "validation_worker"];
|
||||
const WORKER_ARG: &'static str = "validation-worker";
|
||||
const WORKER_ARGS: &[&'static str] = &[WORKER_ARG];
|
||||
|
||||
const NUM_HOSTS: usize = 8;
|
||||
|
||||
/// Execution timeout in seconds;
|
||||
#[cfg(debug_assertions)]
|
||||
pub const EXECUTION_TIMEOUT_SEC: u64 = 30;
|
||||
@@ -69,8 +67,42 @@ enum Event {
|
||||
WorkerReady = 2,
|
||||
}
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
static ref HOSTS: [Mutex<ValidationHost>; NUM_HOSTS] = Default::default();
|
||||
/// A pool of hosts.
|
||||
#[derive(Clone)]
|
||||
pub struct ValidationPool {
|
||||
hosts: Arc<Vec<Mutex<ValidationHost>>>,
|
||||
}
|
||||
|
||||
const DEFAULT_NUM_HOSTS: usize = 8;
|
||||
|
||||
impl ValidationPool {
|
||||
/// Creates a validation pool with the default configuration.
|
||||
pub fn new() -> ValidationPool {
|
||||
ValidationPool {
|
||||
hosts: Arc::new((0..DEFAULT_NUM_HOSTS).map(|_| Default::default()).collect()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Validate a candidate under the given validation code using the next
|
||||
/// free validation host.
|
||||
///
|
||||
/// This will fail if the validation code is not a proper parachain validation module.
|
||||
pub fn validate_candidate<E: Externalities>(
|
||||
&self,
|
||||
validation_code: &[u8],
|
||||
params: ValidationParams,
|
||||
externalities: E,
|
||||
test_mode: bool,
|
||||
) -> Result<ValidationResult, Error> {
|
||||
for host in self.hosts.iter() {
|
||||
if let Some(mut host) = host.try_lock() {
|
||||
return host.validate_candidate(validation_code, params, externalities, test_mode);
|
||||
}
|
||||
}
|
||||
|
||||
// all workers are busy, just wait for the first one
|
||||
self.hosts[0].lock().validate_candidate(validation_code, params, externalities, test_mode)
|
||||
}
|
||||
}
|
||||
|
||||
/// Validation worker process entry point. Runs a loop waiting for candidates to validate
|
||||
@@ -184,25 +216,6 @@ struct ValidationHost {
|
||||
id: u32,
|
||||
}
|
||||
|
||||
/// 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>(
|
||||
validation_code: &[u8],
|
||||
params: ValidationParams,
|
||||
externalities: E,
|
||||
test_mode: bool,
|
||||
) -> Result<ValidationResult, Error> {
|
||||
for host in HOSTS.iter() {
|
||||
if let Some(mut host) = host.try_lock() {
|
||||
return host.validate_candidate(validation_code, params, externalities, test_mode);
|
||||
}
|
||||
}
|
||||
|
||||
// all workers are busy, just wait for the first one
|
||||
HOSTS[0].lock().validate_candidate(validation_code, params, externalities, test_mode)
|
||||
}
|
||||
|
||||
impl Drop for ValidationHost {
|
||||
fn drop(&mut self) {
|
||||
if let Some(ref mut worker) = &mut self.worker {
|
||||
|
||||
Reference in New Issue
Block a user