-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Improving try state running time for staking pallet #13147
Conversation
@@ -1681,12 +1681,6 @@ impl<T: Config> StakingInterface for Pallet<T> { | |||
#[cfg(any(test, feature = "try-runtime"))] | |||
impl<T: Config> Pallet<T> { | |||
pub(crate) fn do_try_state(_: BlockNumberFor<T>) -> Result<(), &'static str> { | |||
ensure!( | |||
T::VoterList::iter() | |||
.all(|x| <Nominators<T>>::contains_key(&x) || <Validators<T>>::contains_key(&x)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you still try what difference it makes if you read the whole maps first and then iterate locally?
This is probably slow since it has quadratic storage complexity.
You can load the whole map with map::iter().collect()
.
Then we keep the tests security.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, will do!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this was totally wrong. I was not copying session keys which is why the try state was running fast for me locally.
The voterlist tests were still fast probably because second reads are from cache.
The test that is taking long time is the check_nominators
. It has cubic time complexity (Total Nominators * Active Validator * Validator exposure list).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moving to draft till I find some way to speed up the above.
This should improve try state running time. We already check that
VoterList::count == Nominators::count + Validators::count
infn check_count()
which should give us sufficient safety as well as speed.Should resolve paritytech/polkadot-sdk#234. It took around 6 second for try staking test to run locally on my computer with polkadot runtime and state.
Note: The try state checks will fail on kusama runtime because of a corrupted ledger. I am following up with the team whose account is affected to get it fixed via governance.