Skip to content

Commit

Permalink
the base fee API
Browse files Browse the repository at this point in the history
Signed-off-by: xermicus <[email protected]>
  • Loading branch information
xermicus committed Dec 19, 2024
1 parent 2cbb437 commit 0017890
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 0 deletions.
34 changes: 34 additions & 0 deletions substrate/frame/revive/fixtures/contracts/base_fee.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 base fee 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::base_fee().to_le_bytes());
}
11 changes: 11 additions & 0 deletions substrate/frame/revive/src/benchmarking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,17 @@ mod benchmarks {
assert_eq!(result.unwrap(), u64::from(GAS_PRICE));
}

#[benchmark(pov_mode = Measured)]
fn seal_base_fee() {
build_runtime!(runtime, memory: []);
let result;
#[block]
{
result = runtime.bench_base_fee(memory.as_mut_slice());
}
assert_eq!(result.unwrap(), 0);
}

#[benchmark(pov_mode = Measured)]
fn seal_block_number() {
build_runtime!(runtime, memory: [[0u8;32], ]);
Expand Down
18 changes: 18 additions & 0 deletions substrate/frame/revive/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4382,6 +4382,24 @@ fn gas_price_api_works() {
});
}

#[test]
fn base_fee_api_works() {
let (code, _) = compile_module("base_fee").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 base fee 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()), 0);
});
}

#[test]
fn call_data_size_api_works() {
let (code, _) = compile_module("call_data_size").unwrap();
Expand Down
11 changes: 11 additions & 0 deletions substrate/frame/revive/src/wasm/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ pub enum RuntimeCosts {
BlockHash,
/// Weight of calling `seal_gas_price`.
GasPrice,
/// Weight of calling `seal_base_fee`.
BaseFee,
/// Weight of calling `seal_now`.
Now,
/// Weight of calling `seal_gas_limit`.
Expand Down Expand Up @@ -481,6 +483,7 @@ impl<T: Config> Token<T> for RuntimeCosts {
BlockNumber => T::WeightInfo::seal_block_number(),
BlockHash => T::WeightInfo::seal_block_hash(),
GasPrice => T::WeightInfo::seal_gas_price(),
BaseFee => T::WeightInfo::seal_base_fee(),
Now => T::WeightInfo::seal_now(),
GasLimit => T::WeightInfo::seal_gas_limit(),
WeightToFee => T::WeightInfo::seal_weight_to_fee(),
Expand Down Expand Up @@ -1575,6 +1578,14 @@ pub mod env {
Ok(GAS_PRICE.into())
}

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

/// Load the latest block timestamp into the supplied buffer
/// See [`pallet_revive_uapi::HostFn::now`].
#[stable]
Expand Down
15 changes: 15 additions & 0 deletions substrate/frame/revive/src/weights.rs

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

4 changes: 4 additions & 0 deletions substrate/frame/revive/uapi/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ pub trait HostFn: private::Sealed {
/// [GASPRICE](https://www.evm.codes/?fork=cancun#3a) opcode.
fn gas_price() -> u64;

/// Returns the base fee, akin to the EVM
/// [BASEFEE](https://www.evm.codes/?fork=cancun#48) opcode.
fn base_fee() -> u64;

/// Returns the call data size.
fn call_data_size() -> u64;

Expand Down
5 changes: 5 additions & 0 deletions substrate/frame/revive/uapi/src/host/riscv64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ mod sys {
data_len: u32,
);
pub fn gas_price() -> u64;
pub fn base_fee() -> u64;
pub fn call_data_size() -> u64;
pub fn block_number(out_ptr: *mut u8);
pub fn block_hash(block_number_ptr: *const u8, out_ptr: *mut u8);
Expand Down Expand Up @@ -374,6 +375,10 @@ impl HostFn for HostFnImpl {
unsafe { sys::gas_price() }
}

fn base_fee() -> u64 {
unsafe { sys::base_fee() }
}

fn balance(output: &mut [u8; 32]) {
unsafe { sys::balance(output.as_mut_ptr()) }
}
Expand Down

0 comments on commit 0017890

Please sign in to comment.