-
Notifications
You must be signed in to change notification settings - Fork 709
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pallet_contracts] Add support for transient storage in contracts hos…
…t functions (#4566) Introduce transient storage, which behaves identically to regular storage but is kept only in memory and discarded after every transaction. This functionality is similar to the `TSTORE` and `TLOAD` operations used in Ethereum. The following new host functions have been introduced: `get_transient_storage` `set_transient_storage` `take_transient_storage` `clear_transient_storage` `contains_transient_storage` Note: These functions are declared as `unstable` and thus are not activated. --------- Co-authored-by: command-bot <> Co-authored-by: PG Herveou <[email protected]> Co-authored-by: Alexander Theißen <[email protected]>
- Loading branch information
1 parent
cde2eb4
commit eac2a22
Showing
19 changed files
with
2,943 additions
and
396 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
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,23 @@ | ||
# 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 support for transient storage in contracts host functions" | ||
|
||
doc: | ||
- audience: Runtime User | ||
description: | | ||
This PR implements transient storage, which behaves identically to regular storage | ||
but is kept only in memory and discarded after every transaction. | ||
This functionality is similar to the `TSTORE` and `TLOAD` operations used in Ethereum. | ||
The following new host functions have been introduced: `get_transient_storage`, | ||
`set_transient_storage`, `take_transient_storage`, `clear_transient_storage` and | ||
`contains_transient_storage`. | ||
These functions are declared as unstable and thus are not activated. | ||
|
||
crates: | ||
- name: pallet-contracts | ||
bump: major | ||
- name: pallet-contracts-uapi | ||
bump: major | ||
- name: contracts-rococo-runtime | ||
bump: minor |
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
56 changes: 56 additions & 0 deletions
56
substrate/frame/contracts/fixtures/contracts/create_transient_storage_and_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,56 @@ | ||
// 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 calls another contract as passed as its account id. It also creates some transient storage. | ||
#![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!( | ||
buffer, | ||
len: u32, | ||
input: [u8; 4], | ||
callee: [u8; 32], | ||
); | ||
|
||
let data = [0u8; 16 * 1024]; | ||
let value = &data[..len as usize]; | ||
#[allow(deprecated)] | ||
api::set_transient_storage(buffer, value); | ||
|
||
// Call the callee | ||
api::call_v2( | ||
uapi::CallFlags::empty(), | ||
callee, | ||
0u64, // How much ref_time weight to devote for the execution. 0 = all. | ||
0u64, // How much proof_size weight to devote for the execution. 0 = all. | ||
None, | ||
&0u64.to_le_bytes(), // Value transferred to the contract. | ||
input, | ||
None, | ||
) | ||
.unwrap(); | ||
} |
42 changes: 42 additions & 0 deletions
42
substrate/frame/contracts/fixtures/contracts/set_transient_storage.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,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] | ||
|
||
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!(len: u32, ); | ||
|
||
let buffer = [0u8; 16 * 1024]; | ||
let data = &buffer[..len as usize]; | ||
|
||
// Place a garbage value in the transient storage, with the size specified by the call input. | ||
let mut key = [0u8; 32]; | ||
key[0] = 1; | ||
|
||
#[allow(deprecated)] | ||
api::set_transient_storage(&key, data); | ||
} |
58 changes: 58 additions & 0 deletions
58
substrate/frame/contracts/fixtures/contracts/transient_storage.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,58 @@ | ||
// 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 contract tests the transient storage APIs. | ||
#![no_std] | ||
#![no_main] | ||
|
||
use common::unwrap_output; | ||
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() { | ||
const KEY: [u8; 32] = [1u8; 32]; | ||
const VALUE_1: [u8; 4] = [1u8; 4]; | ||
const VALUE_2: [u8; 4] = [2u8; 4]; | ||
const VALUE_3: [u8; 4] = [3u8; 4]; | ||
|
||
#[allow(deprecated)] | ||
{ | ||
let existing = api::set_transient_storage(&KEY, &VALUE_1); | ||
assert_eq!(existing, None); | ||
assert_eq!(api::contains_transient_storage(&KEY), Some(VALUE_1.len() as _)); | ||
unwrap_output!(val, [0u8; 4], api::get_transient_storage, &KEY); | ||
assert_eq!(**val, VALUE_1); | ||
|
||
let existing = api::set_transient_storage(&KEY, &VALUE_2); | ||
assert_eq!(existing, Some(VALUE_1.len() as _)); | ||
unwrap_output!(val, [0u8; 4], api::get_transient_storage, &KEY); | ||
assert_eq!(**val, VALUE_2); | ||
|
||
api::clear_transient_storage(&KEY); | ||
assert_eq!(api::contains_transient_storage(&KEY), None); | ||
|
||
let existing = api::set_transient_storage(&KEY, &VALUE_3); | ||
assert_eq!(existing, None); | ||
unwrap_output!(val, [0u8; 32], api::take_transient_storage, &KEY); | ||
assert_eq!(**val, VALUE_3); | ||
} | ||
} |
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.