-
Notifications
You must be signed in to change notification settings - Fork 745
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add READ_ONLY flag to contract call function (#4418)
This PR implements the `READ_ONLY` flag to be used as a `Callflag` in the `call` function. The flag indicates that the callee is restricted from modifying the state during call execution. It is equivalent to Ethereum's [STATICCALL](https://eips.ethereum.org/EIPS/eip-214). --------- Co-authored-by: command-bot <> Co-authored-by: Andrew Jones <ascjones@gmail.com> Co-authored-by: Alexander Theißen <alex.theissen@me.com>
- Loading branch information
1 parent
09de7f1
commit 3e84164
Showing
12 changed files
with
808 additions
and
457 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0 | ||
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json | ||
|
||
title: "[pallet_contracts] Add `READ_ONLY` flag to contract `call` function" | ||
|
||
doc: | ||
- audience: Runtime User | ||
description: | | ||
This PR implements the `READ_ONLY` flag to be used as a `Callflag` in the contract `call` function. | ||
The flag indicates that the callee is restricted from modifying the state during call execution. | ||
It is equivalent to Ethereum's [STATICCALL](https://eips.ethereum.org/EIPS/eip-214). | ||
|
||
crates: | ||
- name: pallet-contracts | ||
bump: minor | ||
- name: pallet-contracts-uapi | ||
bump: minor | ||
- name: pallet-contracts-proc-macro | ||
bump: minor |
51 changes: 51 additions & 0 deletions
51
substrate/frame/contracts/fixtures/contracts/call_with_flags_and_value.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// 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. | ||
|
||
//! This fixture calls the account_id with the flags and value. | ||
#![no_std] | ||
#![no_main] | ||
|
||
use common::input; | ||
use uapi::{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; 32], | ||
flags: u32, | ||
value: u64, | ||
forwarded_input: [u8], | ||
); | ||
|
||
api::call_v2( | ||
uapi::CallFlags::from_bits(flags).unwrap(), | ||
callee_addr, | ||
0u64, // How much ref_time to devote for the execution. 0 = all. | ||
0u64, // How much proof_size to devote for the execution. 0 = all. | ||
None, // No deposit limit. | ||
&value.to_le_bytes(), // Value transferred to the contract. | ||
forwarded_input, | ||
None, | ||
) | ||
.unwrap(); | ||
} |
50 changes: 50 additions & 0 deletions
50
substrate/frame/contracts/fixtures/contracts/read_only_call.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// 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. | ||
|
||
// This fixture tests if read-only call works as expected. | ||
#![no_std] | ||
#![no_main] | ||
|
||
use common::input; | ||
use uapi::{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; 32], | ||
callee_input: [u8], | ||
); | ||
|
||
// Call the callee | ||
api::call_v2( | ||
uapi::CallFlags::READ_ONLY, | ||
callee_addr, | ||
0u64, // How much ref_time to devote for the execution. 0 = all. | ||
0u64, // How much proof_size to devote for the execution. 0 = all. | ||
None, // No deposit limit. | ||
&0u64.to_le_bytes(), // Value transferred to the contract. | ||
callee_input, | ||
None, | ||
) | ||
.unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.