mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-15 20:45:44 +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
@@ -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