This repository has been 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.6k
Use Storage for Tracking Transactional Layers #10744
Closed
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b3b8d3b
use storage for tracking layers
shawntabrizi be58fe0
propagate the limit error
shawntabrizi 5ab450d
put check before creating layer
shawntabrizi baa34d5
improve tests for storage reversion
shawntabrizi b686b8c
update macro
shawntabrizi bd45a95
get rid of TransactionalOutcome
shawntabrizi b1c741f
some renaming
shawntabrizi 6ce2dff
more rename
shawntabrizi 12a9067
fix
shawntabrizi fb5448b
fmt
shawntabrizi f9c8fd4
fix
shawntabrizi 5ae29f7
better partial eq impl
shawntabrizi 9f0ccd5
Revert "better partial eq impl"
shawntabrizi 9548a12
fixes
shawntabrizi f479b5c
add back for now
shawntabrizi 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 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
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,103 @@ | ||
// This file is part of Substrate. | ||
|
||
// Copyright (C) 2020-2022 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. | ||
|
||
use frame_support_procedural_tools::generate_crate_access_2018; | ||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
use syn::{parse::Parse, ItemFn, LitInt, Result}; | ||
|
||
struct StorageLayerLimit { | ||
limit: u8, | ||
} | ||
|
||
impl Default for StorageLayerLimit { | ||
fn default() -> Self { | ||
Self { limit: 1 } | ||
} | ||
} | ||
|
||
impl Parse for StorageLayerLimit { | ||
fn parse(input: syn::parse::ParseStream) -> Result<Self> { | ||
let limit: LitInt = input.parse()?; | ||
Ok(Self { limit: limit.base10_parse()? }) | ||
} | ||
} | ||
|
||
pub fn add_storage_layer(attr: TokenStream, input: TokenStream) -> Result<TokenStream> { | ||
let limit: StorageLayerLimit = syn::parse(attr).unwrap_or_default(); | ||
let limit = limit.limit; | ||
|
||
let ItemFn { attrs, vis, sig, block } = syn::parse(input)?; | ||
|
||
let crate_ = generate_crate_access_2018("frame-support")?; | ||
let output = quote! { | ||
#(#attrs)* | ||
#vis #sig { | ||
use #crate_::storage::execute_with_storage_layer; | ||
// Otherwise, spawn a transaction layer. | ||
execute_with_storage_layer(#limit, || { | ||
(|| { #block })() | ||
}) | ||
} | ||
}; | ||
|
||
Ok(output.into()) | ||
} | ||
|
||
// Similar to `add_storage_layer` but only spawns at most 1 layer. | ||
pub fn with_storage_layer(attr: TokenStream, input: TokenStream) -> Result<TokenStream> { | ||
let limit: StorageLayerLimit = syn::parse(attr).unwrap_or_default(); | ||
let limit = limit.limit; | ||
|
||
let ItemFn { attrs, vis, sig, block } = syn::parse(input)?; | ||
|
||
let crate_ = generate_crate_access_2018("frame-support")?; | ||
let output = quote! { | ||
#(#attrs)* | ||
#vis #sig { | ||
use #crate_::storage::{execute_with_storage_layer, has_storage_layer}; | ||
if has_storage_layer() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this prevents recursive storage layers? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would be an entirely different approach to storage layers, where rather than spawning new transactional layers per thing, we would just spawn at most one. This would be the thing we would probably use by default across all pallet extrinsics. |
||
// We are already in a transaction layer, just execute the block. | ||
(|| { #block })() | ||
} else { | ||
// Otherwise, spawn a transaction layer. | ||
execute_with_storage_layer(#limit, || { | ||
(|| { #block })() | ||
}) | ||
} | ||
} | ||
}; | ||
|
||
Ok(output.into()) | ||
} | ||
|
||
pub fn require_storage_layer(_attr: TokenStream, input: TokenStream) -> Result<TokenStream> { | ||
let ItemFn { attrs, vis, sig, block } = syn::parse(input)?; | ||
|
||
let crate_ = generate_crate_access_2018("frame-support")?; | ||
let output = quote! { | ||
#(#attrs)* | ||
#vis #sig { | ||
if !#crate_::storage::has_storage_layer(){ | ||
return Err(#crate_::pallet_prelude::DispatchError::StorageLayersLimit.into()) | ||
} | ||
#block | ||
} | ||
}; | ||
|
||
Ok(output.into()) | ||
} |
This file was deleted.
Oops, something went wrong.
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this not a tuple struct?