mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-20 03:25:41 +00:00
Make parachain validation wasm executor functional (#1574)
* Make parachain validation wasm executor functional - Increase the size of the validation result in the shared memory. The validation result holds the new runtime when a runtime upgrade is scheduled. So, we need to give it enough memory to send the data between the validator and the wasm execution host. - Add the `CallInWasmExt`. This is required when doing a runtime upgrade to check that we upgrade to something meaningful. * Update parachain/src/wasm_executor/mod.rs * Update parachain/src/wasm_executor/mod.rs Co-authored-by: Nikolay Volf <nikvolf@gmail.com> Co-authored-by: Nikolay Volf <nikvolf@gmail.com>
This commit is contained in:
@@ -35,6 +35,7 @@ mod validation_host;
|
|||||||
// maximum memory in bytes
|
// maximum memory in bytes
|
||||||
const MAX_RUNTIME_MEM: usize = 1024 * 1024 * 1024; // 1 GiB
|
const MAX_RUNTIME_MEM: usize = 1024 * 1024 * 1024; // 1 GiB
|
||||||
const MAX_CODE_MEM: usize = 16 * 1024 * 1024; // 16 MiB
|
const MAX_CODE_MEM: usize = 16 * 1024 * 1024; // 16 MiB
|
||||||
|
const MAX_VALIDATION_RESULT_HEADER_MEM: usize = MAX_CODE_MEM + 1024; // 16.001 MiB
|
||||||
|
|
||||||
/// A stub validation-pool defined when compiling for Android or WASM.
|
/// A stub validation-pool defined when compiling for Android or WASM.
|
||||||
#[cfg(any(target_os = "android", target_os = "unknown"))]
|
#[cfg(any(target_os = "android", target_os = "unknown"))]
|
||||||
@@ -176,11 +177,6 @@ pub fn validate_candidate_internal(
|
|||||||
encoded_call_data: &[u8],
|
encoded_call_data: &[u8],
|
||||||
spawner: impl SpawnNamed + 'static,
|
spawner: impl SpawnNamed + 'static,
|
||||||
) -> Result<ValidationResult, ValidationError> {
|
) -> Result<ValidationResult, ValidationError> {
|
||||||
let mut extensions = Extensions::new();
|
|
||||||
extensions.register(sp_core::traits::TaskExecutorExt::new(spawner));
|
|
||||||
|
|
||||||
let mut ext = ValidationExternalities(extensions);
|
|
||||||
|
|
||||||
let executor = sc_executor::WasmExecutor::new(
|
let executor = sc_executor::WasmExecutor::new(
|
||||||
sc_executor::WasmExecutionMethod::Interpreted,
|
sc_executor::WasmExecutionMethod::Interpreted,
|
||||||
// TODO: Make sure we don't use more than 1GB: https://github.com/paritytech/polkadot/issues/699
|
// TODO: Make sure we don't use more than 1GB: https://github.com/paritytech/polkadot/issues/699
|
||||||
@@ -188,6 +184,13 @@ pub fn validate_candidate_internal(
|
|||||||
HostFunctions::host_functions(),
|
HostFunctions::host_functions(),
|
||||||
8
|
8
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let mut extensions = Extensions::new();
|
||||||
|
extensions.register(sp_core::traits::TaskExecutorExt::new(spawner));
|
||||||
|
extensions.register(sp_core::traits::CallInWasmExt::new(executor.clone()));
|
||||||
|
|
||||||
|
let mut ext = ValidationExternalities(extensions);
|
||||||
|
|
||||||
let res = executor.call_in_wasm(
|
let res = executor.call_in_wasm(
|
||||||
validation_code,
|
validation_code,
|
||||||
None,
|
None,
|
||||||
|
|||||||
@@ -19,8 +19,10 @@
|
|||||||
use std::{process, env, sync::Arc, sync::atomic};
|
use std::{process, env, sync::Arc, sync::atomic};
|
||||||
use codec::{Decode, Encode};
|
use codec::{Decode, Encode};
|
||||||
use crate::primitives::{ValidationParams, ValidationResult};
|
use crate::primitives::{ValidationParams, ValidationResult};
|
||||||
use super::{validate_candidate_internal, ValidationError, InvalidCandidate, InternalError,
|
use super::{
|
||||||
MAX_CODE_MEM, MAX_RUNTIME_MEM};
|
validate_candidate_internal, ValidationError, InvalidCandidate, InternalError,
|
||||||
|
MAX_CODE_MEM, MAX_RUNTIME_MEM, MAX_VALIDATION_RESULT_HEADER_MEM,
|
||||||
|
};
|
||||||
use shared_memory::{SharedMem, SharedMemConf, EventState, WriteLockable, EventWait, EventSet};
|
use shared_memory::{SharedMem, SharedMemConf, EventState, WriteLockable, EventWait, EventSet};
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use log::{debug, trace};
|
use log::{debug, trace};
|
||||||
@@ -113,7 +115,7 @@ pub fn run_worker(mem_id: &str) -> Result<(), String> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let exit = Arc::new(atomic::AtomicBool::new(false));
|
let exit = Arc::new(atomic::AtomicBool::new(false));
|
||||||
let task_executor = TaskExecutor::new()?;
|
let task_executor = TaskExecutor::new()?;
|
||||||
// spawn parent monitor thread
|
// spawn parent monitor thread
|
||||||
let watch_exit = exit.clone();
|
let watch_exit = exit.clone();
|
||||||
std::thread::spawn(move || {
|
std::thread::spawn(move || {
|
||||||
@@ -220,7 +222,7 @@ impl Drop for ValidationHost {
|
|||||||
|
|
||||||
impl ValidationHost {
|
impl ValidationHost {
|
||||||
fn create_memory() -> Result<SharedMem, InternalError> {
|
fn create_memory() -> Result<SharedMem, InternalError> {
|
||||||
let mem_size = MAX_RUNTIME_MEM + MAX_CODE_MEM + 1024;
|
let mem_size = MAX_RUNTIME_MEM + MAX_CODE_MEM + MAX_VALIDATION_RESULT_HEADER_MEM;
|
||||||
let mem_config = SharedMemConf::default()
|
let mem_config = SharedMemConf::default()
|
||||||
.set_size(mem_size)
|
.set_size(mem_size)
|
||||||
.add_lock(shared_memory::LockType::Mutex, 0, mem_size)?
|
.add_lock(shared_memory::LockType::Mutex, 0, mem_size)?
|
||||||
@@ -318,9 +320,16 @@ impl ValidationHost {
|
|||||||
debug!("{} Reading results", self.id);
|
debug!("{} Reading results", self.id);
|
||||||
let data: &[u8] = &**memory.wlock_as_slice(0)
|
let data: &[u8] = &**memory.wlock_as_slice(0)
|
||||||
.map_err(|e| ValidationError::Internal(e.into()))?;
|
.map_err(|e| ValidationError::Internal(e.into()))?;
|
||||||
let (header_buf, _) = data.split_at(1024);
|
let (header_buf, _) = data.split_at(MAX_VALIDATION_RESULT_HEADER_MEM);
|
||||||
let mut header_buf: &[u8] = header_buf;
|
let mut header_buf: &[u8] = header_buf;
|
||||||
let header = ValidationResultHeader::decode(&mut header_buf).unwrap();
|
let header = ValidationResultHeader::decode(&mut header_buf)
|
||||||
|
.map_err(|e|
|
||||||
|
InternalError::System(
|
||||||
|
Box::<dyn std::error::Error + Send + Sync>::from(
|
||||||
|
format!("Failed to decode `ValidationResultHeader`: {:?}", e)
|
||||||
|
) as Box<_>
|
||||||
|
)
|
||||||
|
)?;
|
||||||
match header {
|
match header {
|
||||||
ValidationResultHeader::Ok(result) => Ok(result),
|
ValidationResultHeader::Ok(result) => Ok(result),
|
||||||
ValidationResultHeader::Error(WorkerValidationError::InternalError(e)) => {
|
ValidationResultHeader::Error(WorkerValidationError::InternalError(e)) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user