pub fn prepare_contract<T: Trait, C: ImportSatisfyCheck>(
	original_code: &[u8],
	schedule: &Schedule<T::Gas>,
) -> Result<PrefabWasmModule, &'static str> {
	let mut contract_module = ContractModule::new(original_code, schedule)?;
	contract_module.scan_exports()?;
	contract_module.ensure_no_internal_memory()?;

	struct MemoryDefinition {
		initial: u32,
		maximum: u32,
	}

	let memory_def = if let Some(memory_type) = contract_module.scan_imports::<C>()? {
		// Inspect the module to extract the initial and maximum page count.
		let limits = memory_type.limits();
		match (limits.initial(), limits.maximum()) {
			(initial, Some(maximum)) if initial > maximum => {
				return Err(
					"Requested initial number of pages should not exceed the requested maximum",
				);
			}
			(_, Some(maximum)) if maximum > schedule.max_memory_pages => {
				return Err("Maximum number of pages should not exceed the configured maximum.");
			}
			(initial, Some(maximum)) => MemoryDefinition { initial, maximum },
			(_, None) => {
				// Maximum number of pages should be always declared.
				// This isn't a hard requirement and can be treated as a maxiumum set
				// to configured maximum.
				return Err("Maximum number of pages should be always declared.");
			}
		}
	} else {
		// If none memory imported then just crate an empty placeholder.
		// Any access to it will lead to out of bounds trap.
		MemoryDefinition {
			initial: 0,
			maximum: 0,
		}
	};

	contract_module.inject_gas_metering()?;
	contract_module.inject_stack_height_metering()?;

	Ok(PrefabWasmModule {
		schedule_version: schedule.version,
		initial: memory_def.initial,
		maximum: memory_def.maximum,
		_reserved: None,
		code: contract_module.into_wasm_code()?,
	})
}
/// Loads the given module given in `original_code`, performs some checks on it and
/// does some preprocessing.
///
/// The checks are:
///
/// - module doesn't define an internal memory instance,
/// - imported memory (if any) doesn't reserve more memory than permitted by the `schedule`,
/// - all imported functions from the external environment matches defined by `env` module,
///
/// The preprocessing includes injecting code for gas metering and metering the height of stack.
Loads the given module given in `original_code`, performs some checks on it and
does some preprocessing.

The checks are.

module doesn't define an internal memory instance,
imported memory (if any) doesn't reserve more memory than permitted by the `schedule`,
all imported functions from the external environment matches defined by `env` module.

The preprocessing includes injecting code for gas metering and metering the height of stack.
Loads the given module given in ` original_code ` performs some checks on it and does some preprocessing . The checks are . module doesn ' t define an internal memory instance imported memory ( if any ) doesn ' t reserve more memory than permitted by the ` schedule ` all imported functions from the external environment matches defined by ` env ` module . The preprocessing includes injecting code for gas metering and metering the height of stack .
pub fn prepare_contract<T: Trait, C: ImportSatisfyCheck>(
	original_code: &[u8],
	schedule: &Schedule<T::Gas>,
) -> Result<PrefabWasmModule, &'static str> {
	let mut contract_module = ContractModule::new(original_code, schedule)?;
	contract_module.scan_exports()?;
	contract_module.ensure_no_internal_memory()?;
	struct MemoryDefinition {
		initial: u32,
		maximum: u32,
	}
	let memory_def = if let Some(memory_type) = contract_module.scan_imports::<C>()? {
		// Inspect the module to extract the initial and maximum page count.
		let limits = memory_type.limits();
		match (limits.initial(), limits.maximum()) {
			(initial, Some(maximum)) if initial > maximum => {
				return Err(
					"Requested initial number of pages should not exceed the requested maximum",
				);
			}
			(_, Some(maximum)) if maximum > schedule.max_memory_pages => {
				return Err("Maximum number of pages should not exceed the configured maximum.");
			}
			(initial, Some(maximum)) => MemoryDefinition { initial, maximum },
			(_, None) => {
				// Maximum number of pages should be always declared.
				// This isn't a hard requirement and can be treated as a maxiumum set
				// to configured maximum.
				return Err("Maximum number of pages should be always declared.");
			}
		}
	} else {
		// If none memory imported then just crate an empty placeholder.
		// Any access to it will lead to out of bounds trap.
		MemoryDefinition {
			initial: 0,
			maximum: 0,
		}
	};
	contract_module.inject_gas_metering()?;
	contract_module.inject_stack_height_metering()?;
	Ok(PrefabWasmModule {
		schedule_version: schedule.version,
		initial: memory_def.initial,
		maximum: memory_def.maximum,
		_reserved: None,
		code: contract_module.into_wasm_code()?,
	})
}
pub fn prepare_contract < T : Trait , C : ImportSatisfyCheck > ( original_code : & [ u8 ] , schedule : & Schedule < T :: Gas > , ) -> Result < PrefabWasmModule , & ' static str > { let mut contract_module = ContractModule :: new ( original_code , schedule ) ? ; contract_module . scan_exports ( ) ? ; contract_module . ensure_no_internal_memory ( ) ? ; struct MemoryDefinition { initial : u32 , maximum : u32 , } let memory_def = if let Some ( memory_type ) = contract_module . scan_imports :: < C > ( ) ? { // Inspect the module to extract the initial and maximum page count. let limits = memory_type . limits ( ) ; match ( limits . initial ( ) , limits . maximum ( ) ) { ( initial , Some ( maximum ) ) if initial > maximum => { return Err ( " " , ) ; } ( _ , Some ( maximum ) ) if maximum > schedule . max_memory_pages => { return Err ( " " ) ; } ( initial , Some ( maximum ) ) => MemoryDefinition { initial , maximum } , ( _ , None ) => { // Maximum number of pages should be always declared. // This isn't a hard requirement and can be treated as a maxiumum set // to configured maximum. return Err ( " " ) ; } } } else { // If none memory imported then just crate an empty placeholder. // Any access to it will lead to out of bounds trap. MemoryDefinition { initial : 0 , maximum : 0 , } } ; contract_module . inject_gas_metering ( ) ? ; contract_module . inject_stack_height_metering ( ) ? ; Ok ( PrefabWasmModule { schedule_version : schedule . version , initial : memory_def . initial , maximum : memory_def . maximum , _reserved : None , code : contract_module . into_wasm_code ( ) ? , } ) }
Loads the given module given in `original_code`, performs some checks on it and
does some preprocessing.
Loads the given module given in ` original_code ` performs some checks on it and does some preprocessing .
original_code
&[u8]
schedule
&Schedule<T::Gas>
original_code
&[u8]
schedule
&Schedule<T::Gas>