Skip to content

Commit

Permalink
Add value param to 'XvmCall'.
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunxw committed Jul 29, 2023
1 parent 18ff2b9 commit dd1987d
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 22 deletions.
2 changes: 2 additions & 0 deletions chain-extensions/types/xvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,6 @@ pub struct XvmCallArgs {
pub to: Vec<u8>,
/// Encoded call params
pub input: Vec<u8>,
/// Value to transfer
pub value: u128,
}
10 changes: 7 additions & 3 deletions chain-extensions/xvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use xvm_chain_extension_types::{XvmCallArgs, XvmExecutionResult};

enum XvmFuncId {
Call,
// TODO: expand with other calls too
}

impl TryFrom<u16> for XvmFuncId {
Expand Down Expand Up @@ -89,7 +88,12 @@ where
}
};

let XvmCallArgs { vm_id, to, input } = env.read_as_unbounded(env.in_len())?;
let XvmCallArgs {
vm_id,
to,
input,
value,
} = env.read_as_unbounded(env.in_len())?;

let _origin_address = env.ext().address().clone();
let _value = env.ext().value_transferred();
Expand All @@ -108,7 +112,7 @@ where
}
}
};
let call_result = XC::call(xvm_context, vm_id, caller, to, input);
let call_result = XC::call(xvm_context, vm_id, caller, to, input, value);

let actual_weight = match call_result {
Ok(ref info) => info.used_weight,
Expand Down
2 changes: 1 addition & 1 deletion pallets/xvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pallet-timestamp = { workspace = true, features = ["std"] }
sp-io = { workspace = true }

[features]
default = ["std"]
default = ["std", "runtime-benchmarks"]
std = [
"log/std",
"parity-scale-codec/std",
Expand Down
14 changes: 11 additions & 3 deletions pallets/xvm/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ use parity_scale_codec::Encode;
use sp_core::H160;
use sp_runtime::MultiAddress;

#[benchmarks]
use astar_primitives::Balance;

#[benchmarks(
where <T as pallet_contracts::Config>::Currency: Currency<T::AccountId, Balance = Balance>,
)]
mod benchmarks {
use super::*;

Expand All @@ -38,10 +42,12 @@ mod benchmarks {
let source = whitelisted_caller();
let target = H160::repeat_byte(1).encode();
let input = vec![1, 2, 3];
let value = 1_000_000u128;

#[block]
{
Pallet::<T>::call_without_execution(context, vm_id, source, target, input).unwrap();
Pallet::<T>::call_without_execution(context, vm_id, source, target, input, value)
.unwrap();
}
}

Expand All @@ -55,10 +61,12 @@ mod benchmarks {
let source = whitelisted_caller();
let target = MultiAddress::<T::AccountId, ()>::Id(whitelisted_caller()).encode();
let input = vec![1, 2, 3];
let value = 1_000_000u128;

#[block]
{
Pallet::<T>::call_without_execution(context, vm_id, source, target, input).unwrap();
Pallet::<T>::call_without_execution(context, vm_id, source, target, input, value)
.unwrap();
}
}

Expand Down
42 changes: 31 additions & 11 deletions pallets/xvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@

#![cfg_attr(not(feature = "std"), no_std)]

use frame_support::{ensure, traits::ConstU32, BoundedVec};
use frame_support::{
ensure,
traits::{ConstU32, Currency},
BoundedVec,
};
use pallet_contracts::{CollectEvents, DebugInfo, Determinism};
use pallet_evm::GasWeightMapping;
use parity_scale_codec::Decode;
Expand All @@ -50,6 +54,7 @@ use astar_primitives::{
AccountMapping, CheckedEthereumTransact, CheckedEthereumTx, MAX_ETHEREUM_TX_INPUT_SIZE,
},
xvm::{CallError, CallErrorWithWeight, CallInfo, CallResult, Context, VmId, XvmCall},
Balance,
};

#[cfg(feature = "runtime-benchmarks")]
Expand All @@ -58,7 +63,6 @@ mod benchmarking;
pub mod weights;
pub use weights::WeightInfo;

#[cfg(test)]
mod mock;

pub use pallet::*;
Expand Down Expand Up @@ -88,25 +92,35 @@ pub mod pallet {
}
}

impl<T: Config> XvmCall<T::AccountId> for Pallet<T> {
impl<T> XvmCall<T::AccountId> for Pallet<T>
where
T: Config,
T::Currency: Currency<T::AccountId, Balance = Balance>,
{
fn call(
context: Context,
vm_id: VmId,
source: T::AccountId,
target: Vec<u8>,
input: Vec<u8>,
value: Balance,
) -> CallResult {
Pallet::<T>::do_call(context, vm_id, source, target, input, false)
Pallet::<T>::do_call(context, vm_id, source, target, input, value, false)
}
}

impl<T: Config> Pallet<T> {
impl<T> Pallet<T>
where
T: Config,
T::Currency: Currency<T::AccountId, Balance = Balance>,
{
fn do_call(
context: Context,
vm_id: VmId,
source: T::AccountId,
target: Vec<u8>,
input: Vec<u8>,
value: Balance,
skip_execution: bool,
) -> CallResult {
ensure!(
Expand All @@ -121,8 +135,12 @@ impl<T: Config> Pallet<T> {
);

match vm_id {
VmId::Evm => Pallet::<T>::evm_call(context, source, target, input, skip_execution),
VmId::Wasm => Pallet::<T>::wasm_call(context, source, target, input, skip_execution),
VmId::Evm => {
Pallet::<T>::evm_call(context, source, target, input, value, skip_execution)
}
VmId::Wasm => {
Pallet::<T>::wasm_call(context, source, target, input, value, skip_execution)
}
}
}

Expand All @@ -131,6 +149,7 @@ impl<T: Config> Pallet<T> {
source: T::AccountId,
target: Vec<u8>,
input: Vec<u8>,
value: Balance,
skip_execution: bool,
) -> CallResult {
log::trace!(
Expand All @@ -150,7 +169,7 @@ impl<T: Config> Pallet<T> {
used_weight: WeightInfoOf::<T>::evm_call_overheads(),
})?;

let value = U256::zero();
let value_u256 = U256::from(value);
// With overheads, less weight is available.
let weight_limit = context
.weight_limit
Expand All @@ -161,7 +180,7 @@ impl<T: Config> Pallet<T> {
let tx = CheckedEthereumTx {
gas_limit,
target: target_decoded,
value,
value: value_u256,
input: bounded_input,
maybe_access_list: None,
};
Expand Down Expand Up @@ -210,6 +229,7 @@ impl<T: Config> Pallet<T> {
source: T::AccountId,
target: Vec<u8>,
input: Vec<u8>,
value: Balance,
skip_execution: bool,
) -> CallResult {
log::trace!(
Expand All @@ -231,7 +251,6 @@ impl<T: Config> Pallet<T> {
let weight_limit = context
.weight_limit
.saturating_sub(WeightInfoOf::<T>::wasm_call_overheads());
let value = Default::default();

// Note the skip execution check should be exactly before `pallet_contracts::bare_call`
// to benchmark the correct overheads.
Expand Down Expand Up @@ -277,7 +296,8 @@ impl<T: Config> Pallet<T> {
source: T::AccountId,
target: Vec<u8>,
input: Vec<u8>,
value: Balance,
) -> CallResult {
Self::do_call(context, vm_id, source, target, input, true)
Self::do_call(context, vm_id, source, target, input, value, true)
}
}
1 change: 0 additions & 1 deletion pallets/xvm/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ impl pallet_xvm::Config for TestRuntime {

pub(crate) type AccountId = AccountId32;
pub(crate) type BlockNumber = u64;
pub(crate) type Balance = u128;

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<TestRuntime>;
type Block = frame_system::mocking::MockBlock<TestRuntime>;
Expand Down
8 changes: 5 additions & 3 deletions precompiles/xvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ mod tests;
#[precompile_utils::generate_function_selector]
#[derive(Debug, PartialEq)]
pub enum Action {
XvmCall = "xvm_call(uint8,bytes,bytes)",
XvmCall = "xvm_call(uint8,bytes,bytes,uint256)",
}

/// A precompile that expose XVM related functions.
Expand Down Expand Up @@ -74,7 +74,7 @@ where
{
fn xvm_call(handle: &mut impl PrecompileHandle) -> EvmResult<PrecompileOutput> {
let mut input = handle.read_input()?;
input.expect_arguments(3)?;
input.expect_arguments(4)?;

let vm_id = {
let id = input.read::<u8>()?;
Expand All @@ -90,9 +90,11 @@ where

let call_to = input.read::<Bytes>()?.0;
let call_input = input.read::<Bytes>()?.0;
//TODO: type
let value = input.read::<u128>()?;
let from = R::AddressMapping::into_account_id(handle.context().caller);

let call_result = XC::call(xvm_context, vm_id, from, call_to, call_input);
let call_result = XC::call(xvm_context, vm_id, from, call_to, call_input, value);

let used_weight = match &call_result {
Ok(s) => s.used_weight,
Expand Down
3 changes: 3 additions & 0 deletions primitives/src/xvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
// You should have received a copy of the GNU General Public License
// along with Astar. If not, see <http://www.gnu.org/licenses/>.

use crate::Balance;

use frame_support::weights::Weight;
use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;
Expand Down Expand Up @@ -108,5 +110,6 @@ pub trait XvmCall<AccountId> {
source: AccountId,
target: Vec<u8>,
input: Vec<u8>,
value: Balance,
) -> CallResult;
}

0 comments on commit dd1987d

Please sign in to comment.