This repository was archived by the owner on Jan 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Verify frozen bank from snapshot by hashing #8184
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1826,19 +1826,38 @@ impl Bank { | |
|
|
||
| /// Recalculate the hash_internal_state from the account stores. Would be used to verify a | ||
| /// snapshot. | ||
| pub fn verify_hash_internal_state(&self) -> bool { | ||
| #[must_use] | ||
| fn verify_bank_hash(&self) -> bool { | ||
| self.rc | ||
| .accounts | ||
| .verify_bank_hash(self.slot(), &self.ancestors) | ||
| } | ||
|
|
||
| #[must_use] | ||
| fn verify_hash(&self) -> bool { | ||
| assert!(self.is_frozen()); | ||
| let calculated_hash = self.hash_internal_state(); | ||
| let expected_hash = self.hash(); | ||
|
|
||
| if calculated_hash == expected_hash { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm being conservative here. I'm ballooning |
||
| true | ||
| } else { | ||
| warn!( | ||
| "verify failed: slot: {}, {} (calculated) != {} (expected)", | ||
| self.slot(), | ||
| calculated_hash, | ||
| expected_hash | ||
| ); | ||
| false | ||
| } | ||
| } | ||
|
|
||
| /// A snapshot bank should be purged of 0 lamport accounts which are not part of the hash | ||
| /// calculation and could shield other real accounts. | ||
| pub fn verify_snapshot_bank(&self) -> bool { | ||
| self.purge_zero_lamport_accounts(); | ||
| self.rc | ||
| .accounts | ||
| .verify_bank_hash(self.slot(), &self.ancestors) | ||
| // Order and short-circuiting is significant; verify_hash requires a valid bank hash | ||
| self.verify_bank_hash() && self.verify_hash() | ||
| } | ||
|
|
||
| /// Return the number of hashes per tick | ||
|
|
@@ -3085,21 +3104,21 @@ mod tests { | |
| assert_eq!(bank0.get_account(&keypair.pubkey()).unwrap().lamports, 10); | ||
| assert_eq!(bank1.get_account(&keypair.pubkey()), None); | ||
|
|
||
| assert!(bank0.verify_hash_internal_state()); | ||
| assert!(bank0.verify_bank_hash()); | ||
|
|
||
| // Squash and then verify hash_internal value | ||
| bank0.squash(); | ||
| assert!(bank0.verify_hash_internal_state()); | ||
| assert!(bank0.verify_bank_hash()); | ||
|
|
||
| bank1.squash(); | ||
| assert!(bank1.verify_hash_internal_state()); | ||
| assert!(bank1.verify_bank_hash()); | ||
|
|
||
| // keypair should have 0 tokens on both forks | ||
| assert_eq!(bank0.get_account(&keypair.pubkey()), None); | ||
| assert_eq!(bank1.get_account(&keypair.pubkey()), None); | ||
| bank1.purge_zero_lamport_accounts(); | ||
|
|
||
| assert!(bank1.verify_hash_internal_state()); | ||
| assert!(bank1.verify_bank_hash()); | ||
| } | ||
|
|
||
| #[test] | ||
|
|
@@ -3820,7 +3839,7 @@ mod tests { | |
| let pubkey2 = Pubkey::new_rand(); | ||
| info!("transfer 2 {}", pubkey2); | ||
| bank2.transfer(10, &mint_keypair, &pubkey2).unwrap(); | ||
| assert!(bank2.verify_hash_internal_state()); | ||
| assert!(bank2.verify_bank_hash()); | ||
| } | ||
|
|
||
| #[test] | ||
|
|
@@ -3843,17 +3862,40 @@ mod tests { | |
|
|
||
| // Checkpointing should never modify the checkpoint's state once frozen | ||
| let bank0_state = bank0.hash_internal_state(); | ||
| assert!(bank2.verify_hash_internal_state()); | ||
| assert!(bank2.verify_bank_hash()); | ||
| let bank3 = new_from_parent(&bank0); | ||
| assert_eq!(bank0_state, bank0.hash_internal_state()); | ||
| assert!(bank2.verify_hash_internal_state()); | ||
| assert!(bank3.verify_hash_internal_state()); | ||
| assert!(bank2.verify_bank_hash()); | ||
| assert!(bank3.verify_bank_hash()); | ||
|
|
||
| let pubkey2 = Pubkey::new_rand(); | ||
| info!("transfer 2 {}", pubkey2); | ||
| bank2.transfer(10, &mint_keypair, &pubkey2).unwrap(); | ||
| assert!(bank2.verify_hash_internal_state()); | ||
| assert!(bank3.verify_hash_internal_state()); | ||
| assert!(bank2.verify_bank_hash()); | ||
| assert!(bank3.verify_bank_hash()); | ||
| } | ||
|
|
||
| #[test] | ||
| #[should_panic(expected = "assertion failed: self.is_frozen()")] | ||
| fn test_verify_hash_unfrozen() { | ||
| let (genesis_config, _mint_keypair) = create_genesis_config(2_000); | ||
| let bank = Bank::new(&genesis_config); | ||
| assert!(bank.verify_hash()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_verify_snapshot_bank() { | ||
| let pubkey = Pubkey::new_rand(); | ||
| let (genesis_config, mint_keypair) = create_genesis_config(2_000); | ||
| let bank = Bank::new(&genesis_config); | ||
|
|
||
| bank.transfer(1_000, &mint_keypair, &pubkey).unwrap(); | ||
| bank.freeze(); | ||
| assert!(bank.verify_snapshot_bank()); | ||
|
|
||
| // tamper the bank after freeze! | ||
| bank.increment_signature_count(1); | ||
| assert!(!bank.verify_snapshot_bank()); | ||
| } | ||
|
|
||
| // Test that two bank forks with the same accounts should not hash to the same value. | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Just for the record: I'm little concerned about the verification failure for the restarted (hard-forked) cluster, but it should not be a problem because the renewed hard_fork information itself should be included in the bank of updated snapshot. Current ops doesn't use
solana-validator's--hard-fork.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.
solana-validator's--hard-forkis only there for a validator that never wants to consume a snapshot, for example a node that wants to archive the entire ledger to long-term storage for posterity.Uh oh!
There was an error while loading. Please reload this page.
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.
Oh, I see. Then, necessitated manual restarts (with command argument adjustment) for the hard-fork should be acceptable for such a scenario. Thanks for clarification!