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

Add "run to block" tools #7109

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Add "run to block" tools #7109

wants to merge 4 commits into from

Conversation

aurexav
Copy link
Contributor

@aurexav aurexav commented Jan 10, 2025

Introduce frame_system::Pallet::run_to_block, frame_system::Pallet::run_to_block_with, and frame_system::RunToBlockHooks to establish a generic run_to_block mechanism for mock tests, minimizing redundant implementations across various pallets.

Closes #299.


Polkadot address: 156HGo9setPcU2qhFMVWLkcmtCEGySLwNqa3DaEiYSWtte4Y

@aurexav aurexav requested review from cheme and a team as code owners January 10, 2025 08:24
Signed-off-by: Xavier Lau <x@acg.box>
@aurexav aurexav changed the title Add RunToBlockHooks Add "run to block" tools Jan 10, 2025
Copy link
Member

@bkchr bkchr left a comment

Choose a reason for hiding this comment

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

Nice work! Left some minor things to fix up :)

before_finalize: Box<dyn 'a + FnMut(BlockNumberFor<T>)>,
after_finalize: Box<dyn 'a + FnMut(BlockNumberFor<T>)>,
_marker: PhantomData<&'a T>,
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like grouping the struct and its implementations. (also a lot similar grouping can be found in this lib.rs)

But if you insist, you can commit the suggestion. :)

self.after_finalize = Box::new(f);
self
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
}
}

after_initialize: Box<dyn 'a + FnMut(BlockNumberFor<T>)>,
before_finalize: Box<dyn 'a + FnMut(BlockNumberFor<T>)>,
after_finalize: Box<dyn 'a + FnMut(BlockNumberFor<T>)>,
_marker: PhantomData<&'a T>,
Copy link
Member

Choose a reason for hiding this comment

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

As we are using T, this should not be required.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, did a refactor and forgot about this.

/// provided closures at each block.
///
/// These hooks allows custom logic to be executed at each block at specific location.
/// For example, you might use one of them to set a timestamp for each block.
Copy link
Member

Choose a reason for hiding this comment

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

Maybe mentioning that finalize_block is called before each on_initialize.

Comment on lines 548 to 549
// having `timestamp::on_initialize` called before `staking::on_initialize`. Also, if
// session length is 1, then it is already triggered.
Copy link
Member

Choose a reason for hiding this comment

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

The comment doesn't really hold? :D

Comment on lines 648 to 664
// Revert the `Authorship::OnInitialize`'s `note_author` reward for easy testing.
for i in start_session_idx..end_session_idx {
let blocks_per_session = if i == 0 {
// Skip block 0.
period - 1
} else {
period
};
let extra_reward_points = blocks_per_session as RewardPoint * 20;

ErasRewardPoints::<Test>::mutate(i / sessions_per_era, |p| {
if let Some(author_11) = p.individual.get_mut(&11) {
p.total = p.total.saturating_sub(extra_reward_points);
*author_11 = (*author_11).saturating_sub(extra_reward_points);
}
});
}
Copy link
Member

Choose a reason for hiding this comment

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

Can we not just set the EventHandler to () to prevent this from happening?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Aha, missed this one.

Comment on lines 551 to 556
System::run_to_block_with::<AllPalletsWithSystem>(
1,
frame_system::RunToBlockHooks::default().after_initialize(|_| {
Timestamp::set_timestamp(INIT_TIMESTAMP);
}),
);
Copy link
Member

Choose a reason for hiding this comment

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

Can we not just call the run_to_block function here? (I know that the timestamp would be different, but the current setting here is wrong any way)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But the origin code appears to be equivalent to running to block 1?

@bkchr bkchr added the T1-FRAME This PR/Issue is related to core FRAME, the framework. label Jan 10, 2025
@paritytech-review-bot paritytech-review-bot bot requested a review from a team January 10, 2025 17:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T1-FRAME This PR/Issue is related to core FRAME, the framework.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ease implementation of mock for pallets.
2 participants