Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Use enum for data source with calc_accounts_hash() #28584

Conversation

brooksprumo
Copy link
Contributor

Problem

I find that the raw boolean for the use_index parameter in calc_accounts_hash() could be more obvious. (1) There are multiple boolean parameters to this family of functions, and when boolean literals are used, I lose track of what each value is for. (2) Raw booleans make it possible to accidentally switch parameters around inadvertently during a refactor since the types are all the same.

Summary of Changes

Covert the use_index boolean to an enum. This is now type-safe and self-documenting. Yay!

@brooksprumo brooksprumo added the noCI Suppress CI on this Pull Request label Oct 25, 2022
@brooksprumo brooksprumo self-assigned this Oct 25, 2022
@brooksprumo brooksprumo force-pushed the calculate-accounts-hash/data-source branch 2 times, most recently from 0f3420f to cc590e7 Compare October 25, 2022 18:06
@brooksprumo brooksprumo force-pushed the calculate-accounts-hash/data-source branch from cc590e7 to d58c092 Compare October 25, 2022 20:44
@brooksprumo brooksprumo added CI Pull Request is ready to enter CI and removed noCI Suppress CI on this Pull Request labels Oct 25, 2022
@solana-grimes solana-grimes removed the CI Pull Request is ready to enter CI label Oct 25, 2022
@brooksprumo brooksprumo marked this pull request as ready for review October 25, 2022 21:32
@brooksprumo
Copy link
Contributor Author

This is the last PR for accounts_db.rs!

Copy link
Contributor

@apfitzge apfitzge left a comment

Choose a reason for hiding this comment

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

Love the change!

self.calculate_accounts_hash_from_storages(config, &storages, timings)
} else {
self.calculate_accounts_hash_from_index(slot, config)
self.calculate_accounts_hash_from_storages(config, &storages, timings)
Copy link
Contributor

Choose a reason for hiding this comment

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

outside scope of this PR: it's odd to me that
calculate_accounts_hash_from_index takes (slot, config), but
calculate_accounts_hash_from_storages takes (config, storages, timings)

It seems like it's a cleaner interface to have these take the same args and calculate_accounts_hash_from_storages would wrap the functionality above (7230 - 7255).

/// Specify the source of the accounts data when calculating the accounts hash
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum CalcAccountsHashDataSource {
Index,
Copy link
Contributor

Choose a reason for hiding this comment

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

IndexForTests

This option should not be used by the real validator. This code path could be deleted completely except it is useful as a check on the account index, clean, and the storage based calculation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The Index version is used by Bank::update_accounts_hash(), which is called by snapshot_utils::bank_to_full/incrmental_snapshot_archive(), which is called by solana-leger-tool (with create-snapshot) and solana-test-validator (when warping).

The Index version is also called in Accounts Background Service, if test_hash_calculation is true when handling snapshot requests.

I understand that the normal validator does not use the Index version ever, but it does seem a bit strange to me to see ForTests in code paths/call graphs that are not necessarily tests.

Since the previous parameter was just use_index, and not use_index_for_tests, would it be OK to leave this variant as Index? I could also add comments here (or on the uses) saying this is meant for tests/not-normal-validators. Would that work?

Copy link
Contributor

Choose a reason for hiding this comment

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

it is confusing, but it would be nice if we're documenting it that somehow we indicate that it is for debugging or tests. A comment is better than nothing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok will do. I'm going to fix this in a subsequent PR, since (1) it feels different than this PR's intent, and (2) then I don't need to babysit CI again.

I've created this issue to track the work:
#28606

Copy link
Contributor

@jeffwashington jeffwashington left a comment

Choose a reason for hiding this comment

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

lgtm

@brooksprumo brooksprumo merged commit 27269d8 into solana-labs:master Oct 26, 2022
@brooksprumo brooksprumo deleted the calculate-accounts-hash/data-source branch October 26, 2022 17:04
gnapoli23 pushed a commit to gnapoli23/solana that referenced this pull request Dec 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants