Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tracking issue for adding try_state hook to all pallets. #239

Open
16 of 62 tasks
kianenigma opened this issue Dec 2, 2022 · 9 comments
Open
16 of 62 tasks

Tracking issue for adding try_state hook to all pallets. #239

kianenigma opened this issue Dec 2, 2022 · 9 comments
Labels
C1-mentor A task where a mentor is available. Please indicate in the issue who the mentor could be. I6-meta A specific issue for grouping tasks or bugs of a specific category. T1-FRAME This PR/Issue is related to core FRAME, the framework.

Comments

@kianenigma
Copy link
Contributor

kianenigma commented Dec 2, 2022

This hook was added in paritytech/substrate#10174. The idea is as follows:

/// Execute some checks to ensure the internal state of a pallet is consistent.
///
/// Usually, these checks should check all of the invariants that are expected to be held on all of
/// the storage items of your pallet.
///
/// This hook should not alter any storage.
pub trait TryState<BlockNumber> {
	/// Execute the state checks.
	fn try_state(_: BlockNumber) -> Result<(), &'static str>;
}

(source)

In other words, the goal is to focus on invariants that must always be held in a pallet. A naive, yet important example of this is that in pallet-balances, iterating over all users should yield the correct total issuance.

If in doubt, see a number of other pallets that already have this hook to get further inspiration.

Implementations should provide a clear and concise documentation as to why the given invariant must always hold.

Next, we want all pallets to call their try_state after each test. This can easily be achieved with a helper function like:

pub fn build_and_execute(self, test: impl FnOnce()) {
self.build().execute_with(|| {
test();
Pools::do_try_state(CheckLevel::get()).unwrap();
})
}
}

Implementing this hook will allow these checks to be called in try-runtime-cli, and other testing tools that compile a runtime with feature = try-runtime.

Note that for those who want to tackle a particular pallet, you must for sure deeply understand a pallet in order to correctly identify its list of invariants. Moreover, you might do your future generation a favor by documenting the invariants as good as you can.

TLDR Checklist

  • Read the pallet code carefully, and find invariants that must always hold. This is usually in #[pallet::storage] types, but sometimes also in #[pallet::config] trait.
  • Implement the invariants with respect to details mentioned on the trait: everything must be #[cfg(any(feature = "try-runtime"), test)], and no storage must be altered.
  • tweak the test setup to call try_state after each test, as per the example above.

Pallet list

@vjgaur
Copy link
Contributor

vjgaur commented Dec 9, 2022

I want to take this up. Can you please assign this to me ?

@kianenigma
Copy link
Contributor Author

I want to take this up. Can you please assign this to me ?

This is a tracking issue, you can go ahead and work on any item, no need to assign 👍

@Doordashcon
Copy link
Contributor

Doordashcon commented Jan 20, 2023

@kianenigma The idea of the try_state hook is to reduce testing code?

So removing duplicate tests after try_state inclusion is mandatory?

@ggwpez
Copy link
Member

ggwpez commented Jan 20, 2023

The idea of the try_state hook is to reduce testing code?

There are actually two advantages that try_state has. Once for invariant assertion in unit tests, but also in try-runtime migration testing. This is very nice since currently we dont know if a pallet's state is valid after a migration ran.
So the goal is to increase test coverage by always checking the invariants. We would probably end up with more testing code, not less 😆
There is some more explanation here paritytech/substrate#13115 (comment)

@Doordashcon
Copy link
Contributor

Doordashcon commented Mar 8, 2023

Hello @ggwpez could I list out pallets I think won't require try_state ? For now pallet-timestamp which uses the #[pallet::inherent] and pallet-utility which is stateless.

Hoping this might speed things up for other contributors :)

@ggwpez
Copy link
Member

ggwpez commented Mar 8, 2023

Yes sure @Doordashcon. Marked them together with the nicks pallet.

@Szegoo
Copy link
Contributor

Szegoo commented Apr 17, 2023

I will start working on the try-state for the referenda pallet if no one else picked that already.

@Polkadot-Forum
Copy link

This issue has been mentioned on Polkadot Forum. There might be relevant details there:

https://forum.polkadot.network/t/discussion-shaping-the-future-of-try-runtime-and-try-runtime-cli/2657/4

@pmikolajczyk41
Copy link
Contributor

aura implemented here: paritytech/substrate#14363

@juangirini juangirini transferred this issue from paritytech/substrate Aug 24, 2023
@the-right-joyce the-right-joyce added I6-meta A specific issue for grouping tasks or bugs of a specific category. C1-mentor A task where a mentor is available. Please indicate in the issue who the mentor could be. T1-FRAME This PR/Issue is related to core FRAME, the framework. and removed J1-meta labels Aug 25, 2023
wentelteefje added a commit that referenced this issue Oct 12, 2023
This PR adds a `try_state` hook for the `Treasury` pallet.
Part of #239.
bkchr pushed a commit that referenced this issue Jan 20, 2024
Part of #239.

Invariant

1. The number of entries in `Tips` should be equal to `Reasons`.
2. If `OpenTip.finders_fee` is true, then `OpenTip.deposit` should be
greater than zero.
3. Reasons exists for each Tip[`OpenTip.reason`], implying equal length
of storage.

polkadot address: 12zsKEDVcHpKEWb99iFt3xrTCQQXZMu477nJQsTBBrof5k2h

---------

Co-authored-by: Gonçalo Pestana <g6pestana@gmail.com>
Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
github-merge-queue bot pushed a commit that referenced this issue Feb 8, 2024
Part of: #239

Polkadot address: 12GyGD3QhT4i2JJpNzvMf96sxxBLWymz4RdGCxRH5Rj5agKW

---------

Co-authored-by: Liam Aharon <liam.aharon@hotmail.com>
bgallois pushed a commit to duniter/duniter-polkadot-sdk that referenced this issue Mar 25, 2024
This PR adds a `try_state` hook for the `Treasury` pallet.
Part of paritytech#239.
bgallois pushed a commit to duniter/duniter-polkadot-sdk that referenced this issue Mar 25, 2024
Part of paritytech#239.

Invariant

1. The number of entries in `Tips` should be equal to `Reasons`.
2. If `OpenTip.finders_fee` is true, then `OpenTip.deposit` should be
greater than zero.
3. Reasons exists for each Tip[`OpenTip.reason`], implying equal length
of storage.

polkadot address: 12zsKEDVcHpKEWb99iFt3xrTCQQXZMu477nJQsTBBrof5k2h

---------

Co-authored-by: Gonçalo Pestana <g6pestana@gmail.com>
Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
bgallois pushed a commit to duniter/duniter-polkadot-sdk that referenced this issue Mar 25, 2024
Part of: paritytech#239

Polkadot address: 12GyGD3QhT4i2JJpNzvMf96sxxBLWymz4RdGCxRH5Rj5agKW

---------

Co-authored-by: Liam Aharon <liam.aharon@hotmail.com>
github-merge-queue bot pushed a commit that referenced this issue Mar 28, 2024
Part of: #239

Polkadot address: 12GyGD3QhT4i2JJpNzvMf96sxxBLWymz4RdGCxRH5Rj5agKW
pgherveou pushed a commit that referenced this issue Apr 2, 2024
Part of: #239

Polkadot address: 12GyGD3QhT4i2JJpNzvMf96sxxBLWymz4RdGCxRH5Rj5agKW
serban300 pushed a commit to serban300/polkadot-sdk that referenced this issue Apr 8, 2024
serban300 pushed a commit to serban300/polkadot-sdk that referenced this issue Apr 8, 2024
serban300 pushed a commit to serban300/polkadot-sdk that referenced this issue Apr 9, 2024
dharjeezy added a commit to dharjeezy/polkadot-sdk that referenced this issue Apr 9, 2024
Part of: paritytech#239

Polkadot address: 12GyGD3QhT4i2JJpNzvMf96sxxBLWymz4RdGCxRH5Rj5agKW
serban300 pushed a commit to serban300/polkadot-sdk that referenced this issue Apr 9, 2024
serban300 pushed a commit to serban300/polkadot-sdk that referenced this issue Apr 9, 2024
serban300 pushed a commit to serban300/polkadot-sdk that referenced this issue Apr 9, 2024
serban300 pushed a commit to serban300/polkadot-sdk that referenced this issue Apr 9, 2024
serban300 pushed a commit to serban300/polkadot-sdk that referenced this issue Apr 9, 2024
serban300 pushed a commit to serban300/polkadot-sdk that referenced this issue Apr 10, 2024
serban300 pushed a commit to serban300/polkadot-sdk that referenced this issue Apr 10, 2024
jonathanudd pushed a commit to jonathanudd/polkadot-sdk that referenced this issue Apr 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C1-mentor A task where a mentor is available. Please indicate in the issue who the mentor could be. I6-meta A specific issue for grouping tasks or bugs of a specific category. T1-FRAME This PR/Issue is related to core FRAME, the framework.
Projects
Status: Draft
Status: In Progress
Development

Successfully merging a pull request may close this issue.

9 participants