This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Implement nested storage transactions #6269
Merged
Merged
Changes from 44 commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
5b3d0b5
Add transactional storage functionality to OverlayChanges
athei 68e34f0
Add storage transactions runtime interface
athei 0a70191
Add frame support for transactions
athei 0e32850
Merge branch 'master' into at-storage-tx
athei c767eea
Fix committed typo
athei c48d417
Rename 'changes' variable to 'overlay'
athei 98a89da
Fix renaming change
athei 3fb2e05
Fixed strange line break
athei 1acf7a4
Rename clear to clear_where
athei d4440a4
Add comment regarding delete value on mutation
athei 14738f8
Merge remote-tracking branch 'origin/master' into at-storage-tx
athei 3592246
Add comment which changes are covered by a transaction
athei 7395a03
Do force the arg to with_transaction return a Result
athei 81e5241
Use rust doc comments on every documentable place
athei ff52101
Fix wording of insert_diry doc
athei d789aab
Improve doc on start_transaction
athei 5e65240
Rename value to overlayed in close_transaction
athei 7eb9065
Inline negation
athei 5dd6a85
Improve wording of close_transaction comments
athei 1c8e7c1
Get rid of an expect by using get_or_insert_with
athei 232b68f
Remove trailing whitespace
athei 00b9ff3
Rename should to expected in tests
athei 85617a5
Rolling back a transaction must mark the overlay as dirty
athei 04471b4
Merge remote-tracking branch 'origin/master' into at-storage-tx
athei 4e039a1
Protect client initiated storage tx from being droped by runtime
athei 52888f2
Merge remote-tracking branch 'origin/master' into at-storage-tx
athei db24a2b
Merge remote-tracking branch 'origin/master' into at-storage-tx
athei 8ee3696
Review nits
athei 843c84a
Return Err when entering or exiting runtime fails
athei 5c851f6
Merge remote-tracking branch 'origin/master' into at-storage-tx
athei c60a4c1
Documentation fixup
athei 1ad2077
Remove close type
athei de8af5b
Move enter/exit runtime to excute_aux in the state-machine
athei 552f611
Merge remote-tracking branch 'origin/master' into at-storage-tx
athei 4cf20c9
Rename Discard -> Rollback
athei 10022f6
Move child changeset creation to constructor
athei 5c0c35f
Move child spawning into the closure
athei 8db272c
Merge remote-tracking branch 'origin/master' into at-storage-tx
athei 76ce5cf
Apply suggestions from code review
athei 3d0f162
Fixup for code suggestion
athei 1651f68
Unify re-exports
athei 64f3c90
Rename overlay_changes to mod.rs and move into subdir
athei 1568984
Change proof wording
athei 96b6fd6
Merge branch 'master' into at-storage-tx
athei 5878df6
Merge branch 'master' into at-storage-tx
athei 6a8b3a7
Adapt a new test from master to storage-tx
athei 46a56dc
Suggestions from the latest round of review
athei 1d72286
Merge remote-tracking branch 'origin/master' into at-storage-tx
athei 5145916
Fix warning message
athei 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,159 @@ | ||
| // This file is part of Substrate. | ||
|
|
||
| // Copyright (C) 2019-2020 Parity Technologies (UK) Ltd. | ||
athei marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // 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. | ||
|
|
||
| use codec::{Encode, Decode, EncodeLike}; | ||
| use frame_support::{ | ||
| StorageMap, StorageValue, storage::{with_transaction, TransactionOutcome::*}, | ||
| }; | ||
| use sp_io::TestExternalities; | ||
|
|
||
| pub trait Trait { | ||
| type Origin; | ||
| type BlockNumber: Encode + Decode + EncodeLike + Default + Clone; | ||
| } | ||
|
|
||
| frame_support::decl_module! { | ||
| pub struct Module<T: Trait> for enum Call where origin: T::Origin {} | ||
| } | ||
|
|
||
| frame_support::decl_storage!{ | ||
| trait Store for Module<T: Trait> as StorageTransactions { | ||
| pub Value: u32; | ||
| pub Map: map hasher(twox_64_concat) String => u32; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| #[test] | ||
| fn storage_transaction_basic_commit() { | ||
| TestExternalities::default().execute_with(|| { | ||
|
|
||
| assert_eq!(Value::get(), 0); | ||
| assert!(!Map::contains_key("val0")); | ||
|
|
||
| with_transaction(|| { | ||
| Value::set(99); | ||
| Map::insert("val0", 99); | ||
| assert_eq!(Value::get(), 99); | ||
| assert_eq!(Map::get("val0"), 99); | ||
| ((), Commit) | ||
| }); | ||
|
|
||
| assert_eq!(Value::get(), 99); | ||
| assert_eq!(Map::get("val0"), 99); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn storage_transaction_basic_rollback() { | ||
| TestExternalities::default().execute_with(|| { | ||
|
|
||
| assert_eq!(Value::get(), 0); | ||
| assert_eq!(Map::get("val0"), 0); | ||
|
|
||
| with_transaction(|| { | ||
| Value::set(99); | ||
| Map::insert("val0", 99); | ||
| assert_eq!(Value::get(), 99); | ||
| assert_eq!(Map::get("val0"), 99); | ||
| ((), Rollback) | ||
| }); | ||
|
|
||
| assert_eq!(Value::get(), 0); | ||
| assert_eq!(Map::get("val0"), 0); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn storage_transaction_rollback_then_commit() { | ||
| TestExternalities::default().execute_with(|| { | ||
| Value::set(1); | ||
| Map::insert("val1", 1); | ||
|
|
||
| with_transaction(|| { | ||
| Value::set(2); | ||
| Map::insert("val1", 2); | ||
| Map::insert("val2", 2); | ||
|
|
||
| with_transaction(|| { | ||
| Value::set(3); | ||
| Map::insert("val1", 3); | ||
| Map::insert("val2", 3); | ||
| Map::insert("val3", 3); | ||
|
|
||
| assert_eq!(Value::get(), 3); | ||
| assert_eq!(Map::get("val1"), 3); | ||
| assert_eq!(Map::get("val2"), 3); | ||
| assert_eq!(Map::get("val3"), 3); | ||
|
|
||
| ((), Rollback) | ||
| }); | ||
|
|
||
| assert_eq!(Value::get(), 2); | ||
| assert_eq!(Map::get("val1"), 2); | ||
| assert_eq!(Map::get("val2"), 2); | ||
| assert_eq!(Map::get("val3"), 0); | ||
|
|
||
| ((), Commit) | ||
| }); | ||
|
|
||
| assert_eq!(Value::get(), 2); | ||
| assert_eq!(Map::get("val1"), 2); | ||
| assert_eq!(Map::get("val2"), 2); | ||
| assert_eq!(Map::get("val3"), 0); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn storage_transaction_commit_then_rollback() { | ||
| TestExternalities::default().execute_with(|| { | ||
| Value::set(1); | ||
| Map::insert("val1", 1); | ||
|
|
||
| with_transaction(|| { | ||
| Value::set(2); | ||
| Map::insert("val1", 2); | ||
| Map::insert("val2", 2); | ||
|
|
||
| with_transaction(|| { | ||
| Value::set(3); | ||
| Map::insert("val1", 3); | ||
| Map::insert("val2", 3); | ||
| Map::insert("val3", 3); | ||
|
|
||
| assert_eq!(Value::get(), 3); | ||
| assert_eq!(Map::get("val1"), 3); | ||
| assert_eq!(Map::get("val2"), 3); | ||
| assert_eq!(Map::get("val3"), 3); | ||
|
|
||
| ((), Commit) | ||
| }); | ||
|
|
||
| assert_eq!(Value::get(), 3); | ||
| assert_eq!(Map::get("val1"), 3); | ||
| assert_eq!(Map::get("val2"), 3); | ||
| assert_eq!(Map::get("val3"), 3); | ||
|
|
||
| ((), Rollback) | ||
| }); | ||
|
|
||
| assert_eq!(Value::get(), 1); | ||
| assert_eq!(Map::get("val1"), 1); | ||
| assert_eq!(Map::get("val2"), 0); | ||
| assert_eq!(Map::get("val3"), 0); | ||
| }); | ||
| } | ||
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
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.