Skip to content

Commit bb4ee10

Browse files
ermalkaleciathei
authored andcommitted
[pallet-revive] Update delegate_call to accept address and weight (paritytech#6111)
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]>
1 parent 617bccc commit bb4ee10

11 files changed

Lines changed: 391 additions & 112 deletions

File tree

prdoc/pr_6111.prdoc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
title: "[pallet-revive] Update delegate_call to accept address and weight"
2+
3+
doc:
4+
- audience: Runtime Dev
5+
description: |
6+
Enhance the `delegate_call` function to accept an `address` target parameter instead of a `code_hash`.
7+
This allows direct identification of the target contract using the provided address.
8+
Additionally, introduce parameters for specifying a customizable `ref_time` limit and `proof_size` limit,
9+
thereby improving flexibility and control during contract interactions.
10+
11+
crates:
12+
- name: pallet-revive
13+
bump: major
14+
- name: pallet-revive-fixtures
15+
bump: patch
16+
- name: pallet-revive-uapi
17+
bump: major

substrate/frame/revive/fixtures/contracts/delegate_call.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ pub extern "C" fn deploy() {}
2828
#[no_mangle]
2929
#[polkavm_derive::polkavm_export]
3030
pub extern "C" fn call() {
31-
input!(code_hash: &[u8; 32],);
31+
input!(
32+
address: &[u8; 20],
33+
ref_time: u64,
34+
proof_size: u64,
35+
);
3236

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

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

4751
api::get_storage(StorageFlags::empty(), &key, value).unwrap();
4852
assert!(value[0] == 1u8);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// This file is part of Substrate.
2+
3+
// Copyright (C) Parity Technologies (UK) Ltd.
4+
// SPDX-License-Identifier: Apache-2.0
5+
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
#![no_std]
19+
#![no_main]
20+
21+
use common::{input, u256_bytes};
22+
use uapi::{HostFn, HostFnImpl as api, StorageFlags};
23+
24+
#[no_mangle]
25+
#[polkavm_derive::polkavm_export]
26+
pub extern "C" fn deploy() {}
27+
28+
#[no_mangle]
29+
#[polkavm_derive::polkavm_export]
30+
pub extern "C" fn call() {
31+
input!(
32+
address: &[u8; 20],
33+
deposit_limit: u64,
34+
);
35+
36+
let input = [0u8; 0];
37+
api::delegate_call(uapi::CallFlags::empty(), address, 0, 0, Some(&u256_bytes(deposit_limit)), &input, None).unwrap();
38+
39+
let mut key = [0u8; 32];
40+
key[0] = 1u8;
41+
42+
let mut value = [0u8; 32];
43+
44+
api::get_storage(StorageFlags::empty(), &key, &mut &mut value[..]).unwrap();
45+
assert!(value[0] == 1u8);
46+
}

substrate/frame/revive/fixtures/contracts/delegate_call_simple.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ pub extern "C" fn deploy() {}
2828
#[no_mangle]
2929
#[polkavm_derive::polkavm_export]
3030
pub extern "C" fn call() {
31-
input!(code_hash: &[u8; 32],);
31+
input!(address: &[u8; 20],);
3232

33-
// Delegate call into passed code hash.
33+
// Delegate call into passed address.
3434
let input = [0u8; 0];
35-
api::delegate_call(uapi::CallFlags::empty(), code_hash, &input, None).unwrap();
35+
api::delegate_call(uapi::CallFlags::empty(), address, 0, 0, None, &input, None).unwrap();
3636
}

substrate/frame/revive/fixtures/contracts/locking_delegate_dependency.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const ALICE_FALLBACK: [u8; 20] = [1u8; 20];
3030
fn load_input(delegate_call: bool) {
3131
input!(
3232
action: u32,
33+
address: &[u8; 20],
3334
code_hash: &[u8; 32],
3435
);
3536

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

5354
if delegate_call {
54-
api::delegate_call(uapi::CallFlags::empty(), code_hash, &[], None).unwrap();
55+
api::delegate_call(uapi::CallFlags::empty(), address, 0, 0, None, &[], None).unwrap();
5556
}
5657
}
5758

substrate/frame/revive/src/benchmarking/mod.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,25 +1555,36 @@ mod benchmarks {
15551555

15561556
#[benchmark(pov_mode = Measured)]
15571557
fn seal_delegate_call() -> Result<(), BenchmarkError> {
1558-
let hash = Contract::<T>::with_index(1, WasmModule::dummy(), vec![])?.info()?.code_hash;
1558+
let Contract { account_id: address, .. } =
1559+
Contract::<T>::with_index(1, WasmModule::dummy(), vec![]).unwrap();
1560+
1561+
let address_bytes = address.encode();
1562+
let address_len = address_bytes.len() as u32;
1563+
1564+
let deposit: BalanceOf<T> = (u32::MAX - 100).into();
1565+
let deposit_bytes = Into::<U256>::into(deposit).encode();
15591566

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

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

15671575
let result;
15681576
#[block]
15691577
{
15701578
result = runtime.bench_delegate_call(
15711579
memory.as_mut_slice(),
1572-
0, // flags
1573-
0, // code_hash_ptr
1574-
0, // input_data_ptr
1575-
0, // input_data_len
1576-
SENTINEL, // output_ptr
1580+
0, // flags
1581+
0, // address_ptr
1582+
0, // ref_time_limit
1583+
0, // proof_size_limit
1584+
address_len, // deposit_ptr
1585+
0, // input_data_ptr
1586+
0, // input_data_len
1587+
SENTINEL, // output_ptr
15771588
0,
15781589
);
15791590
}

0 commit comments

Comments
 (0)