2019-05-21 15:19:41 -07:00
|
|
|
//! named accounts for synthesized data accounts for bank state, etc.
|
|
|
|
//!
|
|
|
|
//! this account carries the Bank's most recent blockhashes for some N parents
|
|
|
|
//!
|
|
|
|
use crate::account::Account;
|
2019-09-10 18:53:02 -07:00
|
|
|
use crate::account_info::AccountInfo;
|
2019-05-21 15:19:41 -07:00
|
|
|
use crate::hash::Hash;
|
2019-07-12 16:38:15 -07:00
|
|
|
use crate::sysvar;
|
2019-05-22 19:07:56 -07:00
|
|
|
use bincode::serialized_size;
|
|
|
|
use std::ops::Deref;
|
2019-05-21 15:19:41 -07:00
|
|
|
|
2019-09-06 14:30:56 -07:00
|
|
|
pub use crate::clock::Slot;
|
2019-06-21 20:43:24 -07:00
|
|
|
|
2019-05-22 19:07:56 -07:00
|
|
|
const ID: [u8; 32] = [
|
2019-07-12 19:08:51 -07:00
|
|
|
6, 167, 213, 23, 25, 47, 10, 175, 198, 242, 101, 227, 251, 119, 204, 122, 218, 130, 197, 41,
|
|
|
|
208, 190, 59, 19, 110, 45, 0, 85, 32, 0, 0, 0,
|
2019-05-21 15:19:41 -07:00
|
|
|
];
|
|
|
|
|
2019-07-12 19:08:51 -07:00
|
|
|
crate::solana_name_id!(ID, "SysvarS1otHashes111111111111111111111111111");
|
2019-05-21 21:45:38 -07:00
|
|
|
|
2019-05-22 19:07:56 -07:00
|
|
|
pub const MAX_SLOT_HASHES: usize = 512; // 512 slots to get your vote in
|
|
|
|
|
2019-09-08 11:13:59 -07:00
|
|
|
pub type SlotHash = (Slot, Hash);
|
|
|
|
|
2019-05-22 19:07:56 -07:00
|
|
|
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
2019-09-08 11:13:59 -07:00
|
|
|
pub struct SlotHashes(Vec<SlotHash>);
|
2019-05-21 15:19:41 -07:00
|
|
|
|
|
|
|
impl SlotHashes {
|
2019-09-10 18:53:02 -07:00
|
|
|
pub fn from_account(account: &Account) -> Option<Self> {
|
2019-06-19 19:46:47 -07:00
|
|
|
account.deserialize_data().ok()
|
2019-05-21 15:19:41 -07:00
|
|
|
}
|
2019-09-10 18:53:02 -07:00
|
|
|
pub fn to_account(&self, account: &mut Account) -> Option<()> {
|
|
|
|
account.serialize_data(self).ok()
|
|
|
|
}
|
|
|
|
pub fn from_account_info(account: &AccountInfo) -> Option<Self> {
|
|
|
|
account.deserialize_data().ok()
|
|
|
|
}
|
|
|
|
pub fn to_account_info(&self, account: &mut AccountInfo) -> Option<()> {
|
2019-06-19 19:46:47 -07:00
|
|
|
account.serialize_data(self).ok()
|
2019-05-22 19:07:56 -07:00
|
|
|
}
|
|
|
|
pub fn size_of() -> usize {
|
2019-09-07 10:33:06 -07:00
|
|
|
serialized_size(&SlotHashes(vec![(0, Hash::default()); MAX_SLOT_HASHES])).unwrap() as usize
|
2019-05-22 19:07:56 -07:00
|
|
|
}
|
2019-06-21 20:43:24 -07:00
|
|
|
pub fn add(&mut self, slot: Slot, hash: Hash) {
|
2019-09-07 10:33:06 -07:00
|
|
|
match self.binary_search_by(|probe| slot.cmp(&probe.0)) {
|
|
|
|
Ok(index) => (self.0)[index] = (slot, hash),
|
|
|
|
Err(index) => (self.0).insert(index, (slot, hash)),
|
|
|
|
}
|
|
|
|
(self.0).truncate(MAX_SLOT_HASHES);
|
|
|
|
}
|
|
|
|
#[allow(clippy::trivially_copy_pass_by_ref)]
|
|
|
|
pub fn get(&self, slot: &Slot) -> Option<&Hash> {
|
|
|
|
self.binary_search_by(|probe| slot.cmp(&probe.0))
|
|
|
|
.ok()
|
|
|
|
.map(|index| &self[index].1)
|
2019-05-22 19:07:56 -07:00
|
|
|
}
|
2019-09-08 11:13:59 -07:00
|
|
|
pub fn new(slot_hashes: &[SlotHash]) -> Self {
|
2019-09-07 10:33:06 -07:00
|
|
|
Self(slot_hashes.to_vec())
|
2019-06-21 20:43:24 -07:00
|
|
|
}
|
2019-05-21 15:19:41 -07:00
|
|
|
}
|
2019-05-22 19:07:56 -07:00
|
|
|
|
2019-05-21 15:19:41 -07:00
|
|
|
impl Deref for SlotHashes {
|
2019-09-08 11:13:59 -07:00
|
|
|
type Target = Vec<SlotHash>;
|
2019-05-21 15:19:41 -07:00
|
|
|
fn deref(&self) -> &Self::Target {
|
2019-09-07 10:33:06 -07:00
|
|
|
&self.0
|
2019-05-21 15:19:41 -07:00
|
|
|
}
|
|
|
|
}
|
2019-05-22 19:07:56 -07:00
|
|
|
|
2019-09-08 11:13:59 -07:00
|
|
|
pub fn create_account(lamports: u64, slot_hashes: &[SlotHash]) -> Account {
|
2019-07-12 16:38:15 -07:00
|
|
|
let mut account = Account::new(lamports, SlotHashes::size_of(), &sysvar::id());
|
2019-09-10 18:53:02 -07:00
|
|
|
SlotHashes::new(slot_hashes)
|
|
|
|
.to_account(&mut account)
|
|
|
|
.unwrap();
|
2019-06-21 20:43:24 -07:00
|
|
|
account
|
|
|
|
}
|
|
|
|
|
|
|
|
use crate::account::KeyedAccount;
|
|
|
|
use crate::instruction::InstructionError;
|
|
|
|
pub fn from_keyed_account(account: &KeyedAccount) -> Result<SlotHashes, InstructionError> {
|
|
|
|
if !check_id(account.unsigned_key()) {
|
|
|
|
return Err(InstructionError::InvalidArgument);
|
|
|
|
}
|
2019-09-10 18:53:02 -07:00
|
|
|
SlotHashes::from_account(account.account).ok_or(InstructionError::InvalidArgument)
|
2019-05-21 15:19:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-05-22 19:07:56 -07:00
|
|
|
use crate::hash::hash;
|
|
|
|
|
|
|
|
#[test]
|
2019-05-29 10:08:03 -07:00
|
|
|
fn test_create_account() {
|
2019-05-22 19:07:56 -07:00
|
|
|
let lamports = 42;
|
2019-06-21 20:43:24 -07:00
|
|
|
let account = create_account(lamports, &[]);
|
|
|
|
assert_eq!(account.data.len(), SlotHashes::size_of());
|
2019-09-10 18:53:02 -07:00
|
|
|
let slot_hashes = SlotHashes::from_account(&account);
|
2019-09-07 10:33:06 -07:00
|
|
|
assert_eq!(slot_hashes, Some(SlotHashes(vec![])));
|
2019-05-22 19:07:56 -07:00
|
|
|
let mut slot_hashes = slot_hashes.unwrap();
|
|
|
|
for i in 0..MAX_SLOT_HASHES + 1 {
|
|
|
|
slot_hashes.add(
|
|
|
|
i as u64,
|
|
|
|
hash(&[(i >> 24) as u8, (i >> 16) as u8, (i >> 8) as u8, i as u8]),
|
|
|
|
);
|
|
|
|
}
|
2019-05-29 10:08:03 -07:00
|
|
|
for i in 0..MAX_SLOT_HASHES {
|
|
|
|
assert_eq!(slot_hashes[i].0, (MAX_SLOT_HASHES - i) as u64);
|
|
|
|
}
|
|
|
|
|
2019-05-22 19:07:56 -07:00
|
|
|
assert_eq!(slot_hashes.len(), MAX_SLOT_HASHES);
|
2019-05-21 15:19:41 -07:00
|
|
|
}
|
|
|
|
}
|