Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
6e0ca26
add new trait
shawntabrizi May 17, 2022
9f0d290
implement DispatchableWithStorageLayer
shawntabrizi May 17, 2022
57fe674
at least one transactional
shawntabrizi May 17, 2022
28d35bd
all dispatch is at least transactional
shawntabrizi May 17, 2022
eaa330d
storage_layer api
shawntabrizi May 17, 2022
213e537
add test
shawntabrizi May 17, 2022
b8d32b9
storage layer tests
shawntabrizi May 17, 2022
a03c13f
deprecate transactional tag
shawntabrizi May 17, 2022
5dc88cd
i guess no reason to deprecate
shawntabrizi May 17, 2022
2856ecd
remove transactional from batch_all
shawntabrizi May 17, 2022
b0f7241
update tests
shawntabrizi May 17, 2022
0f26a7d
Merge remote-tracking branch 'origin/master' into shawntabrizi-dispat…
shawntabrizi May 17, 2022
f9f65db
extend trait
shawntabrizi May 19, 2022
738c021
Merge branch 'master' of https://github.com/paritytech/substrate into…
May 19, 2022
d1fd82f
cargo run --quiet --profile=production --features runtime-benchmarks …
May 19, 2022
5afa7e7
cargo run --quiet --profile=production --features runtime-benchmarks …
May 19, 2022
1a447b1
cargo run --quiet --profile=production --features runtime-benchmarks …
May 20, 2022
1e657bf
fix copy paste name
shawntabrizi May 20, 2022
5793a59
Merge branch 'master' of https://github.com/paritytech/substrate into…
May 20, 2022
09b4274
cargo run --quiet --profile=production --features runtime-benchmarks …
May 20, 2022
03c19ec
Create run_all_benchmarks.sh
shawntabrizi May 21, 2022
0e7e939
uncomment build
shawntabrizi May 21, 2022
fe1a750
update number of steps and repeats
shawntabrizi May 21, 2022
2068319
add skip build
shawntabrizi May 21, 2022
71b45db
Update run_all_benchmarks.sh
shawntabrizi May 21, 2022
c357618
Update run_all_benchmarks.sh
shawntabrizi May 21, 2022
85e7a28
new benchmarks
shawntabrizi May 21, 2022
d2aa0ed
Merge remote-tracking branch 'origin/master' into shawntabrizi-dispat…
shawntabrizi May 23, 2022
e0abb8f
Update frame/support/src/traits/dispatch.rs
shawntabrizi May 23, 2022
a87f4bc
Update frame/support/src/traits/dispatch.rs
shawntabrizi May 23, 2022
3633d3c
Update frame/support/test/tests/storage_layers.rs
shawntabrizi May 23, 2022
82b3576
Update frame/support/test/tests/storage_layers.rs
shawntabrizi May 23, 2022
db8dbe3
weights
shawntabrizi May 24, 2022
630f149
Update dispatch.rs
shawntabrizi May 24, 2022
3035fc8
Merge branch 'master' into shawntabrizi-dispatchable-with-storage-layer
shawntabrizi May 25, 2022
993383a
doc link
shawntabrizi May 26, 2022
d7530da
decl_macro support
shawntabrizi May 26, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
storage layer tests
  • Loading branch information
shawntabrizi committed May 17, 2022
commit b8d32b9d66cbe31e6a311ea2fbf63ed40a5c8d92
197 changes: 197 additions & 0 deletions frame/support/test/tests/storage_layers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
// 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::{
assert_noop, assert_ok,
dispatch::{DispatchError, DispatchResult},
storage::with_storage_layer,
StorageMap, StorageValue,
};
use sp_io::TestExternalities;

pub trait Config: frame_support_test::Config {}

frame_support::decl_module! {
pub struct Module<T: Config> for enum Call where origin: T::Origin, system=frame_support_test {
#[weight = 0]
fn value_commits(_origin, v: u32) {
Value::set(v);
}

#[weight = 0]
fn value_rollbacks(_origin, v: u32) -> DispatchResult {
Value::set(v);
Err(DispatchError::Other("nah"))
}
}
}

frame_support::decl_storage! {
trait Store for Module<T: Config> as StorageTransactions {
pub Value: u32;
pub Map: map hasher(twox_64_concat) String => u32;
}
}

struct Runtime;

impl frame_support_test::Config for Runtime {
type Origin = u32;
type BlockNumber = u32;
type PalletInfo = frame_support_test::PanicPalletInfo;
type DbWeight = ();
}

impl Config for Runtime {}

#[test]
fn storage_transaction_basic_commit() {
TestExternalities::default().execute_with(|| {
assert_eq!(Value::get(), 0);
assert!(!Map::contains_key("val0"));

assert_ok!(with_storage_layer(|| -> DispatchResult {
Value::set(99);
Map::insert("val0", 99);
assert_eq!(Value::get(), 99);
assert_eq!(Map::get("val0"), 99);
Ok(())
}));

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);

assert_noop!(
with_storage_layer(|| -> DispatchResult {
Value::set(99);
Map::insert("val0", 99);
assert_eq!(Value::get(), 99);
assert_eq!(Map::get("val0"), 99);
Err("revert".into())
}),
"revert"
);

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);

assert_ok!(with_storage_layer(|| -> DispatchResult {
Value::set(2);
Map::insert("val1", 2);
Map::insert("val2", 2);

assert_noop!(
with_storage_layer(|| -> DispatchResult {
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);

Err("revert".into())
}),
"revert"
);

assert_eq!(Value::get(), 2);
assert_eq!(Map::get("val1"), 2);
assert_eq!(Map::get("val2"), 2);
assert_eq!(Map::get("val3"), 0);

Ok(())
}));

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);

assert_noop!(
with_storage_layer(|| -> DispatchResult {
Value::set(2);
Map::insert("val1", 2);
Map::insert("val2", 2);

assert_ok!(with_storage_layer(|| -> DispatchResult {
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);

Ok(())
}));

assert_eq!(Value::get(), 3);
assert_eq!(Map::get("val1"), 3);
assert_eq!(Map::get("val2"), 3);
assert_eq!(Map::get("val3"), 3);

Err("revert".into())
}),
"revert"
);

assert_eq!(Value::get(), 1);
assert_eq!(Map::get("val1"), 1);
assert_eq!(Map::get("val2"), 0);
assert_eq!(Map::get("val3"), 0);
});
}

#[test]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would like to see one test where we execute a call that returns an error, and we make sure it is reverted.

This should prevent anyone from accidentally removing the 1 storage layer that is injected in the macro.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That test is in the assert_noop below

fn storage_layer_in_decl_module() {
TestExternalities::default().execute_with(|| {
let origin = 0;
assert_ok!(<Module<Runtime>>::value_commits(origin, 2));
assert_eq!(Value::get(), 2);

assert_noop!(<Module<Runtime>>::value_rollbacks(origin, 3), "nah");
});
}