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

Use BankForks on tests - Part 2 #34234

Merged
merged 2 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
17 changes: 16 additions & 1 deletion runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,7 @@ pub(super) enum RewardInterval {
}

impl Bank {
fn wrap_with_bank_forks_for_tests(self) -> (Arc<Self>, Arc<RwLock<BankForks>>) {
pub(super) fn wrap_with_bank_forks_for_tests(self) -> (Arc<Self>, Arc<RwLock<BankForks>>) {
let bank_fork = BankForks::new_rw_arc(self);
let bank_arc = bank_fork.read().unwrap().root_bank();
bank_arc
Expand All @@ -977,6 +977,21 @@ impl Bank {
(bank_arc, bank_fork)
}

#[cfg(feature = "dev-context-only-utils")]
pub fn new_from_parent_with_bank_forks(
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's not add this to Bank as a constructor. This function is using public methods from Bank and BankForks. So it can be easily implemented as part of the tests.

The test code can carry this function as a utility function. We can copy the function to different test files (in case crate boundary is causing it to be inaccessible).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You nailed my problem. I did not want to repeat code between crates, so I guess this is the solution after all.

Copy link
Contributor

Choose a reason for hiding this comment

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

Generally we should avoid repeating the code. In this particular case, adding the constructor to Bank seems incorrect, since it's a test-only code. And, it's two lines of code that calls the public functions. So it seems ok to copy the function to tests.

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 repeated the function. Let me know what you think.

bank_forks: &RwLock<BankForks>,
parent: Arc<Bank>,
collector_id: &Pubkey,
slot: Slot,
) -> Arc<Bank> {
let bank = Bank::new_from_parent(parent, collector_id, slot);
bank_forks
.write()
.unwrap()
.insert(bank)
.clone_without_scheduler()
}

pub fn default_for_tests() -> Self {
Self::default_with_accounts(Accounts::default_for_tests())
}
Expand Down
Loading