Skip to content

Commit

Permalink
Benchmarks.
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunxw committed Jul 13, 2023
1 parent ca3dc07 commit e6b5ce5
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 9 deletions.
2 changes: 2 additions & 0 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ num_enum = { version = "0.5.3", default-features = false }
num-traits = { version = "0.2", default-features = false }
rand = { version = "0.8.5", default-features = false }
bounded-collections = { version = "0.1.5", default-features = false }
hex = { version = "0.4.3", default-features = false }

# (native)
array-bytes = "6.0.0"
Expand All @@ -74,7 +75,6 @@ serde_json = "1.0.92"
tokio = { version = "1.24.2", features = ["macros", "sync"] }
url = "2.2.2"
jsonrpsee = { version = "0.16.2", features = ["server"] }
hex = { version = "0.4.3", features = ["serde"] }
hex-literal = "0.4.1"
regex = "1.6.0"
rlp = "0.5"
Expand Down
1 change: 1 addition & 0 deletions pallets/dapps-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
#![cfg_attr(not(feature = "std"), no_std)]

use astar_primitives::Balance;
use frame_support::traits::Currency;
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_runtime::{traits::Zero, RuntimeDebug};
Expand Down
6 changes: 5 additions & 1 deletion pallets/ethereum-checked/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ homepage.workspace = true
repository.workspace = true

[dependencies]
hex = { workspace = true, optional = true }
parity-scale-codec = { workspace = true }
scale-info = { workspace = true }

Expand All @@ -26,7 +27,6 @@ astar-primitives = { workspace = true }

[dev-dependencies]
ethereum = { workspace = true }
hex = { workspace = true }
pallet-balances = { workspace = true }
pallet-ethereum = { workspace = true }
pallet-evm = { workspace = true }
Expand All @@ -36,6 +36,7 @@ sp-io = { workspace = true }
[features]
default = ["std"]
std = [
"hex/std",
"parity-scale-codec/std",
"scale-info/std",
"ethereum/std",
Expand All @@ -55,9 +56,12 @@ std = [
"astar-primitives/std",
]
runtime-benchmarks = [
"hex",
"frame-benchmarking",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"pallet-ethereum/runtime-benchmarks",
"astar-primitives/runtime-benchmarks",
]
try-runtime = ["frame-support/try-runtime"]
62 changes: 62 additions & 0 deletions pallets/ethereum-checked/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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 astar_primitives::ethereum_checked::MAX_ETHEREUM_TX_INPUT_SIZE;
use frame_benchmarking::{benchmarks, impl_benchmark_test_suite, BenchmarkError};

benchmarks! {
transact_without_apply {
let origin = T::XcmTransactOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;

let target = H160::from_slice(
&hex::decode("dfb975d018f03994a3b943808e3aa0964bd78463").unwrap()
);
let input = BoundedVec::<u8, ConstU32<MAX_ETHEREUM_TX_INPUT_SIZE>>::try_from(
hex::decode("6057361d0000000000000000000000000000000000000000000000000000000000000003").unwrap()
)
.unwrap();
let checked_tx = CheckedEthereumTx {
gas_limit: U256::from(1_000_000),
target,
value: U256::zero(),
input,
maybe_access_list: None,
};
}: _<T::RuntimeOrigin>(origin, checked_tx)
verify {
assert_eq!(Nonce::<T>::get(), U256::one())
}
}

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

pub fn new_test_ext() -> TestExternalities {
mock::ExtBuilder::default().build()
}
}

impl_benchmark_test_suite!(
Pallet,
crate::benchmarking::tests::new_test_ext(),
crate::mock::TestRuntime,
);
47 changes: 44 additions & 3 deletions pallets/ethereum-checked/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ use scale_info::TypeInfo;
use ethereum_types::{H160, U256};
use fp_ethereum::{TransactionData, ValidatedTransaction};
use fp_evm::{
CallInfo, CallOrCreateInfo, CheckEvmTransaction, CheckEvmTransactionConfig,
InvalidEvmTransactionError,
CallInfo, CallOrCreateInfo, CheckEvmTransaction, CheckEvmTransactionConfig, ExitReason,
ExitSucceed, InvalidEvmTransactionError,
};
use pallet_evm::GasWeightMapping;

Expand All @@ -70,6 +70,12 @@ use astar_primitives::ethereum_checked::{

pub use pallet::*;

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

// pub mod weights;
// pub use weights::WeightInfo;

mod mock;
mod tests;

Expand Down Expand Up @@ -135,6 +141,9 @@ pub mod pallet {

/// Origin for `transact` call.
type XcmTransactOrigin: EnsureOrigin<Self::RuntimeOrigin, Success = Self::AccountId>;

// /// Weight information for extrinsics in this pallet.
// type WeightInfo: WeightInfo;
}

#[pallet::origin]
Expand All @@ -161,6 +170,25 @@ pub mod pallet {
T::AccountMapping::into_h160(source),
tx.into(),
CheckedEthereumTxKind::Xcm,
false,
)
.map(|(post_info, _)| post_info)
}

/// Transact an Ethereum transaction but not to apply it. This call is meant only for
/// benchmarks, to get the weight overhead before apply.
#[pallet::call_index(100)]
#[pallet::weight(0)]
pub fn transact_without_apply(
origin: OriginFor<T>,
tx: CheckedEthereumTx,
) -> DispatchResultWithPostInfo {
let source = T::XcmTransactOrigin::ensure_origin(origin)?;
Self::do_transact(
T::AccountMapping::into_h160(source),
tx.into(),
CheckedEthereumTxKind::Xcm,
true,
)
.map(|(post_info, _)| post_info)
}
Expand All @@ -173,6 +201,7 @@ impl<T: Config> Pallet<T> {
source: H160,
checked_tx: CheckedEthereumTx,
tx_kind: CheckedEthereumTxKind,
skip_apply: bool,
) -> Result<(PostDispatchInfo, CallInfo), DispatchErrorWithPostInfo> {
let chain_id = T::ChainId::get();
let nonce = Nonce::<T>::get();
Expand Down Expand Up @@ -203,6 +232,18 @@ impl<T: Config> Pallet<T> {

Nonce::<T>::put(nonce.saturating_add(U256::one()));

if skip_apply {
return Ok((
PostDispatchInfo::default(),
CallInfo {
exit_reason: ExitReason::Succeed(ExitSucceed::Stopped),
value: Default::default(),
used_gas: checked_tx.gas_limit,
logs: Default::default(),
},
));
}

// Execute the tx.
let (post_info, apply_info) = T::ValidatedTransaction::apply(source, tx)?;
match apply_info {
Expand All @@ -229,6 +270,6 @@ impl<T: Config> CheckedEthereumTransact for Pallet<T> {
source: H160,
checked_tx: CheckedEthereumTx,
) -> Result<(PostDispatchInfo, CallInfo), DispatchErrorWithPostInfo> {
Self::do_transact(source, checked_tx, CheckedEthereumTxKind::Xvm)
Self::do_transact(source, checked_tx, CheckedEthereumTxKind::Xvm, false)
}
}
5 changes: 3 additions & 2 deletions pallets/pallet-xvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ sp-std = { workspace = true }
# Benchmarks
frame-benchmarking = { workspace = true, optional = true }

# EVM & Frontier
# EVM
pallet-evm = { workspace = true, optional = true }

# Substrate WASM VM support
Expand All @@ -49,17 +49,18 @@ std = [
"frame-support/std",
"frame-system/std",
"pallet-contracts/std",
"pallet-evm/std",
"scale-info/std",
"serde",
"sp-core/std",
"sp-runtime/std",
"sp-std/std",
"astar-primitives/std",
"pallet-ethereum-checked/std",
"pallet-evm/std",
]

runtime-benchmarks = [
"frame-benchmarking",
"pallet-ethereum-checked/runtime-benchmarks",
]
try-runtime = ["frame-support/try-runtime"]
6 changes: 6 additions & 0 deletions runtime/local/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pallet-contracts = { workspace = true }
pallet-contracts-primitives = { workspace = true }
pallet-democracy = { workspace = true }
pallet-ethereum = { workspace = true }
pallet-ethereum-checked = { workspace = true }
pallet-evm = { workspace = true }
pallet-evm-precompile-blake2 = { workspace = true }
pallet-evm-precompile-bn128 = { workspace = true }
Expand All @@ -50,6 +51,7 @@ sp-block-builder = { workspace = true }
sp-consensus-aura = { workspace = true }
sp-core = { workspace = true }
sp-inherents = { workspace = true }
sp-io = { workspace = true }
sp-offchain = { workspace = true }
sp-runtime = { workspace = true }
sp-session = { workspace = true }
Expand Down Expand Up @@ -149,13 +151,15 @@ std = [
"sp-std/std",
"sp-transaction-pool/std",
"sp-version/std",
"sp-io/std",
"frame-benchmarking/std",
"frame-try-runtime/std",
"pallet-collective/std",
"pallet-democracy/std",
"pallet-scheduler/std",
"pallet-treasury/std",
"pallet-xvm/std",
"pallet-ethereum-checked/std",
"moonbeam-evm-tracer/std",
"moonbeam-rpc-primitives-debug/std",
"moonbeam-rpc-primitives-txpool/std",
Expand All @@ -176,6 +180,7 @@ runtime-benchmarks = [
"pallet-ethereum/runtime-benchmarks",
"pallet-collective/runtime-benchmarks",
"pallet-preimage/runtime-benchmarks",
"pallet-ethereum-checked/runtime-benchmarks",
"astar-primitives/runtime-benchmarks",
]
try-runtime = [
Expand Down Expand Up @@ -209,6 +214,7 @@ try-runtime = [
"pallet-preimage/try-runtime",
"pallet-base-fee/try-runtime",
"pallet-evm/try-runtime",
"pallet-ethereum-checked/try-runtime",
]
evm-tracing = [
"moonbeam-evm-tracer",
Expand Down
31 changes: 30 additions & 1 deletion runtime/local/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,14 +440,41 @@ impl pallet_utility::Config for Runtime {
type WeightInfo = pallet_utility::weights::SubstrateWeight<Runtime>;
}

///TODO: Placeholder account mapping. This would be replaced once account abstraction is finished.
pub struct HashedAccountMapping;
impl astar_primitives::ethereum_checked::AccountMapping<AccountId> for HashedAccountMapping {
fn into_h160(account_id: AccountId) -> H160 {
let data = (b"evm:", account_id);
return H160::from_slice(&data.using_encoded(sp_io::hashing::blake2_256)[0..20]);
}
}

parameter_types! {
/// Equal to normal class dispatch weight limit.
pub XvmTxWeightLimit: Weight = NORMAL_DISPATCH_RATIO * Weight::from_parts(WEIGHT_REF_TIME_PER_SECOND, u64::MAX);
pub ReservedXcmpWeight: Weight = Weight::zero();
}

impl pallet_ethereum_checked::Config for Runtime {
type ReservedXcmpWeight = ReservedXcmpWeight;
type XvmTxWeightLimit = XvmTxWeightLimit;
type InvalidEvmTransactionError = pallet_ethereum::InvalidTransactionWrapper;
type ValidatedTransaction = pallet_ethereum::ValidatedTransaction<Self>;
type AccountMapping = HashedAccountMapping;
type XcmTransactOrigin = pallet_ethereum_checked::EnsureXcmEthereumTx<AccountId>;
}

parameter_types! {
pub EvmId: u8 = 0x0F;
pub WasmId: u8 = 0x1F;
}

use pallet_xvm::{evm, wasm};
impl pallet_xvm::Config for Runtime {
type SyncVM = (evm::EVM<EvmId, Self>, wasm::WASM<WasmId, Self>);
type SyncVM = (
evm::EVM<EvmId, Self, EthereumChecked>,
wasm::WASM<WasmId, Self>,
);
type AsyncVM = ();
type RuntimeEvent = RuntimeEvent;
}
Expand Down Expand Up @@ -987,6 +1014,7 @@ construct_runtime!(
Xvm: pallet_xvm,
Proxy: pallet_proxy,
Preimage: pallet_preimage,
EthereumChecked: pallet_ethereum_checked,
}
);

Expand Down Expand Up @@ -1094,6 +1122,7 @@ mod benches {
[pallet_timestamp, Timestamp]
[pallet_dapps_staking, DappsStaking]
[pallet_block_reward, BlockReward]
[pallet_ethereum_checked, EthereumChecked]
);
}

Expand Down
2 changes: 2 additions & 0 deletions runtime/shibuya/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ runtime-benchmarks = [
"xcm-builder/runtime-benchmarks",
"pallet-preimage/runtime-benchmarks",
"pallet-collator-selection/runtime-benchmarks",
"pallet-ethereum-checked/runtime-benchmarks",
"polkadot-runtime/runtime-benchmarks",
"orml-xtokens/runtime-benchmarks",
"astar-primitives/runtime-benchmarks",
Expand Down Expand Up @@ -303,6 +304,7 @@ try-runtime = [
"pallet-base-fee/try-runtime",
"pallet-evm/try-runtime",
"pallet-state-trie-migration/try-runtime",
"pallet-ethereum-checked/try-runtime",
"orml-xtokens/try-runtime",
]
evm-tracing = [
Expand Down
1 change: 1 addition & 0 deletions runtime/shibuya/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1432,6 +1432,7 @@ mod benches {
[pallet_xc_asset_config, XcAssetConfig]
[pallet_collator_selection, CollatorSelection]
[pallet_xcm, PolkadotXcm]
[pallet_ethereum_checked, EthereumChecked]
);
}

Expand Down
Loading

0 comments on commit e6b5ce5

Please sign in to comment.