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
SDK: Add sysvar to expose recent block hashes to programs #6663
Merged
t-nelson
merged 9 commits into
solana-labs:master
from
t-nelson:recent_block_hashes_sysvar
Nov 4, 2019
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
38a3d08
SDK: Add sysvar to expose recent block hashes to programs
t-nelson 054e1fa
Blockhashes is one word
t-nelson 5211e46
Missed one
t-nelson 486f017
Avoid allocs on update
t-nelson a8f8c3b
unwrap_or_else
t-nelson e83a321
Use iterators
t-nelson 42c5a5f
Add microbench
t-nelson 1c76a87
Revert "unwrap_or_else"
t-nelson 14b26b1
Revert "Avoid allocs on update"
t-nelson 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
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,116 @@ | ||
| use crate::{account::Account, account_info::AccountInfo, hash::Hash, sysvar}; | ||
| use bincode::serialized_size; | ||
| use std::collections::BinaryHeap; | ||
| use std::iter::FromIterator; | ||
| use std::ops::Deref; | ||
|
|
||
| const MAX_ENTRIES: usize = 32; | ||
| const ID: [u8; 32] = [ | ||
| 0x06, 0xa7, 0xd5, 0x17, 0x19, 0x2c, 0x56, 0x8e, 0xe0, 0x8a, 0x84, 0x5f, 0x73, 0xd2, 0x97, 0x88, | ||
| 0xcf, 0x03, 0x5c, 0x31, 0x45, 0xb2, 0x1a, 0xb3, 0x44, 0xd8, 0x06, 0x2e, 0xa9, 0x40, 0x00, 0x00, | ||
| ]; | ||
|
|
||
| crate::solana_sysvar_id!(ID, "SysvarRecentB1ockHashes11111111111111111111"); | ||
|
|
||
| #[repr(C)] | ||
| #[derive(Serialize, Deserialize, Debug, PartialEq)] | ||
| pub struct RecentBlockhashes(Vec<Hash>); | ||
|
|
||
| impl Default for RecentBlockhashes { | ||
| fn default() -> Self { | ||
| Self(Vec::with_capacity(MAX_ENTRIES)) | ||
| } | ||
| } | ||
|
|
||
| impl<'a> FromIterator<&'a Hash> for RecentBlockhashes { | ||
| fn from_iter<I>(iter: I) -> Self | ||
| where | ||
| I: IntoIterator<Item = &'a Hash>, | ||
| { | ||
| let mut new = Self::default(); | ||
| for i in iter { | ||
| new.0.push(*i) | ||
| } | ||
| new | ||
| } | ||
| } | ||
|
|
||
| impl RecentBlockhashes { | ||
| pub fn from_account(account: &Account) -> Option<Self> { | ||
| account.deserialize_data().ok() | ||
| } | ||
| pub fn to_account(&self, account: &mut Account) -> Option<()> { | ||
| account.serialize_data(self).unwrap(); | ||
| Some(()) | ||
| } | ||
| pub fn from_account_info(account: &AccountInfo) -> Option<Self> { | ||
| account.deserialize_data().ok() | ||
| } | ||
| pub fn to_account_info(&self, account: &mut AccountInfo) -> Option<()> { | ||
| account.serialize_data(self).ok() | ||
| } | ||
| pub fn size_of() -> usize { | ||
| serialized_size(&RecentBlockhashes(vec![Hash::default(); MAX_ENTRIES])).unwrap() as usize | ||
| } | ||
| } | ||
|
|
||
| impl Deref for RecentBlockhashes { | ||
| type Target = Vec<Hash>; | ||
| fn deref(&self) -> &Self::Target { | ||
| &self.0 | ||
| } | ||
| } | ||
|
|
||
| pub fn create_account(lamports: u64) -> Account { | ||
| Account::new(lamports, RecentBlockhashes::size_of(), &sysvar::id()) | ||
| } | ||
|
|
||
| pub fn update_account<'a, I>(account: &mut Account, recent_blockhash_iter: I) -> Option<()> | ||
| where | ||
| I: IntoIterator<Item = (u64, &'a Hash)>, | ||
| { | ||
| let sorted = BinaryHeap::from_iter(recent_blockhash_iter); | ||
| let recent_blockhash_iter = sorted.into_iter().take(MAX_ENTRIES).map(|(_, hash)| hash); | ||
| let recent_blockhashes = RecentBlockhashes::from_iter(recent_blockhash_iter); | ||
| recent_blockhashes.to_account(account) | ||
| } | ||
|
|
||
| pub fn create_account_with_data<'a, I>(lamports: u64, recent_blockhash_iter: I) -> Account | ||
| where | ||
| I: IntoIterator<Item = (u64, &'a Hash)>, | ||
| { | ||
| let mut account = create_account(lamports); | ||
| update_account(&mut account, recent_blockhash_iter).unwrap(); | ||
| account | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use crate::hash::Hash; | ||
|
|
||
| #[test] | ||
| fn test_create_account_empty() { | ||
| let account = create_account_with_data(42, vec![].into_iter()); | ||
| let recent_blockhashes = RecentBlockhashes::from_account(&account).unwrap(); | ||
| assert_eq!(recent_blockhashes, RecentBlockhashes::default()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_create_account_full() { | ||
| let def_hash = Hash::default(); | ||
| let account = | ||
| create_account_with_data(42, vec![(0u64, &def_hash); MAX_ENTRIES].into_iter()); | ||
| let recent_blockhashes = RecentBlockhashes::from_account(&account).unwrap(); | ||
| assert_eq!(recent_blockhashes.len(), MAX_ENTRIES); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_create_account_truncate() { | ||
| let def_hash = Hash::default(); | ||
| let account = | ||
| create_account_with_data(42, vec![(0u64, &def_hash); MAX_ENTRIES + 1].into_iter()); | ||
| let recent_blockhashes = RecentBlockhashes::from_account(&account).unwrap(); | ||
| assert_eq!(recent_blockhashes.len(), MAX_ENTRIES); | ||
| } | ||
| } | ||
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.
This is a bug which will be fixed by #7918.