Skip to content
This repository was archived by the owner on Mar 13, 2023. It is now read-only.

Gas to weight = ? #718

Merged
merged 7 commits into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

1 change: 1 addition & 0 deletions bin/node/runtime/pangolin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ runtime-benchmarks = [
"frame-support/runtime-benchmarks",
"pallet-collective/runtime-benchmarks",
"pallet-society/runtime-benchmarks",
"darwinia-evm/runtime-benchmarks",
"darwinia-s2s-issuing/runtime-benchmarks",
]

Expand Down
1 change: 1 addition & 0 deletions bin/node/runtime/pangolin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,7 @@ impl_runtime_apis! {
let params = (&config, &whitelist);

add_benchmark!(params, batches, frame_system, SystemBench::<Runtime>);
add_benchmark!(params, batches, darwinia_evm, EVM);
add_benchmark!(params, batches, darwinia_s2s_issuing, Substrate2SubstrateIssuing);

if batches.is_empty() { return Err("Benchmark not found for this pallet.".into()) }
Expand Down
16 changes: 2 additions & 14 deletions bin/node/runtime/pangolin/src/pallets/evm_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ use sp_core::{crypto::Public, H160, U256};
use sp_std::marker::PhantomData;
// --- darwinia ---
use crate::*;
use darwinia_evm::{
runner::stack::Runner, ConcatAddressMapping, Config, EnsureAddressTruncated, GasWeightMapping,
};
use darwinia_evm::{runner::stack::Runner, ConcatAddressMapping, Config, EnsureAddressTruncated};
use dp_evm::{Precompile, PrecompileSet};
use dvm_ethereum::{
account_basic::{DvmAccountBasic, KtonRemainBalance, RingRemainBalance},
Expand Down Expand Up @@ -72,24 +70,14 @@ where
}
}

pub struct DarwiniaGasWeightMapping;
impl GasWeightMapping for DarwiniaGasWeightMapping {
fn gas_to_weight(gas: u64) -> Weight {
gas * 1_000 as Weight
}
fn weight_to_gas(weight: Weight) -> u64 {
weight / 1_000
}
}

frame_support::parameter_types! {
pub const ChainId: u64 = 43;
pub BlockGasLimit: U256 = u32::max_value().into();
}

impl Config for Runtime {
type FeeCalculator = dvm_dynamic_fee::Pallet<Self>;
type GasWeightMapping = DarwiniaGasWeightMapping;
type GasWeightMapping = ();
type CallOrigin = EnsureAddressTruncated<Self::AccountId>;
type AddressMapping = ConcatAddressMapping<Self::AccountId>;
type FindAuthor = EthereumFindAuthor<Babe>;
Expand Down
23 changes: 16 additions & 7 deletions frame/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ codec = { package = "parity-scale-codec", version = "2.1.1", default-f
evm = { version = "0.27.0", default-features = false, features = ["with-codec"] }
evm-gasometer = { version = "0.27.0", default-features = false }
evm-runtime = { version = "0.27.0", default-features = false }
hex = { optional = true, version = "0.4", default-features = false }
log = { version = "0.4.14" }
primitive-types = { version = "0.9.0", default-features = false, features = ["rlp", "byteorder"] }
rlp = { version = "0.5.0", default-features = false }
Expand All @@ -24,13 +25,14 @@ sha3 = { version = "0.9.1", default-features = false }
darwinia-balances = { default-features = false, path = "../balances" }
dp-evm = { default-features = false, path = "../../primitives/evm" }
# paritytech
frame-support = { default-features = false, git = "https://github.com/darwinia-network/substrate", branch = "main" }
frame-system = { default-features = false, git = "https://github.com/darwinia-network/substrate", branch = "main" }
pallet-timestamp = { default-features = false, git = "https://github.com/darwinia-network/substrate", branch = "main" }
sp-core = { default-features = false, git = "https://github.com/darwinia-network/substrate", branch = "main" }
sp-io = { default-features = false, git = "https://github.com/darwinia-network/substrate", branch = "main" }
sp-runtime = { default-features = false, git = "https://github.com/darwinia-network/substrate", branch = "main" }
sp-std = { default-features = false, git = "https://github.com/darwinia-network/substrate", branch = "main" }
frame-benchmarking = { optional = true, default-features = false, git = "https://github.com/darwinia-network/substrate", branch = "main" }
frame-support = { default-features = false, git = "https://github.com/darwinia-network/substrate", branch = "main" }
frame-system = { default-features = false, git = "https://github.com/darwinia-network/substrate", branch = "main" }
pallet-timestamp = { default-features = false, git = "https://github.com/darwinia-network/substrate", branch = "main" }
sp-core = { default-features = false, git = "https://github.com/darwinia-network/substrate", branch = "main" }
sp-io = { default-features = false, git = "https://github.com/darwinia-network/substrate", branch = "main" }
sp-runtime = { default-features = false, git = "https://github.com/darwinia-network/substrate", branch = "main" }
sp-std = { default-features = false, git = "https://github.com/darwinia-network/substrate", branch = "main" }

[dev-dependencies]
darwinia-support = { features = ["easy-testing"], path = "../support" }
Expand All @@ -45,6 +47,7 @@ std = [
"evm/with-serde",
"evm-gasometer/std",
"evm-runtime/std",
"hex/std",
"primitive-types/std",
"rlp/std",
"serde",
Expand All @@ -53,6 +56,7 @@ std = [
"darwinia-balances/std",
"dp-evm/std",
# paritytech
"frame-benchmarking/std",
"frame-support/std",
"frame-system/std",
"pallet-timestamp/std",
Expand All @@ -61,3 +65,8 @@ std = [
"sp-runtime/std",
"sp-std/std",
]

runtime-benchmarks = [
"hex",
"frame-benchmarking",
]
119 changes: 119 additions & 0 deletions frame/evm/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// This file is part of Darwinia.
//
// Copyright (C) 2018-2021 Darwinia Network
// SPDX-License-Identifier: GPL-3.0
//
// Darwinia 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.
//
// Darwinia 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 Darwinia. If not, see <https://www.gnu.org/licenses/>.

#![cfg(feature = "runtime-benchmarks")]

//! Benchmarking
use crate::{runner::Runner, Config, Pallet};
use frame_benchmarking::benchmarks;
use rlp::RlpStream;
use sha3::{Digest, Keccak256};
use sp_core::{H160, U256};
use sp_std::vec;

benchmarks! {

// This benchmark tests the relationship between gas and weight. It deploys a contract which
// has an infinite loop in a public function. We then call this function with varying amounts
// of gas, expecting it to OOG. The benchmarking framework measures the amount of time (aka
// weight) it takes before OOGing and relates that to the amount of gas provided, leaving us
// with an estimate for gas-to-weight mapping.
runner_execute {

let x in 1..10000000;

// contract bytecode below is for:
//
// pragma solidity >=0.8.0;
//
// contract InfiniteContractVar {
// uint public count;

// constructor() public {
// count = 0;
// }

// function infinite() public {
// while (true) {
// count=count+1;
// }
// }
// }

let contract_bytecode = hex::decode(concat!(
"608060405234801561001057600080fd5b506000808190555061017c806100276000396000f3fe60",
"8060405234801561001057600080fd5b50600436106100365760003560e01c806306661abd146100",
"3b5780635bec9e6714610059575b600080fd5b610043610063565b604051610050919061009c565b",
"60405180910390f35b610061610069565b005b60005481565b5b60011561008b5760016000546100",
"8091906100b7565b60008190555061006a565b565b6100968161010d565b82525050565b60006020",
"820190506100b1600083018461008d565b92915050565b60006100c28261010d565b91506100cd83",
"61010d565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"ff0382111561010257610101610117565b5b828201905092915050565b6000819050919050565b7f",
"4e487b71000000000000000000000000000000000000000000000000000000006000526011600452",
"60246000fdfea2646970667358221220bcab0385167dbed28dee34f1c5b30ecfcd67915495f0a32d",
"2eeada8e094193a364736f6c63430008030033"))
.expect("Bad hex string");

let caller = H160::default();

let mut nonce: u64 = 0;
let nonce_as_u256: U256 = nonce.into();

let value = U256::default();
let gas_limit_create: u64 = 1_250_000 * 1_000_000_000;
let create_runner_results = T::Runner::create(
caller,
contract_bytecode,
value,
gas_limit_create,
None,
Some(nonce_as_u256),
T::config(),
);
assert_eq!(create_runner_results.is_ok(), true, "create() failed");

// derive the resulting contract address from our create
let mut rlp = RlpStream::new_list(2);
rlp.append(&caller);
rlp.append(&0u8);
let contract_address = H160::from_slice(&Keccak256::digest(&rlp.out())[12..]);

// derive encoded contract call -- in this case, just the function selector
let mut encoded_call = vec![0u8; 4];
encoded_call[0..4].copy_from_slice(&Keccak256::digest(b"infinite()")[0..4]);

let gas_limit_call = x as u64;

}: {

nonce = nonce + 1;
let nonce_as_u256: U256 = nonce.into();

let call_runner_results = T::Runner::call(
caller,
contract_address,
encoded_call,
value,
gas_limit_call,
None,
Some(nonce_as_u256),
T::config(),
);
assert_eq!(call_runner_results.is_ok(), true, "call() failed");
}
}
7 changes: 5 additions & 2 deletions frame/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

pub mod runner;

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

Expand Down Expand Up @@ -446,12 +448,13 @@ pub trait GasWeightMapping {
fn gas_to_weight(gas: u64) -> Weight;
fn weight_to_gas(weight: Weight) -> u64;
}
// The radio of gas to weight comes from benchmark test
impl GasWeightMapping for () {
fn gas_to_weight(gas: u64) -> Weight {
gas as Weight
gas * 16_000 as Weight
}
fn weight_to_gas(weight: Weight) -> u64 {
weight
weight / 16_000
}
}

Expand Down
80 changes: 80 additions & 0 deletions frame/evm/src/weight.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// This file is part of Substrate.

// Copyright (C) 2020 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Autogenerated weights for darwinia_evm
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 3.0.0
//! DATE: 2021-07-23, STEPS: [5, ], REPEAT: 2, LOW RANGE: [], HIGH RANGE: []
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128

// Executed Command:
// ./target/release/drml
// benchmark
// --chain
// dev
// --pallet
// darwinia_evm
// --execution
// wasm
// --wasm-execution
// compiled
// --extrinsic=runner_execute
// --steps
// 5
// --repeat
// 2
// --raw
// --heap-pages=4096
// --output=./frame/evm/src/weight.rs
// --template=./.maintain/frame-weight-template.hbs


#![allow(unused_parens)]
#![allow(unused_imports)]

use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
use sp_std::marker::PhantomData;

/// Weight functions needed for darwinia_evm.
pub trait WeightInfo {
fn runner_execute(x: u32, ) -> Weight;

}

/// Weights for darwinia_evm using the Substrate node and recommended hardware.
pub struct SubstrateWeight<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
fn runner_execute(x: u32, ) -> Weight {
(0 as Weight)
// Standard Error: 0
.saturating_add((16_000 as Weight).saturating_mul(x as Weight))
.saturating_add(T::DbWeight::get().reads(4 as Weight))

}

}

// For backwards compatibility and tests
impl WeightInfo for () {
fn runner_execute(x: u32, ) -> Weight {
(0 as Weight)
.saturating_add((16_000 as Weight).saturating_mul(x as Weight))
.saturating_add(RocksDbWeight::get().reads(4 as Weight))

}

}