-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[pallet_contracts] Add support for transient storage in contracts host functions #4566
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
70 commits
Select commit
Hold shift + click to select a range
1732628
Add transient storage
smiasojed 6cda150
Add unit tests
smiasojed 6670170
Merge remote-tracking branch 'origin/master' into sm/contracts-tstore
smiasojed 826e073
Add storage limit
smiasojed daf9ad1
Limit storage per contract
smiasojed d4a29be
Refactored
smiasojed 53ec270
Add terminate function to transient storage
smiasojed 33f6c2d
Add unit tests
smiasojed 199edaa
Refactored to single BtreeMap
smiasojed e683b72
Add comments
smiasojed 1c3c844
Cleanup
smiasojed 18cd9ad
Fix unit tests
smiasojed d5736d5
Merge remote-tracking branch 'origin/master' into sm/contracts-tstore
smiasojed 58d87dc
Fix tests after master merge
smiasojed 29f1305
Rename storage err
smiasojed 63aaaaf
Benchmarking test
smiasojed 78960e0
Fix unit tests
smiasojed f09b80b
Merge branch 'master' of https://github.com/paritytech/polkadot-sdk i…
6d63e58
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
6f713ae
Max storage elements set to 2000
smiasojed d6e1c43
Merge branch 'master' of https://github.com/paritytech/polkadot-sdk i…
8c11871
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
664221b
Storage len 50
smiasojed d579005
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
6667429
Merge remote-tracking branch 'origin/master' into sm/contracts-tstore
smiasojed 8d91109
Merge remote-tracking branch 'origin/master' into sm/contracts-tstore
smiasojed 5794910
Change transient storage meter
smiasojed da8316e
Fix benchmark warnings
smiasojed ac65687
Merge branch 'master' of https://github.com/paritytech/polkadot-sdk i…
d8298f0
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
339e935
Refactor benchmarks
smiasojed 735faae
Refactor transient storage
smiasojed 51fb39c
Add test benchmarks
smiasojed 2f3abc4
Merge branch 'master' of https://github.com/paritytech/polkadot-sdk i…
b2c7005
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
01365cf
Refactored benchmarks
smiasojed a2dfb23
Add wasm tests
smiasojed ca1a323
Add E2E tests
smiasojed 399d541
Merge branch 'master' of https://github.com/paritytech/polkadot-sdk i…
6d8dc05
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
1ed42f4
Add docs
smiasojed 3ec4626
Fix tests
smiasojed 2d3a823
Fix contracts-rococo configuration
smiasojed 09ff908
Fmt
smiasojed 5fa1b1b
Update PR doc
smiasojed f6aeb87
Cleanup
smiasojed 4863431
Remove transient storage limit in benchmark
smiasojed baa943f
Cleanup
smiasojed 7257857
Merge remote-tracking branch 'origin/master' into sm/contracts-tstore
smiasojed bcde2de
Fix description
smiasojed 9a204bd
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
037f0e6
Update substrate/frame/contracts/src/exec.rs
smiasojed ea62ece
Update substrate/frame/contracts/src/exec.rs
smiasojed 0888164
Update substrate/frame/contracts/src/transient_storage.rs
smiasojed 176bbc5
Update substrate/frame/contracts/src/transient_storage.rs
smiasojed fb8f0ad
Change fn visibility and code cleanup
smiasojed 4b148b9
Update tests
smiasojed 057fd0d
Update tests
smiasojed 6acc50f
Remove double key creation
smiasojed b9aa452
Update panic messages
smiasojed 2bb933e
Update substrate/frame/contracts/src/lib.rs
smiasojed 0c189a6
Cleanup benchmarks
smiasojed 865886d
Improve panic messages
smiasojed 03f10f9
Marked functions as unstable
smiasojed 5c59336
Sealed HostFn trait
smiasojed 8d36584
Refactor benchmarks
smiasojed 4db9c95
Merge branch 'master' of https://github.com/paritytech/polkadot-sdk i…
f2cfb76
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
3a8c2d2
Merge remote-tracking branch 'origin/master' into sm/contracts-tstore
smiasojed 45603b8
Comment added
smiasojed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.