-
Notifications
You must be signed in to change notification settings - Fork 33
wip: validated block footer #602
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
Draft
akhi3030
wants to merge
1
commit into
master
Choose a base branch
from
akhi3030/validated-bank-footer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
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
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 |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| use { | ||
| crate::{bank::Bank, block_component_processor::BlockComponentProcessor}, | ||
| solana_entry::block_component::BlockFooterV1, | ||
| solana_hash::Hash, | ||
| thiserror::Error, | ||
| }; | ||
|
|
||
| #[derive(Debug, Error, PartialEq, Eq)] | ||
| pub enum ValidatedBlockFooterError { | ||
| #[error("clock out of bounds")] | ||
| ClockOutOfBounds, | ||
| #[error("clock overflow")] | ||
| ClockOverflow, | ||
| #[error("bank hash mismatch")] | ||
| BankHashMismatch, | ||
| } | ||
|
|
||
| pub struct ValidatedBlockFooter { | ||
| bank_hash: Hash, | ||
| block_producer_time_ns: i64, | ||
| block_user_agent: Vec<u8>, | ||
| } | ||
|
|
||
| impl ValidatedBlockFooter { | ||
| /// Creates a new [`ValidatedBlockFooter`] for the block the block producer. | ||
| /// | ||
| /// Warning! No validation checks are performed, this should not be called during replay but only during block production. | ||
| pub fn new_unchecked_for_block_producer(footer: BlockFooterV1) -> Self { | ||
| let BlockFooterV1 { | ||
| bank_hash, | ||
| block_producer_time_nanos, | ||
| block_user_agent, | ||
| } = footer; | ||
| Self { | ||
| bank_hash, | ||
| block_producer_time_ns: block_producer_time_nanos.try_into().unwrap(), | ||
| block_user_agent, | ||
| } | ||
| } | ||
|
|
||
| /// Creates a [`ValidatedBlockFooter`] and fails if the footer is found to be invalid. | ||
| pub fn try_new( | ||
| parent_bank: &Bank, | ||
| bank: &Bank, | ||
| footer: BlockFooterV1, | ||
| ) -> Result<Self, ValidatedBlockFooterError> { | ||
| let BlockFooterV1 { | ||
| bank_hash, | ||
| block_producer_time_nanos, | ||
| block_user_agent, | ||
| } = footer; | ||
| let block_producer_time_ns = validate_clock(parent_bank, bank, block_producer_time_nanos)?; | ||
| if bank_hash != bank.hash() { | ||
| return Err(ValidatedBlockFooterError::BankHashMismatch); | ||
| } | ||
| Ok(Self { | ||
| bank_hash, | ||
| block_producer_time_ns, | ||
| block_user_agent, | ||
| }) | ||
| } | ||
|
|
||
| pub fn block_producer_time_ns(&self) -> i64 { | ||
| self.block_producer_time_ns | ||
| } | ||
|
|
||
| pub fn bank_hash(&self) -> Hash { | ||
| self.bank_hash | ||
| } | ||
|
|
||
| pub fn block_user_agent(&self) -> &[u8] { | ||
| &self.block_user_agent | ||
| } | ||
| } | ||
|
|
||
| impl From<ValidatedBlockFooter> for BlockFooterV1 { | ||
| fn from(footer: ValidatedBlockFooter) -> Self { | ||
| let ValidatedBlockFooter { | ||
| bank_hash, | ||
| block_producer_time_ns, | ||
| block_user_agent, | ||
| } = footer; | ||
| let block_producer_time_nanos = block_producer_time_ns | ||
| .try_into() | ||
| .expect("i64 to u64 should not overflow"); | ||
| BlockFooterV1 { | ||
| bank_hash, | ||
| block_producer_time_nanos, | ||
| block_user_agent, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn validate_clock( | ||
| parent_bank: &Bank, | ||
| bank: &Bank, | ||
| block_producer_time_ns: u64, | ||
| ) -> Result<i64, ValidatedBlockFooterError> { | ||
| let block_producer_time_ns = block_producer_time_ns | ||
| .try_into() | ||
| .map_err(|_| ValidatedBlockFooterError::ClockOverflow)?; | ||
|
|
||
| // Get parent time from nanosecond clock account | ||
| // If nanosecond clock hasn't been populated, don't enforce the bounds; note that the | ||
| // nanosecond clock is populated as soon as Alpenglow migration is complete. | ||
| let Some(parent_time_nanos) = parent_bank.get_nanosecond_clock() else { | ||
| return Ok(block_producer_time_ns); | ||
| }; | ||
|
|
||
| let parent_slot = parent_bank.slot(); | ||
| let current_slot = bank.slot(); | ||
|
|
||
| let (lower_bound_ns, upper_bound_ns) = BlockComponentProcessor::nanosecond_time_bounds( | ||
| parent_slot, | ||
| parent_time_nanos, | ||
| current_slot, | ||
| ); | ||
|
|
||
| let is_valid = | ||
| lower_bound_ns <= block_producer_time_ns && block_producer_time_ns <= upper_bound_ns; | ||
|
|
||
| match is_valid { | ||
| true => Ok(block_producer_time_ns), | ||
| false => Err(ValidatedBlockFooterError::ClockOutOfBounds), | ||
| } | ||
| } | ||
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.
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.
feedback from Ashwin. This check cannot be performed yet because the bank is not frozen yet.