mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-14 01:41:09 +00:00
Storage Layer for All FRAME Extrinsics (#11431)
* add new trait * implement DispatchableWithStorageLayer * at least one transactional * all dispatch is at least transactional * storage_layer api * add test * storage layer tests * deprecate transactional tag * i guess no reason to deprecate * remove transactional from batch_all * update tests * extend trait * cargo run --quiet --profile=production --features runtime-benchmarks --manifest-path bin/node/cli/Cargo.toml -- benchmark pallet --chain=dev --steps=50 --repeat=20 --pallet=pallet_balances --extrinsic=* --execution=wasm --wasm-execution=compiled --output=./frame/balances/src/weights.rs --template=./.maintain/frame-weight-template.hbs * cargo run --quiet --profile=production --features runtime-benchmarks --manifest-path bin/node/cli/Cargo.toml -- benchmark pallet --chain=dev --steps=50 --repeat=20 --pallet=pallet_balances --extrinsic=* --execution=wasm --wasm-execution=compiled --output=./frame/balances/src/weights.rs --template=./.maintain/frame-weight-template.hbs * cargo run --quiet --profile=production --features runtime-benchmarks --manifest-path bin/node/cli/Cargo.toml -- benchmark pallet --chain=dev --steps=50 --repeat=20 --pallet=pallet_staking --extrinsic=* --execution=wasm --wasm-execution=compiled --output=./frame/staking/src/weights.rs --template=./.maintain/frame-weight-template.hbs * fix copy paste name * cargo run --quiet --profile=production --features runtime-benchmarks --manifest-path bin/node/cli/Cargo.toml -- benchmark pallet --chain=dev --steps=50 --repeat=20 --pallet=pallet_utility --extrinsic=* --execution=wasm --wasm-execution=compiled --output=./frame/utility/src/weights.rs --template=./.maintain/frame-weight-template.hbs * Create run_all_benchmarks.sh * uncomment build * update number of steps and repeats * add skip build * Update run_all_benchmarks.sh * Update run_all_benchmarks.sh * new benchmarks * Update frame/support/src/traits/dispatch.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * Update frame/support/src/traits/dispatch.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * Update frame/support/test/tests/storage_layers.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * Update frame/support/test/tests/storage_layers.rs * weights * Update dispatch.rs * doc link * decl_macro support Co-authored-by: Parity Bot <admin@parity.io> Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
This commit is contained in:
@@ -127,6 +127,19 @@ pub fn expand_outer_dispatch(
|
||||
}
|
||||
}
|
||||
}
|
||||
impl #scrate::traits::DispatchableWithStorageLayer for Call {
|
||||
type Origin = Origin;
|
||||
fn dispatch_with_storage_layer(self, origin: Origin) -> #scrate::dispatch::DispatchResultWithPostInfo {
|
||||
#scrate::storage::with_storage_layer(|| {
|
||||
#scrate::dispatch::Dispatchable::dispatch(self, origin)
|
||||
})
|
||||
}
|
||||
fn dispatch_bypass_filter_with_storage_layer(self, origin: Origin) -> #scrate::dispatch::DispatchResultWithPostInfo {
|
||||
#scrate::storage::with_storage_layer(|| {
|
||||
#scrate::traits::UnfilteredDispatchable::dispatch_bypass_filter(self, origin)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#(
|
||||
impl #scrate::traits::IsSubType<#scrate::dispatch::CallableCallFor<#pallet_names, #runtime>> for Call {
|
||||
|
||||
@@ -433,6 +433,12 @@ pub fn transactional(attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||
transactional::transactional(attr, input).unwrap_or_else(|e| e.to_compile_error().into())
|
||||
}
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn require_transactional(attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||
transactional::require_transactional(attr, input)
|
||||
.unwrap_or_else(|e| e.to_compile_error().into())
|
||||
}
|
||||
|
||||
/// Derive [`Clone`] but do not bound any generic. Docs are at `frame_support::CloneNoBound`.
|
||||
#[proc_macro_derive(CloneNoBound)]
|
||||
pub fn derive_clone_no_bound(input: TokenStream) -> TokenStream {
|
||||
@@ -510,12 +516,6 @@ pub fn derive_default_no_bound(input: TokenStream) -> TokenStream {
|
||||
default_no_bound::derive_default_no_bound(input)
|
||||
}
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn require_transactional(attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||
transactional::require_transactional(attr, input)
|
||||
.unwrap_or_else(|e| e.to_compile_error().into())
|
||||
}
|
||||
|
||||
#[proc_macro]
|
||||
pub fn crate_to_crate_version(input: TokenStream) -> TokenStream {
|
||||
crate_version::crate_to_crate_version(input)
|
||||
|
||||
@@ -267,8 +267,12 @@ pub fn expand_call(def: &mut Def) -> proc_macro2::TokenStream {
|
||||
#frame_support::sp_tracing::enter_span!(
|
||||
#frame_support::sp_tracing::trace_span!(stringify!(#fn_name))
|
||||
);
|
||||
<#pallet_ident<#type_use_gen>>::#fn_name(origin, #( #args_name, )* )
|
||||
.map(Into::into).map_err(Into::into)
|
||||
// We execute all dispatchable in at least one storage layer, allowing them
|
||||
// to return an error at any point, and undoing any storage changes.
|
||||
#frame_support::storage::in_storage_layer(|| {
|
||||
<#pallet_ident<#type_use_gen>>::#fn_name(origin, #( #args_name, )* )
|
||||
.map(Into::into).map_err(Into::into)
|
||||
})
|
||||
},
|
||||
)*
|
||||
Self::__Ignore(_, _) => {
|
||||
|
||||
@@ -27,7 +27,8 @@ pub use crate::{
|
||||
result,
|
||||
},
|
||||
traits::{
|
||||
CallMetadata, GetCallMetadata, GetCallName, GetStorageVersion, UnfilteredDispatchable,
|
||||
CallMetadata, DispatchableWithStorageLayer, GetCallMetadata, GetCallName,
|
||||
GetStorageVersion, UnfilteredDispatchable,
|
||||
},
|
||||
weights::{
|
||||
ClassifyDispatch, DispatchInfo, GetDispatchInfo, PaysFee, PostDispatchInfo,
|
||||
@@ -1469,7 +1470,11 @@ macro_rules! decl_module {
|
||||
$ignore:ident
|
||||
$mod_type:ident<$trait_instance:ident $(, $instance:ident)?> $fn_name:ident $origin:ident $system:ident [ $( $param_name:ident),* ]
|
||||
) => {
|
||||
<$mod_type<$trait_instance $(, $instance)?>>::$fn_name( $origin $(, $param_name )* ).map(Into::into).map_err(Into::into)
|
||||
// We execute all dispatchable in at least one storage layer, allowing them
|
||||
// to return an error at any point, and undoing any storage changes.
|
||||
$crate::storage::in_storage_layer(|| {
|
||||
<$mod_type<$trait_instance $(, $instance)?>>::$fn_name( $origin $(, $param_name )* ).map(Into::into).map_err(Into::into)
|
||||
})
|
||||
};
|
||||
|
||||
// no `deposit_event` function wanted
|
||||
|
||||
@@ -30,7 +30,9 @@ use sp_runtime::generic::{Digest, DigestItem};
|
||||
use sp_std::prelude::*;
|
||||
|
||||
pub use self::{
|
||||
transactional::{with_transaction, with_transaction_unchecked},
|
||||
transactional::{
|
||||
in_storage_layer, with_storage_layer, with_transaction, with_transaction_unchecked,
|
||||
},
|
||||
types::StorageEntryMetadataBuilder,
|
||||
};
|
||||
pub use sp_runtime::TransactionOutcome;
|
||||
|
||||
@@ -158,6 +158,40 @@ pub fn with_transaction_unchecked<R>(f: impl FnOnce() -> TransactionOutcome<R>)
|
||||
}
|
||||
}
|
||||
|
||||
/// Execute the supplied function, adding a new storage layer.
|
||||
///
|
||||
/// This is the same as `with_transaction`, but assuming that any function returning
|
||||
/// an `Err` should rollback, and any function returning `Ok` should commit. This
|
||||
/// provides a cleaner API to the developer who wants this behavior.
|
||||
pub fn with_storage_layer<T, E>(f: impl FnOnce() -> Result<T, E>) -> Result<T, E>
|
||||
where
|
||||
E: From<DispatchError>,
|
||||
{
|
||||
with_transaction(|| {
|
||||
let r = f();
|
||||
if r.is_ok() {
|
||||
TransactionOutcome::Commit(r)
|
||||
} else {
|
||||
TransactionOutcome::Rollback(r)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Execute the supplied function, ensuring we are at least in one storage layer.
|
||||
///
|
||||
/// If we are already in a storage layer, we just execute the provided closure.
|
||||
/// If we are not, we execute the closure within a [`with_storage_layer`].
|
||||
pub fn in_storage_layer<T, E>(f: impl FnOnce() -> Result<T, E>) -> Result<T, E>
|
||||
where
|
||||
E: From<DispatchError>,
|
||||
{
|
||||
if is_transactional() {
|
||||
f()
|
||||
} else {
|
||||
with_storage_layer(f)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -229,4 +263,33 @@ mod tests {
|
||||
assert_eq!(get_transaction_level(), 0);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn in_storage_layer_works() {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
assert_eq!(get_transaction_level(), 0);
|
||||
|
||||
let res = in_storage_layer(|| -> DispatchResult {
|
||||
assert_eq!(get_transaction_level(), 1);
|
||||
in_storage_layer(|| -> DispatchResult {
|
||||
// We are still in the same layer :)
|
||||
assert_eq!(get_transaction_level(), 1);
|
||||
Ok(())
|
||||
})
|
||||
});
|
||||
|
||||
assert_ok!(res);
|
||||
|
||||
let res = in_storage_layer(|| -> DispatchResult {
|
||||
assert_eq!(get_transaction_level(), 1);
|
||||
in_storage_layer(|| -> DispatchResult {
|
||||
// We are still in the same layer :)
|
||||
assert_eq!(get_transaction_level(), 1);
|
||||
Err("epic fail".into())
|
||||
})
|
||||
});
|
||||
|
||||
assert_noop!(res, "epic fail");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,8 +94,8 @@ pub use storage::{
|
||||
|
||||
mod dispatch;
|
||||
pub use dispatch::{
|
||||
AsEnsureOriginWithArg, EnsureOneOf, EnsureOrigin, EnsureOriginWithArg, OriginTrait,
|
||||
UnfilteredDispatchable,
|
||||
AsEnsureOriginWithArg, DispatchableWithStorageLayer, EnsureOneOf, EnsureOrigin,
|
||||
EnsureOriginWithArg, OriginTrait, UnfilteredDispatchable,
|
||||
};
|
||||
|
||||
mod voting;
|
||||
|
||||
@@ -101,6 +101,23 @@ pub trait UnfilteredDispatchable {
|
||||
fn dispatch_bypass_filter(self, origin: Self::Origin) -> DispatchResultWithPostInfo;
|
||||
}
|
||||
|
||||
/// Type that can be dispatched with an additional storage layer which is used to execute the call.
|
||||
pub trait DispatchableWithStorageLayer {
|
||||
/// The origin type of the runtime, (i.e. `frame_system::Config::Origin`).
|
||||
type Origin;
|
||||
|
||||
/// Same as `dispatch` from the [`frame_support::dispatch::Dispatchable`] trait, but
|
||||
/// specifically spawns a new storage layer to execute the call inside of.
|
||||
fn dispatch_with_storage_layer(self, origin: Self::Origin) -> DispatchResultWithPostInfo;
|
||||
|
||||
/// Same as `dispatch_bypass_filter` from the [`UnfilteredDispatchable`] trait, but specifically
|
||||
/// spawns a new storage layer to execute the call inside of.
|
||||
fn dispatch_bypass_filter_with_storage_layer(
|
||||
self,
|
||||
origin: Self::Origin,
|
||||
) -> DispatchResultWithPostInfo;
|
||||
}
|
||||
|
||||
/// Methods available on `frame_system::Config::Origin`.
|
||||
pub trait OriginTrait: Sized {
|
||||
/// Runtime call type, as in `frame_system::Config::Call`
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
// limitations under the License.
|
||||
|
||||
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
|
||||
//! DATE: 2022-05-23 (Y/M/D)
|
||||
//! DATE: 2022-05-24 (Y/M/D)
|
||||
//!
|
||||
//! SHORT-NAME: `block`, LONG-NAME: `BlockExecution`, RUNTIME: `Development`
|
||||
//! WARMUPS: `10`, REPEAT: `100`
|
||||
@@ -27,7 +27,7 @@
|
||||
// ./target/production/substrate
|
||||
// benchmark
|
||||
// overhead
|
||||
// --dev
|
||||
// --chain=dev
|
||||
// --execution=wasm
|
||||
// --wasm-execution=compiled
|
||||
// --weight-path=./frame/support/src/weights/
|
||||
@@ -44,16 +44,16 @@ parameter_types! {
|
||||
/// Calculated by multiplying the *Average* with `1` and adding `0`.
|
||||
///
|
||||
/// Stats nanoseconds:
|
||||
/// Min, Max: 5_295_788, 5_473_440
|
||||
/// Average: 5_343_308
|
||||
/// Median: 5_323_240
|
||||
/// Std-Dev: 38368.68
|
||||
/// Min, Max: 5_303_128, 5_507_784
|
||||
/// Average: 5_346_284
|
||||
/// Median: 5_328_139
|
||||
/// Std-Dev: 41749.5
|
||||
///
|
||||
/// Percentiles nanoseconds:
|
||||
/// 99th: 5_470_141
|
||||
/// 95th: 5_418_269
|
||||
/// 75th: 5_355_579
|
||||
pub const BlockExecutionWeight: Weight = 5_343_308 * WEIGHT_PER_NANOS;
|
||||
/// 99th: 5_489_273
|
||||
/// 95th: 5_433_314
|
||||
/// 75th: 5_354_812
|
||||
pub const BlockExecutionWeight: Weight = 5_346_284 * WEIGHT_PER_NANOS;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
// limitations under the License.
|
||||
|
||||
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
|
||||
//! DATE: 2022-05-23 (Y/M/D)
|
||||
//! DATE: 2022-05-24 (Y/M/D)
|
||||
//!
|
||||
//! SHORT-NAME: `extrinsic`, LONG-NAME: `ExtrinsicBase`, RUNTIME: `Development`
|
||||
//! WARMUPS: `10`, REPEAT: `100`
|
||||
@@ -27,7 +27,7 @@
|
||||
// ./target/production/substrate
|
||||
// benchmark
|
||||
// overhead
|
||||
// --dev
|
||||
// --chain=dev
|
||||
// --execution=wasm
|
||||
// --wasm-execution=compiled
|
||||
// --weight-path=./frame/support/src/weights/
|
||||
@@ -44,16 +44,16 @@ parameter_types! {
|
||||
/// Calculated by multiplying the *Average* with `1` and adding `0`.
|
||||
///
|
||||
/// Stats nanoseconds:
|
||||
/// Min, Max: 85_423, 86_142
|
||||
/// Average: 85_795
|
||||
/// Median: 85_790
|
||||
/// Std-Dev: 162.37
|
||||
/// Min, Max: 86_060, 86_999
|
||||
/// Average: 86_298
|
||||
/// Median: 86_248
|
||||
/// Std-Dev: 207.19
|
||||
///
|
||||
/// Percentiles nanoseconds:
|
||||
/// 99th: 86_115
|
||||
/// 95th: 86_069
|
||||
/// 75th: 85_937
|
||||
pub const ExtrinsicBaseWeight: Weight = 85_795 * WEIGHT_PER_NANOS;
|
||||
/// 99th: 86_924
|
||||
/// 95th: 86_828
|
||||
/// 75th: 86_347
|
||||
pub const ExtrinsicBaseWeight: Weight = 86_298 * WEIGHT_PER_NANOS;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -205,8 +205,7 @@ pub mod pallet {
|
||||
|
||||
/// Doc comment put in metadata
|
||||
#[pallet::weight(1)]
|
||||
#[frame_support::transactional]
|
||||
pub fn foo_transactional(
|
||||
pub fn foo_storage_layer(
|
||||
_origin: OriginFor<T>,
|
||||
#[pallet::compact] foo: u32,
|
||||
) -> DispatchResultWithPostInfo {
|
||||
@@ -373,7 +372,7 @@ pub mod pallet {
|
||||
fn validate_unsigned(_source: TransactionSource, call: &Self::Call) -> TransactionValidity {
|
||||
let _ = T::AccountId::from(SomeType1); // Test for where clause
|
||||
let _ = T::AccountId::from(SomeType5); // Test for where clause
|
||||
if matches!(call, Call::foo_transactional { .. }) {
|
||||
if matches!(call, Call::foo_storage_layer { .. }) {
|
||||
return Ok(ValidTransaction::default())
|
||||
}
|
||||
Err(TransactionValidityError::Invalid(InvalidTransaction::Call))
|
||||
@@ -611,13 +610,13 @@ fn transactional_works() {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
frame_system::Pallet::<Runtime>::set_block_number(1);
|
||||
|
||||
pallet::Call::<Runtime>::foo_transactional { foo: 0 }
|
||||
pallet::Call::<Runtime>::foo_storage_layer { foo: 0 }
|
||||
.dispatch_bypass_filter(None.into())
|
||||
.err()
|
||||
.unwrap();
|
||||
assert!(frame_system::Pallet::<Runtime>::events().is_empty());
|
||||
|
||||
pallet::Call::<Runtime>::foo_transactional { foo: 1 }
|
||||
pallet::Call::<Runtime>::foo_storage_layer { foo: 1 }
|
||||
.dispatch_bypass_filter(None.into())
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
@@ -640,7 +639,7 @@ fn call_expand() {
|
||||
assert_eq!(call_foo.get_call_name(), "foo");
|
||||
assert_eq!(
|
||||
pallet::Call::<Runtime>::get_call_names(),
|
||||
&["foo", "foo_transactional", "foo_no_post_info"],
|
||||
&["foo", "foo_storage_layer", "foo_no_post_info"],
|
||||
);
|
||||
}
|
||||
|
||||
@@ -744,7 +743,7 @@ fn inherent_expand() {
|
||||
Digest::default(),
|
||||
),
|
||||
vec![UncheckedExtrinsic {
|
||||
function: Call::Example(pallet::Call::foo_transactional { foo: 0 }),
|
||||
function: Call::Example(pallet::Call::foo_storage_layer { foo: 0 }),
|
||||
signature: None,
|
||||
}],
|
||||
);
|
||||
@@ -785,7 +784,7 @@ fn inherent_expand() {
|
||||
signature: None,
|
||||
},
|
||||
UncheckedExtrinsic {
|
||||
function: Call::Example(pallet::Call::foo_transactional { foo: 0 }),
|
||||
function: Call::Example(pallet::Call::foo_storage_layer { foo: 0 }),
|
||||
signature: None,
|
||||
},
|
||||
],
|
||||
@@ -807,7 +806,7 @@ fn inherent_expand() {
|
||||
signature: None,
|
||||
},
|
||||
UncheckedExtrinsic {
|
||||
function: Call::Example(pallet::Call::foo_transactional { foo: 0 }),
|
||||
function: Call::Example(pallet::Call::foo_storage_layer { foo: 0 }),
|
||||
signature: None,
|
||||
},
|
||||
UncheckedExtrinsic {
|
||||
@@ -857,7 +856,7 @@ fn validate_unsigned_expand() {
|
||||
let validity = pallet::Pallet::validate_unsigned(TransactionSource::Local, &call).unwrap_err();
|
||||
assert_eq!(validity, TransactionValidityError::Invalid(InvalidTransaction::Call));
|
||||
|
||||
let call = pallet::Call::<Runtime>::foo_transactional { foo: 0 };
|
||||
let call = pallet::Call::<Runtime>::foo_storage_layer { foo: 0 };
|
||||
|
||||
let validity = pallet::Pallet::validate_unsigned(TransactionSource::External, &call).unwrap();
|
||||
assert_eq!(validity, ValidTransaction::default());
|
||||
|
||||
@@ -93,8 +93,7 @@ pub mod pallet {
|
||||
|
||||
/// Doc comment put in metadata
|
||||
#[pallet::weight(1)]
|
||||
#[frame_support::transactional]
|
||||
pub fn foo_transactional(
|
||||
pub fn foo_storage_layer(
|
||||
origin: OriginFor<T>,
|
||||
#[pallet::compact] _foo: u32,
|
||||
) -> DispatchResultWithPostInfo {
|
||||
@@ -316,7 +315,7 @@ fn call_expand() {
|
||||
DispatchInfo { weight: 3, class: DispatchClass::Normal, pays_fee: Pays::Yes }
|
||||
);
|
||||
assert_eq!(call_foo.get_call_name(), "foo");
|
||||
assert_eq!(pallet::Call::<Runtime>::get_call_names(), &["foo", "foo_transactional"]);
|
||||
assert_eq!(pallet::Call::<Runtime>::get_call_names(), &["foo", "foo_storage_layer"]);
|
||||
|
||||
let call_foo = pallet::Call::<Runtime, pallet::Instance1>::foo { foo: 3 };
|
||||
assert_eq!(
|
||||
@@ -326,7 +325,7 @@ fn call_expand() {
|
||||
assert_eq!(call_foo.get_call_name(), "foo");
|
||||
assert_eq!(
|
||||
pallet::Call::<Runtime, pallet::Instance1>::get_call_names(),
|
||||
&["foo", "foo_transactional"],
|
||||
&["foo", "foo_storage_layer"],
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,280 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 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::{
|
||||
assert_noop, assert_ok, dispatch::DispatchResult, pallet_prelude::ConstU32,
|
||||
storage::with_storage_layer,
|
||||
};
|
||||
use pallet::*;
|
||||
use sp_io::TestExternalities;
|
||||
|
||||
#[frame_support::pallet]
|
||||
pub mod pallet {
|
||||
use frame_support::{ensure, pallet_prelude::*};
|
||||
use frame_system::pallet_prelude::*;
|
||||
|
||||
#[pallet::pallet]
|
||||
pub struct Pallet<T>(_);
|
||||
|
||||
#[pallet::config]
|
||||
pub trait Config: frame_system::Config {}
|
||||
|
||||
#[pallet::storage]
|
||||
pub type Value<T> = StorageValue<_, u32, ValueQuery>;
|
||||
|
||||
#[pallet::storage]
|
||||
pub type Map<T> = StorageMap<_, Blake2_128Concat, u32, u32, ValueQuery>;
|
||||
|
||||
#[pallet::error]
|
||||
pub enum Error<T> {
|
||||
Revert,
|
||||
}
|
||||
|
||||
#[pallet::call]
|
||||
impl<T: Config> Pallet<T> {
|
||||
#[pallet::weight(1)]
|
||||
pub fn set_value(_origin: OriginFor<T>, value: u32) -> DispatchResult {
|
||||
Value::<T>::put(value);
|
||||
ensure!(value != 1, Error::<T>::Revert);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub mod decl_pallet {
|
||||
pub trait Config: frame_system::Config {}
|
||||
|
||||
frame_support::decl_module! {
|
||||
pub struct Module<T: Config> for enum Call where origin: T::Origin {
|
||||
#[weight = 0]
|
||||
pub fn set_value(_origin, value: u32) {
|
||||
DeclValue::put(value);
|
||||
frame_support::ensure!(value != 1, "Revert!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
frame_support::decl_storage! {
|
||||
trait Store for Module<T: Config> as StorageTransactions {
|
||||
pub DeclValue: u32;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type BlockNumber = u64;
|
||||
pub type Index = u64;
|
||||
pub type Header = sp_runtime::generic::Header<u32, sp_runtime::traits::BlakeTwo256>;
|
||||
pub type Block = sp_runtime::generic::Block<Header, UncheckedExtrinsic>;
|
||||
pub type UncheckedExtrinsic = sp_runtime::generic::UncheckedExtrinsic<u32, Call, (), ()>;
|
||||
|
||||
impl frame_system::Config for Runtime {
|
||||
type BlockWeights = ();
|
||||
type BlockLength = ();
|
||||
type DbWeight = ();
|
||||
type BaseCallFilter = frame_support::traits::Everything;
|
||||
type Origin = Origin;
|
||||
type Index = u64;
|
||||
type BlockNumber = u32;
|
||||
type Call = Call;
|
||||
type Hash = sp_runtime::testing::H256;
|
||||
type Hashing = sp_runtime::traits::BlakeTwo256;
|
||||
type AccountId = u64;
|
||||
type Lookup = sp_runtime::traits::IdentityLookup<Self::AccountId>;
|
||||
type Header = Header;
|
||||
type Event = Event;
|
||||
type BlockHashCount = ConstU32<250>;
|
||||
type Version = ();
|
||||
type PalletInfo = PalletInfo;
|
||||
type AccountData = ();
|
||||
type OnNewAccount = ();
|
||||
type OnKilledAccount = ();
|
||||
type SystemWeightInfo = ();
|
||||
type SS58Prefix = ();
|
||||
type OnSetCode = ();
|
||||
type MaxConsumers = ConstU32<16>;
|
||||
}
|
||||
|
||||
impl pallet::Config for Runtime {}
|
||||
|
||||
impl decl_pallet::Config for Runtime {}
|
||||
|
||||
frame_support::construct_runtime!(
|
||||
pub enum Runtime where
|
||||
Block = Block,
|
||||
NodeBlock = Block,
|
||||
UncheckedExtrinsic = UncheckedExtrinsic
|
||||
{
|
||||
System: frame_system,
|
||||
MyPallet: pallet,
|
||||
DeclPallet: decl_pallet::{Call, Storage},
|
||||
}
|
||||
);
|
||||
|
||||
#[test]
|
||||
fn storage_layer_basic_commit() {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
assert_eq!(Value::<Runtime>::get(), 0);
|
||||
assert!(!Map::<Runtime>::contains_key(0));
|
||||
|
||||
assert_ok!(with_storage_layer(|| -> DispatchResult {
|
||||
Value::<Runtime>::set(99);
|
||||
Map::<Runtime>::insert(0, 99);
|
||||
assert_eq!(Value::<Runtime>::get(), 99);
|
||||
assert_eq!(Map::<Runtime>::get(0), 99);
|
||||
Ok(())
|
||||
}));
|
||||
|
||||
assert_eq!(Value::<Runtime>::get(), 99);
|
||||
assert_eq!(Map::<Runtime>::get(0), 99);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn storage_layer_basic_rollback() {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
assert_eq!(Value::<Runtime>::get(), 0);
|
||||
assert_eq!(Map::<Runtime>::get(0), 0);
|
||||
|
||||
assert_noop!(
|
||||
with_storage_layer(|| -> DispatchResult {
|
||||
Value::<Runtime>::set(99);
|
||||
Map::<Runtime>::insert(0, 99);
|
||||
assert_eq!(Value::<Runtime>::get(), 99);
|
||||
assert_eq!(Map::<Runtime>::get(0), 99);
|
||||
Err("revert".into())
|
||||
}),
|
||||
"revert"
|
||||
);
|
||||
|
||||
assert_eq!(Value::<Runtime>::get(), 0);
|
||||
assert_eq!(Map::<Runtime>::get(0), 0);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn storage_layer_rollback_then_commit() {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
Value::<Runtime>::set(1);
|
||||
Map::<Runtime>::insert(1, 1);
|
||||
|
||||
assert_ok!(with_storage_layer(|| -> DispatchResult {
|
||||
Value::<Runtime>::set(2);
|
||||
Map::<Runtime>::insert(1, 2);
|
||||
Map::<Runtime>::insert(2, 2);
|
||||
|
||||
assert_noop!(
|
||||
with_storage_layer(|| -> DispatchResult {
|
||||
Value::<Runtime>::set(3);
|
||||
Map::<Runtime>::insert(1, 3);
|
||||
Map::<Runtime>::insert(2, 3);
|
||||
Map::<Runtime>::insert(3, 3);
|
||||
|
||||
assert_eq!(Value::<Runtime>::get(), 3);
|
||||
assert_eq!(Map::<Runtime>::get(1), 3);
|
||||
assert_eq!(Map::<Runtime>::get(2), 3);
|
||||
assert_eq!(Map::<Runtime>::get(3), 3);
|
||||
|
||||
Err("revert".into())
|
||||
}),
|
||||
"revert"
|
||||
);
|
||||
|
||||
assert_eq!(Value::<Runtime>::get(), 2);
|
||||
assert_eq!(Map::<Runtime>::get(1), 2);
|
||||
assert_eq!(Map::<Runtime>::get(2), 2);
|
||||
assert_eq!(Map::<Runtime>::get(3), 0);
|
||||
|
||||
Ok(())
|
||||
}));
|
||||
|
||||
assert_eq!(Value::<Runtime>::get(), 2);
|
||||
assert_eq!(Map::<Runtime>::get(1), 2);
|
||||
assert_eq!(Map::<Runtime>::get(2), 2);
|
||||
assert_eq!(Map::<Runtime>::get(3), 0);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn storage_layer_commit_then_rollback() {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
Value::<Runtime>::set(1);
|
||||
Map::<Runtime>::insert(1, 1);
|
||||
|
||||
assert_noop!(
|
||||
with_storage_layer(|| -> DispatchResult {
|
||||
Value::<Runtime>::set(2);
|
||||
Map::<Runtime>::insert(1, 2);
|
||||
Map::<Runtime>::insert(2, 2);
|
||||
|
||||
assert_ok!(with_storage_layer(|| -> DispatchResult {
|
||||
Value::<Runtime>::set(3);
|
||||
Map::<Runtime>::insert(1, 3);
|
||||
Map::<Runtime>::insert(2, 3);
|
||||
Map::<Runtime>::insert(3, 3);
|
||||
|
||||
assert_eq!(Value::<Runtime>::get(), 3);
|
||||
assert_eq!(Map::<Runtime>::get(1), 3);
|
||||
assert_eq!(Map::<Runtime>::get(2), 3);
|
||||
assert_eq!(Map::<Runtime>::get(3), 3);
|
||||
|
||||
Ok(())
|
||||
}));
|
||||
|
||||
assert_eq!(Value::<Runtime>::get(), 3);
|
||||
assert_eq!(Map::<Runtime>::get(1), 3);
|
||||
assert_eq!(Map::<Runtime>::get(2), 3);
|
||||
assert_eq!(Map::<Runtime>::get(3), 3);
|
||||
|
||||
Err("revert".into())
|
||||
}),
|
||||
"revert"
|
||||
);
|
||||
|
||||
assert_eq!(Value::<Runtime>::get(), 1);
|
||||
assert_eq!(Map::<Runtime>::get(1), 1);
|
||||
assert_eq!(Map::<Runtime>::get(2), 0);
|
||||
assert_eq!(Map::<Runtime>::get(3), 0);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn storage_layer_in_pallet_call() {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
use sp_runtime::traits::Dispatchable;
|
||||
let call1 = Call::MyPallet(pallet::Call::set_value { value: 2 });
|
||||
assert_ok!(call1.dispatch(Origin::signed(0)));
|
||||
assert_eq!(Value::<Runtime>::get(), 2);
|
||||
|
||||
let call2 = Call::MyPallet(pallet::Call::set_value { value: 1 });
|
||||
assert_noop!(call2.dispatch(Origin::signed(0)), Error::<Runtime>::Revert);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn storage_layer_in_decl_pallet_call() {
|
||||
TestExternalities::default().execute_with(|| {
|
||||
use frame_support::StorageValue;
|
||||
use sp_runtime::traits::Dispatchable;
|
||||
|
||||
let call1 = Call::DeclPallet(decl_pallet::Call::set_value { value: 2 });
|
||||
assert_ok!(call1.dispatch(Origin::signed(0)));
|
||||
assert_eq!(decl_pallet::DeclValue::get(), 2);
|
||||
|
||||
let call2 = Call::DeclPallet(decl_pallet::Call::set_value { value: 1 });
|
||||
assert_noop!(call2.dispatch(Origin::signed(0)), "Revert!");
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user