mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 15:47:58 +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:
@@ -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");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user