Skip to content

Commit

Permalink
[pallet-revive] Update delegate_call to accept address and weight (#6111
Browse files Browse the repository at this point in the history
)

Enhance the `delegate_call` function to accept an `address` target
parameter instead of a `code_hash`. This allows direct identification of
the target contract using the provided address.
Additionally, introduce parameters for specifying a customizable
`ref_time` limit and `proof_size` limit, thereby improving flexibility
and control during contract interactions.

---------

Co-authored-by: Alexander Theißen <[email protected]>
  • Loading branch information
ermalkaleci and athei authored Nov 19, 2024
1 parent 5e8348f commit 293d4a5
Show file tree
Hide file tree
Showing 11 changed files with 391 additions and 112 deletions.
17 changes: 17 additions & 0 deletions prdoc/pr_6111.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
title: "[pallet-revive] Update delegate_call to accept address and weight"

doc:
- audience: Runtime Dev
description: |
Enhance the `delegate_call` function to accept an `address` target parameter instead of a `code_hash`.
This allows direct identification of the target contract using the provided address.
Additionally, introduce parameters for specifying a customizable `ref_time` limit and `proof_size` limit,
thereby improving flexibility and control during contract interactions.

crates:
- name: pallet-revive
bump: major
- name: pallet-revive-fixtures
bump: patch
- name: pallet-revive-uapi
bump: major
8 changes: 6 additions & 2 deletions substrate/frame/revive/fixtures/contracts/delegate_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ pub extern "C" fn deploy() {}
#[no_mangle]
#[polkavm_derive::polkavm_export]
pub extern "C" fn call() {
input!(code_hash: &[u8; 32],);
input!(
address: &[u8; 20],
ref_time: u64,
proof_size: u64,
);

let mut key = [0u8; 32];
key[0] = 1u8;
Expand All @@ -42,7 +46,7 @@ pub extern "C" fn call() {
assert!(value[0] == 2u8);

let input = [0u8; 0];
api::delegate_call(uapi::CallFlags::empty(), code_hash, &input, None).unwrap();
api::delegate_call(uapi::CallFlags::empty(), address, ref_time, proof_size, None, &input, None).unwrap();

api::get_storage(StorageFlags::empty(), &key, value).unwrap();
assert!(value[0] == 1u8);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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]

use common::{input, u256_bytes};
use uapi::{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],
deposit_limit: u64,
);

let input = [0u8; 0];
api::delegate_call(uapi::CallFlags::empty(), address, 0, 0, Some(&u256_bytes(deposit_limit)), &input, None).unwrap();

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

let mut value = [0u8; 32];

api::get_storage(StorageFlags::empty(), &key, &mut &mut value[..]).unwrap();
assert!(value[0] == 1u8);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ pub extern "C" fn deploy() {}
#[no_mangle]
#[polkavm_derive::polkavm_export]
pub extern "C" fn call() {
input!(code_hash: &[u8; 32],);
input!(address: &[u8; 20],);

// Delegate call into passed code hash.
// Delegate call into passed address.
let input = [0u8; 0];
api::delegate_call(uapi::CallFlags::empty(), code_hash, &input, None).unwrap();
api::delegate_call(uapi::CallFlags::empty(), address, 0, 0, None, &input, None).unwrap();
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const ALICE_FALLBACK: [u8; 20] = [1u8; 20];
fn load_input(delegate_call: bool) {
input!(
action: u32,
address: &[u8; 20],
code_hash: &[u8; 32],
);

Expand All @@ -51,7 +52,7 @@ fn load_input(delegate_call: bool) {
}

if delegate_call {
api::delegate_call(uapi::CallFlags::empty(), code_hash, &[], None).unwrap();
api::delegate_call(uapi::CallFlags::empty(), address, 0, 0, None, &[], None).unwrap();
}
}

Expand Down
25 changes: 18 additions & 7 deletions substrate/frame/revive/src/benchmarking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1555,25 +1555,36 @@ mod benchmarks {

#[benchmark(pov_mode = Measured)]
fn seal_delegate_call() -> Result<(), BenchmarkError> {
let hash = Contract::<T>::with_index(1, WasmModule::dummy(), vec![])?.info()?.code_hash;
let Contract { account_id: address, .. } =
Contract::<T>::with_index(1, WasmModule::dummy(), vec![]).unwrap();

let address_bytes = address.encode();
let address_len = address_bytes.len() as u32;

let deposit: BalanceOf<T> = (u32::MAX - 100).into();
let deposit_bytes = Into::<U256>::into(deposit).encode();

let mut setup = CallSetup::<T>::default();
setup.set_storage_deposit_limit(deposit);
setup.set_origin(Origin::from_account_id(setup.contract().account_id.clone()));

let (mut ext, _) = setup.ext();
let mut runtime = crate::wasm::Runtime::<_, [u8]>::new(&mut ext, vec![]);
let mut memory = memory!(hash.encode(),);
let mut memory = memory!(address_bytes, deposit_bytes,);

let result;
#[block]
{
result = runtime.bench_delegate_call(
memory.as_mut_slice(),
0, // flags
0, // code_hash_ptr
0, // input_data_ptr
0, // input_data_len
SENTINEL, // output_ptr
0, // flags
0, // address_ptr
0, // ref_time_limit
0, // proof_size_limit
address_len, // deposit_ptr
0, // input_data_ptr
0, // input_data_len
SENTINEL, // output_ptr
0,
);
}
Expand Down
Loading

0 comments on commit 293d4a5

Please sign in to comment.