Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pallet-revive] implement the ref_time_left API #6908

Merged
merged 11 commits into from
Dec 18, 2024
12 changes: 12 additions & 0 deletions prdoc/pr_6908.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
title: '[pallet-revive] implement the ref_time_left API'
doc:
- audience: Runtime Dev
description: This PR implements the ref_time_left API method. Solidity knows only
a single "gas" dimension; Solidity contracts will use this to query the gas left.
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/ref_time_left.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.

#![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() {
assert!(api::ref_time_left() > api::ref_time_left());
}

#[no_mangle]
#[polkavm_derive::polkavm_export]
pub extern "C" fn call() {
api::return_value(ReturnFlags::empty(), &api::ref_time_left().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 @@ -671,6 +671,18 @@ mod benchmarks {
);
}

#[benchmark(pov_mode = Measured)]
fn seal_ref_time_left() {
build_runtime!(runtime, memory: [vec![], ]);

let result;
#[block]
{
result = runtime.bench_ref_time_left(memory.as_mut_slice());
}
assert_eq!(result.unwrap(), runtime.ext().gas_meter().gas_left().ref_time());
}

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

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

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

// Create fixture: Constructor calls ref_time_left twice and asserts it to decrease
let Contract { addr, .. } =
builder::bare_instantiate(Code::Upload(code)).build_and_unwrap_contract();

// Call the contract: It echoes back the ref_time returned by the ref_time_left API.
let received = builder::bare_call(addr).build_and_unwrap_result();
assert_eq!(received.flags, ReturnFlags::empty());

let returned_value = u64::from_le_bytes(received.data[..8].try_into().unwrap());
assert!(returned_value > 0);
assert!(returned_value < GAS_LIMIT.ref_time());
});
}

#[test]
fn lazy_removal_partial_remove_works() {
let (code, _hash) = compile_module("self_destruct").unwrap();
Expand Down
10 changes: 10 additions & 0 deletions substrate/frame/revive/src/wasm/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ pub enum RuntimeCosts {
CallerIsRoot,
/// Weight of calling `seal_address`.
Address,
/// Weight of calling `seal_ref_time_left`.
RefTimeLeft,
/// Weight of calling `seal_weight_left`.
WeightLeft,
/// Weight of calling `seal_balance`.
Expand Down Expand Up @@ -444,6 +446,7 @@ impl<T: Config> Token<T> for RuntimeCosts {
CallerIsOrigin => T::WeightInfo::seal_caller_is_origin(),
CallerIsRoot => T::WeightInfo::seal_caller_is_root(),
Address => T::WeightInfo::seal_address(),
RefTimeLeft => T::WeightInfo::seal_ref_time_left(),
WeightLeft => T::WeightInfo::seal_weight_left(),
Balance => T::WeightInfo::seal_balance(),
BalanceOf => T::WeightInfo::seal_balance_of(),
Expand Down Expand Up @@ -1997,6 +2000,13 @@ pub mod env {
self.terminate(memory, beneficiary_ptr)
}

/// Returns the amount of ref_time left.
/// See [`pallet_revive_uapi::HostFn::ref_time_left`].
fn ref_time_left(&mut self, memory: &mut M) -> Result<u64, TrapReason> {
self.charge_gas(RuntimeCosts::RefTimeLeft)?;
Ok(self.ext.gas_meter().gas_left().ref_time())
}
athei marked this conversation as resolved.
Show resolved Hide resolved

/// Stores the amount of weight left into the supplied buffer.
/// See [`pallet_revive_uapi::HostFn::weight_left`].
fn weight_left(
Expand Down
Loading
Loading