Skip to content

Commit

Permalink
[pallet-revive] implement the gas price API (#6954)
Browse files Browse the repository at this point in the history
This PR implements the EVM gas price syscall API method. Currently this
is a compile time constant in revive, but in the EVM it is an opcode.
Thus we should provide an opcode for this in the pallet.

---------

Signed-off-by: Cyrill Leutwiler <[email protected]>
Signed-off-by: xermicus <[email protected]>
Co-authored-by: command-bot <>
  • Loading branch information
xermicus authored Dec 19, 2024
1 parent cbeb66f commit 2cbb437
Show file tree
Hide file tree
Showing 8 changed files with 571 additions and 470 deletions.
13 changes: 13 additions & 0 deletions prdoc/pr_6954.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
title: '[pallet-revive] implement the gas price API'
doc:
- audience: Runtime Dev
description: This PR implements the EVM gas price syscall API method. Currently
this is a compile time constant in revive, but in the EVM it is an opcode. Thus
we should provide an opcode for this in the pallet.
crates:
- name: pallet-revive-fixtures
bump: minor
- name: pallet-revive
bump: minor
- name: pallet-revive-uapi
bump: minor
34 changes: 34 additions & 0 deletions substrate/frame/revive/fixtures/contracts/gas_price.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// This file is part of Substrate.

// Copyright (C) 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.

//! Returns the gas price back to the caller.
#![no_std]
#![no_main]

extern crate common;
use uapi::{HostFn, HostFnImpl as api, ReturnFlags};

#[no_mangle]
#[polkavm_derive::polkavm_export]
pub extern "C" fn deploy() {}

#[no_mangle]
#[polkavm_derive::polkavm_export]
pub extern "C" fn call() {
api::return_value(ReturnFlags::empty(), &api::gas_price().to_le_bytes());
}
12 changes: 12 additions & 0 deletions substrate/frame/revive/src/benchmarking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ mod call_builder;
mod code;
use self::{call_builder::CallSetup, code::WasmModule};
use crate::{
evm::runtime::GAS_PRICE,
exec::{Key, MomentOf},
limits,
storage::WriteOutcome,
Expand Down Expand Up @@ -820,6 +821,17 @@ mod benchmarks {
assert_eq!(result.unwrap(), T::BlockWeights::get().max_block.ref_time());
}

#[benchmark(pov_mode = Measured)]
fn seal_gas_price() {
build_runtime!(runtime, memory: []);
let result;
#[block]
{
result = runtime.bench_gas_price(memory.as_mut_slice());
}
assert_eq!(result.unwrap(), u64::from(GAS_PRICE));
}

#[benchmark(pov_mode = Measured)]
fn seal_block_number() {
build_runtime!(runtime, memory: [[0u8;32], ]);
Expand Down
20 changes: 19 additions & 1 deletion substrate/frame/revive/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::{
ChainExtension, Environment, Ext, RegisteredChainExtension, Result as ExtensionResult,
RetVal, ReturnFlags,
},
evm::GenericTransaction,
evm::{runtime::GAS_PRICE, GenericTransaction},
exec::Key,
limits,
primitives::CodeUploadReturnValue,
Expand Down Expand Up @@ -4364,6 +4364,24 @@ fn create1_with_value_works() {
});
}

#[test]
fn gas_price_api_works() {
let (code, _) = compile_module("gas_price").unwrap();

ExtBuilder::default().existential_deposit(100).build().execute_with(|| {
let _ = <Test as Config>::Currency::set_balance(&ALICE, 1_000_000);

// Create fixture: Constructor does nothing
let Contract { addr, .. } =
builder::bare_instantiate(Code::Upload(code)).build_and_unwrap_contract();

// Call the contract: It echoes back the value returned by the gas price API.
let received = builder::bare_call(addr).build_and_unwrap_result();
assert_eq!(received.flags, ReturnFlags::empty());
assert_eq!(u64::from_le_bytes(received.data[..].try_into().unwrap()), u64::from(GAS_PRICE));
});
}

#[test]
fn call_data_size_api_works() {
let (code, _) = compile_module("call_data_size").unwrap();
Expand Down
12 changes: 12 additions & 0 deletions substrate/frame/revive/src/wasm/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use crate::{
address::AddressMapper,
evm::runtime::GAS_PRICE,
exec::{ExecError, ExecResult, Ext, Key},
gas::{ChargedAmount, Token},
limits,
Expand Down Expand Up @@ -324,6 +325,8 @@ pub enum RuntimeCosts {
BlockNumber,
/// Weight of calling `seal_block_hash`.
BlockHash,
/// Weight of calling `seal_gas_price`.
GasPrice,
/// Weight of calling `seal_now`.
Now,
/// Weight of calling `seal_gas_limit`.
Expand Down Expand Up @@ -477,6 +480,7 @@ impl<T: Config> Token<T> for RuntimeCosts {
MinimumBalance => T::WeightInfo::seal_minimum_balance(),
BlockNumber => T::WeightInfo::seal_block_number(),
BlockHash => T::WeightInfo::seal_block_hash(),
GasPrice => T::WeightInfo::seal_gas_price(),
Now => T::WeightInfo::seal_now(),
GasLimit => T::WeightInfo::seal_gas_limit(),
WeightToFee => T::WeightInfo::seal_weight_to_fee(),
Expand Down Expand Up @@ -1563,6 +1567,14 @@ pub mod env {
)?)
}

/// Returns the simulated ethereum `GASPRICE` value.
/// See [`pallet_revive_uapi::HostFn::gas_price`].
#[stable]
fn gas_price(&mut self, memory: &mut M) -> Result<u64, TrapReason> {
self.charge_gas(RuntimeCosts::GasPrice)?;
Ok(GAS_PRICE.into())
}

/// Load the latest block timestamp into the supplied buffer
/// See [`pallet_revive_uapi::HostFn::now`].
#[stable]
Expand Down
Loading

0 comments on commit 2cbb437

Please sign in to comment.