diff --git a/pallets/xvm/src/lib.rs b/pallets/xvm/src/lib.rs index 71111d0d93..0cc260b155 100644 --- a/pallets/xvm/src/lib.rs +++ b/pallets/xvm/src/lib.rs @@ -79,17 +79,20 @@ pub struct CallErrorWithWeight { /// XVM call result. pub type XvmCallResult = Result; -#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, TypeInfo)] +#[derive(PartialEq, Eq, Copy, Clone, Encode, Decode, RuntimeDebug, TypeInfo)] pub enum Vm { Evm, Wasm, } +// TODO: Note caller shouldn't be able to specify `source_vm`. /// XVM context. #[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, TypeInfo)] pub struct XvmContext { /// The source VM of the call. pub source_vm: Vm, + /// The target VM of the call. + pub target_vm: Vm, /// Max weight limit. pub weight_limit: Weight, /// Optional encoded execution environment. @@ -97,28 +100,14 @@ pub struct XvmContext { } pub trait XvmCall { - /// Call a contract in EVM. + /// Call a contract in XVM. /// /// Parameters: /// - `context`: XVM context. /// - `source`: Caller Id. /// - `target`: Target contract address. /// - `input`: call input data. - fn evm_call( - context: XvmContext, - source: AccountId, - target: Vec, - input: Vec, - ) -> XvmCallResult; - - /// Call a contract in EVM. - /// - /// Parameters: - /// - `context`: XVM context. - /// - `source`: Caller Id. - /// - `target`: Target contract address. - /// - `input`: call input data. - fn wasm_call( + fn xvm_call( context: XvmContext, source: AccountId, target: Vec, @@ -146,30 +135,41 @@ pub mod pallet { } impl XvmCall for Pallet { - fn evm_call( + fn xvm_call( context: XvmContext, source: T::AccountId, target: Vec, input: Vec, ) -> XvmCallResult { - Pallet::::do_evm_call(context, source, target, input, false) + Pallet::::do_xvm_call(context, source, target, input, false) } +} - fn wasm_call( +// TODO: benchmark XVM calls overhead +pub const PLACEHOLDER_WEIGHT: Weight = Weight::from_parts(1_000_000, 1024); + +impl Pallet { + fn do_xvm_call( context: XvmContext, source: T::AccountId, target: Vec, input: Vec, + skip_apply: bool, ) -> XvmCallResult { - Pallet::::do_wasm_call(context, source, target, input, false) - } -} + if context.source_vm == context.target_vm { + return Err(CallErrorWithWeight { + error: CallError::SameVmCallNotAllowed, + used_weight: PLACEHOLDER_WEIGHT, + }); + } -// TODO: benchmark XVM calls overhead -pub const PLACEHOLDER_WEIGHT: Weight = Weight::from_parts(1_000_000, 1024); + match context.source_vm { + Vm::Evm => Pallet::::evm_call(context, source, target, input, skip_apply), + Vm::Wasm => Pallet::::wasm_call(context, source, target, input, skip_apply), + } + } -impl Pallet { - fn do_evm_call( + fn evm_call( context: XvmContext, source: T::AccountId, target: Vec, @@ -182,13 +182,6 @@ impl Pallet { context, source, target, input, ); - if context.source_vm == Vm::Evm { - return Err(CallErrorWithWeight { - error: CallError::SameVmCallNotAllowed, - used_weight: PLACEHOLDER_WEIGHT, - }); - } - let value = U256::zero(); let gas_limit = T::GasWeightMapping::weight_to_gas(context.weight_limit); @@ -240,7 +233,7 @@ impl Pallet { }) } - fn do_wasm_call( + fn wasm_call( context: XvmContext, source: T::AccountId, target: Vec, @@ -253,13 +246,6 @@ impl Pallet { context, source, target, input, ); - if context.source_vm == Vm::Wasm { - return Err(CallErrorWithWeight { - error: CallError::SameVmCallNotAllowed, - used_weight: PLACEHOLDER_WEIGHT, - }); - } - let dest = { let error = CallErrorWithWeight { error: CallError::InvalidTarget, @@ -303,11 +289,9 @@ impl Pallet { }), } } -} -#[cfg(feature = "runtime-benchmarks")] -impl Pallet { - pub fn evm_call_without_apply( + #[cfg(feature = "runtime-benchmarks")] + pub fn xvm_call_without_apply( context: XvmContext, source: T::AccountId, target: Vec, @@ -315,13 +299,4 @@ impl Pallet { ) -> XvmCallResult { Self::do_evm_call(context, source, target, input, true) } - - pub fn wasm_call_without_apply( - context: XvmContext, - source: T::AccountId, - target: Vec, - input: Vec, - ) -> XvmCallResult { - Self::do_wasm_call(context, source, target, input, true) - } }