-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Description
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:
polkadot-sdk/substrate/frame/nomination-pools/src/mock.rs
Lines 401 to 407 in 53f615d
| 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_stateafter each test, as per the example above.
Pallet list
- alliance
- assets
- atomic-swap
- aura Add
try-statehook to pallet aura substrate#14363 - authority-discovery
- authorship
- babe
- bags-list
- balances
- beefy Try State Hook for Beefy #3246
- beefy-mmr
- bounties
- child-bounties
- collective Try-state for Collective pallet substrate#13645
- contracts
- conviction-voting
- democracy
- election-provider-multi-phase Implements
try_statehook in elections and EPM pallets substrate#13979 - elections-phragmen Implements
try_statehook in elections and EPM pallets substrate#13979 - fast-unstake
- gilt
- grandpa
- identity
- im-online
- indices Refactor
indicespallet #1789 - lottery
- membership
- merkle-mountain-range
- message-queue Add
TryStatehook forMessageQueuesubstrate#13115 - nicks
- multisig
- node-authorization
- nomination-pools
- offences
- preimage
- proxy
- randomness-collective-flip
- ranked-collective Try State Hook for Ranked Collective #3007
- recovery
- referenda @Szegoo Try-state for Referenda pallet substrate#13949
- remark
- root-offences noop
- root-testing noop
- scheduler
- session
- society
- staking
- state-trie-migration
- sudo
- system
- timestamp
- tips @Doordashcon Adding
try_statehook(tips & vesting) substrate#13515 - transaction-payment
- transaction-storage
- treasury Adding
try_statehook forTreasurypallet #1820 - uniques
- utility
- vesting @Doordashcon Adding
try_statehook(tips & vesting) substrate#13515 - whitelist
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
Status