Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 14 additions & 0 deletions prdoc/pr_10554.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
title: '[pallet-revive] add EVM gas call syscalls'
doc:
- audience: Runtime Dev
description: |-
This PR adds two new syscalls for calls accepting EVM gas instead of Weight and Deposit.

This is an important change for the initial release as it will align PVM contracts closer to EVM (the problem can't be solved in the Solidity compiler).
crates:
- name: pallet-revive-fixtures
bump: minor
- name: pallet-revive
bump: minor
- name: pallet-revive-uapi
bump: minor
42 changes: 42 additions & 0 deletions substrate/frame/revive/fixtures/contracts/call_with_gas.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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]

include!("../panic_handler.rs");

use uapi::{input, CallFlags, HostFn, HostFnImpl as api};

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

#[no_mangle]
#[polkavm_derive::polkavm_export]
pub extern "C" fn call() {
input!(
256,
callee_addr: &[u8; 20],
gas: u64,
);

let mut value = [0; 32];
api::value_transferred(&mut value);

api::call_evm(CallFlags::empty(), callee_addr, gas, &value, &[], None).unwrap();
}
52 changes: 52 additions & 0 deletions substrate/frame/revive/fixtures/contracts/delegate_call_evm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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]
include!("../panic_handler.rs");

use uapi::{input, CallFlags, HostFn, HostFnImpl as api, StorageFlags};

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

#[no_mangle]
#[polkavm_derive::polkavm_export]
pub extern "C" fn call() {
input!(
address: &[u8; 20],
gas: u64,
);

let mut key = [0u8; 32];
key[0] = 1u8;

let mut value = [0u8; 32];
let value = &mut &mut value[..];
value[0] = 2u8;

api::set_storage(StorageFlags::empty(), &key, value);
api::get_storage(StorageFlags::empty(), &key, value).unwrap();
assert!(value[0] == 2u8);

api::delegate_call_evm(CallFlags::empty(), address, gas, &[], None).unwrap();

api::get_storage(StorageFlags::empty(), &key, value).unwrap();
assert!(value[0] == 1u8);
}

75 changes: 75 additions & 0 deletions substrate/frame/revive/src/tests/pvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5536,3 +5536,78 @@ fn self_destruct_by_syscall_tracing_works() {
});
}
}

#[test]
fn delegate_call_with_gas_limit() {
let (caller_binary, _caller_code_hash) = compile_module("delegate_call_evm").unwrap();
let (callee_binary, _callee_code_hash) = compile_module("delegate_call_lib").unwrap();

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

let Contract { addr: caller_addr, .. } =
builder::bare_instantiate(Code::Upload(caller_binary))
.native_value(300_000)
.build_and_unwrap_contract();

let Contract { addr: callee_addr, .. } =
builder::bare_instantiate(Code::Upload(callee_binary))
.native_value(100_000)
.build_and_unwrap_contract();

// fails, not enough gas
assert_err!(
builder::bare_call(caller_addr)
.native_value(1337)
.data((callee_addr, 100u64).encode())
.build()
.result,
Error::<Test>::ContractTrapped,
);

assert_ok!(builder::call(caller_addr)
.value(1337)
.data((callee_addr, 100_000_000_000u64).encode())
.build());
});
}

#[test]
fn call_with_gas_limit() {
let (caller_binary, _caller_code_hash) = compile_module("call_with_gas").unwrap();
let (callee_binary, _callee_code_hash) = compile_module("dummy").unwrap();

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

let Contract { addr: caller_addr, .. } =
builder::bare_instantiate(Code::Upload(caller_binary))
.native_value(300_000)
.build_and_unwrap_contract();

let Contract { addr: callee_addr, .. } =
builder::bare_instantiate(Code::Upload(callee_binary))
.native_value(100_000)
.build_and_unwrap_contract();

// fails, not enough gas
assert_err!(
builder::bare_call(caller_addr)
.data((callee_addr, 1u64).encode())
.build()
.result,
Error::<Test>::ContractTrapped,
);

// succeeds, not enough gas but call stipend will be added
assert_ok!(builder::call(caller_addr)
.value(1337)
.data((callee_addr, 100u64).encode())
.build());

// succeeds, enough gas
assert_ok!(builder::call(caller_addr)
.data((callee_addr, 100_000_000_000u64).encode())
.build());
});
}
20 changes: 3 additions & 17 deletions substrate/frame/revive/src/vm/pvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,8 +631,7 @@ impl<'a, E: Ext, M: ?Sized + Memory<E::T>> Runtime<'a, E, M> {
flags: CallFlags,
call_type: CallType,
callee_ptr: u32,
deposit_ptr: u32,
weight: Weight,
resources: &CallResources<E::T>,
input_data_ptr: u32,
input_data_len: u32,
output_ptr: u32,
Expand All @@ -647,8 +646,6 @@ impl<'a, E: Ext, M: ?Sized + Memory<E::T>> Runtime<'a, E, M> {
None => self.charge_gas(call_type.cost())?,
};

let deposit_limit = memory.read_u256(deposit_ptr)?;

// we do check this in exec.rs but we want to error out early
if input_data_len > limits::CALLDATA_BYTES {
Err(<Error<E::T>>::CallDataTooLarge)?;
Expand Down Expand Up @@ -693,24 +690,13 @@ impl<'a, E: Ext, M: ?Sized + Memory<E::T>> Runtime<'a, E, M> {
ReentrancyProtection::Strict
};

self.ext.call(
&CallResources::from_weight_and_deposit(weight, deposit_limit),
&callee,
value,
input_data,
reentrancy,
read_only,
)
self.ext.call(resources, &callee, value, input_data, reentrancy, read_only)
},
CallType::DelegateCall => {
if flags.intersects(CallFlags::ALLOW_REENTRY | CallFlags::READ_ONLY) {
return Err(Error::<E::T>::InvalidCallFlags.into());
}
self.ext.delegate_call(
&CallResources::from_weight_and_deposit(weight, deposit_limit),
callee,
input_data,
)
self.ext.delegate_call(resources, callee, input_data)
},
};

Expand Down
85 changes: 81 additions & 4 deletions substrate/frame/revive/src/vm/pvm/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,55 @@ pub mod env {
let (deposit_ptr, value_ptr) = extract_hi_lo(deposit_and_value);
let (input_data_len, input_data_ptr) = extract_hi_lo(input_data);
let (output_len_ptr, output_ptr) = extract_hi_lo(output_data);
let weight = Weight::from_parts(ref_time_limit, proof_size_limit);

self.charge_gas(RuntimeCosts::CopyFromContract(32))?;
let deposit_limit = memory.read_u256(deposit_ptr)?;

self.call(
memory,
CallFlags::from_bits(flags).ok_or(Error::<E::T>::InvalidCallFlags)?,
CallType::Call { value_ptr },
callee_ptr,
deposit_ptr,
Weight::from_parts(ref_time_limit, proof_size_limit),
&CallResources::from_weight_and_deposit(weight, deposit_limit),
input_data_ptr,
input_data_len,
output_ptr,
output_len_ptr,
)
}

/// Make a call to another contract.
/// See [`pallet_revive_uapi::HostFn::call_evm`].
#[stable]
fn call_evm(
&mut self,
memory: &mut M,
flags: u32,
callee: u32,
value_ptr: u32,
gas: u64,
input_data: u64,
output_data: u64,
) -> Result<ReturnErrorCode, TrapReason> {
let (input_data_len, input_data_ptr) = extract_hi_lo(input_data);
let (output_len_ptr, output_ptr) = extract_hi_lo(output_data);
let resources = if gas == u64::MAX {
CallResources::NoLimits
} else {
self.charge_gas(RuntimeCosts::CopyFromContract(32))?;
let value = memory.read_u256(value_ptr)?;
// We also need to detect the 2300: We need to add something scaled.
let add_stipend = !value.is_zero() || gas == revm::interpreter::gas::CALL_STIPEND;
CallResources::from_ethereum_gas(gas.into(), add_stipend)
};

self.call(
memory,
CallFlags::from_bits(flags).ok_or(Error::<E::T>::InvalidCallFlags)?,
CallType::Call { value_ptr },
callee,
&resources,
input_data_ptr,
input_data_len,
output_ptr,
Expand All @@ -335,14 +376,50 @@ pub mod env {
let (flags, address_ptr) = extract_hi_lo(flags_and_callee);
let (input_data_len, input_data_ptr) = extract_hi_lo(input_data);
let (output_len_ptr, output_ptr) = extract_hi_lo(output_data);
let weight = Weight::from_parts(ref_time_limit, proof_size_limit);

self.charge_gas(RuntimeCosts::CopyFromContract(32))?;
let deposit_limit = memory.read_u256(deposit_ptr)?;

self.call(
memory,
CallFlags::from_bits(flags).ok_or(Error::<E::T>::InvalidCallFlags)?,
CallType::DelegateCall,
address_ptr,
deposit_ptr,
Weight::from_parts(ref_time_limit, proof_size_limit),
&CallResources::from_weight_and_deposit(weight, deposit_limit),
input_data_ptr,
input_data_len,
output_ptr,
output_len_ptr,
)
}

/// Same as `delegate_call` but with EVM gas.
/// See [`pallet_revive_uapi::HostFn::delegate_call_evm`].
#[stable]
fn delegate_call_evm(
&mut self,
memory: &mut M,
flags: u32,
callee: u32,
gas: u64,
input_data: u64,
output_data: u64,
) -> Result<ReturnErrorCode, TrapReason> {
let (input_data_len, input_data_ptr) = extract_hi_lo(input_data);
let (output_len_ptr, output_ptr) = extract_hi_lo(output_data);
let resources = if gas == u64::MAX {
CallResources::NoLimits
} else {
CallResources::from_ethereum_gas(gas.into(), false)
};

self.call(
memory,
CallFlags::from_bits(flags).ok_or(Error::<E::T>::InvalidCallFlags)?,
CallType::DelegateCall,
callee,
&resources,
input_data_ptr,
input_data_len,
output_ptr,
Expand Down
25 changes: 25 additions & 0 deletions substrate/frame/revive/uapi/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,20 @@ pub trait HostFn: private::Sealed {
output: Option<&mut &mut [u8]>,
) -> Result;

/// Same as [HostFn::call] but receives the one-dimensional EVM gas argument.
///
/// Adds the EVM gas stipend for non-zero value calls.
///
/// If gas is `u64::MAX`, the call will run with uncapped limits.
fn call_evm(
flags: CallFlags,
callee: &[u8; 20],
gas: u64,
value: &[u8; 32],
input_data: &[u8],
output: Option<&mut &mut [u8]>,
) -> Result;

/// Stores the address of the caller into the supplied buffer.
///
/// If this is a top-level call (i.e. initiated by an extrinsic) the origin address of the
Expand Down Expand Up @@ -207,6 +221,17 @@ pub trait HostFn: private::Sealed {
output: Option<&mut &mut [u8]>,
) -> Result;

/// Same as [HostFn::delegate_call] but receives the one-dimensional EVM gas argument.
///
/// If gas is `u64::MAX`, the call will run with uncapped limits.
fn delegate_call_evm(
flags: CallFlags,
address: &[u8; 20],
gas: u64,
input_data: &[u8],
output: Option<&mut &mut [u8]>,
) -> Result;

/// Deposit a contract event with the data buffer and optional list of topics. There is a limit
/// on the maximum number of topics specified by `event_topics`.
///
Expand Down
Loading
Loading