Skip to content

Commit

Permalink
Benchmarks and mock.
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunxw committed Jul 26, 2023
1 parent 819c7f8 commit 50c04b2
Show file tree
Hide file tree
Showing 11 changed files with 326 additions and 24 deletions.
6 changes: 5 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pallets/ethereum-checked/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
//!
//! ## Overview
//!
//! A `pallet-ethererum` like pallet that execute transactions from checked source,
//! A `pallet-ethereum like pallet that execute transactions from checked source,
//! like XCM remote call, cross-VM call, etc. Only `Call` transactions are supported
//! (no `Create`).
//!
Expand Down
10 changes: 6 additions & 4 deletions pallets/xvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ pallet-contracts = { workspace = true }

# Astar
astar-primitives = { workspace = true }
pallet-ethereum-checked = { workspace = true }

[dev-dependencies]
sp-io = { workspace = true }
fp-evm = { workspace = true }
pallet-timestamp = { workspace = true, features = ["std"] }
pallet-balances = { workspace = true, features = ["std"] }
pallet-insecure-randomness-collective-flip = { workspace = true, features = ["std"] }

[features]
default = ["std"]
Expand All @@ -43,22 +47,20 @@ std = [
"frame-system/std",
"pallet-contracts/std",
"pallet-evm/std",
"pallet-insecure-randomness-collective-flip/std",
"scale-info/std",
"serde",
"sp-core/std",
"sp-runtime/std",
"sp-std/std",
"astar-primitives/std",
"pallet-ethereum-checked/std",
]

runtime-benchmarks = [
"frame-benchmarking",
"pallet-ethereum-checked/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
"pallet-contracts/try-runtime",
"pallet-evm/try-runtime",
"pallet-ethereum-checked/try-runtime",
]
78 changes: 78 additions & 0 deletions pallets/xvm/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// This file is part of Astar.

// Copyright (C) 2019-2023 Stake Technologies Pte.Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later

// Astar is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Astar is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Astar. If not, see <http://www.gnu.org/licenses/>.

use super::*;

use frame_benchmarking::v2::*;
use parity_scale_codec::Encode;
use sp_core::H160;

#[benchmarks]
mod benchmarks {
use super::*;

#[benchmark]
fn evm_call_without_execution() {
let context = Context {
source_vm_id: VmId::Wasm,
weight_limit: Weight::from_parts(1_000_000, 1_000_000),
};
let vm_id = VmId::Evm;
let source = whitelisted_caller();
let target = H160::repeat_byte(1).encode();
let input = vec![1, 2, 3];

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

#[benchmark]
fn wasm_call_without_execution() {
let context = Context {
source_vm_id: VmId::Evm,
weight_limit: Weight::from_parts(1_000_000, 1_000_000),
};
let vm_id = VmId::Wasm;
let source = whitelisted_caller();
let target = whitelisted_caller::<T::AccountId>().encode();
let input = vec![1, 2, 3];

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

impl_benchmark_test_suite!(
Pallet,
crate::benchmarking::tests::new_test_ext(),
crate::mock::TestRuntime,
);
}

#[cfg(test)]
mod tests {
use crate::mock;
use sp_io::TestExternalities;

pub fn new_test_ext() -> TestExternalities {
mock::ExtBuilder::default().build()
}
}
33 changes: 19 additions & 14 deletions pallets/xvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,15 @@ use astar_primitives::{
ethereum_checked::{
AccountMapping, CheckedEthereumTransact, CheckedEthereumTx, MAX_ETHEREUM_TX_INPUT_SIZE,
},
xvm::{CallError, CallErrorWithWeight, CallInfo, Context, VmId, XvmCall, XvmCallResult},
xvm::{CallError, CallErrorWithWeight, CallInfo, CallResult, Context, VmId, XvmCall},
};

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;

#[cfg(test)]
mod mock;

pub use pallet::*;

#[frame_support::pallet]
Expand All @@ -55,12 +61,11 @@ pub mod pallet {
pub struct Pallet<T>(PhantomData<T>);

#[pallet::config]
pub trait Config:
frame_system::Config
+ pallet_evm::Config
+ pallet_ethereum_checked::Config
+ pallet_contracts::Config
{
pub trait Config: frame_system::Config + pallet_contracts::Config {
/// Mapping from `Account` to `H160`.
type AccountMapping: AccountMapping<Self::AccountId>;
/// Mapping from Ethereum gas to Substrate weight.
type GasWeightMapping: GasWeightMapping;
/// `CheckedEthereumTransact` implementation.
type EthereumTransact: CheckedEthereumTransact;
}
Expand All @@ -73,7 +78,7 @@ impl<T: Config> XvmCall<T::AccountId> for Pallet<T> {
source: T::AccountId,
target: Vec<u8>,
input: Vec<u8>,
) -> XvmCallResult {
) -> CallResult {
Pallet::<T>::do_call(context, vm_id, source, target, input, false)
}
}
Expand All @@ -89,7 +94,7 @@ impl<T: Config> Pallet<T> {
target: Vec<u8>,
input: Vec<u8>,
skip_execution: bool,
) -> XvmCallResult {
) -> CallResult {
ensure!(
context.source_vm_id != vm_id,
CallErrorWithWeight {
Expand All @@ -98,7 +103,7 @@ impl<T: Config> Pallet<T> {
}
);

match context.source_vm_id {
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),
}
Expand All @@ -110,7 +115,7 @@ impl<T: Config> Pallet<T> {
target: Vec<u8>,
input: Vec<u8>,
skip_execution: bool,
) -> XvmCallResult {
) -> CallResult {
log::trace!(
target: "xvm::evm_call",
"Calling EVM: {:?} {:?}, {:?}, {:?}",
Expand Down Expand Up @@ -174,7 +179,7 @@ impl<T: Config> Pallet<T> {
target: Vec<u8>,
input: Vec<u8>,
skip_execution: bool,
) -> XvmCallResult {
) -> CallResult {
log::trace!(
target: "xvm::wasm_call",
"Calling WASM: {:?} {:?}, {:?}, {:?}",
Expand Down Expand Up @@ -226,13 +231,13 @@ impl<T: Config> Pallet<T> {
}

#[cfg(feature = "runtime-benchmarks")]
pub fn xvm_call_without_execution(
pub fn call_without_execution(
context: Context,
vm_id: VmId,
source: T::AccountId,
target: Vec<u8>,
input: Vec<u8>,
) -> XvmCallResult {
) -> CallResult {
Self::do_call(context, vm_id, source, target, input, true)
}
}
Loading

0 comments on commit 50c04b2

Please sign in to comment.