Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Runtime State Test + Integration with try-runtime #10174

Merged
merged 48 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
69e92ad
add missing version to dependencies
kianenigma Nov 2, 2021
9f20cbe
Huh
kianenigma Nov 3, 2021
4586de1
add features more
kianenigma Nov 3, 2021
c980770
more fixing
kianenigma Nov 3, 2021
822a4d0
Merge branch 'master' of github.com:paritytech/substrate into kiz-san…
kianenigma Nov 4, 2021
3dad278
last touches
kianenigma Nov 4, 2021
266fcb2
Master.into()
kianenigma Mar 22, 2022
d473421
Mostly merged with master again
kianenigma Jun 17, 2022
f1ceefa
merge current branch and make it compile
kianenigma Jun 18, 2022
844a86f
it all finally works
kianenigma Jun 23, 2022
0a43345
remove some feature gates
kianenigma Jun 23, 2022
d9ddd3e
Merge branch 'master' of github.com:paritytech/substrate into kiz-san…
kianenigma Jun 23, 2022
d6db0d5
remove unused
kianenigma Jun 23, 2022
312749d
fix old macro
kianenigma Jun 24, 2022
d72ff85
make it work again
kianenigma Jun 24, 2022
123a16c
fmt
kianenigma Jun 24, 2022
f07e714
remove unused import
kianenigma Jun 24, 2022
57d9a0e
Master.into()
kianenigma Aug 14, 2022
436afe0
".git/.scripts/fmt.sh" 1
Aug 14, 2022
bab0c67
Merge branch 'master' of github.com:paritytech/substrate into kiz-san…
kianenigma Aug 16, 2022
de27834
Cleanup more
kianenigma Aug 16, 2022
6a3fdd2
fix and rename everything
kianenigma Aug 19, 2022
6eddd2b
a few clippy fixes
kianenigma Aug 19, 2022
10b1af7
Add try-runtime feature
ggwpez Aug 19, 2022
d7f1907
Merge branch 'master' of github.com:paritytech/substrate into kiz-san…
kianenigma Aug 20, 2022
5648534
Merge branch 'kiz-sanity-check-trait' of github.com:paritytech/substr…
kianenigma Aug 20, 2022
d79d33b
small fixes
kianenigma Aug 20, 2022
b2a1ff8
fmt
kianenigma Aug 20, 2022
7590566
Update bin/node-template/runtime/src/lib.rs
kianenigma Aug 21, 2022
8c17b93
fix build
kianenigma Aug 21, 2022
9ccb198
Merge branch 'kiz-sanity-check-trait' of github.com:paritytech/substr…
kianenigma Aug 21, 2022
42450b5
Merge branch 'master' of github.com:paritytech/substrate into kiz-san…
kianenigma Aug 28, 2022
73aa952
Update utils/frame/try-runtime/cli/src/lib.rs
kianenigma Aug 28, 2022
ade3b1c
Update utils/frame/try-runtime/cli/src/commands/execute_block.rs
kianenigma Aug 28, 2022
e53d4eb
address all review comments
kianenigma Aug 28, 2022
1b21397
Merge branch 'kiz-sanity-check-trait' of github.com:paritytech/substr…
kianenigma Aug 28, 2022
e705762
fix typos
kianenigma Aug 29, 2022
6d801e9
Merge branch 'master' of github.com:paritytech/substrate into kiz-san…
kianenigma Aug 29, 2022
f733b6f
revert spec change
kianenigma Aug 29, 2022
dfe0d8e
last touches
kianenigma Aug 29, 2022
73bae7e
update docs
kianenigma Aug 29, 2022
c12202c
fmt
kianenigma Aug 29, 2022
d5bae73
Merge remote-tracking branch 'origin/master' into kiz-sanity-check-trait
Aug 30, 2022
8a76db4
Merge branch 'master' of github.com:paritytech/substrate into kiz-san…
kianenigma Aug 30, 2022
58850ad
Merge branch 'kiz-sanity-check-trait' of github.com:paritytech/substr…
kianenigma Aug 30, 2022
f102730
remove some debug_assertions
kianenigma Aug 30, 2022
3a60fb5
Merge branch 'master' of github.com:paritytech/substrate into kiz-san…
kianenigma Sep 1, 2022
b491796
fmt
kianenigma Sep 1, 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
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion bin/node-template/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ std = [
"frame-support/std",
"frame-system-rpc-runtime-api/std",
"frame-system/std",
"frame-try-runtime/std",
"pallet-aura/std",
"pallet-balances/std",
"pallet-grandpa/std",
Expand Down Expand Up @@ -97,9 +98,10 @@ runtime-benchmarks = [
"sp-runtime/runtime-benchmarks",
]
try-runtime = [
"frame-executive/try-runtime",
"frame-try-runtime",
"frame-executive/try-runtime",
"frame-system/try-runtime",
"frame-support/try-runtime",
"pallet-aura/try-runtime",
"pallet-balances/try-runtime",
"pallet-grandpa/try-runtime",
Expand Down
6 changes: 3 additions & 3 deletions bin/node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ pub type Executive = frame_executive::Executive<
Block,
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
AllPalletsWithSystemFlat,
>;

#[cfg(feature = "runtime-benchmarks")]
Expand Down Expand Up @@ -524,8 +524,8 @@ impl_runtime_apis! {
(weight, BlockWeights::get().max_block)
}

fn execute_block_no_check(block: Block) -> Weight {
Executive::execute_block_no_check(block)
fn execute_block(block: Block, state_root_check: bool, sanity_checks: frame_try_runtime::SanityCheckTargets) -> Weight {
Executive::try_execute_block(block, state_root_check, sanity_checks)
}
}
}
20 changes: 13 additions & 7 deletions bin/node/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,16 @@ runtime-benchmarks = [
"hex-literal",
]
try-runtime = [
"frame-executive/try-runtime",
"frame-try-runtime",
"frame-executive/try-runtime",
"frame-system/try-runtime",
"frame-support/try-runtime",
"pallet-alliance/try-runtime",
"pallet-assets/try-runtime",
"pallet-authority-discovery/try-runtime",
"pallet-authorship/try-runtime",
"pallet-babe/try-runtime",
"pallet-bags-list/try-runtime",
"pallet-balances/try-runtime",
"pallet-bounties/try-runtime",
"pallet-child-bounties/try-runtime",
Expand All @@ -262,32 +264,36 @@ try-runtime = [
"pallet-elections-phragmen/try-runtime",
"pallet-gilt/try-runtime",
"pallet-grandpa/try-runtime",
"pallet-identity/try-runtime",
"pallet-im-online/try-runtime",
"pallet-indices/try-runtime",
"pallet-identity/try-runtime",
"pallet-lottery/try-runtime",
"pallet-membership/try-runtime",
"pallet-mmr/try-runtime",
"pallet-multisig/try-runtime",
"pallet-nomination-pools/try-runtime",
"pallet-offences/try-runtime",
"pallet-preimage/try-runtime",
"pallet-proxy/try-runtime",
"pallet-ranked-collective/try-runtime",
"pallet-randomness-collective-flip/try-runtime",
"pallet-ranked-collective/try-runtime",
"pallet-recovery/try-runtime",
"pallet-referenda/try-runtime",
"pallet-scheduler/try-runtime",
"pallet-remark/try-runtime",
"pallet-session/try-runtime",
"pallet-society/try-runtime",
"pallet-staking/try-runtime",
"pallet-state-trie-migration/try-runtime",
"pallet-scheduler/try-runtime",
"pallet-society/try-runtime",
"pallet-sudo/try-runtime",
"pallet-timestamp/try-runtime",
"pallet-tips/try-runtime",
"pallet-transaction-payment/try-runtime",
"pallet-treasury/try-runtime",
"pallet-uniques/try-runtime",
"pallet-utility/try-runtime",
"pallet-transaction-payment/try-runtime",
"pallet-asset-tx-payment/try-runtime",
"pallet-transaction-storage/try-runtime",
"pallet-uniques/try-runtime",
"pallet-vesting/try-runtime",
"pallet-whitelist/try-runtime",
]
Expand Down
12 changes: 9 additions & 3 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1674,7 +1674,7 @@ pub type Executive = frame_executive::Executive<
Block,
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
AllPalletsWithSystemFlat,
>;

/// MMR helper types.
Expand Down Expand Up @@ -2042,8 +2042,14 @@ impl_runtime_apis! {
(weight, RuntimeBlockWeights::get().max_block)
}

fn execute_block_no_check(block: Block) -> Weight {
Executive::execute_block_no_check(block)
fn execute_block(block: Block, state_root_check: bool, sanity_checks: frame_try_runtime::SanityCheckTargets) -> Weight {
log::info!(
target: "node-runtime", "try-runtime: executing block {:?} / root checks: {:?} / sanity-checks: {:?}",
block.header.hash(),
state_root_check,
sanity_checks,
);
Executive::try_execute_block(block, state_root_check, sanity_checks)
}
}

Expand Down
11 changes: 5 additions & 6 deletions frame/bags-list/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ pub mod pallet {
"thresholds must strictly increase, and have no duplicates",
);
}

#[cfg(feature = "try-runtime")]
fn sanity_check(_: BlockNumberFor<T>) -> Result<(), &'static str> {
<Self as SortedListProvider<T::AccountId>>::sanity_check()
}
}
}

Expand Down Expand Up @@ -333,16 +338,10 @@ impl<T: Config<I>, I: 'static> SortedListProvider<T::AccountId> for Pallet<T, I>
List::<T, I>::unsafe_regenerate(all, score_of)
}

#[cfg(feature = "std")]
fn sanity_check() -> Result<(), &'static str> {
List::<T, I>::sanity_check()
}

#[cfg(not(feature = "std"))]
fn sanity_check() -> Result<(), &'static str> {
Ok(())
}

fn unsafe_clear() {
// NOTE: This call is unsafe for the same reason as SortedListProvider::unsafe_clear.
// I.e. because it can lead to many storage accesses.
Expand Down
10 changes: 1 addition & 9 deletions frame/bags-list/src/list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ impl<T: Config<I>, I: 'static> List<T, I> {
crate::ListBags::<T, I>::remove(removed_bag);
}

#[cfg(feature = "std")]
debug_assert_eq!(Self::sanity_check(), Ok(()));

num_affected
Expand Down Expand Up @@ -517,7 +518,6 @@ impl<T: Config<I>, I: 'static> List<T, I> {
/// * length of this list is in sync with `ListNodes::count()`,
/// * and sanity-checks all bags and nodes. This will cascade down all the checks and makes sure
/// all bags and nodes are checked per *any* update to `List`.
#[cfg(feature = "std")]
pub(crate) fn sanity_check() -> Result<(), &'static str> {
let mut seen_in_list = BTreeSet::new();
ensure!(
Expand Down Expand Up @@ -563,11 +563,6 @@ impl<T: Config<I>, I: 'static> List<T, I> {
Ok(())
}

#[cfg(not(feature = "std"))]
pub(crate) fn sanity_check() -> Result<(), &'static str> {
Ok(())
}

/// Returns the nodes of all non-empty bags. For testing and benchmarks.
#[cfg(any(feature = "std", feature = "runtime-benchmarks"))]
#[allow(dead_code)]
Expand Down Expand Up @@ -761,7 +756,6 @@ impl<T: Config<I>, I: 'static> Bag<T, I> {
/// * Ensures head has no prev.
/// * Ensures tail has no next.
/// * Ensures there are no loops, traversal from head to tail is correct.
#[cfg(feature = "std")]
fn sanity_check(&self) -> Result<(), &'static str> {
frame_support::ensure!(
self.head()
Expand Down Expand Up @@ -801,7 +795,6 @@ impl<T: Config<I>, I: 'static> Bag<T, I> {
}

/// Check if the bag contains a node with `id`.
#[cfg(feature = "std")]
fn contains(&self, id: &T::AccountId) -> bool {
self.iter().any(|n| n.id() == id)
}
Expand Down Expand Up @@ -906,7 +899,6 @@ impl<T: Config<I>, I: 'static> Node<T, I> {
self.bag_upper
}

#[cfg(feature = "std")]
fn sanity_check(&self) -> Result<(), &'static str> {
let expected_bag = Bag::<T, I>::get(self.bag_upper).ok_or("bag not found for node")?;

Expand Down
1 change: 1 addition & 0 deletions frame/beefy-mmr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ std = [
"sp-runtime/std",
"sp-std/std",
]
try-runtime = ["frame-support/try-runtime"]
1 change: 1 addition & 0 deletions frame/beefy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ std = [
"sp-runtime/std",
"sp-std/std",
]
try-runtime = ["frame-support/try-runtime"]
3 changes: 2 additions & 1 deletion frame/executive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ codec = { package = "parity-scale-codec", version = "3.0.0", default-features =
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }
frame-support = { version = "4.0.0-dev", default-features = false, path = "../support" }
frame-system = { version = "4.0.0-dev", default-features = false, path = "../system" }
frame-try-runtime = { version = "0.10.0-dev", default-features = false, path = "../try-runtime", optional = true }
sp-core = { version = "6.0.0", default-features = false, path = "../../primitives/core" }
sp-io = { version = "6.0.0", default-features = false, path = "../../primitives/io" }
sp-runtime = { version = "6.0.0", default-features = false, path = "../../primitives/runtime" }
Expand Down Expand Up @@ -48,4 +49,4 @@ std = [
"sp-std/std",
"sp-tracing/std",
]
try-runtime = ["frame-support/try-runtime"]
try-runtime = ["frame-support/try-runtime", "frame-try-runtime" ]
64 changes: 54 additions & 10 deletions frame/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ where
}
}

#[cfg(feature = "try-runtime")]
impl<
System: frame_system::Config + EnsureInherentsAreFirst<Block>,
Block: traits::Block<Header = System::Header, Hash = System::Hash>,
Expand All @@ -211,7 +212,8 @@ impl<
+ OnInitialize<System::BlockNumber>
+ OnIdle<System::BlockNumber>
+ OnFinalize<System::BlockNumber>
+ OffchainWorker<System::BlockNumber>,
+ OffchainWorker<System::BlockNumber>
+ frame_support::traits::SanityCheck<System::BlockNumber>,
COnRuntimeUpgrade: OnRuntimeUpgrade,
> Executive<System, Block, Context, UnsignedValidator, AllPalletsWithSystem, COnRuntimeUpgrade>
where
Expand All @@ -222,23 +224,32 @@ where
OriginOf<Block::Extrinsic, Context>: From<Option<System::AccountId>>,
UnsignedValidator: ValidateUnsigned<Call = CallOf<Block::Extrinsic, Context>>,
{
/// Execute all `OnRuntimeUpgrade` of this runtime, and return the aggregate weight.
pub fn execute_on_runtime_upgrade() -> frame_support::weights::Weight {
<(COnRuntimeUpgrade, AllPalletsWithSystem) as OnRuntimeUpgrade>::on_runtime_upgrade()
}

/// Execute given block, but don't do any of the `final_checks`.
///
/// Should only be used for testing.
kianenigma marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(feature = "try-runtime")]
pub fn execute_block_no_check(block: Block) -> frame_support::weights::Weight {
pub fn try_execute_block(
kianenigma marked this conversation as resolved.
Show resolved Hide resolved
block: Block,
check_state_root: bool,
sanity_checks: frame_try_runtime::SanityCheckTargets,
) -> frame_support::weights::Weight {
Self::initialize_block(block.header());
Self::initial_checks(&block);

let (header, extrinsics) = block.deconstruct();

Self::execute_extrinsics_with_book_keeping(extrinsics, *header.number());

// run the sanity-checks of all pallets.
<AllPalletsWithSystem as frame_support::traits::SanityCheck<System::BlockNumber>>::sanity_check(
*header.number(),
sanity_checks,
)
.map_err(|e| {
sp_runtime::print("failure:");
sp_runtime::print(e);
kianenigma marked this conversation as resolved.
Show resolved Hide resolved
})
.expect("sanity-checks should not fail");

// do some of the checks that would normally happen in `final_checks`, but definitely skip
kianenigma marked this conversation as resolved.
Show resolved Hide resolved
// the state root check.
{
Expand All @@ -249,6 +260,15 @@ where
assert!(header_item == computed_item, "Digest item must match that calculated.");
}

if check_state_root {
let storage_root = new_header.state_root();
header.state_root().check_equal(storage_root);
assert!(
header.state_root() == storage_root,
"Storage root must match that calculated."
);
}

assert!(
header.extrinsics_root() == new_header.extrinsics_root(),
"Transaction trie root must be valid.",
Expand All @@ -261,15 +281,39 @@ where
/// Execute all `OnRuntimeUpgrade` of this runtime, including the pre and post migration checks.
///
/// This should only be used for testing.
#[cfg(feature = "try-runtime")]
pub fn try_runtime_upgrade() -> Result<frame_support::weights::Weight, &'static str> {
<(COnRuntimeUpgrade, AllPalletsWithSystem) as OnRuntimeUpgrade>::pre_upgrade().unwrap();
let weight = Self::execute_on_runtime_upgrade();

<(COnRuntimeUpgrade, AllPalletsWithSystem) as OnRuntimeUpgrade>::post_upgrade().unwrap();

Ok(weight)
}
}

impl<
System: frame_system::Config + EnsureInherentsAreFirst<Block>,
Block: traits::Block<Header = System::Header, Hash = System::Hash>,
Context: Default,
UnsignedValidator,
AllPalletsWithSystem: OnRuntimeUpgrade
+ OnInitialize<System::BlockNumber>
+ OnIdle<System::BlockNumber>
+ OnFinalize<System::BlockNumber>
+ OffchainWorker<System::BlockNumber>,
COnRuntimeUpgrade: OnRuntimeUpgrade,
kianenigma marked this conversation as resolved.
Show resolved Hide resolved
> Executive<System, Block, Context, UnsignedValidator, AllPalletsWithSystem, COnRuntimeUpgrade>
where
Block::Extrinsic: Checkable<Context> + Codec,
CheckedOf<Block::Extrinsic, Context>: Applyable + GetDispatchInfo,
CallOf<Block::Extrinsic, Context>:
Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>,
OriginOf<Block::Extrinsic, Context>: From<Option<System::AccountId>>,
UnsignedValidator: ValidateUnsigned<Call = CallOf<Block::Extrinsic, Context>>,
{
/// Execute all `OnRuntimeUpgrade` of this runtime, and return the aggregate weight.
pub fn execute_on_runtime_upgrade() -> frame_support::weights::Weight {
<(COnRuntimeUpgrade, AllPalletsWithSystem) as OnRuntimeUpgrade>::on_runtime_upgrade()
}

/// Start the execution of a particular block.
pub fn initialize_block(header: &System::Header) {
Expand Down
1 change: 1 addition & 0 deletions frame/remark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ std = [
"sp-runtime/std",
"sp-std/std",
]
try-runtime = [ "frame-support/try-runtime" ]
Loading