Add slot_history for slashing (#7589)

* Add slot_history for slashing

* fixup

* fixup
This commit is contained in:
Rob Walker
2019-12-23 12:23:45 -08:00
committed by GitHub
parent 352a367570
commit 120c8f244c
15 changed files with 209 additions and 53 deletions

View File

@@ -14,6 +14,7 @@ pub mod recent_blockhashes;
pub mod rent;
pub mod rewards;
pub mod slot_hashes;
pub mod slot_history;
pub mod stake_history;
pub fn is_sysvar_id(id: &Pubkey) -> bool {
@@ -24,6 +25,7 @@ pub fn is_sysvar_id(id: &Pubkey) -> bool {
|| rent::check_id(id)
|| rewards::check_id(id)
|| slot_hashes::check_id(id)
|| slot_history::check_id(id)
|| stake_history::check_id(id)
}
@@ -59,11 +61,8 @@ pub trait SysvarId {
pub trait Sysvar:
SysvarId + Default + Sized + serde::Serialize + serde::de::DeserializeOwned
{
fn biggest() -> Self {
Self::default()
}
fn size_of() -> usize {
bincode::serialized_size(&Self::biggest()).unwrap() as usize
bincode::serialized_size(&Self::default()).unwrap() as usize
}
fn from_account(account: &Account) -> Option<Self> {
bincode::deserialize(&account.data).ok()
@@ -84,7 +83,8 @@ pub trait Sysvar:
Self::from_account(account.account).ok_or(InstructionError::InvalidArgument)
}
fn create_account(&self, lamports: u64) -> Account {
let mut account = Account::new(lamports, Self::size_of(), &id());
let data_len = Self::size_of().max(bincode::serialized_size(self).unwrap() as usize);
let mut account = Account::new(lamports, data_len, &id());
self.to_account(&mut account).unwrap();
account
}

View File

@@ -39,8 +39,9 @@ impl<'a> FromIterator<&'a Hash> for RecentBlockhashes {
}
impl Sysvar for RecentBlockhashes {
fn biggest() -> Self {
RecentBlockhashes(vec![Hash::default(); MAX_ENTRIES])
fn size_of() -> usize {
// hard-coded so that we don't have to construct an empty
1032 // golden, update if MAX_ENTRIES changes
}
}
@@ -86,6 +87,15 @@ mod tests {
use super::*;
use crate::hash::Hash;
#[test]
fn test_size_of() {
assert_eq!(
bincode::serialized_size(&RecentBlockhashes(vec![Hash::default(); MAX_ENTRIES]))
.unwrap() as usize,
RecentBlockhashes::size_of()
);
}
#[test]
fn test_create_account_empty() {
let account = create_account_with_data(42, vec![].into_iter());

View File

@@ -2,32 +2,42 @@
//!
//! this account carries the Bank's most recent blockhashes for some N parents
//!
pub use crate::slot_hashes::{Slot, SlotHash, SlotHashes, MAX_SLOT_HASHES};
use crate::{account::Account, hash::Hash, sysvar::Sysvar};
pub use crate::slot_hashes::SlotHashes;
use crate::sysvar::Sysvar;
crate::declare_sysvar_id!("SysvarS1otHashes111111111111111111111111111", SlotHashes);
impl Sysvar for SlotHashes {
fn biggest() -> Self {
// override
(0..MAX_SLOT_HASHES)
.map(|slot| (slot as Slot, Hash::default()))
.collect::<Self>()
// override
fn size_of() -> usize {
// hard-coded so that we don't have to construct an empty
20_488 // golden, update if MAX_ENTRIES changes
}
}
pub fn create_account(lamports: u64, slot_hashes: &[SlotHash]) -> Account {
SlotHashes::new(slot_hashes).create_account(lamports)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{clock::Slot, hash::Hash, slot_hashes::MAX_ENTRIES};
#[test]
fn test_size_of() {
assert_eq!(
SlotHashes::size_of(),
bincode::serialized_size(
&(0..MAX_ENTRIES)
.map(|slot| (slot as Slot, Hash::default()))
.collect::<SlotHashes>()
)
.unwrap() as usize
);
}
#[test]
fn test_create_account() {
let lamports = 42;
let account = create_account(lamports, &[]);
let account = SlotHashes::new(&[]).create_account(lamports);
assert_eq!(account.data.len(), SlotHashes::size_of());
let slot_hashes = SlotHashes::from_account(&account);
assert_eq!(slot_hashes, Some(SlotHashes::default()));

View File

@@ -0,0 +1,30 @@
//! named accounts for synthesized data accounts for bank state, etc.
//!
//! this account carries a bitvector of slots present over the past
//! epoch
//!
pub use crate::slot_history::SlotHistory;
use crate::sysvar::Sysvar;
crate::declare_sysvar_id!("SysvarS1otHistory11111111111111111111111111", SlotHistory);
impl Sysvar for SlotHistory {
// override
fn size_of() -> usize {
// hard-coded so that we don't have to construct an empty
131_097 // golden, update if MAX_ENTRIES changes
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_size_of() {
assert_eq!(
SlotHistory::size_of(),
bincode::serialized_size(&SlotHistory::default()).unwrap() as usize
);
}
}

View File

@@ -9,7 +9,7 @@ use std::ops::Deref;
crate::declare_sysvar_id!("SysvarStakeHistory1111111111111111111111111", StakeHistory);
pub const MAX_STAKE_HISTORY: usize = 512; // it should never take as many as 512 epochs to warm up or cool down
pub const MAX_ENTRIES: usize = 512; // it should never take as many as 512 epochs to warm up or cool down
#[derive(Debug, Serialize, Deserialize, PartialEq, Default, Clone)]
pub struct StakeHistoryEntry {
@@ -23,8 +23,10 @@ pub struct StakeHistoryEntry {
pub struct StakeHistory(Vec<(Epoch, StakeHistoryEntry)>);
impl Sysvar for StakeHistory {
fn biggest() -> Self {
StakeHistory(vec![(0, StakeHistoryEntry::default()); MAX_STAKE_HISTORY])
// override
fn size_of() -> usize {
// hard-coded so that we don't have to construct an empty
16392 // golden, update if MAX_ENTRIES changes
}
}
@@ -41,7 +43,7 @@ impl StakeHistory {
Ok(index) => (self.0)[index] = (epoch, entry),
Err(index) => (self.0).insert(index, (epoch, entry)),
}
(self.0).truncate(MAX_STAKE_HISTORY);
(self.0).truncate(MAX_ENTRIES);
}
}
@@ -56,30 +58,33 @@ pub fn create_account(lamports: u64, stake_history: &StakeHistory) -> Account {
stake_history.create_account(lamports)
}
use crate::account::KeyedAccount;
use crate::instruction::InstructionError;
pub fn from_keyed_account(account: &KeyedAccount) -> Result<StakeHistory, InstructionError> {
if !check_id(account.unsigned_key()) {
return Err(InstructionError::InvalidArgument);
}
StakeHistory::from_account(account.account).ok_or(InstructionError::InvalidArgument)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_size_of() {
assert_eq!(
bincode::serialized_size(&StakeHistory(vec![
(0, StakeHistoryEntry::default());
MAX_ENTRIES
]))
.unwrap() as usize,
StakeHistory::size_of()
);
}
#[test]
fn test_create_account() {
let lamports = 42;
let account = create_account(lamports, &StakeHistory::default());
let account = StakeHistory::default().create_account(lamports);
assert_eq!(account.data.len(), StakeHistory::size_of());
let stake_history = StakeHistory::from_account(&account);
assert_eq!(stake_history, Some(StakeHistory::default()));
let mut stake_history = stake_history.unwrap();
for i in 0..MAX_STAKE_HISTORY as u64 + 1 {
for i in 0..MAX_ENTRIES as u64 + 1 {
stake_history.add(
i,
StakeHistoryEntry {
@@ -88,7 +93,7 @@ mod tests {
},
);
}
assert_eq!(stake_history.len(), MAX_STAKE_HISTORY);
assert_eq!(stake_history.len(), MAX_ENTRIES);
assert_eq!(stake_history.iter().map(|entry| entry.0).min().unwrap(), 1);
assert_eq!(stake_history.get(&0), None);
assert_eq!(
@@ -100,7 +105,7 @@ mod tests {
);
// verify the account can hold a full instance
assert_eq!(
StakeHistory::from_account(&create_account(lamports, &stake_history)),
StakeHistory::from_account(&stake_history.create_account(lamports)),
Some(stake_history)
);
}