Skip to content

Commit

Permalink
Keep XVM call interface unified.
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunxw committed Jul 20, 2023
1 parent 57ad0c9 commit 46263d2
Showing 1 changed file with 30 additions and 55 deletions.
85 changes: 30 additions & 55 deletions pallets/xvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,46 +79,35 @@ pub struct CallErrorWithWeight {
/// XVM call result.
pub type XvmCallResult = Result<CallInfo, CallErrorWithWeight>;

#[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.
pub env: Option<Vec<u8>>,
}

pub trait XvmCall<AccountId> {
/// 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<u8>,
input: Vec<u8>,
) -> 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<u8>,
Expand Down Expand Up @@ -146,30 +135,41 @@ pub mod pallet {
}

impl<T: Config> XvmCall<T::AccountId> for Pallet<T> {
fn evm_call(
fn xvm_call(
context: XvmContext,
source: T::AccountId,
target: Vec<u8>,
input: Vec<u8>,
) -> XvmCallResult {
Pallet::<T>::do_evm_call(context, source, target, input, false)
Pallet::<T>::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<T: Config> Pallet<T> {
fn do_xvm_call(
context: XvmContext,
source: T::AccountId,
target: Vec<u8>,
input: Vec<u8>,
skip_apply: bool,
) -> XvmCallResult {
Pallet::<T>::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::<T>::evm_call(context, source, target, input, skip_apply),
Vm::Wasm => Pallet::<T>::wasm_call(context, source, target, input, skip_apply),
}
}

impl<T: Config> Pallet<T> {
fn do_evm_call(
fn evm_call(
context: XvmContext,
source: T::AccountId,
target: Vec<u8>,
Expand All @@ -182,13 +182,6 @@ impl<T: Config> Pallet<T> {
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);

Expand Down Expand Up @@ -240,7 +233,7 @@ impl<T: Config> Pallet<T> {
})
}

fn do_wasm_call(
fn wasm_call(
context: XvmContext,
source: T::AccountId,
target: Vec<u8>,
Expand All @@ -253,13 +246,6 @@ impl<T: Config> Pallet<T> {
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,
Expand Down Expand Up @@ -303,25 +289,14 @@ impl<T: Config> Pallet<T> {
}),
}
}
}

#[cfg(feature = "runtime-benchmarks")]
impl<T: Config> Pallet<T> {
pub fn evm_call_without_apply(
#[cfg(feature = "runtime-benchmarks")]
pub fn xvm_call_without_apply(
context: XvmContext,
source: T::AccountId,
target: Vec<u8>,
input: Vec<u8>,
) -> XvmCallResult {
Self::do_evm_call(context, source, target, input, true)

Check failure on line 300 in pallets/xvm/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

no function or associated item named `do_evm_call` found for struct `Pallet` in the current scope

error[E0599]: no function or associated item named `do_evm_call` found for struct `Pallet` in the current scope --> pallets/xvm/src/lib.rs:300:15 | 123 | pub struct Pallet<T>(PhantomData<T>); | -------------------- function or associated item `do_evm_call` not found for this struct ... 300 | Self::do_evm_call(context, source, target, input, true) | ^^^^^^^^^^^ | | | function or associated item not found in `Pallet<T>` | help: there is an associated function with a similar name: `do_xvm_call`
}

pub fn wasm_call_without_apply(
context: XvmContext,
source: T::AccountId,
target: Vec<u8>,
input: Vec<u8>,
) -> XvmCallResult {
Self::do_wasm_call(context, source, target, input, true)
}
}

0 comments on commit 46263d2

Please sign in to comment.